otlp-cost-controls.test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { ROOT_CONTEXT, SpanKind, TraceFlags, trace } from "@opentelemetry/api";
  2. import { AggregationType } from "@opentelemetry/sdk-metrics";
  3. import { SamplingDecision } from "@opentelemetry/sdk-trace-base";
  4. import { describe, expect, it } from "vitest";
  5. import {
  6. AGENT_METRIC_EXPORT_INTERVAL_MS,
  7. AGENT_TRACE_SAMPLE_RATIO,
  8. createAgentTraceSampler,
  9. metricViews,
  10. } from "../src/otlp-exporter.js";
  11. const TRACE_ID = "1234567890abcdef1234567890abcdef";
  12. const SPAN_ID = "1234567890abcdef";
  13. describe("OTLP ARMS cost controls", () => {
  14. it("默认把 Metric 周期降为 5 分钟,并把 Root Trace 稳定采样为 10%", () => {
  15. expect(AGENT_METRIC_EXPORT_INTERVAL_MS).toBe(300_000);
  16. expect(AGENT_TRACE_SAMPLE_RATIO).toBe(0.1);
  17. });
  18. it("Trace 子 Span 继承父 Span 的采样决定,不形成残缺 Trace 树", () => {
  19. const sampler = createAgentTraceSampler(0);
  20. const sampledParent = trace.setSpanContext(ROOT_CONTEXT, {
  21. isRemote: false,
  22. spanId: SPAN_ID,
  23. traceFlags: TraceFlags.SAMPLED,
  24. traceId: TRACE_ID,
  25. });
  26. const unsampledParent = trace.setSpanContext(ROOT_CONTEXT, {
  27. isRemote: false,
  28. spanId: SPAN_ID,
  29. traceFlags: TraceFlags.NONE,
  30. traceId: TRACE_ID,
  31. });
  32. expect(
  33. sampler.shouldSample(sampledParent, TRACE_ID, "agent_step", SpanKind.INTERNAL, {}, [])
  34. .decision,
  35. ).toBe(SamplingDecision.RECORD_AND_SAMPLED);
  36. expect(
  37. sampler.shouldSample(unsampledParent, TRACE_ID, "agent_step", SpanKind.INTERNAL, {}, [])
  38. .decision,
  39. ).toBe(SamplingDecision.NOT_RECORD);
  40. expect(
  41. createAgentTraceSampler(0).shouldSample(
  42. ROOT_CONTEXT,
  43. TRACE_ID,
  44. "agent_turn",
  45. SpanKind.INTERNAL,
  46. {},
  47. [],
  48. ).decision,
  49. ).toBe(SamplingDecision.NOT_RECORD);
  50. expect(
  51. createAgentTraceSampler(1).shouldSample(
  52. ROOT_CONTEXT,
  53. TRACE_ID,
  54. "agent_turn",
  55. SpanKind.INTERNAL,
  56. {},
  57. [],
  58. ).decision,
  59. ).toBe(SamplingDecision.RECORD_AND_SAMPLED);
  60. });
  61. it("Histogram 关闭 Min/Max,且每项最多保留 10 个 Bucket", () => {
  62. const histogramViews = metricViews().filter(
  63. (view) => view.aggregation?.type === AggregationType.EXPLICIT_BUCKET_HISTOGRAM,
  64. );
  65. expect(histogramViews.length).toBeGreaterThan(0);
  66. for (const view of histogramViews) {
  67. expect(view.aggregation).toMatchObject({
  68. options: { recordMinMax: false },
  69. });
  70. if (view.aggregation?.type === AggregationType.EXPLICIT_BUCKET_HISTOGRAM) {
  71. expect(view.aggregation.options.boundaries).toHaveLength(
  72. Math.min(view.aggregation.options.boundaries.length, 10),
  73. );
  74. }
  75. }
  76. const attempts = histogramViews.find(
  77. (view) => view.instrumentName === "zcode.model.call.attempts",
  78. );
  79. expect(attempts?.aggregation).toMatchObject({
  80. options: { boundaries: [1, 2, 3, 5, 8] },
  81. });
  82. });
  83. });