{"version":3,"file":"MeterProvider.mjs","names":["MeterProvider"],"sources":["../src/MeterProvider.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type {\n  MeterProvider as IMeterProvider,\n  Meter as IMeter,\n  MeterOptions,\n} from '@opentelemetry/api';\nimport { diag, createNoopMeter } from '@opentelemetry/api';\nimport type { Resource } from '@opentelemetry/resources';\nimport { defaultResource } from '@opentelemetry/resources';\nimport { MetricReader, type IMetricReader } from './export/MetricReader';\nimport { MeterProviderSharedState } from './state/MeterProviderSharedState';\nimport { MetricCollector } from './state/MetricCollector';\nimport type { ForceFlushOptions, ShutdownOptions } from './types';\nimport type { ViewOptions } from './view/View';\nimport { View } from './view/View';\n\n/**\n * MeterProviderOptions provides an interface for configuring a MeterProvider.\n */\nexport interface MeterProviderOptions {\n  /** Resource associated with metric telemetry  */\n  resource?: Resource;\n  views?: ViewOptions[];\n  readers?: IMetricReader[];\n\n  /**\n   * Whether to enable SDK metrics for this meter provider.\n   * @experimental This option is experimental and is subject to breaking changes in minor releases.\n   */\n  sdkMetricsEnabled?: boolean;\n}\n\n/**\n * This class implements the {@link MeterProvider} interface.\n */\nexport class MeterProvider implements IMeterProvider {\n  private _sharedState: MeterProviderSharedState;\n  private _shutdown = false;\n\n  constructor(options?: MeterProviderOptions) {\n    this._sharedState = new MeterProviderSharedState(\n      options?.resource ?? defaultResource()\n    );\n    if (options?.views != null && options.views.length > 0) {\n      for (const viewOption of options.views) {\n        this._sharedState.viewRegistry.addView(new View(viewOption));\n      }\n    }\n\n    if (options?.readers != null && options.readers.length > 0) {\n      for (const metricReader of options.readers) {\n        const collector = new MetricCollector(this._sharedState, metricReader);\n        metricReader.setMetricProducer(collector);\n        this._sharedState.metricCollectors.push(collector);\n        if (options.sdkMetricsEnabled && metricReader instanceof MetricReader) {\n          metricReader._setSelfObsMeterProvider(this);\n        }\n      }\n    }\n  }\n\n  /**\n   * Get a meter with the configuration of the MeterProvider.\n   */\n  getMeter(name: string, version = '', options: MeterOptions = {}): IMeter {\n    // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#meter-creation\n    if (this._shutdown) {\n      diag.warn('A shutdown MeterProvider cannot provide a Meter');\n      return createNoopMeter();\n    }\n\n    if (typeof name !== 'string' || name.length === 0) {\n      diag.warn(\n        'Invalid MeterProvider instrumentation scope name, using empty string'\n      );\n      name = '';\n    }\n\n    return this._sharedState.getMeterSharedState({\n      name,\n      version,\n      schemaUrl: options.schemaUrl,\n    }).meter;\n  }\n\n  /**\n   * Shut down the MeterProvider and all registered\n   * MetricReaders.\n   *\n   * Returns a promise which is resolved when all flushes are complete.\n   */\n  async shutdown(options?: ShutdownOptions): Promise<void> {\n    if (this._shutdown) {\n      diag.warn('shutdown may only be called once per MeterProvider');\n      return;\n    }\n\n    this._shutdown = true;\n\n    await Promise.all(\n      this._sharedState.metricCollectors.map(collector => {\n        return collector.shutdown(options);\n      })\n    );\n  }\n\n  /**\n   * Notifies all registered MetricReaders to flush any buffered data.\n   *\n   * Returns a promise which is resolved when all flushes are complete.\n   */\n  async forceFlush(options?: ForceFlushOptions): Promise<void> {\n    // do not flush after shutdown\n    if (this._shutdown) {\n      diag.warn('invalid attempt to force flush after MeterProvider shutdown');\n      return;\n    }\n\n    await Promise.all(\n      this._sharedState.metricCollectors.map(collector => {\n        return collector.forceFlush(options);\n      })\n    );\n  }\n}\n"],"mappings":";;;;;;;;;;AAuCA,IAAaA,kBAAb,MAAqD;CACnD;CACA,YAAoB;CAEpB,YAAY,SAAgC;EAC1C,KAAK,eAAe,IAAI,yBACtB,SAAS,YAAY,gBAAgB,CACvC;EACA,IAAI,SAAS,SAAS,QAAQ,QAAQ,MAAM,SAAS,GACnD,KAAK,MAAM,cAAc,QAAQ,OAC/B,KAAK,aAAa,aAAa,QAAQ,IAAI,KAAK,UAAU,CAAC;EAI/D,IAAI,SAAS,WAAW,QAAQ,QAAQ,QAAQ,SAAS,GACvD,KA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