{"version":3,"file":"instrumentation.mjs","names":["utils.isIgnored","utils.getOperationName"],"sources":["../src/instrumentation.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport { LookupAddress } from 'dns';\nimport type * as dns from 'dns';\nimport * as dnsPromises from 'dns/promises';\nimport { diag, Span, SpanKind } from '@opentelemetry/api';\nimport {\n  InstrumentationBase,\n  InstrumentationNodeModuleDefinition,\n  isWrapped,\n  safeExecuteInTheMiddle,\n} from '@opentelemetry/instrumentation';\nimport { AddressFamily } from './enums/AddressFamily';\nimport { DnsInstrumentationConfig } from './types';\nimport * as utils from './utils';\n/** @knipignore */\nimport { PACKAGE_NAME, PACKAGE_VERSION } from './version';\nimport {\n  LookupCallbackSignature,\n  LookupPromiseSignature,\n} from './internal-types';\n\n/**\n * Dns instrumentation for Opentelemetry\n */\nexport class DnsInstrumentation extends InstrumentationBase<DnsInstrumentationConfig> {\n  constructor(config: DnsInstrumentationConfig = {}) {\n    super(PACKAGE_NAME, PACKAGE_VERSION, config);\n  }\n\n  init(): (\n    | InstrumentationNodeModuleDefinition\n    | InstrumentationNodeModuleDefinition\n  )[] {\n    return [\n      new InstrumentationNodeModuleDefinition(\n        'dns',\n        ['*'],\n        (moduleExports: typeof dns) => {\n          if (isWrapped(moduleExports.lookup)) {\n            this._unwrap(moduleExports, 'lookup');\n          }\n          // eslint-disable-next-line @typescript-eslint/no-explicit-any\n          this._wrap(moduleExports, 'lookup', this._getLookup() as any);\n          this._wrap(\n            moduleExports.promises,\n            'lookup',\n            // eslint-disable-next-line @typescript-eslint/no-explicit-any\n            this._getLookup() as any\n          );\n          return moduleExports;\n        },\n        moduleExports => {\n          if (moduleExports === undefined) return;\n          this._unwrap(moduleExports, 'lookup');\n          this._unwrap(moduleExports.promises, 'lookup');\n        }\n      ),\n      new InstrumentationNodeModuleDefinition(\n        'dns/promises',\n        ['*'],\n        (moduleExports: typeof dnsPromises) => {\n          if (isWrapped(moduleExports.lookup)) {\n            this._unwrap(moduleExports, 'lookup');\n          }\n          // eslint-disable-next-line @typescript-eslint/no-explicit-any\n          this._wrap(moduleExports, 'lookup', this._getLookup() as any);\n          return moduleExports;\n        },\n        moduleExports => {\n          if (moduleExports === undefined) return;\n          this._unwrap(moduleExports, 'lookup');\n        }\n      ),\n    ];\n  }\n\n  /**\n   * Get the patched lookup function\n   */\n  private _getLookup() {\n    return (original: (hostname: string, ...args: unknown[]) => void) => {\n      const patchedLookup = this._getPatchLookupFunction(original);\n\n      // Preserve symbols such as Node.js' internal customPromisifyArgs.\n      // util.promisify() relies on it to combine address and family into an\n      // object instead of resolving with only the address string.\n      for (const symbol of Object.getOwnPropertySymbols(original)) {\n        const descriptor = Object.getOwnPropertyDescriptor(original, symbol);\n        if (descriptor !== undefined) {\n          Object.defineProperty(patchedLookup, symbol, descriptor);\n        }\n      }\n\n      return patchedLookup;\n    };\n  }\n\n  /**\n   * Creates spans for lookup operations, restoring spans' context if applied.\n   */\n  private _getPatchLookupFunction(\n    original: (hostname: string, ...args: unknown[]) => void\n  ) {\n    const plugin = this;\n    return function patchedLookup(\n      this: {},\n      hostname: string,\n      ...args: unknown[]\n    ) {\n      if (\n        utils.isIgnored(\n          hostname,\n          plugin.getConfig().ignoreHostnames,\n          (e: Error) => diag.error('caught ignoreHostname error: ', e)\n        )\n      ) {\n        return original.apply(this, [hostname, ...args]);\n      }\n\n      const argsCount = args.length;\n      diag.debug('wrap lookup callback function and starts span');\n      const name = utils.getOperationName('lookup');\n      const span = plugin.tracer.startSpan(name, {\n        kind: SpanKind.CLIENT,\n      });\n\n      const originalCallback = args[argsCount - 1];\n      if (typeof originalCallback === 'function') {\n        args[argsCount - 1] = plugin._wrapLookupCallback(\n          originalCallback,\n          span\n        );\n        return safeExecuteInTheMiddle(\n          () => original.apply(this, [hostname, ...args]),\n          error => {\n            if (error != null) {\n              utils.setError(error, span);\n              span.end();\n            }\n          }\n        );\n      } else {\n        const promise = safeExecuteInTheMiddle(\n          () =>\n            (original as LookupPromiseSignature).apply(this, [\n              hostname,\n              ...args,\n            ]),\n          error => {\n            if (error != null) {\n              utils.setError(error, span);\n              span.end();\n            }\n          }\n        );\n        promise.then(\n          result => {\n            utils.setLookupAttributes(span, result as LookupAddress);\n            span.end();\n          },\n          (e: NodeJS.ErrnoException) => {\n            utils.setError(e, span);\n            span.end();\n          }\n        );\n\n        return promise;\n      }\n    };\n  }\n\n  /**\n   * Wrap lookup callback function\n   */\n  private _wrapLookupCallback(\n    original: Function,\n    span: Span\n  ): LookupCallbackSignature {\n    return function wrappedLookupCallback(\n      this: {},\n      err: NodeJS.ErrnoException | null,\n      address: string | LookupAddress[],\n      family?: AddressFamily\n    ): void {\n      diag.debug('executing wrapped lookup callback function');\n\n      if (err !== null) {\n        utils.setError(err, span);\n      } else {\n        utils.setLookupAttributes(span, address, family);\n      }\n\n      span.end();\n      diag.debug('executing original lookup callback function');\n      return original.apply(this, arguments);\n    };\n  }\n}\n"],"mappings":";;;;;;;;;AA4BA,IAAa,qBAAb,cAAwC,oBAA8C;CACpF,YAAY,SAAmC,CAAC,GAAG;EACjD,MAAM,cAAc,iBAAiB,MAAM;CAC7C;CAEA,OAGI;EACF,OAAO,CACL,IAAI,oCACF,OACA,CAAC,GAAG,IACH,kBAA8B;GAC7B,IAAI,UAAU,cAAc,MAAM,GAChC,KAAK,QAAQ,eAAe,QAAQ;GAGtC,KAAK,MAAM,eAAe,UAAU,KAAK,WAAW,CAAQ;GAC5D,KAAK,MACH,cAAc,UACd,UAEA,KAAK,WAAW,CAClB;GACA,OAAO;EACT,IACA,kBAAiB;GACf,IAAI,kBAAkB,KAAA,GAAW;GACjC,KAAK,QAAQ,eAAe,QAAQ;GACpC,KAAK,QAAQ,cAAc,UAAU,QAAQ;EAC/C,CACF,GACA,IAAI,oCACF,gBACA,CAAC,GAAG,IACH,kBAAsC;GACrC,IAAI,UAAU,cAAc,MAAM,GAChC,KAAK,QAAQ,eAAe,QAAQ;GAGtC,KAA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