@effect/opentelemetry 0.31.19 → 0.31.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/Metrics.js +1 -1
- package/dist/cjs/Metrics.js.map +1 -1
- package/dist/cjs/NodeSdk.js +1 -1
- package/dist/cjs/NodeSdk.js.map +1 -1
- package/dist/cjs/Resource.js +5 -5
- package/dist/cjs/Resource.js.map +1 -1
- package/dist/cjs/Tracer.js +1 -1
- package/dist/cjs/Tracer.js.map +1 -1
- package/dist/cjs/WebSdk.js +1 -1
- package/dist/cjs/WebSdk.js.map +1 -1
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/internal/metrics.js +136 -93
- package/dist/cjs/internal/metrics.js.map +1 -1
- package/dist/cjs/internal/tracer.js +1 -1
- package/dist/cjs/internal/tracer.js.map +1 -1
- package/dist/dts/Resource.d.ts.map +1 -1
- package/dist/esm/Metrics.js.map +1 -1
- package/dist/esm/NodeSdk.js.map +1 -1
- package/dist/esm/Resource.js +5 -5
- package/dist/esm/Resource.js.map +1 -1
- package/dist/esm/Tracer.js.map +1 -1
- package/dist/esm/WebSdk.js.map +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/internal/metrics.js +135 -92
- package/dist/esm/internal/metrics.js.map +1 -1
- package/dist/esm/internal/tracer.js.map +1 -1
- package/package.json +2 -2
- package/src/Resource.ts +15 -8
- package/src/internal/metrics.ts +138 -93
package/src/internal/metrics.ts
CHANGED
|
@@ -4,6 +4,7 @@ import type * as Resources from "@opentelemetry/resources"
|
|
|
4
4
|
import type {
|
|
5
5
|
CollectionResult,
|
|
6
6
|
DataPoint,
|
|
7
|
+
Histogram,
|
|
7
8
|
MetricCollectOptions,
|
|
8
9
|
MetricData,
|
|
9
10
|
MetricDescriptor,
|
|
@@ -26,13 +27,27 @@ const sdkName = "@effect/opentelemetry/Metrics"
|
|
|
26
27
|
|
|
27
28
|
/** @internal */
|
|
28
29
|
export class MetricProducerImpl implements MetricProducer {
|
|
29
|
-
constructor(readonly resource: Resources.Resource) {
|
|
30
|
+
constructor(readonly resource: Resources.Resource) {}
|
|
31
|
+
|
|
32
|
+
startTimes = new Map<string, HrTime>()
|
|
33
|
+
|
|
34
|
+
startTimeFor(name: string, hrTime: HrTime) {
|
|
35
|
+
if (this.startTimes.has(name)) {
|
|
36
|
+
return this.startTimes.get(name)!
|
|
37
|
+
}
|
|
38
|
+
this.startTimes.set(name, hrTime)
|
|
39
|
+
return hrTime
|
|
30
40
|
}
|
|
31
41
|
|
|
32
42
|
collect(_options?: MetricCollectOptions): Promise<CollectionResult> {
|
|
33
43
|
const snapshot = Metric.unsafeSnapshot()
|
|
34
44
|
const hrTimeNow = currentHrTime()
|
|
35
45
|
const metricData: Array<MetricData> = []
|
|
46
|
+
const metricDataByName = new Map<string, MetricData>()
|
|
47
|
+
const addMetricData = (data: MetricData) => {
|
|
48
|
+
metricData.push(data)
|
|
49
|
+
metricDataByName.set(data.descriptor.name, data)
|
|
50
|
+
}
|
|
36
51
|
|
|
37
52
|
for (let i = 0, len = snapshot.length; i < len; i++) {
|
|
38
53
|
const { metricKey, metricState } = snapshot[i]
|
|
@@ -41,32 +56,43 @@ export class MetricProducerImpl implements MetricProducer {
|
|
|
41
56
|
return acc
|
|
42
57
|
})
|
|
43
58
|
const descriptor = descriptorFromKey(metricKey, attributes)
|
|
59
|
+
const startTime = this.startTimeFor(descriptor.name, hrTimeNow)
|
|
44
60
|
|
|
45
61
|
if (MetricState.isCounterState(metricState)) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
62
|
+
const dataPoint: DataPoint<number> = {
|
|
63
|
+
startTime,
|
|
64
|
+
endTime: hrTimeNow,
|
|
65
|
+
attributes,
|
|
66
|
+
value: Number(metricState.count)
|
|
67
|
+
}
|
|
68
|
+
if (metricDataByName.has(descriptor.name)) {
|
|
69
|
+
metricDataByName.get(descriptor.name)!.dataPoints.push(dataPoint as any)
|
|
70
|
+
} else {
|
|
71
|
+
addMetricData({
|
|
72
|
+
dataPointType: DataPointType.SUM,
|
|
73
|
+
descriptor,
|
|
74
|
+
isMonotonic: descriptor.type === InstrumentType.COUNTER,
|
|
75
|
+
aggregationTemporality: AggregationTemporality.CUMULATIVE,
|
|
76
|
+
dataPoints: [dataPoint]
|
|
77
|
+
})
|
|
78
|
+
}
|
|
58
79
|
} else if (MetricState.isGaugeState(metricState)) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
80
|
+
const dataPoint: DataPoint<number> = {
|
|
81
|
+
startTime,
|
|
82
|
+
endTime: hrTimeNow,
|
|
83
|
+
attributes,
|
|
84
|
+
value: Number(metricState.value)
|
|
85
|
+
}
|
|
86
|
+
if (metricDataByName.has(descriptor.name)) {
|
|
87
|
+
metricDataByName.get(descriptor.name)!.dataPoints.push(dataPoint as any)
|
|
88
|
+
} else {
|
|
89
|
+
addMetricData({
|
|
90
|
+
dataPointType: DataPointType.GAUGE,
|
|
91
|
+
descriptor,
|
|
92
|
+
aggregationTemporality: AggregationTemporality.CUMULATIVE,
|
|
93
|
+
dataPoints: [dataPoint]
|
|
94
|
+
})
|
|
95
|
+
}
|
|
70
96
|
} else if (MetricState.isHistogramState(metricState)) {
|
|
71
97
|
const size = metricState.buckets.length
|
|
72
98
|
const buckets = {
|
|
@@ -83,29 +109,34 @@ export class MetricProducerImpl implements MetricProducer {
|
|
|
83
109
|
prev = value
|
|
84
110
|
i++
|
|
85
111
|
}
|
|
112
|
+
const dataPoint: DataPoint<Histogram> = {
|
|
113
|
+
startTime,
|
|
114
|
+
endTime: hrTimeNow,
|
|
115
|
+
attributes,
|
|
116
|
+
value: {
|
|
117
|
+
buckets,
|
|
118
|
+
count: metricState.count,
|
|
119
|
+
min: metricState.min,
|
|
120
|
+
max: metricState.max,
|
|
121
|
+
sum: metricState.sum
|
|
122
|
+
}
|
|
123
|
+
}
|
|
86
124
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
count: metricState.count,
|
|
98
|
-
min: metricState.min,
|
|
99
|
-
max: metricState.max,
|
|
100
|
-
sum: metricState.sum
|
|
101
|
-
}
|
|
102
|
-
}]
|
|
103
|
-
})
|
|
125
|
+
if (metricDataByName.has(descriptor.name)) {
|
|
126
|
+
metricDataByName.get(descriptor.name)!.dataPoints.push(dataPoint as any)
|
|
127
|
+
} else {
|
|
128
|
+
addMetricData({
|
|
129
|
+
dataPointType: DataPointType.HISTOGRAM,
|
|
130
|
+
descriptor,
|
|
131
|
+
aggregationTemporality: AggregationTemporality.CUMULATIVE,
|
|
132
|
+
dataPoints: [dataPoint]
|
|
133
|
+
})
|
|
134
|
+
}
|
|
104
135
|
} else if (MetricState.isFrequencyState(metricState)) {
|
|
105
136
|
const dataPoints: Array<DataPoint<number>> = []
|
|
106
137
|
for (const [freqKey, value] of metricState.occurrences) {
|
|
107
138
|
dataPoints.push({
|
|
108
|
-
startTime
|
|
139
|
+
startTime,
|
|
109
140
|
endTime: hrTimeNow,
|
|
110
141
|
attributes: {
|
|
111
142
|
...attributes,
|
|
@@ -114,76 +145,90 @@ export class MetricProducerImpl implements MetricProducer {
|
|
|
114
145
|
value
|
|
115
146
|
})
|
|
116
147
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
descriptor
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
148
|
+
if (metricDataByName.has(descriptor.name)) {
|
|
149
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
150
|
+
metricDataByName.get(descriptor.name)!.dataPoints.push(...dataPoints as any)
|
|
151
|
+
} else {
|
|
152
|
+
addMetricData({
|
|
153
|
+
dataPointType: DataPointType.SUM,
|
|
154
|
+
descriptor: descriptorFromKey(metricKey, attributes),
|
|
155
|
+
aggregationTemporality: AggregationTemporality.CUMULATIVE,
|
|
156
|
+
isMonotonic: true,
|
|
157
|
+
dataPoints
|
|
158
|
+
})
|
|
159
|
+
}
|
|
124
160
|
} else if (MetricState.isSummaryState(metricState)) {
|
|
125
161
|
const dataPoints: Array<DataPoint<number>> = [{
|
|
126
|
-
startTime
|
|
162
|
+
startTime,
|
|
127
163
|
endTime: hrTimeNow,
|
|
128
164
|
attributes: { ...attributes, quantile: "min" },
|
|
129
165
|
value: metricState.min
|
|
130
166
|
}]
|
|
131
167
|
for (const [quantile, value] of metricState.quantiles) {
|
|
132
168
|
dataPoints.push({
|
|
133
|
-
startTime
|
|
169
|
+
startTime,
|
|
134
170
|
endTime: hrTimeNow,
|
|
135
171
|
attributes: { ...attributes, quantile: quantile.toString() },
|
|
136
172
|
value: value._tag === "Some" ? value.value : 0
|
|
137
173
|
})
|
|
138
174
|
}
|
|
139
175
|
dataPoints.push({
|
|
140
|
-
startTime
|
|
176
|
+
startTime,
|
|
141
177
|
endTime: hrTimeNow,
|
|
142
178
|
attributes: { ...attributes, quantile: "max" },
|
|
143
179
|
value: metricState.max
|
|
144
180
|
})
|
|
181
|
+
const countDataPoint: DataPoint<number> = {
|
|
182
|
+
startTime,
|
|
183
|
+
endTime: hrTimeNow,
|
|
184
|
+
attributes,
|
|
185
|
+
value: metricState.count
|
|
186
|
+
}
|
|
187
|
+
const sumDataPoint: DataPoint<number> = {
|
|
188
|
+
startTime,
|
|
189
|
+
endTime: hrTimeNow,
|
|
190
|
+
attributes,
|
|
191
|
+
value: metricState.sum
|
|
192
|
+
}
|
|
145
193
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
descriptor
|
|
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
|
-
value: metricState.sum
|
|
185
|
-
}]
|
|
186
|
-
})
|
|
194
|
+
if (metricDataByName.has(`${descriptor.name}_quantiles`)) {
|
|
195
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
196
|
+
metricDataByName.get(`${descriptor.name}_quantiles`)!.dataPoints.push(...dataPoints as any)
|
|
197
|
+
metricDataByName.get(`${descriptor.name}_count`)!.dataPoints.push(countDataPoint as any)
|
|
198
|
+
metricDataByName.get(`${descriptor.name}_sum`)!.dataPoints.push(sumDataPoint as any)
|
|
199
|
+
} else {
|
|
200
|
+
addMetricData({
|
|
201
|
+
dataPointType: DataPointType.SUM,
|
|
202
|
+
descriptor: descriptorFromKey(metricKey, attributes, "quantiles"),
|
|
203
|
+
aggregationTemporality: AggregationTemporality.CUMULATIVE,
|
|
204
|
+
isMonotonic: false,
|
|
205
|
+
dataPoints
|
|
206
|
+
})
|
|
207
|
+
addMetricData({
|
|
208
|
+
dataPointType: DataPointType.SUM,
|
|
209
|
+
descriptor: {
|
|
210
|
+
...descriptorMeta(metricKey, "count"),
|
|
211
|
+
unit: "1",
|
|
212
|
+
type: InstrumentType.COUNTER,
|
|
213
|
+
valueType: ValueType.INT
|
|
214
|
+
},
|
|
215
|
+
aggregationTemporality: AggregationTemporality.CUMULATIVE,
|
|
216
|
+
isMonotonic: true,
|
|
217
|
+
dataPoints: [countDataPoint]
|
|
218
|
+
})
|
|
219
|
+
addMetricData({
|
|
220
|
+
dataPointType: DataPointType.SUM,
|
|
221
|
+
descriptor: {
|
|
222
|
+
...descriptorMeta(metricKey, "sum"),
|
|
223
|
+
unit: "1",
|
|
224
|
+
type: InstrumentType.COUNTER,
|
|
225
|
+
valueType: ValueType.DOUBLE
|
|
226
|
+
},
|
|
227
|
+
aggregationTemporality: AggregationTemporality.CUMULATIVE,
|
|
228
|
+
isMonotonic: true,
|
|
229
|
+
dataPoints: [sumDataPoint]
|
|
230
|
+
})
|
|
231
|
+
}
|
|
187
232
|
}
|
|
188
233
|
}
|
|
189
234
|
|