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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/**
 * Exports Effect metrics over OTLP/HTTP.
 *
 * This module periodically snapshots metrics from the current Effect context,
 * serializes them as OTLP resource metrics, and posts them to a metrics
 * endpoint such as an OpenTelemetry Collector or vendor intake. It is meant for
 * long-running services that already update `Metric` counters, gauges,
 * histograms, frequencies, or summaries. The exporter supports cumulative
 * reporting from a fixed start time and delta reporting from the previous
 * export.
 *
 * @since 4.0.0
 */
import * as Arr from "../../Array.js";
import { Clock } from "../../Clock.js";
import * as Config from "../../Config.js";
import * as Duration from "../../Duration.js";
import * as Effect from "../../Effect.js";
import * as Layer from "../../Layer.js";
import * as Metric from "../../Metric.js";
import * as Option from "../../Option.js";
import * as OtlpEnv from "./internal/otlpEnv.js";
import * as Exporter from "./OtlpExporter.js";
import * as OtlpResource from "./OtlpResource.js";
import { OtlpSerialization } from "./OtlpSerialization.js";
/**
 * Starts a scoped OTLP metrics exporter.
 *
 * **Details**
 *
 * The exporter snapshots registered Effect metrics on the configured interval, serializes them with the selected aggregation temporality, and flushes during scope finalization up to `shutdownTimeout`.
 *
 * @category constructors
 * @since 4.0.0
 */
