{"version":3,"file":"DeltaMetricProcessor.mjs","names":[],"sources":["../../src/state/DeltaMetricProcessor.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type { HrTime, Attributes } from '@opentelemetry/api';\nimport { millisToHrTime } from '@opentelemetry/core';\nimport type { Maybe } from '../utils';\nimport { hashAttributes } from '../utils';\nimport type { Accumulation, Aggregator } from '../aggregator/types';\nimport { AttributeHashMap } from './HashMap';\n\n/**\n * Internal interface.\n *\n * Allows synchronous collection of metrics. This processor should allow\n * allocation of new aggregation cells for metrics and convert cumulative\n * recording to delta data points.\n */\nexport class DeltaMetricProcessor<T extends Maybe<Accumulation>> {\n  private _activeCollectionStorage = new AttributeHashMap<T>();\n  // TODO: find a reasonable mean to clean the memo;\n  // https://github.com/open-telemetry/opentelemetry-specification/pull/2208\n  private _cumulativeMemoStorage = new AttributeHashMap<T>();\n  private _cardinalityLimit: number;\n  private _overflowAttributes = { 'otel.metric.overflow': true };\n  private _overflowHashCode: string;\n  private _aggregator: Aggregator<T>;\n\n  constructor(aggregator: Aggregator<T>, aggregationCardinalityLimit?: number) {\n    this._aggregator = aggregator;\n    this._cardinalityLimit = (aggregationCardinalityLimit ?? 2000) - 1;\n    this._overflowHashCode = hashAttributes(this._overflowAttributes);\n  }\n\n  record(value: number, attributes: Attributes, collectionTime: number) {\n    let accumulation = this._activeCollectionStorage.get(attributes);\n\n    if (!accumulation) {\n      const hrTime = millisToHrTime(collectionTime);\n      if (this._activeCollectionStorage.size >= this._cardinalityLimit) {\n        const overflowAccumulation = this._activeCollectionStorage.getOrDefault(\n          this._overflowAttributes,\n          () => this._aggregator.createAccumulation(hrTime)\n        );\n        overflowAccumulation?.record(value);\n        return;\n      }\n\n      accumulation = this._aggregator.createAccumulation(hrTime);\n      this._activeCollectionStorage.set(attributes, accumulation);\n    }\n\n    accumulation?.record(value);\n  }\n\n  batchCumulate(\n    measurements: AttributeHashMap<number>,\n    collectionTime: HrTime\n  ) {\n    for (const [\n      originalAttributes,\n      value,\n      originalHashCode,\n    ] of measurements.entries()) {\n      let attributes = originalAttributes;\n      let hashCode = originalHashCode;\n      const accumulation = this._aggregator.createAccumulation(collectionTime);\n      accumulation?.record(value);\n      let delta = accumulation;\n      // Diff with recorded cumulative memo.\n      if (this._cumulativeMemoStorage.has(attributes, hashCode)) {\n        // has() returned true, previous is present.\n        const previous = this._cumulativeMemoStorage.get(attributes, hashCode)!;\n        delta = this._aggregator.diff(previous, accumulation);\n      } else {\n        // If the cardinality limit is reached, we need to change the attributes\n        if (this._cumulativeMemoStorage.size >= this._cardinalityLimit) {\n          attributes = this._overflowAttributes;\n          hashCode = this._overflowHashCode;\n          if (this._cumulativeMemoStorage.has(attributes, hashCode)) {\n            // has() returned true, previous is present.\n            const previous = this._cumulativeMemoStorage.get(\n              attributes,\n              hashCode\n            )!;\n            delta = this._aggregator.diff(previous, accumulation);\n          }\n        }\n      }\n      // Merge with uncollected active delta.\n      if (this._activeCollectionStorage.has(attributes, hashCode)) {\n        // has() returned true, active is present.\n        const active = this._activeCollectionStorage.get(attributes, hashCode)!;\n        delta = this._aggregator.merge(active, delta);\n      }\n\n      // Save the current record and the delta record.\n      this._cumulativeMemoStorage.set(attributes, accumulation, hashCode);\n      this._activeCollectionStorage.set(attributes, delta, hashCode);\n    }\n  }\n\n  /**\n   * Returns a collection of delta metrics. Start time is the when first\n   * time event collected.\n   */\n  collect() {\n    const unreportedDelta = this._activeCollectionStorage;\n    this._activeCollectionStorage = new AttributeHashMap();\n\n    return unreportedDelta;\n  }\n}\n"],"mappings":";;;;;;;;;;;AAmBA,IAAa,uBAAb,MAAiE;CAC/D,2BAAmC,IAAI,iBAAoB;CAG3D,yBAAiC,IAAI,iBAAoB;CACzD;CACA,sBAA8B,EAAE,wBAAwB,KAAK;CAC7D;CACA;CAEA,YAAY,YAA2B,6BAAsC;EAC3E,KAAK,cAAc;EACnB,KAAK,qBAAqB,+BAA+B,OAAQ;EACjE,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