| 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 | "golang.org/x/tools/internal/event/export/metric" |
| 9 | "golang.org/x/tools/internal/event/label" |
| 10 | ) |
| 11 | |
| 12 | // convertMetric converts internal metric.Data to an OTLP metric. |
| 13 | func convertMetric(data metric.Data) otlpMetric { |
| 14 | switch data := data.(type) { |
| 15 | case *metric.Int64Data: |
| 16 | return convertInt64Data(data) |
| 17 | case *metric.Float64Data: |
| 18 | return convertFloat64Data(data) |
| 19 | case *metric.HistogramInt64Data: |
| 20 | return convertHistogramInt64Data(data) |
| 21 | case *metric.HistogramFloat64Data: |
| 22 | return convertHistogramFloat64Data(data) |
| 23 | default: |
| 24 | return otlpMetric{Name: data.Handle()} |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | func convertInt64Data(d *metric.Int64Data) otlpMetric { |
| 29 | m := otlpMetric{ |
| 30 | Name: d.Info.Name, |
| 31 | Description: d.Info.Description, |
| 32 | } |
| 33 | |
| 34 | groups := d.Groups() |
| 35 | dataPoints := make([]otlpNumberDataPoint, len(d.Rows)) |
| 36 | for i, value := range d.Rows { |
| 37 | v := intAsString(value) |
| 38 | dp := otlpNumberDataPoint{ |
| 39 | TimeUnixNano: intAsString(d.EndTime.UnixNano()), |
| 40 | AsInt: &v, // TODO: use go1.26 new(value) |
| 41 | Attributes: labelsToAttributes(groups[i]), |
| 42 | } |
| 43 | dataPoints[i] = dp |
| 44 | } |
| 45 | |
| 46 | if d.IsGauge { |
| 47 | m.Gauge = &otlpGauge{DataPoints: dataPoints} |
| 48 | } else { |
| 49 | m.Sum = &otlpSum{ |
| 50 | DataPoints: dataPoints, |
| 51 | AggregationTemporality: 2, // cumulative |
| 52 | IsMonotonic: true, |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return m |
| 57 | } |
| 58 | |
| 59 | func convertFloat64Data(d *metric.Float64Data) otlpMetric { |
| 60 | m := otlpMetric{ |
| 61 | Name: d.Info.Name, |
| 62 | Description: d.Info.Description, |
| 63 | } |
| 64 | |
| 65 | groups := d.Groups() |
| 66 | dataPoints := make([]otlpNumberDataPoint, len(d.Rows)) |
| 67 | for i, value := range d.Rows { |
| 68 | v := value |
| 69 | dp := otlpNumberDataPoint{ |
| 70 | TimeUnixNano: intAsString(d.EndTime.UnixNano()), |
| 71 | AsDouble: &v, // TODO: use go1.26 new(value) |
| 72 | Attributes: labelsToAttributes(groups[i]), |
| 73 | } |
| 74 | dataPoints[i] = dp |
| 75 | } |
| 76 | |
| 77 | if d.IsGauge { |
| 78 | m.Gauge = &otlpGauge{DataPoints: dataPoints} |
| 79 | } else { |
| 80 | m.Sum = &otlpSum{ |
| 81 | DataPoints: dataPoints, |
| 82 | AggregationTemporality: 2, // cumulative |
| 83 | IsMonotonic: true, |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return m |
| 88 | } |
| 89 | |
| 90 | func convertHistogramInt64Data(d *metric.HistogramInt64Data) otlpMetric { |
| 91 | dataPoints := make([]otlpHistogramDataPoint, len(d.Rows)) |
| 92 | |
| 93 | // Convert bucket boundaries to float64 |
| 94 | bounds := make([]float64, len(d.Info.Buckets)) |
| 95 | for i, b := range d.Info.Buckets { |
| 96 | bounds[i] = float64(b) |
| 97 | } |
| 98 | |
| 99 | groups := d.Groups() |
| 100 | for i, row := range d.Rows { |
| 101 | // OTLP expects bucket counts, not cumulative counts |
| 102 | // The internal format uses cumulative, so we need to convert |
| 103 | bucketCounts := make([]intAsString, len(row.Values)+1) |
| 104 | var prev int64 |
| 105 | for j, v := range row.Values { |
| 106 | bucketCounts[j] = intAsString(v - prev) |
| 107 | prev = v |
| 108 | } |
| 109 | // Last bucket is for values > last bound |
| 110 | bucketCounts[len(row.Values)] = intAsString(row.Count - prev) |
| 111 | |
| 112 | sum := float64(row.Sum) |
| 113 | min := float64(row.Min) |
| 114 | max := float64(row.Max) |
| 115 | dp := otlpHistogramDataPoint{ |
| 116 | TimeUnixNano: intAsString(d.EndTime.UnixNano()), |
| 117 | Count: intAsString(row.Count), |
| 118 | Sum: &sum, // TODO: use go1.26 new(value) |
| 119 | BucketCounts: bucketCounts, |
| 120 | ExplicitBounds: bounds, |
| 121 | Min: &min, // TODO: use go1.26 new(value) |
| 122 | Max: &max, // TODO: use go1.26 new(value) |
| 123 | Attributes: labelsToAttributes(groups[i]), |
| 124 | } |
| 125 | dataPoints[i] = dp |
| 126 | } |
| 127 | |
| 128 | return otlpMetric{ |
| 129 | Name: d.Info.Name, |
| 130 | Description: d.Info.Description, |
| 131 | Histogram: &otlpHistogram{ |
| 132 | DataPoints: dataPoints, |
| 133 | AggregationTemporality: 2, // cumulative |
| 134 | }, |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | func convertHistogramFloat64Data(d *metric.HistogramFloat64Data) otlpMetric { |
| 139 | dataPoints := make([]otlpHistogramDataPoint, len(d.Rows)) |
| 140 | |
| 141 | groups := d.Groups() |
| 142 | for i, row := range d.Rows { |
| 143 | // Convert cumulative to bucket counts |
| 144 | bucketCounts := make([]intAsString, len(row.Values)+1) |
| 145 | var prev int64 |
| 146 | for j, v := range row.Values { |
| 147 | bucketCounts[j] = intAsString(v - prev) |
| 148 | prev = v |
| 149 | } |
| 150 | bucketCounts[len(row.Values)] = intAsString(row.Count - prev) |
| 151 | |
| 152 | sum := row.Sum |
| 153 | min := row.Min |
| 154 | max := row.Max |
| 155 | dp := otlpHistogramDataPoint{ |
| 156 | TimeUnixNano: intAsString(d.EndTime.UnixNano()), |
| 157 | Count: intAsString(row.Count), |
| 158 | Sum: &sum, // TODO: use go1.26 new(value) |
| 159 | BucketCounts: bucketCounts, |
| 160 | ExplicitBounds: d.Info.Buckets, |
| 161 | Min: &min, // TODO: use go1.26 new(value) |
| 162 | Max: &max, // TODO: use go1.26 new(value) |
| 163 | Attributes: labelsToAttributes(groups[i]), |
| 164 | } |
| 165 | dataPoints[i] = dp |
| 166 | } |
| 167 | |
| 168 | return otlpMetric{ |
| 169 | Name: d.Info.Name, |
| 170 | Description: d.Info.Description, |
| 171 | Histogram: &otlpHistogram{ |
| 172 | DataPoints: dataPoints, |
| 173 | AggregationTemporality: 2, // cumulative |
| 174 | }, |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // labelsToAttributes converts a slice of labels to OTLP attributes. |
| 179 | func labelsToAttributes(labels []label.Label) []otlpAttribute { |
| 180 | attrs := make([]otlpAttribute, 0, len(labels)) |
| 181 | for _, l := range labels { |
| 182 | if attr, ok := labelToAttribute(l); ok { |
| 183 | attrs = append(attrs, attr) |
| 184 | } |
| 185 | } |
| 186 | return attrs |
| 187 | } |
| 188 | |
| 189 | // exportMetrics sends metrics to the OTLP endpoint. |
| 190 | func (e *Exporter) exportMetrics(metrics []otlpMetric) error { |
| 191 | req := otlpMetricsRequest{ |
| 192 | ResourceMetrics: []otlpResourceMetrics{{ |
| 193 | Resource: e.resource, |
| 194 | ScopeMetrics: []otlpScopeMetrics{{ |
| 195 | Scope: otlpScope{Name: "golang.org/x/tools"}, |
| 196 | Metrics: metrics, |
| 197 | }}, |
| 198 | }}, |
| 199 | } |
| 200 | return e.post("/v1/metrics", req) |
| 201 | } |