export const make = /*#__PURE__*/Effect.fnUntraced(function* (options) {
  const clock = yield* Clock;
  const serialization = yield* OtlpSerialization;
  const startTimeNanos = yield* clock.currentTimeNanos;
  const startTime = String(startTimeNanos);
  const temporality = options.temporality ?? "cumulative";
  const resource = yield* OtlpResource.fromConfig(options.resource);
  const metricsScope = {
    name: OtlpResource.serviceNameUnsafe(resource)
  };
  const services = yield* Effect.context();
  // State for delta temporality tracking
  let previousExportTimeNanos = startTimeNanos;
  const previousCounterState = new Map();
  const previousHistogramState = new Map();
  const previousFrequencyState = new Map();
  const previousSummaryState = new Map();
  const snapshot = () => {
    const snapshot = Metric.snapshotUnsafe(services);
    const nowNanos = clock.currentTimeNanosUnsafe();
    const nowTime = String(nowNanos);
    const metricData = [];
    const metricDataByName = new Map();
    const addMetricData = data => {
      metricData.push(data);
      metricDataByName.set(data.name, data);
    };
    const isDelta = temporality === "delta";
    const aggregationTemporalityEnum = isDelta ? EAggregationTemporality.AGGREGATION_TEMPORALITY_DELTA : EAggregationTemporality.AGGREGATION_TEMPORALITY_CUMULATIVE;
    const intervalStartTime = isDelta ? String(previousExportTimeNanos) : startTime;
    for (let i = 0, len = snapshot.length; i < len; i++) {
      const state = snapshot[i];
      const unit = state.attributes?.unit ?? state.attributes?.time_unit ?? "1";
      const attributes = state.attributes ? OtlpResource.entriesToAttributes(Object.entries(state.attributes)) : [];
      const metricKey = makeMetricKey(state.id, state.attributes);
      switch (state.type) {
        case "Counter":
          {
            const currentCount = state.state.count;
            let reportValue = currentCount;
            if (isDelta) {
              const previousCount = previousCounterState.get(metricKey);
              if (previousCount !== undefined) {
                if (typeof currentCount === "bigint" && typeof previousCount === "bigint") {
                  reportValue = currentCount - previousCount;
                  // Handle reset: if current < previous, report current value
                  if (reportValue < BigInt(0)) {
                    reportValue = currentCount;
                  }
                } else {
                  const curr = Number(currentCount);
                  const prev = Number(previousCount);
                  reportValue = curr - prev;
                  // Handle reset
                  if (reportValue < 0) {
                    reportValue = curr;
                  }
                }
              }
              previousCounterState.set(metricKey, currentCount);
            }
            const dataPoint = {
              attributes,
              startTimeUnixNano: intervalStartTime,
              timeUnixNano: nowTime
            };
            if (typeof reportValue === "bigint") {
              dataPoint.asInt = Number(reportValue);
            } else {
              dataPoint.asDouble = reportValue;
            }
            if (metricDataByName.has(state.id)) {
              metricDataByName.get(state.id).sum.dataPoints.push(dataPoint);
            } else {
              addMetricData({
                name: state.id,
                description: state.description,
                unit,
                sum: {
                  aggregationTemporality: aggregationTemporalityEnum,
                  isMonotonic: state.state.incremental,
                  dataPoints: [dataPoint]
                }
              });
            }
            break;
          }
        case "Gauge":
          {
            // Gauges don't have temporality - they always report current value
            const dataPoint = {
              attributes,
              startTimeUnixNano: startTime,
              timeUnixNano: nowTime
            };
            if (typeof state.state.value === "bigint") {
              dataPoint.asInt = Number(state.state.value);
            } else {
              dataPoint.asDouble = state.state.value;
            }
            if (metricDataByName.has(state.id)) {
              metricDataByName.get(state.id).gauge.dataPoints.push(dataPoint);
            } else {
              addMetricData({
                name: state.id,
                description: state.description,
                unit,
                gauge: {
                  dataPoints: [dataPoint]
                }
              });
            }
            break;
          }
        case "Histogram":
          {
            const size = state.state.buckets.length;
            const currentBuckets = {
              boundaries: Arr.allocate(size - 1),
              counts: Arr.allocate(size)
            };
            let idx = 0;
            let prev = 0;
            for (const [boundary, value] of state.state.buckets) {
              if (idx < size - 1) {
                currentBuckets.boundaries[idx] = boundary;
              }
              currentBuckets.counts[idx] = value - prev;
              prev = value;
              idx++;
            }
            let reportCount = state.state.count;
            let reportSum = state.state.sum;
            let reportBucketCounts = currentBuckets.counts;
            const reportMin = state.state.min;
            const reportMax = state.state.max;
            if (isDelta) {
              const previousState = previousHistogramState.get(metricKey);
              if (previousState !== undefined) {
                reportCount = state.state.count - previousState.count;
                reportSum = state.state.sum - previousState.sum;
                reportBucketCounts = currentBuckets.counts.map((c, i) => Math.max(0, c - (previousState.bucketCounts[i] ?? 0)));
                // For delta, min/max represent the interval's min/max
                // We can't compute these from cumulative state, so we use current values
                // Note: This is a limitation - true delta min/max would require tracking
                // observations within each interval
              }
              previousHistogramState.set(metricKey, {
                count: state.state.count,
                sum: state.state.sum,
                bucketCounts: currentBuckets.counts.slice(),
                min: state.state.min,
                max: state.state.max
              });
            }
            const dataPoint = {
              attributes,
              startTimeUnixNano: intervalStartTime,
              timeUnixNano: nowTime,
              count: reportCount,
              min: reportMin,
              max: reportMax,
              sum: reportSum,
              bucketCounts: reportBucketCounts,
              explicitBounds: currentBuckets.boundaries
            };
            if (metricDataByName.has(state.id)) {
              metricDataByName.get(state.id).histogram.dataPoints.push(dataPoint);
            } else {
              addMetricData({
                name: state.id,
                description: state.description,
                unit,
                histogram: {
                  aggregationTemporality: aggregationTemporalityEnum,
                  dataPoints: [dataPoint]
                }
              });
            }
            break;
          }
        case "Frequency":
          {
            const dataPoints = [];
            const currentOccurrences = new Map();
            for (const [freqKey, value] of state.state.occurrences) {
              currentOccurrences.set(freqKey, value);
              let reportValue = value;
              if (isDelta) {
                const previousOccurrences = previousFrequencyState.get(metricKey);
                if (previousOccurrences !== undefined) {
                  const previousValue = previousOccurrences.get(freqKey) ?? 0;
                  reportValue = Math.max(0, value - previousValue);
                }
              }
              dataPoints.push({
                attributes: [...attributes, {
                  key: "key",
                  value: {
                    stringValue: freqKey
                  }
                }],
                startTimeUnixNano: intervalStartTime,
                timeUnixNano: nowTime,
                asInt: reportValue
              });
            }
            if (isDelta) {
              previousFrequencyState.set(metricKey, currentOccurrences);
            }
            if (metricDataByName.has(state.id)) {
              metricDataByName.get(state.id).sum.dataPoints.push(...dataPoints);
            } else {
              addMetricData({
                name: state.id,
                description: state.description,
                unit,
                sum: {
                  aggregationTemporality: aggregationTemporalityEnum,
                  isMonotonic: true,
                  dataPoints
                }
              });
            }
            break;
          }
        case "Summary":
          {
            // Quantiles are always computed fresh from the sliding window
            // They don't have temporality in the traditional sense
            const dataPoints = [{
              attributes: [...attributes, {
                key: "quantile",
                value: {
                  stringValue: "min"
                }
              }],
              startTimeUnixNano: intervalStartTime,
              timeUnixNano: nowTime,
              asDouble: state.state.min
            }];
            for (const [quantile, value] of state.state.quantiles) {
              dataPoints.push({
                attributes: [...attributes, {
                  key: "quantile",
                  value: {
                    stringValue: quantile.toString()
                  }
                }],
                startTimeUnixNano: intervalStartTime,
                timeUnixNano: nowTime,
                asDouble: value ?? 0
              });
            }
            dataPoints.push({
              attributes: [...attributes, {
                key: "quantile",
                value: {
                  stringValue: "max"
                }
              }],
              startTimeUnixNano: intervalStartTime,
              timeUnixNano: nowTime,
              asDouble: state.state.max
            });
            let reportCount = state.state.count;
            let reportSum = state.state.sum;
            if (isDelta) {
              const previousState = previousSummaryState.get(metricKey);
              if (previousState !== undefined) {
                reportCount = state.state.count - previousState.count;
                reportSum = state.state.sum - previousState.sum;
              }
              previousSummaryState.set(metricKey, {
                count: state.state.count,
                sum: state.state.sum
              });
            }
            const countDataPoint = {
              attributes,
              startTimeUnixNano: intervalStartTime,
              timeUnixNano: nowTime,
              asInt: reportCount
            };
            const sumDataPoint = {
              attributes,
              startTimeUnixNano: intervalStartTime,
              timeUnixNano: nowTime,
              asDouble: reportSum
            };
            if (metricDataByName.has(`${state.id}_quantiles`)) {
              metricDataByName.get(`${state.id}_quantiles`).sum.dataPoints.push(...dataPoints);
              metricDataByName.get(`${state.id}_count`).sum.dataPoints.push(countDataPoint);
              metricDataByName.get(`${state.id}_sum`).sum.dataPoints.push(sumDataPoint);
            } else {
              addMetricData({
                name: `${state.id}_quantiles`,
                description: state.description,
                unit,
                sum: {
                  aggregationTemporality: aggregationTemporalityEnum,
                  isMonotonic: false,
                  dataPoints
                }
              });
              addMetricData({
                name: `${state.id}_count`,
                description: state.description,
                unit: "1",
                sum: {
                  aggregationTemporality: aggregationTemporalityEnum,
                  isMonotonic: true,
                  dataPoints: [countDataPoint]
                }
              });
              addMetricData({
                name: `${state.id}_sum`,
                description: state.description,
                unit: "1",
                sum: {
                  aggregationTemporality: aggregationTemporalityEnum,
                  isMonotonic: true,
                  dataPoints: [sumDataPoint]
                }
              });
            }
            break;
          }
      }
    }
    // Update the previous export time for delta calculations
    if (isDelta) {
      previousExportTimeNanos = nowNanos;
    }
    return serialization.metrics({
      resourceMetrics: [{
        resource,
        scopeMetrics: [{
          scope: metricsScope,
          metrics: metricData
        }]
      }]
    });
  };
  yield* Exporter.make({
    label: "OtlpMetrics",
    url: options.url,
    headers: options.headers,
    maxBatchSize: "disabled",
    exportInterval: options.exportInterval ?? Duration.seconds(10),
    body: snapshot,
    shutdownTimeout: options.shutdownTimeout ?? Duration.seconds(3)
  });
});
/**
 * Layer that starts the OTLP metrics exporter created by `make`.
 *
 * @category layers
 * @since 4.0.0
 */
