{"version":3,"file":"ExporterMetrics.cjs","names":["VERSION","createNoopMeter","ATTR_OTEL_COMPONENT_TYPE","ATTR_OTEL_COMPONENT_NAME","ATTR_SERVER_ADDRESS","ATTR_SERVER_PORT","hrTime","ATTR_ERROR_TYPE","hrTimeToMilliseconds","hrTimeDuration"],"sources":["../src/ExporterMetrics.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport {\n  type Attributes,\n  type Counter,\n  type Histogram,\n  type MeterProvider,\n  type UpDownCounter,\n  createNoopMeter,\n} from '@opentelemetry/api';\nimport {\n  hrTime,\n  hrTimeDuration,\n  hrTimeToMilliseconds,\n} from '@opentelemetry/core';\nimport { type IExporterMetricsHelper } from '@opentelemetry/otlp-transformer';\nimport {\n  ATTR_ERROR_TYPE,\n  ATTR_OTEL_COMPONENT_NAME,\n  ATTR_OTEL_COMPONENT_TYPE,\n  ATTR_SERVER_ADDRESS,\n  ATTR_SERVER_PORT,\n} from './semconv';\nimport { VERSION } from './version';\n\nconst componentCounter = new Map<string, number>();\n\nexport interface ExporterMetricsOptions<Internal> {\n  componentType: string;\n  metricsHelper: IExporterMetricsHelper<Internal>;\n  url: string | undefined;\n  meterProvider: MeterProvider | undefined;\n  responseAttributesFromError: (\n    error: Error | string | undefined\n  ) => Attributes;\n}\n\n/**\n * Generates `otel.sdk.exporter.*` metrics.\n * https://opentelemetry.io/docs/specs/semconv/otel/sdk-metrics\n */\nexport class ExporterMetrics<Internal> {\n  private readonly inflight: UpDownCounter;\n  private readonly exported: Counter;\n  private readonly duration: Histogram;\n  private readonly standardAttrs: Attributes;\n  private readonly responseAttributesFromError: (\n    error: Error | string | undefined\n  ) => Attributes;\n\n  private readonly helper: IExporterMetricsHelper<Internal>;\n\n  constructor(options: ExporterMetricsOptions<Internal>) {\n    const {\n      componentType,\n      metricsHelper,\n      meterProvider,\n      url,\n      responseAttributesFromError,\n    } = options;\n    this.responseAttributesFromError = responseAttributesFromError;\n    const meter = meterProvider\n      ? meterProvider.getMeter('@opentelemetry/otlp-exporter', VERSION)\n      : createNoopMeter();\n\n    const counter = componentCounter.get(componentType) ?? 0;\n    componentCounter.set(componentType, counter + 1);\n\n    this.standardAttrs = {\n      [ATTR_OTEL_COMPONENT_TYPE]: componentType,\n      [ATTR_OTEL_COMPONENT_NAME]: `${componentType}/${counter}`,\n    };\n    if (url) {\n      // URLs may exclude scheme for gRPC endpoints, but in this case they always\n      // have a port number. Because the URL constructor requires a scheme, we\n      // can still handle gRPC endpoints by prepending an arbitrary scheme.\n      let urlToParse = url;\n      if (!url.includes('://')) {\n        urlToParse = `http://${url}`;\n      }\n      try {\n        const parsedUrl = new URL(urlToParse);\n        this.standardAttrs[ATTR_SERVER_ADDRESS] = parsedUrl.hostname;\n        let port: number | undefined = undefined;\n        if (parsedUrl.port) {\n          port = Number(parsedUrl.port);\n        } else if (parsedUrl.protocol === 'http:') {\n          port = 80;\n        } else if (parsedUrl.protocol === 'https:') {\n          port = 443;\n        }\n        if (typeof port === 'number') {\n          this.standardAttrs[ATTR_SERVER_PORT] = port;\n        }\n      } catch {\n        // In practice, URLs will be valid or something else will break. Better to let that\n        // inform the user than an exception in this internal code and proceed best-effort\n        // here.\n      }\n    }\n\n    this.helper = metricsHelper;\n\n    this.inflight = meter.createUpDownCounter(\n      `otel.sdk.exporter.${this.helper.name}.inflight`,\n      {\n        unit: `{${this.helper.name}}`,\n        description: `The number of ${this.helper.name}s which were passed to the exporter, but that have not been exported yet (neither successful, nor failed).`,\n      }\n    );\n    this.exported = meter.createCounter(\n      `otel.sdk.exporter.${this.helper.name}.exported`,\n      {\n        unit: `{${this.helper.name}}`,\n        description: `The number of ${this.helper.name}s for which the export has finished, either successful or failed.`,\n      }\n    );\n    this.duration = meter.createHistogram(\n      'otel.sdk.exporter.operation.duration',\n      {\n        unit: 's',\n        description: 'The duration of exporting a batch of telemetry records.',\n        advice: {\n          explicitBucketBoundaries: [],\n        },\n      }\n    );\n  }\n\n  startExport(request: Internal): (error: Error | string | undefined) => void {\n    const numItems = this.helper.countItems(request);\n    const startTime = hrTime();\n    this.inflight.add(numItems, this.standardAttrs);\n    return (error: Error | string | undefined) => {\n      const endTime = hrTime();\n      this.inflight.add(-numItems, this.standardAttrs);\n      const exportedAttrs = error\n        ? {\n            ...this.standardAttrs,\n            [ATTR_ERROR_TYPE]:\n              error instanceof Error ? error.name : 'export_failed',\n          }\n        : this.standardAttrs;\n      this.exported.add(numItems, exportedAttrs);\n      const durationAttrs = {\n        ...exportedAttrs,\n        ...this.responseAttributesFromError(error),\n      };\n      const duration =\n        hrTimeToMilliseconds(hrTimeDuration(startTime, endTime)) / 1000;\n      this.duration.record(duration, durationAttrs);\n    };\n  }\n}\n"],"mappings":";;;;;AA4BA,MAAM,mCAAmB,IAAI,IAAoB;;;;;AAgBjD,IAAa,kBAAb,MAAuC;CACrC;CACA;CACA;CACA;CACA;CAIA;CAEA,YAAY,SAA2C;EACrD,MAAM,EACJ,eACA,eACA,eACA,KACA,gCACE;EACJ,KAAK,8BAA8B;EACnC,MAAM,QAAQ,gBACV,cAAc,SAAS,gCAAgCA,gBAAAA,OAAO,KAAA,GAC9DC,mBAAAA,gBAAAA,CAAgB;EAEpB,MAAM,UAAU,iBAAiB,IAAI,aAAa,KAAK;EACvD,iBAAiB,IAAI,eAAe,UAAU,CAAC;EAE/C,KAAK,gBAAgB;IAClBC,gBAAAA,2BAA2B;IAC3BC,gBAAAA,2BAA2B,GAAG,cAAc,GAAG;EAClD;EACA,IAAI,KAAK;GAIP,IAAI,aAAa;GACjB,IAAI,CAAC,IAAI,SAAS,KAAK,GACrB,aAAa,UAAU;GAEzB,IAAI;IACF,MAAM,YAAY,IAAI,IAAI,UAAU;IACpC,KAAK,cAAcC,gBAAAA,uBAAuB,UAAU;IACpD,IAAI,OAA2B,KAAA;IAC/B,IAAI,UAAU,MACZ,OAAO,OAAO,UAAU,IAAI;SACvB,IAAI,UAAU,aAAa,SAChC,OAAO;SACF,I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