{"version":3,"file":"SpanProcessorMetrics.cjs","names":["ATTR_OTEL_COMPONENT_TYPE","ATTR_OTEL_COMPONENT_NAME","ATTR_ERROR_TYPE","METRIC_OTEL_SDK_PROCESSOR_SPAN_PROCESSED","METRIC_OTEL_SDK_PROCESSOR_SPAN_QUEUE_CAPACITY","METRIC_OTEL_SDK_PROCESSOR_SPAN_QUEUE_SIZE"],"sources":["../../src/export/SpanProcessorMetrics.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type {\n  Attributes,\n  Counter,\n  Meter,\n  ObservableCallback,\n  ObservableUpDownCounter,\n} from '@opentelemetry/api';\nimport { ATTR_ERROR_TYPE } from '@opentelemetry/semantic-conventions';\nimport {\n  ATTR_OTEL_COMPONENT_NAME,\n  ATTR_OTEL_COMPONENT_TYPE,\n  METRIC_OTEL_SDK_PROCESSOR_SPAN_PROCESSED,\n  METRIC_OTEL_SDK_PROCESSOR_SPAN_QUEUE_CAPACITY,\n  METRIC_OTEL_SDK_PROCESSOR_SPAN_QUEUE_SIZE,\n} from '../semconv';\n\nconst componentCounter = new Map<string, number>();\n\ninterface QueueConfig {\n  capacity: number;\n  getQueueSize: () => number;\n}\n\nexport class SpanProcessorMetrics {\n  private readonly processedSpans: Counter;\n  private readonly queueSize: ObservableUpDownCounter | undefined;\n  private readonly queueSizeCallback: ObservableCallback | undefined;\n\n  private readonly standardAttrs: Attributes;\n  private readonly droppedAttrs: Attributes;\n\n  constructor(componentType: string, meter: Meter, queueConfig?: QueueConfig) {\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\n    this.droppedAttrs = {\n      ...this.standardAttrs,\n      [ATTR_ERROR_TYPE]: 'queue_full',\n    };\n\n    this.processedSpans = meter.createCounter(\n      METRIC_OTEL_SDK_PROCESSOR_SPAN_PROCESSED,\n      {\n        unit: '{span}',\n        description:\n          'The number of spans for which the processing has finished, either successful or failed.',\n      }\n    );\n\n    if (queueConfig) {\n      const { capacity, getQueueSize } = queueConfig;\n      const queueCapacity = meter.createUpDownCounter(\n        METRIC_OTEL_SDK_PROCESSOR_SPAN_QUEUE_CAPACITY,\n        {\n          unit: '{span}',\n          description:\n            'The maximum number of spans the queue of a given instance of an SDK span processor can hold.',\n        }\n      );\n      queueCapacity.add(capacity, this.standardAttrs);\n\n      this.queueSize = meter.createObservableUpDownCounter(\n        METRIC_OTEL_SDK_PROCESSOR_SPAN_QUEUE_SIZE,\n        {\n          unit: '{span}',\n          description:\n            'The number of spans in the queue of a given instance of an SDK span processor.',\n        }\n      );\n      this.queueSizeCallback = result =>\n        result.observe(getQueueSize(), this.standardAttrs);\n      this.queueSize.addCallback(this.queueSizeCallback);\n    }\n  }\n\n  dropSpans(count: number) {\n    this.processedSpans.add(count, this.droppedAttrs);\n  }\n\n  finishSpans(count: number, error: Error | undefined) {\n    if (!error) {\n      this.processedSpans.add(count, this.standardAttrs);\n      return;\n    }\n\n    const attrs = {\n      ...this.standardAttrs,\n      [ATTR_ERROR_TYPE]: error.name,\n    };\n    this.processedSpans.add(count, attrs);\n  }\n\n  shutdown() {\n    if (this.queueSize && this.queueSizeCallback) {\n      this.queueSize.removeCallback(this.queueSizeCallback);\n    }\n  }\n}\n"],"mappings":";;;AAqBA,MAAM,mCAAmB,IAAI,IAAoB;AAOjD,IAAa,uBAAb,MAAkC;CAChC;CACA;CACA;CAEA;CACA;CAEA,YAAY,eAAuB,OAAc,aAA2B;EAC1E,MAAM,UAAU,iBAAiB,IAAI,aAAa,KAAK;EACvD,iBAAiB,IAAI,eAAe,UAAU,CAAC;EAE/C,KAAK,gBAAgB;IAClBA,gBAAAA,2BAA2B;IAC3BC,gBAAAA,2BAA2B,GAAG,cAAc,GAAG;EAClD;EAEA,KAAK,eAAe;GAClB,G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