export const layer = options => Layer.effectDiscard(make(options));
/**
 * Creates an OTLP metrics layer from OpenTelemetry configuration.
 *
 * @category layers
 * @since 4.0.0
 */
export const layerFromConfig = options => Effect.gen(function* () {
  const {
    disabled,
    endpoint,
    exporters
  } = yield* Config.all({
    disabled: Config.boolean("OTEL_SDK_DISABLED").pipe(Config.withDefault(false)),
    endpoint: OtlpEnv.endpoint("METRICS"),
    exporters: OtlpEnv.exporters("METRICS")
  });
  if (disabled || !endpoint || !exporters.includes("otlp")) {
    return Layer.empty;
  }
  const {
    baseTimeout,
    metricsTimeout,
    exportTimeout,
    exportInterval,
    temporalityPreference
  } = yield* Config.all({
    baseTimeout: Config.option(Config.int("OTEL_EXPORTER_OTLP_TIMEOUT")),
    metricsTimeout: Config.option(Config.int("OTEL_EXPORTER_OTLP_METRICS_TIMEOUT")),
    exportTimeout: Config.option(Config.int("OTEL_METRIC_EXPORT_TIMEOUT")),
    exportInterval: Config.option(Config.int("OTEL_METRIC_EXPORT_INTERVAL").pipe(Config.map(Duration.millis))),
    temporalityPreference: Config.option(Config.literals(["delta", "cumulative"], "OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE"))
  });
  const shutdownTimeout = Option.firstSomeOf([metricsTimeout, baseTimeout, exportTimeout]).pipe(Option.map(_ => Duration.millis(_)));
  return layer({
    url: endpoint.toString(),
    resource: options?.resource,
    headers: options?.headers ?? (yield* OtlpEnv.headers("METRICS")),
    exportInterval: Option.getOrUndefined(exportInterval),
    shutdownTimeout: Option.getOrUndefined(shutdownTimeout),
    temporality: Option.getOrUndefined(temporalityPreference)
  });
}).pipe(Effect.orDie, Layer.unwrap);
// internal
/** Creates a unique key for a metric including its attributes */
const makeMetricKey = (id, attributes) => {
  if (attributes === undefined || Object.keys(attributes).length === 0) {
    return id;
  }
  const sortedEntries = Object.entries(attributes).sort((a, b) => a[0].localeCompare(b[0]));
  return `${id}:${JSON.stringify(sortedEntries)}`;
};
/**
 * AggregationTemporality defines how a metric aggregator reports aggregated
 * values. It describes how those values relate to the time interval over
 * which they are aggregated.
 */
