The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as internal from "@effect/opentelemetry/internal/metrics" |
| 2 | import * as OtelMetrics from "@effect/opentelemetry/OtelMetrics" |
| 3 | import { assert, describe, it } from "@effect/vitest" |
| 4 | import { ValueType } from "@opentelemetry/api" |
| 5 | import { resourceFromAttributes } from "@opentelemetry/resources" |
| 6 | import { MetricReader } from "@opentelemetry/sdk-metrics" |
| 7 | import * as Cause from "effect/Cause" |
| 8 | import * as Effect from "effect/Effect" |
| 9 | import * as Exit from "effect/Exit" |
| 10 | import * as Metric from "effect/Metric" |
| 11 | |
| 12 | const findMetric = (metrics: any, name: string) => |
| 13 | metrics.resourceMetrics.scopeMetrics[0].metrics.find((_: any) => _.descriptor.name === name) |
| 14 | |
| 15 | class ShutdownTrackingReader extends MetricReader { |
| 16 | shutdowns = 0 |
| 17 | |
| 18 | constructor(private readonly rejectShutdown = false) { |
| 19 | super() |
| 20 | } |
| 21 | |
| 22 | protected onShutdown(): Promise<void> { |
| 23 | this.shutdowns++ |
| 24 | return this.rejectShutdown ? Promise.reject(new Error("shutdown failed")) : Promise.resolve() |
| 25 | } |
| 26 | |
| 27 | protected onForceFlush(): Promise<void> { |
| 28 | return Promise.resolve() |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | describe("Metrics", () => { |
| 33 | it.effect.each([false, true])( |
| 34 | "rolls back bound readers and preserves the binding defect (shutdown rejects: %s)", |
| 35 | (rejectShutdown) => |
| 36 | Effect.gen(function*() { |
| 37 | const services = yield* Effect.context<never>() |
| 38 | const producer = new internal.MetricProducerImpl(resourceFromAttributes({}), services) |
| 39 | const fresh = new ShutdownTrackingReader(rejectShutdown) |
| 40 | const alreadyBound = new ShutdownTrackingReader() |
| 41 | alreadyBound.setMetricProducer(producer) |
| 42 | |
| 43 | const exit = yield* OtelMetrics.registerProducer(producer, () => [fresh, alreadyBound]).pipe( |
| 44 | Effect.scoped, |
| 45 | Effect.exit |
| 46 | ) |
| 47 | |
| 48 | assert.isTrue(Exit.isFailure(exit) && Cause.hasDies(exit.cause)) |
| 49 | assert.strictEqual(alreadyBound.shutdowns, 0) |
| 50 | assert.strictEqual(fresh.shutdowns, 1) |
| 51 | if (Exit.isFailure(exit)) { |
| 52 | assert.include(Cause.pretty(exit.cause), "MetricReader can not be bound to a MeterProvider again.") |
| 53 | } |
| 54 | }) |
| 55 | ) |
| 56 | |
| 57 | it.effect("gauge", () => |
| 58 | Effect.gen(function*() { |
| 59 | const services = yield* Effect.context<never>() |
| 60 | const resource = resourceFromAttributes({ |
| 61 | name: "test", |
| 62 | version: "1.0.0" |
| 63 | }) |
| 64 | const producer = new internal.MetricProducerImpl(resource, services) |
| 65 | const gauge = Metric.gauge("rps") |
| 66 | |
| 67 | yield* Metric.withAttributes(gauge, { |
| 68 | key: "value", |
| 69 | unit: "requests" |
| 70 | }).pipe(Metric.update(10)) |
| 71 | yield* Metric.withAttributes(gauge, { |
| 72 | key: "value" |
| 73 | }).pipe(Metric.update(10)) |
| 74 | yield* Metric.withAttributes(gauge, { |
| 75 | key: "value" |
| 76 | }).pipe(Metric.update(20)) |
| 77 | |
| 78 | const results = yield* Effect.promise(() => producer.collect()) |
| 79 | const object = JSON.parse(JSON.stringify(results)) |
| 80 | assert.deepEqual(object.resourceMetrics.resource._rawAttributes, [ |
| 81 | ["name", "test"], |
| 82 | ["version", "1.0.0"] |
| 83 | ]) |
| 84 | assert.equal(object.resourceMetrics.scopeMetrics.length, 1) |
| 85 | const metric = findMetric(object, "rps") |
| 86 | assert.deepStrictEqual(metric, { |
| 87 | "dataPointType": 2, |
| 88 | "descriptor": { |
| 89 | "advice": {}, |
| 90 | "name": "rps", |
| 91 | "description": "", |
| 92 | "unit": "requests", |
| 93 | "type": "OBSERVABLE_GAUGE", |
| 94 | "valueType": ValueType.DOUBLE |
| 95 | }, |
| 96 | "aggregationTemporality": 1, |
| 97 | "dataPoints": [ |
| 98 | { |
| 99 | "startTime": metric.dataPoints[0].startTime, |
| 100 | "endTime": metric.dataPoints[0].endTime, |
| 101 | "attributes": { |
| 102 | "unit": "requests", |
| 103 | "key": "value" |
| 104 | }, |
| 105 | "value": 10 |
| 106 | }, |
| 107 | { |
| 108 | "startTime": metric.dataPoints[0].startTime, |
| 109 | "endTime": metric.dataPoints[0].endTime, |
| 110 | "attributes": { |
| 111 | "key": "value" |
| 112 | }, |
| 113 | "value": 20 |
| 114 | } |
| 115 | ] |
| 116 | }) |
| 117 | })) |
| 118 | |
| 119 | it.effect("gauge bigint", () => |
| 120 | Effect.gen(function*() { |
| 121 | const services = yield* Effect.context<never>() |
| 122 | const producer = new internal.MetricProducerImpl( |
| 123 | resourceFromAttributes({ |
| 124 | name: "test", |
| 125 | version: "1.0.0" |
| 126 | }), |
| 127 | services |
| 128 | ) |
| 129 | const gauge = Metric.gauge("rps-bigint", { bigint: true }) |
| 130 | |
| 131 | yield* Metric.withAttributes(gauge, { |
| 132 | key: "value", |
| 133 | unit: "requests" |
| 134 | }).pipe(Metric.update(10n)) |
| 135 | yield* Metric.withAttributes(gauge, { |
| 136 | key: "value" |
| 137 | }).pipe(Metric.update(10n)) |
| 138 | yield* Metric.withAttributes(gauge, { |
| 139 | key: "value" |
| 140 | }).pipe(Metric.update(20n)) |
| 141 | |
| 142 | const results = yield* Effect.promise(() => producer.collect()) |
| 143 | const object = JSON.parse(JSON.stringify(results)) |
| 144 | assert.deepEqual(object.resourceMetrics.resource._rawAttributes, [ |
| 145 | ["name", "test"], |
| 146 | ["version", "1.0.0"] |
| 147 | ]) |
| 148 | assert.equal(object.resourceMetrics.scopeMetrics.length, 1) |
| 149 | const metric = findMetric(object, "rps-bigint") |
| 150 | assert.deepEqual(metric, { |
| 151 | "dataPointType": 2, |
| 152 | "descriptor": { |
| 153 | "advice": {}, |
| 154 | "name": "rps-bigint", |
| 155 | "description": "", |
| 156 | "unit": "requests", |
| 157 | "type": "OBSERVABLE_GAUGE", |
| 158 | "valueType": ValueType.INT |
| 159 | }, |
| 160 | "aggregationTemporality": 1, |
| 161 | "dataPoints": [ |
| 162 | { |
| 163 | "startTime": metric.dataPoints[0].startTime, |
| 164 | "endTime": metric.dataPoints[0].endTime, |
| 165 | "attributes": { |
| 166 | "unit": "requests", |
| 167 | "key": "value" |
| 168 | }, |
| 169 | "value": 10 |
| 170 | }, |
| 171 | { |
| 172 | "startTime": metric.dataPoints[0].startTime, |
| 173 | "endTime": metric.dataPoints[0].endTime, |
| 174 | "attributes": { |
| 175 | "key": "value" |
| 176 | }, |
| 177 | "value": 20 |
| 178 | } |
| 179 | ] |
| 180 | }) |
| 181 | })) |
| 182 | |
| 183 | it.effect("counter", () => |
| 184 | Effect.gen(function*() { |
| 185 | const services = yield* Effect.context<never>() |
| 186 | const producer = new internal.MetricProducerImpl( |
| 187 | resourceFromAttributes({ |
| 188 | name: "test", |
| 189 | version: "1.0.0" |
| 190 | }), |
| 191 | services |
| 192 | ) |
| 193 | const counter = Metric.counter("counter", { description: "Example" }) |
| 194 | |
| 195 | yield* Metric.withAttributes(counter, { |
| 196 | key: "value", |
| 197 | unit: "requests" |
| 198 | }).pipe(Metric.update(1)) |
| 199 | yield* Metric.withAttributes(counter, { |
| 200 | key: "value" |
| 201 | }).pipe(Metric.update(1)) |
| 202 | yield* Metric.withAttributes(counter, { |
| 203 | key: "value" |
| 204 | }).pipe(Metric.update(1)) |
| 205 | |
| 206 | const results = yield* Effect.promise(() => producer.collect()) |
| 207 | const object = JSON.parse(JSON.stringify(results)) |
| 208 | assert.deepEqual(object.resourceMetrics.resource._rawAttributes, [ |
| 209 | ["name", "test"], |
| 210 | ["version", "1.0.0"] |
| 211 | ]) |
| 212 | assert.equal(object.resourceMetrics.scopeMetrics.length, 1) |
| 213 | const metric = findMetric(object, "counter") |
| 214 | assert.deepEqual(metric, { |
| 215 | "dataPointType": 3, |
| 216 | "descriptor": { |
| 217 | "advice": {}, |
| 218 | "name": "counter", |
| 219 | "description": "Example", |
| 220 | "unit": "requests", |
| 221 | "type": "UP_DOWN_COUNTER", |
| 222 | "valueType": ValueType.DOUBLE |
| 223 | }, |
| 224 | "isMonotonic": false, |
| 225 | "aggregationTemporality": 1, |
| 226 | "dataPoints": [ |
| 227 | { |
| 228 | "startTime": metric.dataPoints[0].startTime, |
| 229 | "endTime": metric.dataPoints[0].endTime, |
| 230 | "attributes": { |
| 231 | "unit": "requests", |
| 232 | "key": "value" |
| 233 | }, |
| 234 | "value": 1 |
| 235 | }, |
| 236 | { |
| 237 | "startTime": metric.dataPoints[0].startTime, |
| 238 | "endTime": metric.dataPoints[0].endTime, |
| 239 | "attributes": { |
| 240 | "key": "value" |
| 241 | }, |
| 242 | "value": 2 |
| 243 | } |
| 244 | ] |
| 245 | }) |
| 246 | })) |
| 247 | |
| 248 | it.effect("counter-inc", () => |
| 249 | Effect.gen(function*() { |
| 250 | const services = yield* Effect.context<never>() |
| 251 | const producer = new internal.MetricProducerImpl( |
| 252 | resourceFromAttributes({ |
| 253 | name: "test", |
| 254 | version: "1.0.0" |
| 255 | }), |
| 256 | services |
| 257 | ) |
| 258 | const counter = Metric.counter("counter-inc", { |
| 259 | description: "Example", |
| 260 | incremental: true |
| 261 | }) |
| 262 | |
| 263 | yield* Metric.withAttributes(counter, { |
| 264 | key: "value", |
| 265 | unit: "requests" |
| 266 | }).pipe(Metric.update(1)) |
| 267 | yield* Metric.withAttributes(counter, { |
| 268 | key: "value" |
| 269 | }).pipe(Metric.update(1)) |
| 270 | yield* Metric.withAttributes(counter, { |
| 271 | key: "value" |
| 272 | }).pipe(Metric.update(1)) |
| 273 | |
| 274 | const results = yield* Effect.promise(() => producer.collect()) |
| 275 | const object = JSON.parse(JSON.stringify(results)) |
| 276 | assert.deepEqual(object.resourceMetrics.resource._rawAttributes, [ |
| 277 | ["name", "test"], |
| 278 | ["version", "1.0.0"] |
| 279 | ]) |
| 280 | assert.equal(object.resourceMetrics.scopeMetrics.length, 1) |
| 281 | const metric = findMetric(object, "counter-inc") |
| 282 | assert.deepEqual(metric, { |
| 283 | "dataPointType": 3, |
| 284 | "descriptor": { |
| 285 | "advice": {}, |
| 286 | "name": "counter-inc", |
| 287 | "description": "Example", |
| 288 | "unit": "requests", |
| 289 | "type": "COUNTER", |
| 290 | "valueType": ValueType.DOUBLE |
| 291 | }, |
| 292 | "isMonotonic": true, |
| 293 | "aggregationTemporality": 1, |
| 294 | "dataPoints": [ |
| 295 | { |
| 296 | "startTime": metric.dataPoints[0].startTime, |
| 297 | "endTime": metric.dataPoints[0].endTime, |
| 298 | "attributes": { |
| 299 | "unit": "requests", |
| 300 | "key": "value" |
| 301 | }, |
| 302 | "value": 1 |
| 303 | }, |
| 304 | { |
| 305 | "startTime": metric.dataPoints[0].startTime, |
| 306 | "endTime": metric.dataPoints[0].endTime, |
| 307 | "attributes": { |
| 308 | "key": "value" |
| 309 | }, |
| 310 | "value": 2 |
| 311 | } |
| 312 | ] |
| 313 | }) |
| 314 | })) |
| 315 | |
| 316 | it.effect("counter-bigint", () => |
| 317 | Effect.gen(function*() { |
| 318 | const services = yield* Effect.context<never>() |
| 319 | const producer = new internal.MetricProducerImpl( |
| 320 | resourceFromAttributes({ |
| 321 | name: "test", |
| 322 | version: "1.0.0" |
| 323 | }), |
| 324 | services |
| 325 | ) |
| 326 | const counter = Metric.counter("counter-bigint", { |
| 327 | description: "Example", |
| 328 | incremental: true, |
| 329 | bigint: true |
| 330 | }) |
| 331 | |
| 332 | yield* Metric.withAttributes(counter, { |
| 333 | key: "value", |
| 334 | unit: "requests" |
| 335 | }).pipe(Metric.update(1n)) |
| 336 | yield* Metric.withAttributes(counter, { |
| 337 | key: "value" |
| 338 | }).pipe(Metric.update(1n)) |
| 339 | yield* Metric.withAttributes(counter, { |
| 340 | key: "value" |
| 341 | }).pipe(Metric.update(1n)) |
| 342 | |
| 343 | const results = yield* Effect.promise(() => producer.collect()) |
| 344 | const object = JSON.parse(JSON.stringify(results)) |
| 345 | assert.deepEqual(object.resourceMetrics.resource._rawAttributes, [ |
| 346 | ["name", "test"], |
| 347 | ["version", "1.0.0"] |
| 348 | ]) |
| 349 | assert.equal(object.resourceMetrics.scopeMetrics.length, 1) |
| 350 | const metric = findMetric(object, "counter-bigint") |
| 351 | assert.deepEqual(metric, { |
| 352 | "dataPointType": 3, |
| 353 | "descriptor": { |
| 354 | "advice": {}, |
| 355 | "name": "counter-bigint", |
| 356 | "description": "Example", |
| 357 | "unit": "requests", |
| 358 | "type": "COUNTER", |
| 359 | "valueType": ValueType.INT |
| 360 | }, |
| 361 | "isMonotonic": true, |
| 362 | "aggregationTemporality": 1, |
| 363 | "dataPoints": [ |
| 364 | { |
| 365 | "startTime": metric.dataPoints[0].startTime, |
| 366 | "endTime": metric.dataPoints[0].endTime, |
| 367 | "attributes": { |
| 368 | "unit": "requests", |
| 369 | "key": "value" |
| 370 | }, |
| 371 | "value": 1 |
| 372 | }, |
| 373 | { |
| 374 | "startTime": metric.dataPoints[0].startTime, |
| 375 | "endTime": metric.dataPoints[0].endTime, |
| 376 | "attributes": { |
| 377 | "key": "value" |
| 378 | }, |
| 379 | "value": 2 |
| 380 | } |
| 381 | ] |
| 382 | }) |
| 383 | })) |
| 384 | |
| 385 | it.effect("reports the same delta to every registered reader", () => |
| 386 | Effect.gen(function*() { |
| 387 | const services = yield* Effect.context<never>() |
| 388 | const producer = new internal.MetricProducerImpl(resourceFromAttributes({}), services, "delta") |
| 389 | const first = new ShutdownTrackingReader() |
| 390 | const second = new ShutdownTrackingReader() |
| 391 | yield* OtelMetrics.registerProducer(producer, () => [first, second]) |
| 392 | yield* Metric.update(Metric.counter("requests", { incremental: true }), 1) |
| 393 | |
| 394 | const firstResult = yield* Effect.promise(() => first.collect()) |
| 395 | const secondResult = yield* Effect.promise(() => second.collect()) |
| 396 | const firstValue = (firstResult.resourceMetrics.scopeMetrics[0]!.metrics[0] as any).dataPoints[0].value |
| 397 | const secondValue = (secondResult.resourceMetrics.scopeMetrics[0]!.metrics[0] as any).dataPoints[0].value |
| 398 | assert.deepStrictEqual([firstValue, secondValue], [1, 1]) |
| 399 | }).pipe(Effect.provideService(Metric.MetricRegistry, new Map()))) |
| 400 | |
| 401 | it.effect("reports a removed and recreated series from zero with delta temporality", () => |
| 402 | Effect.gen(function*() { |
| 403 | const services = yield* Effect.context<never>() |
| 404 | const producer = new internal.MetricProducerImpl(resourceFromAttributes({}), services, "delta") |
| 405 | const collect = Effect.promise(() => producer.collect()) |
| 406 | const counter = Metric.counter("requests") |
| 407 | const histogram = Metric.histogram("latency", { boundaries: [10] }) |
| 408 | |
| 409 | yield* Metric.update(counter, 5) |
| 410 | yield* Metric.update(histogram, 5) |
| 411 | yield* collect |
| 412 | yield* Metric.remove(counter) |
| 413 | yield* Metric.remove(histogram) |
| 414 | yield* collect |
| 415 | yield* Metric.update(counter, 7) |
| 416 | yield* Metric.update(histogram, 7) |
| 417 | const result = yield* collect |
| 418 | |
| 419 | assert.strictEqual(findMetric(result, "requests").dataPoints[0].value, 7) |
| 420 | const histogramValue = findMetric(result, "latency").dataPoints[0].value |
| 421 | assert.deepStrictEqual([histogramValue.count, histogramValue.sum], [1, 7]) |
| 422 | }).pipe(Effect.provideService(Metric.MetricRegistry, new Map()))) |
| 423 | |
| 424 | it.effect.each(["delta", "cumulative"] as const)( |
| 425 | "histogram extrema with %s temporality", |
| 426 | (temporality) => |
| 427 | Effect.gen(function*() { |
| 428 | const services = yield* Effect.context<never>() |
| 429 | const producer = new internal.MetricProducerImpl(resourceFromAttributes({}), services, temporality) |
| 430 | const histogram = Metric.histogram("latency", { |
| 431 | boundaries: Metric.linearBoundaries({ start: 0, width: 50, count: 3 }) |
| 432 | }) |
| 433 | |
| 434 | yield* Metric.update(histogram, 1) |
| 435 | yield* Metric.update(histogram, 100) |
| 436 | const first = findMetric(yield* Effect.promise(() => producer.collect()), "latency").dataPoints[0].value |
| 437 | assert.deepStrictEqual(first, { |
| 438 | buckets: { boundaries: [50], counts: [1, 1] }, |
| 439 | count: 2, |
| 440 | sum: 101, |
| 441 | min: 1, |
| 442 | max: 100 |
| 443 | }) |
| 444 | |
| 445 | yield* Metric.update(histogram, 50) |
| 446 | yield* Metric.update(histogram, 60) |
| 447 | const second = findMetric(yield* Effect.promise(() => producer.collect()), "latency").dataPoints[0].value |
| 448 | assert.deepStrictEqual( |
| 449 | second, |
| 450 | temporality === "delta" |
| 451 | ? { |
| 452 | buckets: { boundaries: [50], counts: [1, 1] }, |
| 453 | count: 2, |
| 454 | sum: 110 |
| 455 | } |
| 456 | : { |
| 457 | buckets: { boundaries: [50], counts: [2, 2] }, |
| 458 | count: 4, |
| 459 | sum: 211, |
| 460 | min: 1, |
| 461 | max: 100 |
| 462 | } |
| 463 | ) |
| 464 | }).pipe(Effect.provideService(Metric.MetricRegistry, new Map())) |
| 465 | ) |
| 466 | |
| 467 | it.effect("histogram buckets above the last custom boundary", () => |
| 468 | Effect.gen(function*() { |
| 469 | const services = yield* Effect.context<never>() |
| 470 | const producer = new internal.MetricProducerImpl(resourceFromAttributes({}), services) |
| 471 | const histogram = Metric.histogram("latency", { boundaries: [10, 50, 100] }) |
| 472 | |
| 473 | yield* Metric.update(histogram, 25) |
| 474 | yield* Metric.update(histogram, 75) |
| 475 | yield* Metric.update(histogram, 500) |
| 476 | const value = findMetric(yield* Effect.promise(() => producer.collect()), "latency").dataPoints[0].value |
| 477 | assert.deepStrictEqual(value.buckets, { boundaries: [10, 50, 100], counts: [0, 1, 1, 1] }) |
| 478 | }).pipe(Effect.provideService(Metric.MetricRegistry, new Map()))) |
| 479 | }) |