{"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 { GenkitError } from './error.mjs';\nimport { logger } from './logging.mjs';\nimport type { TelemetryConfig } from './telemetryTypes.mjs';\n\nexport * from './tracing/exporter.mjs';\nexport * from './tracing/instrumentation.mjs';\nexport * from './tracing/types.mjs';\n\nconst oTelInitializationKey = '__GENKIT_DISABLE_GENKIT_OTEL_INITIALIZATION';\nconst instrumentationKey = '__GENKIT_TELEMETRY_INSTRUMENTED';\nconst telemetryProviderKey = '__GENKIT_TELEMETRY_PROVIDER';\n\n/**\n * @hidden\n */\nexport async function ensureBasicTelemetryInstrumentation() {\n  await checkFirebaseMonitoringAutoInit();\n\n  if (global[instrumentationKey]) {\n    return await global[instrumentationKey];\n  }\n\n  await enableTelemetry({});\n}\n\n/**\n * Checks to see if the customer is using Firebase Genkit Monitoring\n * auto initialization via environment variable by attempting to resolve\n * the firebase plugin.\n *\n * Enables Firebase Genkit Monitoring if the plugin is installed and warns\n * if it hasn't been installed.\n */\nasync function checkFirebaseMonitoringAutoInit() {\n  if (\n    !global[instrumentationKey] &&\n    process.env.ENABLE_FIREBASE_MONITORING === 'true'\n  ) {\n    try {\n      const importModule = new Function(\n        'moduleName',\n        'return import(moduleName)'\n      );\n      const firebaseModule = await importModule('@genkit-ai/firebase');\n\n      firebaseModule.enableFirebaseTelemetry();\n    } catch (e) {\n      logger.warn(\n        \"It looks like you're trying to enable firebase monitoring, but \" +\n          \"haven't installed the firebase plugin. Please run \" +\n          '`npm i --save @genkit-ai/firebase` and redeploy.'\n      );\n    }\n  }\n}\n\nexport interface TelemetryProvider {\n  enableTelemetry(\n    telemetryConfig: TelemetryConfig | Promise<TelemetryConfig>\n  ): Promise<void>;\n  flushTracing(): Promise<void>;\n}\n\nfunction getTelemetryProvider(): TelemetryProvider {\n  if (global[telemetryProviderKey]) {\n    return global[telemetryProviderKey];\n  }\n  throw new GenkitError({\n    status: 'FAILED_PRECONDITION',\n    message: 'TelemetryProvider is not initialized.',\n  });\n}\nexport function setTelemetryProvider(provider: TelemetryProvider) {\n  if (global[telemetryProviderKey]) return;\n  global[telemetryProviderKey] = provider;\n}\n\n/**\n * Enables tracing and metrics open telemetry configuration.\n */\nexport async function enableTelemetry(\n  telemetryConfig: TelemetryConfig | Promise<TelemetryConfig>\n) {\n  if (isOTelInitializationDisabled()) {\n    return;\n  }\n  global[instrumentationKey] =\n    telemetryConfig instanceof Promise ? telemetryConfig : Promise.resolve();\n  return getTelemetryProvider().enableTelemetry(telemetryConfig);\n}\n\n/**\n * Flushes all configured span processors.\n *\n * @hidden\n */\nexport async function flushTracing() {\n  return getTelemetryProvider().flushTracing();\n}\n\nfunction isOTelInitializationDisabled(): boolean {\n  return global[oTelInitializationKey] === true;\n}\n\n/**\n * Disables Genkit's OTel initialization. This is useful when you want to\n * control the OTel initialization yourself.\n *\n * This function attempts to control Genkit's internal OTel instrumentation behaviour,\n * since internal implementation details are subject to change at any time consider\n * this function \"unstable\" and subject to breaking changes as well.\n *\n * @hidden\n */\nexport function disableGenkitOTelInitialization() {\n  global[oTelInitializationKey] = true;\n}\n"],"mappings":"AAgBA,SAAS,mBAAmB;AAC5B,SAAS,cAAc;AAGvB,cAAc;AACd,cAAc;AACd,cAAc;AAEd,MAAM,wBAAwB;AAC9B,MAAM,qBAAqB;AAC3B,MAAM,uBAAuB;AAK7B,eAAsB,sCAAsC;AAC1D,QAAM,gCAAgC;AAEtC,MAAI,OAAO,kBAAkB,GAAG;AAC9B,WAAO,MAAM,OAAO,kBAAkB;AAAA,EACxC;A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