{"version":3,"file":"utils.mjs","names":[],"sources":["../src/utils.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport { diag } from '@opentelemetry/api';\nimport { Instrumentation } from '@opentelemetry/instrumentation';\n\nimport { AmqplibInstrumentation } from '@opentelemetry/instrumentation-amqplib';\nimport { AwsLambdaInstrumentation } from '@opentelemetry/instrumentation-aws-lambda';\nimport { AwsInstrumentation } from '@opentelemetry/instrumentation-aws-sdk';\nimport { BunyanInstrumentation } from '@opentelemetry/instrumentation-bunyan';\nimport { CassandraDriverInstrumentation } from '@opentelemetry/instrumentation-cassandra-driver';\nimport { ConnectInstrumentation } from '@opentelemetry/instrumentation-connect';\nimport { CucumberInstrumentation } from '@opentelemetry/instrumentation-cucumber';\nimport { DataloaderInstrumentation } from '@opentelemetry/instrumentation-dataloader';\nimport { DnsInstrumentation } from '@opentelemetry/instrumentation-dns';\nimport { ExpressInstrumentation } from '@opentelemetry/instrumentation-express';\nimport { FsInstrumentation } from '@opentelemetry/instrumentation-fs';\nimport { GenericPoolInstrumentation } from '@opentelemetry/instrumentation-generic-pool';\nimport { GraphQLInstrumentation } from '@opentelemetry/instrumentation-graphql';\nimport { GrpcInstrumentation } from '@opentelemetry/instrumentation-grpc';\nimport { HapiInstrumentation } from '@opentelemetry/instrumentation-hapi';\nimport { HostMetricsInstrumentation } from '@opentelemetry/instrumentation-host-metrics';\nimport { HttpInstrumentation } from '@opentelemetry/instrumentation-http';\nimport { IORedisInstrumentation } from '@opentelemetry/instrumentation-ioredis';\nimport { KafkaJsInstrumentation } from '@opentelemetry/instrumentation-kafkajs';\nimport { KnexInstrumentation } from '@opentelemetry/instrumentation-knex';\nimport { KoaInstrumentation } from '@opentelemetry/instrumentation-koa';\nimport { LruMemoizerInstrumentation } from '@opentelemetry/instrumentation-lru-memoizer';\nimport { MemcachedInstrumentation } from '@opentelemetry/instrumentation-memcached';\nimport { MongoDBInstrumentation } from '@opentelemetry/instrumentation-mongodb';\nimport { MongooseInstrumentation } from '@opentelemetry/instrumentation-mongoose';\nimport { MySQL2Instrumentation } from '@opentelemetry/instrumentation-mysql2';\nimport { MySQLInstrumentation } from '@opentelemetry/instrumentation-mysql';\nimport { NestInstrumentation } from '@opentelemetry/instrumentation-nestjs-core';\nimport { NetInstrumentation } from '@opentelemetry/instrumentation-net';\nimport { OpenAIInstrumentation } from '@opentelemetry/instrumentation-openai';\nimport { OracleInstrumentation } from '@opentelemetry/instrumentation-oracledb';\nimport { PgInstrumentation } from '@opentelemetry/instrumentation-pg';\nimport { PinoInstrumentation } from '@opentelemetry/instrumentation-pino';\nimport { RedisInstrumentation } from '@opentelemetry/instrumentation-redis';\nimport { RestifyInstrumentation } from '@opentelemetry/instrumentation-restify';\nimport { RouterInstrumentation } from '@opentelemetry/instrumentation-router';\nimport { RuntimeNodeInstrumentation } from '@opentelemetry/instrumentation-runtime-node';\nimport { SocketIoInstrumentation } from '@opentelemetry/instrumentation-socket.io';\nimport { TediousInstrumentation } from '@opentelemetry/instrumentation-tedious';\nimport { UndiciInstrumentation } from '@opentelemetry/instrumentation-undici';\nimport { WinstonInstrumentation } from '@opentelemetry/instrumentation-winston';\n\nimport { alibabaCloudEcsDetector } from '@opentelemetry/resource-detector-alibaba-cloud';\nimport {\n  awsBeanstalkDetector,\n  awsEc2Detector,\n  awsEcsDetector,\n  awsEksDetector,\n  awsLambdaDetector,\n} from '@opentelemetry/resource-detector-aws';\nimport { containerDetector } from '@opentelemetry/resource-detector-container';\nimport { gcpDetector } from '@opentelemetry/resource-detector-gcp';\nimport {\n  ResourceDetector,\n  envDetector,\n  hostDetector,\n  osDetector,\n  processDetector,\n  serviceInstanceIdDetector,\n} from '@opentelemetry/resources';\nimport {\n  azureAppServiceDetector,\n  azureFunctionsDetector,\n  azureVmDetector,\n} from '@opentelemetry/resource-detector-azure';\n\nconst RESOURCE_DETECTOR_CONTAINER = 'container';\nconst RESOURCE_DETECTOR_ENVIRONMENT = 'env';\nconst RESOURCE_DETECTOR_HOST = 'host';\nconst RESOURCE_DETECTOR_OS = 'os';\nconst RESOURCE_DETECTOR_SERVICE_INSTANCE_ID = 'serviceinstance';\nconst RESOURCE_DETECTOR_PROCESS = 'process';\nconst RESOURCE_DETECTOR_ALIBABA = 'alibaba';\nconst RESOURCE_DETECTOR_AWS = 'aws';\nconst RESOURCE_DETECTOR_AZURE = 'azure';\nconst RESOURCE_DETECTOR_GCP = 'gcp';\n\nconst InstrumentationMap = {\n  '@opentelemetry/instrumentation-amqplib': AmqplibInstrumentation,\n  '@opentelemetry/instrumentation-aws-lambda': AwsLambdaInstrumentation,\n  '@opentelemetry/instrumentation-aws-sdk': AwsInstrumentation,\n  '@opentelemetry/instrumentation-bunyan': BunyanInstrumentation,\n  '@opentelemetry/instrumentation-cassandra-driver':\n    CassandraDriverInstrumentation,\n  '@opentelemetry/instrumentation-connect': ConnectInstrumentation,\n  '@opentelemetry/instrumentation-cucumber': CucumberInstrumentation,\n  '@opentelemetry/instrumentation-dataloader': DataloaderInstrumentation,\n  '@opentelemetry/instrumentation-dns': DnsInstrumentation,\n  '@opentelemetry/instrumentation-express': ExpressInstrumentation,\n  '@opentelemetry/instrumentation-fs': FsInstrumentation,\n  '@opentelemetry/instrumentation-generic-pool': GenericPoolInstrumentation,\n  '@opentelemetry/instrumentation-graphql': GraphQLInstrumentation,\n  '@opentelemetry/instrumentation-grpc': GrpcInstrumentation,\n  '@opentelemetry/instrumentation-hapi': HapiInstrumentation,\n  '@opentelemetry/instrumentation-host-metrics': HostMetricsInstrumentation,\n  '@opentelemetry/instrumentation-http': HttpInstrumentation,\n  '@opentelemetry/instrumentation-ioredis': IORedisInstrumentation,\n  '@opentelemetry/instrumentation-kafkajs': KafkaJsInstrumentation,\n  '@opentelemetry/instrumentation-knex': KnexInstrumentation,\n  '@opentelemetry/instrumentation-koa': KoaInstrumentation,\n  '@opentelemetry/instrumentation-lru-memoizer': LruMemoizerInstrumentation,\n  '@opentelemetry/instrumentation-memcached': MemcachedInstrumentation,\n  '@opentelemetry/instrumentation-mongodb': MongoDBInstrumentation,\n  '@opentelemetry/instrumentation-mongoose': MongooseInstrumentation,\n  '@opentelemetry/instrumentation-mysql2': MySQL2Instrumentation,\n  '@opentelemetry/instrumentation-mysql': MySQLInstrumentation,\n  '@opentelemetry/instrumentation-nestjs-core': NestInstrumentation,\n  '@opentelemetry/instrumentation-net': NetInstrumentation,\n  '@opentelemetry/instrumentation-openai': OpenAIInstrumentation,\n  '@opentelemetry/instrumentation-oracledb': OracleInstrumentation,\n  '@opentelemetry/instrumentation-pg': PgInstrumentation,\n  '@opentelemetry/instrumentation-pino': PinoInstrumentation,\n  '@opentelemetry/instrumentation-redis': RedisInstrumentation,\n  '@opentelemetry/instrumentation-restify': RestifyInstrumentation,\n  '@opentelemetry/instrumentation-router': RouterInstrumentation,\n  '@opentelemetry/instrumentation-runtime-node': RuntimeNodeInstrumentation,\n  '@opentelemetry/instrumentation-socket.io': SocketIoInstrumentation,\n  '@opentelemetry/instrumentation-tedious': TediousInstrumentation,\n  '@opentelemetry/instrumentation-undici': UndiciInstrumentation,\n  '@opentelemetry/instrumentation-winston': WinstonInstrumentation,\n};\n\nconst defaultExcludedInstrumentations = [\n  '@opentelemetry/instrumentation-fs',\n  '@opentelemetry/instrumentation-host-metrics',\n];\n\n// Config types inferred automatically from the first argument of the constructor\ntype ConfigArg<T> = T extends new (...args: infer U) => unknown ? U[0] : never;\nexport type InstrumentationConfigMap = {\n  [Name in keyof typeof InstrumentationMap]?: ConfigArg<\n    (typeof InstrumentationMap)[Name]\n  >;\n};\n\nfunction shouldDisableInstrumentation(\n  name: string,\n  userConfig: any,\n  enabledInstrumentationsFromEnv: string[],\n  disabledInstrumentationsFromEnv: string[]\n): boolean {\n  // Priority 1: Programmatic config\n  if (userConfig.enabled === false) {\n    diag.debug(\n      `Disabling instrumentation for ${name} - disabled by user config`\n    );\n    return true;\n  }\n\n  if (userConfig.enabled === true) {\n    diag.debug(\n      `Enabling instrumentation for ${name} - explicitly enabled by user config`\n    );\n    return false;\n  }\n\n  // Priority 2: Environment variables\n  if (disabledInstrumentationsFromEnv.includes(name)) {\n    diag.debug(`Disabling instrumentation for ${name} - disabled by env var`);\n    return true;\n  }\n\n  const isEnabledEnvSet = !!process.env.OTEL_NODE_ENABLED_INSTRUMENTATIONS;\n  if (isEnabledEnvSet && !enabledInstrumentationsFromEnv.includes(name)) {\n    diag.debug(\n      `Disabling instrumentation for ${name} - not in enabled env var list`\n    );\n    return true;\n  }\n\n  // Priority 3: Default exclusions\n  if (!isEnabledEnvSet && defaultExcludedInstrumentations.includes(name)) {\n    diag.debug(`Disabling instrumentation for ${name} - excluded by default`);\n    return true;\n  }\n\n  return false;\n}\n\nexport function getNodeAutoInstrumentations(\n  inputConfigs: InstrumentationConfigMap = {}\n): Instrumentation[] {\n  checkManuallyProvidedInstrumentationNames(Object.keys(inputConfigs));\n  const enabledInstrumentationsFromEnv = getEnabledInstrumentationsFromEnv();\n  const disabledInstrumentationsFromEnv = getDisabledInstrumentationsFromEnv();\n\n  const instrumentations: Instrumentation[] = [];\n\n  for (const name of Object.keys(InstrumentationMap) as Array<\n    keyof typeof InstrumentationMap\n  >) {\n    const Instance = InstrumentationMap[name];\n    // Defaults are defined by the instrumentation itself\n    const userConfig: any = inputConfigs[name] ?? {};\n\n    const shouldDisable = shouldDisableInstrumentation(\n      name,\n      userConfig,\n      enabledInstrumentationsFromEnv,\n      disabledInstrumentationsFromEnv\n    );\n\n    if (shouldDisable) {\n      continue;\n    }\n\n    try {\n      diag.debug(`Loading instrumentation for ${name}`);\n      instrumentations.push(new Instance(userConfig));\n    } catch (e: any) {\n      diag.error(e);\n    }\n  }\n\n  return instrumentations;\n}\n\nfunction checkManuallyProvidedInstrumentationNames(\n  manuallyProvidedInstrumentationNames: string[]\n) {\n  for (const name of manuallyProvidedInstrumentationNames) {\n    if (!Object.prototype.hasOwnProperty.call(InstrumentationMap, name)) {\n      diag.error(`Provided instrumentation name \"${name}\" not found`);\n    }\n  }\n}\n\nfunction getInstrumentationsFromEnv(envVar: string): string[] {\n  const envVarValue = process.env[envVar];\n  if (envVarValue == null) {\n    return [];\n  }\n\n  const instrumentationsFromEnv = envVarValue\n    ?.split(',')\n    .map(\n      instrumentationPkgSuffix =>\n        `@opentelemetry/instrumentation-${instrumentationPkgSuffix.trim()}`\n    );\n  checkManuallyProvidedInstrumentationNames(instrumentationsFromEnv);\n  return instrumentationsFromEnv;\n}\n\n/**\n * Returns the list of instrumentations that are enabled based on the environment variable.\n * If the environment variable is unset, returns all instrumentation that are enabled by default.\n */\nfunction getEnabledInstrumentationsFromEnv() {\n  if (!process.env.OTEL_NODE_ENABLED_INSTRUMENTATIONS) {\n    // all keys in the InstrumentationMap except for everything that is not enabled by default.\n    return Object.keys(InstrumentationMap).filter(\n      key => !defaultExcludedInstrumentations.includes(key)\n    );\n  }\n\n  const instrumentationsFromEnv = getInstrumentationsFromEnv(\n    'OTEL_NODE_ENABLED_INSTRUMENTATIONS'\n  );\n  return instrumentationsFromEnv;\n}\n\n/**\n * Returns the list of instrumentations that are disabled based on the environment variable.\n */\nfunction getDisabledInstrumentationsFromEnv() {\n  if (!process.env.OTEL_NODE_DISABLED_INSTRUMENTATIONS) {\n    return [];\n  }\n\n  const instrumentationsFromEnv = getInstrumentationsFromEnv(\n    'OTEL_NODE_DISABLED_INSTRUMENTATIONS'\n  );\n  return instrumentationsFromEnv;\n}\n\nexport function getResourceDetectorsFromEnv(): Array<ResourceDetector> {\n  const resourceDetectors = new Map<\n    string,\n    ResourceDetector | ResourceDetector[]\n  >([\n    [RESOURCE_DETECTOR_CONTAINER, containerDetector],\n    [RESOURCE_DETECTOR_HOST, hostDetector],\n    [RESOURCE_DETECTOR_OS, osDetector],\n    [RESOURCE_DETECTOR_SERVICE_INSTANCE_ID, serviceInstanceIdDetector],\n    [RESOURCE_DETECTOR_PROCESS, processDetector],\n    [RESOURCE_DETECTOR_ALIBABA, alibabaCloudEcsDetector],\n    [RESOURCE_DETECTOR_GCP, gcpDetector],\n    [\n      RESOURCE_DETECTOR_AWS,\n      [\n        awsEc2Detector,\n        awsEcsDetector,\n        awsEksDetector,\n        awsBeanstalkDetector,\n        awsLambdaDetector,\n      ],\n    ],\n    [\n      RESOURCE_DETECTOR_AZURE,\n      [azureAppServiceDetector, azureFunctionsDetector, azureVmDetector],\n    ],\n    [RESOURCE_DETECTOR_ENVIRONMENT, envDetector],\n  ]);\n\n  const resourceDetectorsFromEnv =\n    process.env.OTEL_NODE_RESOURCE_DETECTORS?.split(',') ?? ['all'];\n\n  if (resourceDetectorsFromEnv.includes('all')) {\n    return [...resourceDetectors.values()].flat();\n  }\n\n  if (resourceDetectorsFromEnv.includes('none')) {\n    return [];\n  }\n\n  return resourceDetectorsFromEnv.flatMap(detector => {\n    const resourceDetector = resourceDetectors.get(detector);\n    if (!resourceDetector) {\n      diag.error(\n        `Invalid resource detector \"${detector}\" specified in the environment variable OTEL_NODE_RESOURCE_DETECTORS`\n      );\n    }\n    return resourceDetector || [];\n  });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0EA,MAAM,8BAA8B;AACpC,MAAM,gCAAgC;AACtC,MAAM,yBAAyB;AAC/B,MAAM,uBAAuB;AAC7B,MAAM,wCAAwC;AAC9C,MAAM,4BAA4B;AAClC,MAAM,4BAA4B;AAClC,MAAM,wBAAwB;AAC9B,MAAM,0BAA0B;AAChC,MAAM,wBAAwB;AAE9B,MAAM,qBAAqB;CACzB,0CAA0C;CAC1C,6CAA6C;CAC7C,0CAA0C;CAC1C,yCAAyC;CACzC,mDACE;CACF,0CAA0C;CAC1C,2CAA2C;CAC3C,6CAA6C;CAC7C,sCAAsC;CACtC,0CAA0C;CAC1C,qCAAqC;CACrC,+CAA+C;CAC/C,0CAA0C;CAC1C,uCAAuC;CACvC,uCAAuC;CACvC,+CAA+C;CAC/C,uCAAuC;CACvC,0CAA0C;CAC1C,0CAA0C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