{"version":3,"file":"MetricStorageRegistry.mjs","names":[],"sources":["../../src/state/MetricStorageRegistry.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type { MetricStorage } from './MetricStorage';\nimport type { InstrumentDescriptor } from '../InstrumentDescriptor';\nimport { isDescriptorCompatibleWith } from '../InstrumentDescriptor';\nimport * as api from '@opentelemetry/api';\nimport {\n  getConflictResolutionRecipe,\n  getIncompatibilityDetails,\n} from '../view/RegistrationConflicts';\nimport type { MetricCollectorHandle } from './MetricCollector';\n\ntype StorageMap = Map<string, MetricStorage[]>;\n\n/**\n * Internal class for storing {@link MetricStorage}\n */\nexport class MetricStorageRegistry {\n  private readonly _sharedRegistry: StorageMap = new Map();\n  private readonly _perCollectorRegistry = new Map<\n    MetricCollectorHandle,\n    StorageMap\n  >();\n\n  static create() {\n    return new MetricStorageRegistry();\n  }\n\n  getStorages(collector: MetricCollectorHandle): MetricStorage[] {\n    let storages: MetricStorage[] = [];\n    for (const metricStorages of this._sharedRegistry.values()) {\n      storages = storages.concat(metricStorages);\n    }\n\n    const perCollectorStorages = this._perCollectorRegistry.get(collector);\n    if (perCollectorStorages != null) {\n      for (const metricStorages of perCollectorStorages.values()) {\n        storages = storages.concat(metricStorages);\n      }\n    }\n\n    return storages;\n  }\n\n  register(storage: MetricStorage) {\n    this._registerStorage(storage, this._sharedRegistry);\n  }\n\n  registerForCollector(\n    collector: MetricCollectorHandle,\n    storage: MetricStorage\n  ) {\n    let storageMap = this._perCollectorRegistry.get(collector);\n    if (storageMap == null) {\n      storageMap = new Map();\n      this._perCollectorRegistry.set(collector, storageMap);\n    }\n    this._registerStorage(storage, storageMap);\n  }\n\n  findOrUpdateCompatibleStorage<T extends MetricStorage>(\n    expectedDescriptor: InstrumentDescriptor\n  ): T | null {\n    const storages = this._sharedRegistry.get(expectedDescriptor.name);\n    if (storages === undefined) {\n      return null;\n    }\n\n    // If the descriptor is compatible, the type of their metric storage\n    // (either SyncMetricStorage or AsyncMetricStorage) must be compatible.\n    return this._findOrUpdateCompatibleStorage<T>(expectedDescriptor, storages);\n  }\n\n  findOrUpdateCompatibleCollectorStorage<T extends MetricStorage>(\n    collector: MetricCollectorHandle,\n    expectedDescriptor: InstrumentDescriptor\n  ): T | null {\n    const storageMap = this._perCollectorRegistry.get(collector);\n    if (storageMap === undefined) {\n      return null;\n    }\n\n    const storages = storageMap.get(expectedDescriptor.name);\n    if (storages === undefined) {\n      return null;\n    }\n\n    // If the descriptor is compatible, the type of their metric storage\n    // (either SyncMetricStorage or AsyncMetricStorage) must be compatible.\n    return this._findOrUpdateCompatibleStorage<T>(expectedDescriptor, storages);\n  }\n\n  private _registerStorage(storage: MetricStorage, storageMap: StorageMap) {\n    const descriptor = storage.getInstrumentDescriptor();\n    const storages = storageMap.get(descriptor.name);\n\n    if (storages === undefined) {\n      storageMap.set(descriptor.name, [storage]);\n      return;\n    }\n\n    storages.push(storage);\n  }\n\n  private _findOrUpdateCompatibleStorage<T extends MetricStorage>(\n    expectedDescriptor: InstrumentDescriptor,\n    existingStorages: MetricStorage[]\n  ): T | null {\n    let compatibleStorage = null;\n\n    for (const existingStorage of existingStorages) {\n      const existingDescriptor = existingStorage.getInstrumentDescriptor();\n\n      if (isDescriptorCompatibleWith(existingDescriptor, expectedDescriptor)) {\n        // Use the longer description if it does not match.\n        if (existingDescriptor.description !== expectedDescriptor.description) {\n          if (\n            expectedDescriptor.description.length >\n            existingDescriptor.description.length\n          ) {\n            existingStorage.updateDescription(expectedDescriptor.description);\n          }\n\n          api.diag.warn(\n            'A view or instrument with the name ',\n            expectedDescriptor.name,\n            ' has already been registered, but has a different description and is incompatible with another registered view.\\n',\n            'Details:\\n',\n            getIncompatibilityDetails(existingDescriptor, expectedDescriptor),\n            'The longer description will be used.\\nTo resolve the conflict:',\n            getConflictResolutionRecipe(existingDescriptor, expectedDescriptor)\n          );\n        }\n        // Storage is fully compatible. There will never be more than one pre-existing fully compatible storage.\n        compatibleStorage = existingStorage as T;\n      } else {\n        // The implementation SHOULD warn about duplicate instrument registration\n        // conflicts after applying View configuration.\n        api.diag.warn(\n          'A view or instrument with the name ',\n          expectedDescriptor.name,\n          ' has already been registered and is incompatible with another registered view.\\n',\n          'Details:\\n',\n          getIncompatibilityDetails(existingDescriptor, expectedDescriptor),\n          'To resolve the conflict:\\n',\n          getConflictResolutionRecipe(existingDescriptor, expectedDescriptor)\n        );\n      }\n    }\n\n    return compatibleStorage;\n  }\n}\n"],"mappings":";;;;;;;AAoBA,IAAa,wBAAb,MAAa,sBAAsB;CACjC,kCAA+C,IAAI,IAAI;CACvD,wCAAyC,IAAI,IAG3C;CAEF,OAAO,SAAS;EACd,OAAO,IAAI,sBAAsB;CACnC;CAEA,YAAY,WAAmD;EAC7D,IAAI,WAA4B,CAAC;EACjC,KAAK,MAAM,kBAAkB,KAAK,gBAAgB,OAAO,GACvD,WAAW,SAAS,OAAO,cAAc;EAG3C,MAAM,uBAAuB,KAAK,sBAAsB,IAAI,SAAS;EACrE,IAAI,wBAAwB,MAC1B,KAAK,MAAM,kBAAkB,qBAAqB,OAAO,GACvD,WAAW,SAAS,OAAO,cAAc;EAI7C,OAAO;CACT;CAEA,SAAS,SAAwB;EAC/B,KAAK,iBAAiB,SAAS,KAAK,eAAe;CACrD;CAEA,qBACE,WACA,SACA;EACA,IAAI,aAAa,KAAK,sBAAsB,IAAI,SAAS;EACzD,IAAI,cAAc,MAAM;GACtB,6BAAa,IAAI,IAAI;GACrB,KAAK,sBAAsB,IAAI,WAAW,UAAU;EACtD;EACA,KAAK,iBAAiB,SAAS,UAAU;CAC3C;CAEA,8BACE,oBACU;EACV,MAAM,WAAW,KAAK,gBAAgB,IAAI,mBAAmB,IAAI;EACjE,IAAI,aAAa,KAAA,GACf,OAAO;EAKT,OAAO,KAAK,+BAAkC,oBAAoB,QAAQ;CAC5E;CAEA,uCACE,WACA,oBACU;EACV,MAAM,aAAa,KAAK,sBAAsB,IAAI,SAAS;EAC3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