{"version":3,"file":"ObservableResult.cjs","names":["AttributeHashMap","ValueType","isObservableInstrument"],"sources":["../src/ObservableResult.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type {\n  ObservableResult,\n  Attributes,\n  BatchObservableResult,\n  Observable,\n} from '@opentelemetry/api';\nimport { diag, ValueType } from '@opentelemetry/api';\nimport { AttributeHashMap } from './state/HashMap';\nimport type { ObservableInstrument } from './Instruments';\nimport { isObservableInstrument } from './Instruments';\n\n/**\n * The class implements {@link ObservableResult} interface.\n */\nexport class ObservableResultImpl implements ObservableResult {\n  /**\n   * @internal\n   */\n  _buffer = new AttributeHashMap<number>();\n  private _instrumentName: string;\n  private _valueType: ValueType;\n\n  constructor(instrumentName: string, valueType: ValueType) {\n    this._instrumentName = instrumentName;\n    this._valueType = valueType;\n  }\n\n  /**\n   * Observe a measurement of the value associated with the given attributes.\n   */\n  observe(value: number, attributes: Attributes = {}): void {\n    if (typeof value !== 'number') {\n      diag.warn(\n        `non-number value provided to metric ${this._instrumentName}: ${value}`\n      );\n      return;\n    }\n    if (this._valueType === ValueType.INT && !Number.isInteger(value)) {\n      diag.warn(\n        `INT value type cannot accept a floating-point value for ${this._instrumentName}, ignoring the fractional digits.`\n      );\n      value = Math.trunc(value);\n      // ignore non-finite values.\n      if (!Number.isInteger(value)) {\n        return;\n      }\n    }\n    // Intentionally *not* guarding the given attributes with `cleanAttributes`\n    // or similar, because of the performance impact. Users are strongly\n    // encouraged to use only simple attribute types with metrics.\n    this._buffer.set(attributes, value);\n  }\n}\n\n/**\n * The class implements {@link BatchObservableCallback} interface.\n */\nexport class BatchObservableResultImpl implements BatchObservableResult {\n  /**\n   * @internal\n   */\n  _buffer: Map<ObservableInstrument, AttributeHashMap<number>> = new Map();\n\n  /**\n   * Observe a measurement of the value associated with the given attributes.\n   */\n  observe(\n    metric: Observable,\n    value: number,\n    attributes: Attributes = {}\n  ): void {\n    if (!isObservableInstrument(metric)) {\n      return;\n    }\n    let map = this._buffer.get(metric);\n    if (map == null) {\n      map = new AttributeHashMap();\n      this._buffer.set(metric, map);\n    }\n    if (typeof value !== 'number') {\n      diag.warn(\n        `non-number value provided to metric ${metric._descriptor.name}: ${value}`\n      );\n      return;\n    }\n    if (\n      metric._descriptor.valueType === ValueType.INT &&\n      !Number.isInteger(value)\n    ) {\n      diag.warn(\n        `INT value type cannot accept a floating-point value for ${metric._descriptor.name}, ignoring the fractional digits.`\n      );\n      value = Math.trunc(value);\n      // ignore non-finite values.\n      if (!Number.isInteger(value)) {\n        return;\n      }\n    }\n    map.set(attributes, value);\n  }\n}\n"],"mappings":";;;;;;;AAmBA,IAAa,uBAAb,MAA8D;;;;CAI5D,UAAU,IAAIA,gBAAAA,iBAAyB;CACvC;CACA;CAEA,YAAY,gBAAwB,WAAsB;EACxD,KAAK,kBAAkB;EACvB,KAAK,aAAa;CACpB;;;;CAKA,QAAQ,OAAe,aAAyB,CAAC,GAAS;EACxD,IAAI,OAAO,UAAU,UAAU;GAC7B,mBAAA,KAAK,KACH,uCAAuC,KAAK,gBAAgB,IAAI,OAClE;GACA;EACF;EACA,IAAI,KAAK,eAAeC,mBAAAA,UAAU,OAAO,CAAC,OAAO,UAAU,KAAK,GAAG;GACjE,mBAAA,KAAK,KACH,2DAA2D,KAAK,gBAAgB,kCAClF;GACA,QAAQ,KAAK,MAAM,KAAK;GAExB,IAAI,CAAC,OAAO,UAAU,KAAK,GACzB;EAEJ;EAIA,KAAK,QAAQ,IAAI,YAAY,KAAK;CACpC;AACF;;;;AAKA,IAAa,4BAAb,MAAwE;;;;CAItE,0BAA+D,IAAI,IAAI;;;;CAKvE,QACE,QACA,OACA,aAAyB,CAAC,GACpB;EACN,IAAI,CAACC,oBAAAA,uBAAuB,MAAM,GAChC;EAEF,IAAI,MAAM,KAAK,QAAQ,IAAI,MAAM;EACjC,IAAI,OAAO,MAAM;GACf,MAAM,IAAIF,gBAAAA,iBAAiB;GAC3B,KAAK,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