{"version":3,"file":"Instruments.mjs","names":[],"sources":["../src/Instruments.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type {\n  Context,\n  Attributes,\n  UpDownCounter,\n  Counter,\n  Gauge,\n  Histogram,\n  Observable,\n  ObservableCallback,\n  ObservableCounter,\n  ObservableGauge,\n  ObservableUpDownCounter,\n} from '@opentelemetry/api';\nimport { diag, ValueType } from '@opentelemetry/api';\nimport type { InstrumentDescriptor } from './InstrumentDescriptor';\nimport type { ObservableRegistry } from './state/ObservableRegistry';\nimport type {\n  AsyncWritableMetricStorage,\n  WritableMetricStorage,\n} from './state/WritableMetricStorage';\n\nexport class SyncInstrument {\n  private _writableMetricStorage: WritableMetricStorage;\n  protected _descriptor: InstrumentDescriptor;\n\n  constructor(\n    writableMetricStorage: WritableMetricStorage,\n    descriptor: InstrumentDescriptor\n  ) {\n    this._writableMetricStorage = writableMetricStorage;\n    this._descriptor = descriptor;\n  }\n\n  protected _record(\n    value: number,\n    attributes: Attributes = {},\n    context?: Context\n  ) {\n    if (typeof value !== 'number') {\n      diag.warn(\n        `non-number value provided to metric ${this._descriptor.name}: ${value}`\n      );\n      return;\n    }\n    if (\n      this._descriptor.valueType === ValueType.INT &&\n      !Number.isInteger(value)\n    ) {\n      diag.warn(\n        `INT value type cannot accept a floating-point value for ${this._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\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._writableMetricStorage.record(value, attributes, context, Date.now());\n  }\n}\n\n/**\n * The class implements {@link UpDownCounter} interface.\n */\nexport class UpDownCounterInstrument\n  extends SyncInstrument\n  implements UpDownCounter\n{\n  /**\n   * Increment value of counter by the input. Inputs may be negative.\n   */\n  add(value: number, attributes?: Attributes, ctx?: Context): void {\n    this._record(value, attributes, ctx);\n  }\n}\n\n/**\n * The class implements {@link Counter} interface.\n */\nexport class CounterInstrument extends SyncInstrument implements Counter {\n  /**\n   * Increment value of counter by the input. Inputs may not be negative.\n   */\n  add(value: number, attributes?: Attributes, ctx?: Context): void {\n    if (value < 0) {\n      diag.warn(\n        `negative value provided to counter ${this._descriptor.name}: ${value}`\n      );\n      return;\n    }\n\n    this._record(value, attributes, ctx);\n  }\n}\n\n/**\n * The class implements {@link Gauge} interface.\n */\nexport class GaugeInstrument extends SyncInstrument implements Gauge {\n  /**\n   * Records a measurement.\n   */\n  record(value: number, attributes?: Attributes, ctx?: Context): void {\n    this._record(value, attributes, ctx);\n  }\n}\n\n/**\n * The class implements {@link Histogram} interface.\n */\nexport class HistogramInstrument extends SyncInstrument implements Histogram {\n  /**\n   * Records a measurement. Value of the measurement must not be negative.\n   */\n  record(value: number, attributes?: Attributes, ctx?: Context): void {\n    if (value < 0) {\n      diag.warn(\n        `negative value provided to histogram ${this._descriptor.name}: ${value}`\n      );\n      return;\n    }\n    this._record(value, attributes, ctx);\n  }\n}\n\nexport class ObservableInstrument implements Observable {\n  /** @internal */\n  _metricStorages: AsyncWritableMetricStorage[];\n  /** @internal */\n  _descriptor: InstrumentDescriptor;\n  private _observableRegistry: ObservableRegistry;\n\n  constructor(\n    descriptor: InstrumentDescriptor,\n    metricStorages: AsyncWritableMetricStorage[],\n    observableRegistry: ObservableRegistry\n  ) {\n    this._descriptor = descriptor;\n    this._metricStorages = metricStorages;\n    this._observableRegistry = observableRegistry;\n  }\n\n  /**\n   * @see {Observable.addCallback}\n   */\n  addCallback(callback: ObservableCallback) {\n    this._observableRegistry.addCallback(callback, this);\n  }\n\n  /**\n   * @see {Observable.removeCallback}\n   */\n  removeCallback(callback: ObservableCallback) {\n    this._observableRegistry.removeCallback(callback, this);\n  }\n}\n\nexport class ObservableCounterInstrument\n  extends ObservableInstrument\n  implements ObservableCounter {}\nexport class ObservableGaugeInstrument\n  extends ObservableInstrument\n  implements ObservableGauge {}\nexport class ObservableUpDownCounterInstrument\n  extends ObservableInstrument\n  implements ObservableUpDownCounter {}\n\nexport function isObservableInstrument(\n  it: unknown\n): it is ObservableInstrument {\n  return it instanceof ObservableInstrument;\n}\n"],"mappings":";;AA0BA,IAAa,iBAAb,MAA4B;CAC1B;CACA;CAEA,YACE,uBACA,YACA;EACA,KAAK,yBAAyB;EAC9B,KAAK,cAAc;CACrB;CAEA,QACE,OACA,aAAyB,CAAC,GAC1B,SACA;EACA,IAAI,OAAO,UAAU,UAAU;GAC7B,KAAK,KACH,uCAAuC,KAAK,YAAY,KAAK,IAAI,OACnE;GACA;EACF;EACA,IACE,KAAK,YAAY,cAAc,UAAU,OACzC,CAAC,OAAO,UAAU,KAAK,GACvB;GACA,KAAK,KACH,2DAA2D,KAAK,YAAY,KAAK,kCACnF;GACA,QAAQ,KAAK,MAAM,KAAK;GAExB,IAAI,CAAC,OAAO,UAAU,KAAK,GACzB;EAEJ;EAKA,KAAK,uBAAuB,OAAO,OAAO,YAAY,SAAS,KAAK,IAAI,CAAC;CAC3E;AACF;;;;AAKA,IAAa,0BAAb,cACU,eAEV;;;;CAIE,IAAI,OAAe,YAAyB,KAAqB;EAC/D,KAAK,QAAQ,OAAO,YAAY,GAAG;CACrC;AACF;;;;AAKA,IAAa,oBAAb,cAAuC,eAAkC;;;;CAIvE,IAAI,OAAe,YAAyB,KAAqB;EAC/D,IAAI,QAAQ,GAAG;GACb,KAAK,KACH,sCAAsC,KAAK,YAAY,KAAK,IAAI,OAClE;GACA;EACF;EAEA,KAAK,QAAQ,OAAO,YAAY,GAAG;CACrC;AACF;;;;AAKA,IAAa,kBAAb,cAAqC,eAAgC;;;;CAInE,OAAO,OAAe,YAAyB,KAAqB;EAClE,KAAK,QAAQ,OAAO,YAAY,GAAG;CACrC;AACF;;;;AAKA,IAAa,sBAAb,cAAyC,eAAoC;;;;CAI3E,OAAO,OAAe,YAAyB,KAAqB;EAClE,IA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