{"version":3,"file":"MetricCollector.cjs","names":["millisToHrTime"],"sources":["../../src/state/MetricCollector.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport { millisToHrTime } from '@opentelemetry/core';\nimport type { AggregationTemporalitySelector } from '../export/AggregationSelector';\nimport type {\n  CollectionResult,\n  InstrumentType,\n  ScopeMetrics,\n} from '../export/MetricData';\nimport type {\n  MetricCollectOptions,\n  MetricProducer,\n} from '../export/MetricProducer';\nimport type { IMetricReader } from '../export/MetricReader';\nimport type { ForceFlushOptions, ShutdownOptions } from '../types';\nimport type { MeterProviderSharedState } from './MeterProviderSharedState';\n\n/**\n * An internal opaque interface that the MetricReader receives as\n * MetricProducer. It acts as the storage key to the internal metric stream\n * state for each MetricReader.\n */\nexport class MetricCollector implements MetricProducer {\n  private _sharedState: MeterProviderSharedState;\n  private _metricReader: IMetricReader;\n\n  constructor(\n    sharedState: MeterProviderSharedState,\n    metricReader: IMetricReader\n  ) {\n    this._sharedState = sharedState;\n    this._metricReader = metricReader;\n  }\n\n  async collect(options?: MetricCollectOptions): Promise<CollectionResult> {\n    const collectionTime = millisToHrTime(Date.now());\n    const scopeMetrics: ScopeMetrics[] = [];\n    const errors: unknown[] = [];\n\n    const meterCollectionPromises = Array.from(\n      this._sharedState.meterSharedStates.values()\n    ).map(async meterSharedState => {\n      const current = await meterSharedState.collect(\n        this,\n        collectionTime,\n        options\n      );\n\n      // only add scope metrics if available\n      if (current?.scopeMetrics != null) {\n        scopeMetrics.push(current.scopeMetrics);\n      }\n\n      // only add errors if available\n      if (current?.errors != null) {\n        errors.push(...current.errors);\n      }\n    });\n    await Promise.all(meterCollectionPromises);\n\n    return {\n      resourceMetrics: {\n        resource: this._sharedState.resource,\n        scopeMetrics: scopeMetrics,\n      },\n      errors: errors,\n    };\n  }\n\n  /**\n   * Delegates for MetricReader.forceFlush.\n   */\n  async forceFlush(options?: ForceFlushOptions): Promise<void> {\n    await this._metricReader.forceFlush(options);\n  }\n\n  /**\n   * Delegates for MetricReader.shutdown.\n   */\n  async shutdown(options?: ShutdownOptions): Promise<void> {\n    await this._metricReader.shutdown(options);\n  }\n\n  selectAggregationTemporality(instrumentType: InstrumentType) {\n    return this._metricReader.selectAggregationTemporality(instrumentType);\n  }\n\n  selectAggregation(instrumentType: InstrumentType) {\n    return this._metricReader.selectAggregation(instrumentType);\n  }\n\n  /**\n   * Select the cardinality limit for the given {@link InstrumentType} for this\n   * collector.\n   */\n  selectCardinalityLimit(instrumentType: InstrumentType): number {\n    return this._metricReader.selectCardinalityLimit?.(instrumentType) ?? 2000;\n  }\n}\n\n/**\n * An internal interface for MetricCollector. Exposes the necessary\n * information for metric collection.\n */\nexport interface MetricCollectorHandle {\n  selectAggregationTemporality: AggregationTemporalitySelector;\n  selectCardinalityLimit(instrumentType: InstrumentType): number;\n}\n"],"mappings":";;;;;;;AAyBA,IAAa,kBAAb,MAAuD;CACrD;CACA;CAEA,YACE,aACA,cACA;EACA,KAAK,eAAe;EACpB,KAAK,gBAAgB;CACvB;CAEA,MAAM,QAAQ,SAA2D;EACvE,MAAM,kBAAA,GAAiBA,oBAAAA,eAAAA,CAAe,KAAK,IAAI,CAAC;EAChD,MAAM,eAA+B,CAAC;EACtC,MAAM,SAAoB,CAAC;EAE3B,MAAM,0BAA0B,MAAM,KACpC,KAAK,aAAa,kBAAkB,OAAO,CAC7C,CAAC,CAAC,IAAI,OAAM,qBAAoB;GAC9B,MAAM,UAAU,MAAM,iBAAiB,QACrC,MACA,gBACA,OACF;GAGA,IAAI,SAAS,gBAAgB,MAC3B,aAAa,KAAK,QAAQ,YAAY;GAIxC,IAAI,SAAS,UAAU,MACrB,OAAO,KAAK,GAAG,QAAQ,MAAM;EAEjC,CAAC;EACD,MAAM,QAAQ,IAAI,uBAAuB;EAEzC,OAAO;GACL,iBAAiB;IACf,UAAU,KAAK,aAAa;IACd;GAChB;GACQ;EACV;CACF;;;;CAKA,MAAM,WAAW,SAA4C;EAC3D,MAAM,KAAK,cAAc,WAAW,OAAO;CAC7C;;;;CAKA,MAAM,SAAS,SAA0C;EACvD,MAAM,KAAK,cAAc,SAAS,OAAO;CAC3C;CAEA,6BAA6B,gBAAgC;EAC3D,OAAO,KAAK,cAAc,6BAA6B,cAAc;CACvE;CAEA,kBAAkB,gBAAgC;EAChD,OAAO,KAAK,cAAc,kBAAkB,cAAc;CAC5D;;;;;CAMA,uBAAuB,gBAAwC;EAC7D,OAAO,KAAK,cAAc,yBAAyB,cAAc,KAAK;CACxE;AACF"}