| 1 | // Copyright 2026 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package otel |
| 6 | |
| 7 | import ( |
| 8 | "context" |
| 9 | "testing" |
| 10 | |
| 11 | "golang.org/x/tools/internal/event" |
| 12 | "golang.org/x/tools/internal/event/core" |
| 13 | "golang.org/x/tools/internal/event/export/metric" |
| 14 | "golang.org/x/tools/internal/event/keys" |
| 15 | "golang.org/x/tools/internal/event/label" |
| 16 | ) |
| 17 | |
| 18 | // captureMetricData creates a metric using the production event system and returns the resulting data. |
| 19 | func captureMetricData(name string, setup func(*metric.Config), record func(context.Context)) metric.Data { |
| 20 | var captured metric.Data |
| 21 | |
| 22 | output := func(ctx context.Context, ev core.Event, lm label.Map) context.Context { |
| 23 | if event.IsMetric(ev) { |
| 24 | if entries := metric.Entries.Get(lm); entries != nil { |
| 25 | for _, data := range entries.([]metric.Data) { |
| 26 | if data.Handle() == name { |
| 27 | captured = data |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | return ctx |
| 33 | } |
| 34 | |
| 35 | cfg := metric.Config{} |
| 36 | setup(&cfg) |
| 37 | |
| 38 | event.SetExporter(cfg.Exporter(output)) |
| 39 | defer event.SetExporter(nil) |
| 40 | |
| 41 | record(context.Background()) |
| 42 | return captured |
| 43 | } |
| 44 | |
| 45 | func TestConvertInt64Data_Counter(t *testing.T) { |
| 46 | key := keys.NewInt("value", "") |
| 47 | |
| 48 | data := captureMetricData("request_count", func(cfg *metric.Config) { |
| 49 | metric.Scalar{ |
| 50 | Name: "request_count", |
| 51 | Description: "Number of requests", |
| 52 | }.SumInt64(cfg, key) |
| 53 | }, func(ctx context.Context) { |
| 54 | event.Metric(ctx, key.Of(42)) |
| 55 | }).(*metric.Int64Data) |
| 56 | |
| 57 | m := convertInt64Data(data) |
| 58 | |
| 59 | if m.Name != "request_count" { |
| 60 | t.Errorf("expected name 'request_count', got %q", m.Name) |
| 61 | } |
| 62 | if m.Description != "Number of requests" { |
| 63 | t.Errorf("expected description 'Number of requests', got %q", m.Description) |
| 64 | } |
| 65 | if m.Gauge != nil { |
| 66 | t.Error("expected Gauge to be nil for counter") |
| 67 | } |
| 68 | if m.Sum == nil { |
| 69 | t.Fatal("expected Sum to be non-nil for counter") |
| 70 | } |
| 71 | if len(m.Sum.DataPoints) != 1 { |
| 72 | t.Fatalf("expected 1 data point, got %d", len(m.Sum.DataPoints)) |
| 73 | } |
| 74 | if m.Sum.DataPoints[0].AsInt == nil || *m.Sum.DataPoints[0].AsInt != 42 { |
| 75 | t.Errorf("expected value 42, got %v", m.Sum.DataPoints[0].AsInt) |
| 76 | } |
| 77 | if !m.Sum.IsMonotonic { |
| 78 | t.Error("expected IsMonotonic to be true for counter") |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func TestConvertInt64Data_Gauge(t *testing.T) { |
| 83 | key := keys.NewInt("value", "") |
| 84 | |
| 85 | data := captureMetricData("current_connections", func(cfg *metric.Config) { |
| 86 | metric.Scalar{Name: "current_connections"}.LatestInt64(cfg, key) |
| 87 | }, func(ctx context.Context) { |
| 88 | event.Metric(ctx, key.Of(100)) |
| 89 | }).(*metric.Int64Data) |
| 90 | |
| 91 | m := convertInt64Data(data) |
| 92 | |
| 93 | if m.Sum != nil { |
| 94 | t.Error("expected Sum to be nil for gauge") |
| 95 | } |
| 96 | if m.Gauge == nil { |
| 97 | t.Fatal("expected Gauge to be non-nil") |
| 98 | } |
| 99 | if len(m.Gauge.DataPoints) != 1 { |
| 100 | t.Fatalf("expected 1 data point, got %d", len(m.Gauge.DataPoints)) |
| 101 | } |
| 102 | if m.Gauge.DataPoints[0].AsInt == nil || *m.Gauge.DataPoints[0].AsInt != 100 { |
| 103 | t.Errorf("expected value 100, got %v", m.Gauge.DataPoints[0].AsInt) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | func TestConvertFloat64Data(t *testing.T) { |
| 108 | key := keys.NewFloat("value", "") |
| 109 | |
| 110 | data := captureMetricData("cpu_usage", func(cfg *metric.Config) { |
| 111 | metric.Scalar{Name: "cpu_usage"}.LatestFloat64(cfg, key) |
| 112 | }, func(ctx context.Context) { |
| 113 | event.Metric(ctx, key.Of(0.75)) |
| 114 | }).(*metric.Float64Data) |
| 115 | |
| 116 | m := convertFloat64Data(data) |
| 117 | |
| 118 | if m.Gauge == nil { |
| 119 | t.Fatal("expected Gauge to be non-nil") |
| 120 | } |
| 121 | if len(m.Gauge.DataPoints) != 1 { |
| 122 | t.Fatalf("expected 1 data point, got %d", len(m.Gauge.DataPoints)) |
| 123 | } |
| 124 | if m.Gauge.DataPoints[0].AsDouble == nil || *m.Gauge.DataPoints[0].AsDouble != 0.75 { |
| 125 | t.Errorf("expected value 0.75, got %v", m.Gauge.DataPoints[0].AsDouble) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | func TestConvertHistogramInt64Data(t *testing.T) { |
| 130 | key := keys.NewInt("value", "") |
| 131 | buckets := []int64{10, 50, 100, 500} |
| 132 | |
| 133 | // Record multiple values across different buckets: |
| 134 | // Bucket boundaries: <=10, <=50, <=100, <=500, >500 |
| 135 | // Values: 5, 8 (<=10), 20, 30, 40 (<=50), 75 (<=100), 200, 300 (<=500), 1000 (>500) |
| 136 | values := []int{5, 8, 20, 30, 40, 75, 200, 300, 1000} |
| 137 | |
| 138 | data := captureMetricData("response_time", func(cfg *metric.Config) { |
| 139 | metric.HistogramInt64{ |
| 140 | Name: "response_time", |
| 141 | Description: "Response time in ms", |
| 142 | Buckets: buckets, |
| 143 | }.Record(cfg, key) |
| 144 | }, func(ctx context.Context) { |
| 145 | for _, v := range values { |
| 146 | event.Metric(ctx, key.Of(v)) |
| 147 | } |
| 148 | }).(*metric.HistogramInt64Data) |
| 149 | |
| 150 | m := convertHistogramInt64Data(data) |
| 151 | |
| 152 | if m.Histogram == nil { |
| 153 | t.Fatal("expected Histogram to be non-nil") |
| 154 | } |
| 155 | dp := m.Histogram.DataPoints[0] |
| 156 | |
| 157 | // Verify count |
| 158 | if dp.Count != 9 { |
| 159 | t.Errorf("expected count 9, got %d", dp.Count) |
| 160 | } |
| 161 | |
| 162 | // Verify sum: 5+8+20+30+40+75+200+300+1000 = 1678 |
| 163 | if dp.Sum == nil || *dp.Sum != 1678 { |
| 164 | t.Errorf("expected sum 1678, got %v", dp.Sum) |
| 165 | } |
| 166 | |
| 167 | // Verify min/max |
| 168 | if dp.Min == nil || *dp.Min != 5 { |
| 169 | t.Errorf("expected min 5, got %v", dp.Min) |
| 170 | } |
| 171 | if dp.Max == nil || *dp.Max != 1000 { |
| 172 | t.Errorf("expected max 1000, got %v", dp.Max) |
| 173 | } |
| 174 | |
| 175 | // Verify bucket counts (non-cumulative): |
| 176 | // <=10: 2 (5, 8) |
| 177 | // <=50: 3 (20, 30, 40) |
| 178 | // <=100: 1 (75) |
| 179 | // <=500: 2 (200, 300) |
| 180 | // >500: 1 (1000) |
| 181 | expectedBuckets := []intAsString{2, 3, 1, 2, 1} |
| 182 | if len(dp.BucketCounts) != len(expectedBuckets) { |
| 183 | t.Fatalf("expected %d buckets, got %d", len(expectedBuckets), len(dp.BucketCounts)) |
| 184 | } |
| 185 | for i, expected := range expectedBuckets { |
| 186 | if dp.BucketCounts[i] != expected { |
| 187 | t.Errorf("bucket[%d]: expected %d, got %d", i, expected, dp.BucketCounts[i]) |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // Verify explicit bounds match configured buckets |
| 192 | expectedBounds := []float64{10, 50, 100, 500} |
| 193 | if len(dp.ExplicitBounds) != len(expectedBounds) { |
| 194 | t.Fatalf("expected %d bounds, got %d", len(expectedBounds), len(dp.ExplicitBounds)) |
| 195 | } |
| 196 | for i, expected := range expectedBounds { |
| 197 | if dp.ExplicitBounds[i] != expected { |
| 198 | t.Errorf("bound[%d]: expected %v, got %v", i, expected, dp.ExplicitBounds[i]) |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | func TestConvertInt64Data_WithAttributes(t *testing.T) { |
| 204 | valueKey := keys.NewInt("value", "") |
| 205 | methodKey := keys.NewString("method", "") |
| 206 | |
| 207 | data := captureMetricData("requests_by_method", func(cfg *metric.Config) { |
| 208 | metric.Scalar{ |
| 209 | Name: "requests_by_method", |
| 210 | Keys: []label.Key{methodKey}, // Group by method |
| 211 | }.SumInt64(cfg, valueKey) |
| 212 | }, func(ctx context.Context) { |
| 213 | event.Metric(ctx, valueKey.Of(10), methodKey.Of("GET")) |
| 214 | event.Metric(ctx, valueKey.Of(5), methodKey.Of("POST")) |
| 215 | }).(*metric.Int64Data) |
| 216 | |
| 217 | m := convertInt64Data(data) |
| 218 | |
| 219 | // Should have 2 data points (one per method) |
| 220 | if len(m.Sum.DataPoints) != 2 { |
| 221 | t.Fatalf("expected 2 data points, got %d", len(m.Sum.DataPoints)) |
| 222 | } |
| 223 | |
| 224 | // Verify attributes contain the method labels |
| 225 | for _, dp := range m.Sum.DataPoints { |
| 226 | if len(dp.Attributes) != 1 { |
| 227 | t.Errorf("expected 1 attribute, got %d", len(dp.Attributes)) |
| 228 | } |
| 229 | if dp.Attributes[0].Key != "method" { |
| 230 | t.Errorf("expected attribute key 'method', got %q", dp.Attributes[0].Key) |
| 231 | } |
| 232 | // Value should be "GET" or "POST" |
| 233 | sv := dp.Attributes[0].Value.StringValue |
| 234 | if sv == nil || (*sv != "GET" && *sv != "POST") { |
| 235 | t.Errorf("unexpected method value: %v", sv) |
| 236 | } |
| 237 | } |
| 238 | } |