const EAggregationTemporality = {
  AGGREGATION_TEMPORALITY_UNSPECIFIED: 0,
  /** DELTA is an AggregationTemporality for a metric aggregator which reports
    changes since last report time. Successive metrics contain aggregation of
    values from continuous and non-overlapping intervals.
       The values for a DELTA metric are based only on the time interval
    associated with one measurement cycle. There is no dependency on
    previous measurements like is the case for CUMULATIVE metrics.
       For example, consider a system measuring the number of requests that
    it receives and reports the sum of these requests every second as a
    DELTA metric:
       1. The system starts receiving at time=t_0.
    2. A request is received, the system measures 1 request.
    3. A request is received, the system measures 1 request.
    4. A request is received, the system measures 1 request.
    5. The 1 second collection cycle ends. A metric is exported for the
        number of requests received over the interval of time t_0 to
        t_0+1 with a value of 3.
    6. A request is received, the system measures 1 request.
    7. A request is received, the system measures 1 request.
    8. The 1 second collection cycle ends. A metric is exported for the
        number of requests received over the interval of time t_0+1 to
        t_0+2 with a value of 2. */
  AGGREGATION_TEMPORALITY_DELTA: 1,
  /** CUMULATIVE is an AggregationTemporality for a metric aggregator which
    reports changes since a fixed start time. This means that current values
    of a CUMULATIVE metric depend on all previous measurements since the
    start time. Because of this, the sender is required to retain this state
    in some form. If this state is lost or invalidated, the CUMULATIVE metric
    values MUST be reset and a new fixed start time following the last
    reported measurement time sent MUST be used.
       For example, consider a system measuring the number of requests that
    it receives and reports the sum of these requests every second as a
    CUMULATIVE metric:
       1. The system starts receiving at time=t_0.
    2. A request is received, the system measures 1 request.
    3. A request is received, the system measures 1 request.
    4. A request is received, the system measures 1 request.
    5. The 1 second collection cycle ends. A metric is exported for the
        number of requests received over the interval of time t_0 to
        t_0+1 with a value of 3.
    6. A request is received, the system measures 1 request.
    7. A request is received, the system measures 1 request.
    8. The 1 second collection cycle ends. A metric is exported for the
        number of requests received over the interval of time t_0 to
        t_0+2 with a value of 5.
    9. The system experiences a fault and loses state.
    10. The system recovers and resumes receiving at time=t_1.
    11. A request is received, the system measures 1 request.
    12. The 1 second collection cycle ends. A metric is exported for the
        number of requests received over the interval of time t_1 to
        t_0+1 with a value of 1.
       Note: Even though, when reporting changes since last report time, using
    CUMULATIVE is valid, it is not recommended. This may cause problems for
    systems that do not use start_time to determine when the aggregation
    value was reset (e.g. Prometheus). */
  AGGREGATION_TEMPORALITY_CUMULATIVE: 2
};
//# sourceMappingURL=OtlpMetrics.js.map