@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 473 lines 16.6 KB
Raw
1 import { type HrTime, ValueType } from "@opentelemetry/api"
2 import type * as Resources from "@opentelemetry/resources"
3 import type {
4 CollectionResult,
5 DataPoint,
6 Histogram,
7 MetricCollectOptions,
8 MetricData,
9 MetricProducer
10 } from "@opentelemetry/sdk-metrics"
11 import { AggregationTemporality, DataPointType, InstrumentType } from "@opentelemetry/sdk-metrics"
12 import type { InstrumentDescriptor } from "@opentelemetry/sdk-metrics/build/src/InstrumentDescriptor.js"
13 import * as Arr from "effect/Array"
14 import type * as Context from "effect/Context"
15 import * as Metric from "effect/Metric"
16 import * as Rec from "effect/Record"
17 import type * as Metrics from "../OtelMetrics.ts"
18
19 const sdkName = "@effect/opentelemetry/Metrics"
20
21 type MetricDataWithInstrumentDescriptor = MetricData & {
22 readonly descriptor: InstrumentDescriptor
23 }
24
25 interface PreviousHistogramState {
26 readonly count: number
27 readonly sum: number
28 readonly bucketCounts: ReadonlyArray<number>
29 }
30
31 interface PreviousSummaryState {
32 readonly count: number
33 readonly sum: number
34 }
35
36 /** @internal */
37 export class MetricProducerImpl implements MetricProducer {
38 resource: Resources.Resource
39 context: Context.Context<never>
40 temporality: Metrics.TemporalityPreference
41 startTimes: Map<string, HrTime>
42 startTimeNanos: HrTime
43 previousExportTimeNanos: HrTime
44 previousCounterState: Map<string, number | bigint>
45 previousHistogramState: Map<string, PreviousHistogramState>
46 previousFrequencyState: Map<string, Map<string, number>>
47 previousSummaryState: Map<string, PreviousSummaryState>
48
49 constructor(
50 resource: Resources.Resource,
51 context: Context.Context<never>,
52 temporality: Metrics.TemporalityPreference = "cumulative"
53 ) {
54 this.resource = resource
55 this.context = context
56 this.temporality = temporality
57 this.startTimes = new Map()
58 this.startTimeNanos = currentHrTime()
59 this.previousExportTimeNanos = this.startTimeNanos
60 this.previousCounterState = new Map()
61 this.previousHistogramState = new Map()
62 this.previousFrequencyState = new Map()
63 this.previousSummaryState = new Map()
64 }
65
66 fork(): MetricProducerImpl {
67 return new MetricProducerImpl(this.resource, this.context, this.temporality)
68 }
69
70 startTimeFor(name: string, hrTime: HrTime) {
71 if (this.startTimes.has(name)) {
72 return this.startTimes.get(name)!
73 }
74 this.startTimes.set(name, hrTime)
75 return hrTime
76 }
77
78 collect(_options?: MetricCollectOptions): Promise<CollectionResult> {
79 const snapshot = Metric.snapshotUnsafe(this.context)
80 const hrTimeNow = currentHrTime()
81 // Only series in this snapshot carry their delta state forward, so removed
82 // series do not leak state or report a negative delta when recreated.
83 const previousCounterState = this.previousCounterState
84 const previousHistogramState = this.previousHistogramState
85 const previousFrequencyState = this.previousFrequencyState
86 const previousSummaryState = this.previousSummaryState
87 this.previousCounterState = new Map()
88 this.previousHistogramState = new Map()
89 this.previousFrequencyState = new Map()
90 this.previousSummaryState = new Map()
91 const metricData: Array<MetricData> = []
92 const metricDataByName = new Map<string, MetricData>()
93 const addMetricData = (data: MetricDataWithInstrumentDescriptor) => {
94 metricData.push(data)
95 metricDataByName.set(data.descriptor.name, data)
96 }
97
98 const isDelta = this.temporality === "delta"
99 const aggregationTemporality = isDelta
100 ? AggregationTemporality.DELTA
101 : AggregationTemporality.CUMULATIVE
102 const intervalStartTime = isDelta
103 ? this.previousExportTimeNanos
104 : this.startTimeNanos
105
106 for (let i = 0, len = snapshot.length; i < len; i++) {
107 const state = snapshot[i]
108 const attributes = state.attributes
109 ? Arr.reduce(Object.entries(state.attributes), {} as Record<string, string>, (acc, [key, value]) => {
110 Rec.assignProperty(acc, key, String(value))
111 return acc
112 })
113 : {}
114 const metricKey = makeMetricKey(state.id, state.attributes)
115
116 switch (state.type) {
117 case "Counter": {
118 const currentCount = state.state.count
119 let reportValue: number | bigint = currentCount
120
121 if (isDelta) {
122 const previousCount = previousCounterState.get(metricKey)
123 if (previousCount !== undefined) {
124 if (typeof currentCount === "bigint" && typeof previousCount === "bigint") {
125 reportValue = currentCount - previousCount
126 // Handle reset: if current < previous, report current value
127 if (state.state.incremental && reportValue < BigInt(0)) {
128 reportValue = currentCount
129 }
130 } else {
131 const curr = Number(currentCount)
132 const prev = Number(previousCount)
133 reportValue = curr - prev
134 // Handle reset
135 if (state.state.incremental && reportValue < 0) {
136 reportValue = curr
137 }
138 }
139 }
140 this.previousCounterState.set(metricKey, currentCount)
141 }
142
143 const descriptor = descriptorFromState(state, attributes)
144 const startTime = isDelta ? intervalStartTime : this.startTimeFor(descriptor.name, intervalStartTime)
145 const dataPoint: DataPoint<number> = {
146 startTime,
147 endTime: hrTimeNow,
148 attributes,
149 value: Number(reportValue)
150 }
151 if (metricDataByName.has(state.id)) {
152 metricDataByName.get(state.id)!.dataPoints.push(dataPoint as any)
153 } else {
154 addMetricData({
155 dataPointType: DataPointType.SUM,
156 descriptor,
157 isMonotonic: state.state.incremental,
158 aggregationTemporality,
159 dataPoints: [dataPoint]
160 })
161 }
162 break
163 }
164 case "Gauge": {
165 // Gauges don't have temporality - they always report current value
166 const descriptor = descriptorFromState(state, attributes)
167 const startTime = this.startTimeFor(descriptor.name, this.startTimeNanos)
168 const dataPoint: DataPoint<number> = {
169 startTime,
170 endTime: hrTimeNow,
171 attributes,
172 value: Number(state.state.value)
173 }
174 if (metricDataByName.has(state.id)) {
175 metricDataByName.get(state.id)!.dataPoints.push(dataPoint as any)
176 } else {
177 addMetricData({
178 dataPointType: DataPointType.GAUGE,
179 descriptor,
180 aggregationTemporality: AggregationTemporality.CUMULATIVE,
181 dataPoints: [dataPoint]
182 })
183 }
184 break
185 }
186 case "Histogram": {
187 const currentBuckets = {
188 boundaries: [] as Array<number>,
189 counts: [] as Array<number>
190 }
191 let prev = 0
192 for (const [boundary, value] of state.state.buckets) {
193 if (boundary === Number.POSITIVE_INFINITY) break
194 currentBuckets.boundaries.push(boundary)
195 currentBuckets.counts.push(value - prev)
196 prev = value
197 }
198 // Derive overflow from the total count, whether or not boundaries include Infinity.
199 currentBuckets.counts.push(state.state.count - prev)
200
201 let reportCount = state.state.count
202 let reportSum = state.state.sum
203 let reportBucketCounts = currentBuckets.counts
204 let reportExtrema = true
205
206 if (isDelta) {
207 const previousState = previousHistogramState.get(metricKey)
208 if (previousState !== undefined) {
209 reportCount = state.state.count - previousState.count
210 reportSum = state.state.sum - previousState.sum
211 reportBucketCounts = currentBuckets.counts.map((c, i) =>
212 Math.max(0, c - (previousState.bucketCounts[i] ?? 0))
213 )
214 // Interval extrema cannot be derived from cumulative min / max,
215 // so omit the optional fields rather than report stale values
216 reportExtrema = false
217 }
218 this.previousHistogramState.set(metricKey, {
219 count: state.state.count,
220 sum: state.state.sum,
221 bucketCounts: currentBuckets.counts.slice()
222 })
223 }
224
225 const descriptor = descriptorFromState(state, attributes)
226 const startTime = isDelta ? intervalStartTime : this.startTimeFor(descriptor.name, intervalStartTime)
227 const dataPoint: DataPoint<Histogram> = {
228 startTime,
229 endTime: hrTimeNow,
230 attributes,
231 value: {
232 buckets: {
233 boundaries: currentBuckets.boundaries,
234 counts: reportBucketCounts
235 },
236 count: reportCount,
237 ...(reportExtrema ? { min: state.state.min, max: state.state.max } : undefined),
238 sum: reportSum
239 }
240 }
241
242 if (metricDataByName.has(state.id)) {
243 metricDataByName.get(state.id)!.dataPoints.push(dataPoint as any)
244 } else {
245 addMetricData({
246 dataPointType: DataPointType.HISTOGRAM,
247 descriptor,
248 aggregationTemporality,
249 dataPoints: [dataPoint]
250 })
251 }
252 break
253 }
254 case "Frequency": {
255 const dataPoints: Array<DataPoint<number>> = []
256 const currentOccurrences = new Map<string, number>()
257
258 for (const [freqKey, value] of state.state.occurrences) {
259 currentOccurrences.set(freqKey, value)
260 let reportValue = value
261
262 if (isDelta) {
263 const previousOccurrences = previousFrequencyState.get(metricKey)
264 if (previousOccurrences !== undefined) {
265 const previousValue = previousOccurrences.get(freqKey) ?? 0
266 reportValue = Math.max(0, value - previousValue)
267 }
268 }
269
270 const descriptor = descriptorFromState(state, attributes)
271 const startTime = isDelta ? intervalStartTime : this.startTimeFor(descriptor.name, intervalStartTime)
272 dataPoints.push({
273 startTime,
274 endTime: hrTimeNow,
275 attributes: {
276 ...attributes,
277 key: freqKey
278 },
279 value: reportValue
280 })
281 }
282
283 if (isDelta) {
284 this.previousFrequencyState.set(metricKey, currentOccurrences)
285 }
286
287 if (metricDataByName.has(state.id)) {
288 // oxlint-disable-next-line no-restricted-syntax
289 metricDataByName.get(state.id)!.dataPoints.push(...dataPoints as any)
290 } else {
291 const descriptor = descriptorFromState(state, attributes)
292 addMetricData({
293 dataPointType: DataPointType.SUM,
294 descriptor,
295 aggregationTemporality,
296 isMonotonic: true,
297 dataPoints
298 })
299 }
300 break
301 }
302 case "Summary": {
303 // Quantiles are always computed fresh from the sliding window
304 const dataPoints: Array<DataPoint<number>> = [{
305 startTime: intervalStartTime,
306 endTime: hrTimeNow,
307 attributes: { ...attributes, quantile: "min" },
308 value: state.state.min
309 }]
310 for (const [quantile, value] of state.state.quantiles) {
311 dataPoints.push({
312 startTime: intervalStartTime,
313 endTime: hrTimeNow,
314 attributes: { ...attributes, quantile: quantile.toString() },
315 value: value ?? 0
316 })
317 }
318 dataPoints.push({
319 startTime: intervalStartTime,
320 endTime: hrTimeNow,
321 attributes: { ...attributes, quantile: "max" },
322 value: state.state.max
323 })
324
325 let reportCount = state.state.count
326 let reportSum = state.state.sum
327
328 if (isDelta) {
329 const previousState = previousSummaryState.get(metricKey)
330 if (previousState !== undefined) {
331 reportCount = state.state.count - previousState.count
332 reportSum = state.state.sum - previousState.sum
333 }
334 this.previousSummaryState.set(metricKey, {
335 count: state.state.count,
336 sum: state.state.sum
337 })
338 }
339
340 const countDataPoint: DataPoint<number> = {
341 startTime: intervalStartTime,
342 endTime: hrTimeNow,
343 attributes,
344 value: reportCount
345 }
346 const sumDataPoint: DataPoint<number> = {
347 startTime: intervalStartTime,
348 endTime: hrTimeNow,
349 attributes,
350 value: reportSum
351 }
352
353 if (metricDataByName.has(`${state.id}_quantiles`)) {
354 // oxlint-disable-next-line no-restricted-syntax
355 metricDataByName.get(`${state.id}_quantiles`)!.dataPoints.push(...dataPoints as any)
356 metricDataByName.get(`${state.id}_count`)!.dataPoints.push(countDataPoint as any)
357 metricDataByName.get(`${state.id}_sum`)!.dataPoints.push(sumDataPoint as any)
358 } else {
359 const descriptor = descriptorFromState(state, attributes)
360 addMetricData({
361 dataPointType: DataPointType.SUM,
362 descriptor: {
363 ...descriptor,
364 name: `${descriptor.name}_quantiles`
365 },
366 aggregationTemporality,
367 isMonotonic: false,
368 dataPoints
369 })
370 addMetricData({
371 dataPointType: DataPointType.SUM,
372 descriptor: {
373 name: `${state.id}_count`,
374 description: state.description ?? "",
375 unit: "1",
376 type: InstrumentType.COUNTER,
377 valueType: ValueType.INT,
378 advice: {}
379 },
380 aggregationTemporality,
381 isMonotonic: true,
382 dataPoints: [countDataPoint]
383 })
384 addMetricData({
385 dataPointType: DataPointType.SUM,
386 descriptor: {
387 name: `${state.id}_sum`,
388 description: state.description ?? "",
389 unit: "1",
390 type: InstrumentType.COUNTER,
391 valueType: ValueType.DOUBLE,
392 advice: {}
393 },
394 aggregationTemporality,
395 isMonotonic: true,
396 dataPoints: [sumDataPoint]
397 })
398 }
399 break
400 }
401 }
402 }
403
404 // Update the previous export time for delta calculations
405 if (isDelta) {
406 this.previousExportTimeNanos = hrTimeNow
407 }
408
409 return Promise.resolve({
410 resourceMetrics: {
411 resource: this.resource,
412 scopeMetrics: [{
413 scope: { name: sdkName },
414 metrics: metricData
415 }]
416 },
417 errors: []
418 })
419 }
420 }
421
422 /** Creates a unique key for a metric including its attributes */
423 const makeMetricKey = (id: string, attributes: Metric.Metric.AttributeSet | undefined): string => {
424 if (attributes === undefined || Object.keys(attributes).length === 0) {
425 return id
426 }
427 const sortedEntries = Object.entries(attributes).sort((a, b) => a[0].localeCompare(b[0]))
428 return `${id}:${JSON.stringify(sortedEntries)}`
429 }
430
431 const currentHrTime = (): HrTime => {
432 const now = Date.now()
433 return [Math.floor(now / 1000), (now % 1000) * 1000000]
434 }
435
436 const descriptorFromState = (
437 state: Metric.Metric.Snapshot,
438 attributes: Record<string, string>
439 ): InstrumentDescriptor => {
440 const unit = attributes.unit ?? attributes.time_unit ?? "1"
441 return {
442 name: state.id,
443 description: state.description ?? "",
444 unit,
445 type: instrumentTypeFromSnapshot(state),
446 valueType: determineValueType(state),
447 advice: {}
448 }
449 }
450
451 const instrumentTypeFromSnapshot = (state: Metric.Metric.Snapshot): InstrumentType => {
452 switch (state.type) {
453 case "Histogram":
454 return InstrumentType.HISTOGRAM
455 case "Gauge":
456 return InstrumentType.OBSERVABLE_GAUGE
457 case "Frequency":
458 return InstrumentType.COUNTER
459 case "Counter":
460 return state.state.incremental ? InstrumentType.COUNTER : InstrumentType.UP_DOWN_COUNTER
461 case "Summary":
462 return InstrumentType.COUNTER
463 }
464 }
465
466 const determineValueType = (state: Metric.Metric.Snapshot): ValueType => {
467 if (state.type === "Counter") {
468 return typeof state.state.count === "bigint" ? ValueType.INT : ValueType.DOUBLE
469 } else if (state.type === "Gauge") {
470 return typeof state.state.value === "bigint" ? ValueType.INT : ValueType.DOUBLE
471 }
472 return ValueType.DOUBLE
473 }