@genkit-ai/core
Version:
Genkit AI framework core libraries.
1 lines • 5.31 kB
Source Map (JSON)
{"version":3,"sources":["../src/tracing.ts"],"sourcesContent":["/**\n * Copyright 2024 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { NodeSDK } from '@opentelemetry/sdk-node';\nimport { NodeSDKConfiguration } from '@opentelemetry/sdk-node/build/src/types';\nimport {\n BatchSpanProcessor,\n SimpleSpanProcessor,\n SpanProcessor,\n} from '@opentelemetry/sdk-trace-base';\nimport { getCurrentEnv } from './config.js';\nimport { logger } from './logging.js';\nimport { TelemetryConfig } from './telemetryTypes.js';\nimport { TraceStore } from './tracing.js';\nimport { TraceStoreExporter } from './tracing/exporter.js';\nimport { MultiSpanProcessor } from './tracing/multiSpanProcessor.js';\n\nexport * from './tracing/exporter.js';\nexport * from './tracing/instrumentation.js';\nexport * from './tracing/localFileTraceStore.js';\nexport * from './tracing/processor.js';\nexport * from './tracing/types.js';\n\nconst processors: SpanProcessor[] = [];\nlet telemetrySDK: NodeSDK | null = null;\nlet nodeOtelConfig: Partial<NodeSDKConfiguration> | null = null;\n\n/**\n * Enables trace spans to be written to the trace store.\n */\nexport function enableTracingAndMetrics(\n telemetryConfig: TelemetryConfig,\n traceStore?: TraceStore,\n traceStoreOptions: {\n processor?: 'batch' | 'simple';\n } = {}\n) {\n if (traceStore) {\n addProcessor(\n createTraceStoreProcessor(\n traceStore,\n traceStoreOptions.processor || 'batch'\n )\n );\n }\n\n nodeOtelConfig = telemetryConfig.getConfig() || {};\n\n addProcessor(nodeOtelConfig.spanProcessor);\n nodeOtelConfig.spanProcessor = new MultiSpanProcessor(processors);\n telemetrySDK = new NodeSDK(nodeOtelConfig);\n telemetrySDK.start();\n process.on('SIGTERM', async () => await cleanUpTracing());\n}\n\nexport async function cleanUpTracing(): Promise<void> {\n return new Promise((resolve) => {\n if (telemetrySDK) {\n // Metrics are not flushed as part of the shutdown operation. If metrics\n // are enabled, we need to manually flush them *before* the reader\n // receives shutdown order.\n const metricFlush = maybeFlushMetrics();\n\n return metricFlush.then(() => {\n return telemetrySDK!.shutdown().then(() => {\n logger.debug('OpenTelemetry SDK shut down.');\n telemetrySDK = null;\n resolve();\n });\n });\n } else {\n resolve();\n }\n });\n}\n\n/**\n * Creates a new SpanProcessor for exporting data to the configured TraceStore.\n *\n * Returns `undefined` if no trace store implementation is configured.\n */\nfunction createTraceStoreProcessor(\n traceStore: TraceStore,\n processor: 'batch' | 'simple'\n): SpanProcessor {\n const exporter = new TraceStoreExporter(traceStore);\n return processor === 'simple' || getCurrentEnv() === 'dev'\n ? new SimpleSpanProcessor(exporter)\n : new BatchSpanProcessor(exporter);\n}\n\n/** Adds the given {SpanProcessor} to the list of processors */\nfunction addProcessor(processor: SpanProcessor | undefined) {\n if (processor) processors.push(processor);\n}\n\n/** Flush metrics if present. */\nfunction maybeFlushMetrics(): Promise<void> {\n if (nodeOtelConfig?.metricReader) {\n return nodeOtelConfig.metricReader.forceFlush();\n }\n return Promise.resolve();\n}\n\n/**\n * Flushes all configured span processors\n */\nexport async function flushTracing() {\n await Promise.all(processors.map((p) => p.forceFlush()));\n}\n"],"mappings":";;;AAgBA,SAAS,eAAe;AAExB;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AACP,SAAS,qBAAqB;AAC9B,SAAS,cAAc;AAGvB,SAAS,0BAA0B;AACnC,SAAS,0BAA0B;AAEnC,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AAEd,MAAM,aAA8B,CAAC;AACrC,IAAI,eAA+B;AACnC,IAAI,iBAAuD;AAKpD,SAAS,wBACd,iBACA,YACA,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