| 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 | "fmt" |
| 9 | "strconv" |
| 10 | ) |
| 11 | |
| 12 | // OTLP JSON types for telemetry export. |
| 13 | // |
| 14 | // Specification |
| 15 | // https://github.com/open-telemetry/opentelemetry-proto/blob/v1.9.0/docs/specification.md |
| 16 | |
| 17 | // intAsString is an int64 that marshals to/from a JSON string. |
| 18 | // |
| 19 | // int64 values are encoded as strings because JSON numbers are IEEE 754 |
| 20 | // double-precision floats, which can only represent integers exactly up to |
| 21 | // 2^53. Timestamps in nanoseconds and large counts would lose precision. |
| 22 | // See: https://github.com/open-telemetry/opentelemetry-proto/issues/268 |
| 23 | // And: https://protobuf.dev/programming-guides/json/ |
| 24 | type intAsString int64 |
| 25 | |
| 26 | func (i intAsString) MarshalJSON() ([]byte, error) { |
| 27 | return fmt.Appendf(nil, `"%d"`, i), nil |
| 28 | } |
| 29 | |
| 30 | func (i *intAsString) UnmarshalJSON(data []byte) error { |
| 31 | // Strip quotes if present. Per the spec, both JSON strings ("123") |
| 32 | // and JSON numbers (123) must be accepted for decoding int64 values. |
| 33 | if len(data) >= 2 && data[0] == '"' && data[len(data)-1] == '"' { |
| 34 | data = data[1 : len(data)-1] |
| 35 | } |
| 36 | v, err := strconv.ParseInt(string(data), 10, 64) |
| 37 | if err != nil { |
| 38 | return err |
| 39 | } |
| 40 | *i = intAsString(v) |
| 41 | return nil |
| 42 | } |
| 43 | |
| 44 | // Common oltp types |
| 45 | |
| 46 | // otlpResource corresponds to Resource. |
| 47 | // https://github.com/open-telemetry/opentelemetry-proto/blob/v1.9.0/opentelemetry/proto/resource/v1/resource.proto#L28 |
| 48 | type otlpResource struct { |
| 49 | Attributes []otlpAttribute `json:"attributes"` |
| 50 | } |
| 51 | |
| 52 | // otlpScope corresponds to InstrumentationScope. |
| 53 | // https://github.com/open-telemetry/opentelemetry-proto/blob/v1.9.0/opentelemetry/proto/common/v1/common.proto#L76 |
| 54 | type otlpScope struct { |
| 55 | Name string `json:"name,omitempty"` |
| 56 | Version string `json:"version,omitempty"` |
| 57 | } |
| 58 | |
| 59 | // otlpAttribute corresponds to KeyValue. |
| 60 | // https://github.com/open-telemetry/opentelemetry-proto/blob/v1.9.0/opentelemetry/proto/common/v1/common.proto#L66 |
| 61 | type otlpAttribute struct { |
| 62 | Key string `json:"key"` |
| 63 | Value otlpAttributeValue `json:"value"` |
| 64 | } |
| 65 | |
| 66 | // otlpAttributeValue corresponds to AnyValue. |
| 67 | // https://github.com/open-telemetry/opentelemetry-proto/blob/v1.9.0/opentelemetry/proto/common/v1/common.proto#L28 |
| 68 | type otlpAttributeValue struct { |
| 69 | StringValue *string `json:"stringValue,omitempty"` |
| 70 | IntValue *intAsString `json:"intValue,omitempty"` |
| 71 | DoubleValue *float64 `json:"doubleValue,omitempty"` |
| 72 | BoolValue *bool `json:"boolValue,omitempty"` |
| 73 | } |
| 74 | |
| 75 | // Trace types |
| 76 | |
| 77 | // otlpTraceExportRequest is the payload for ExportTraceServiceRequest. |
| 78 | // https://github.com/open-telemetry/opentelemetry-proto/blob/v1.9.0/opentelemetry/proto/collector/trace/v1/trace_service.proto#L34 |
| 79 | type otlpTraceExportRequest struct { |
| 80 | ResourceSpans []otlpResourceSpans `json:"resourceSpans"` |
| 81 | } |
| 82 | |
| 83 | // otlpResourceSpans corresponds to ResourceSpans. |
| 84 | // https://github.com/open-telemetry/opentelemetry-proto/blob/v1.9.0/opentelemetry/proto/trace/v1/trace.proto#L48 |
| 85 | type otlpResourceSpans struct { |
| 86 | Resource otlpResource `json:"resource"` |
| 87 | ScopeSpans []otlpScopeSpans `json:"scopeSpans"` |
| 88 | } |
| 89 | |
| 90 | // otlpScopeSpans corresponds to ScopeSpans. |
| 91 | // https://github.com/open-telemetry/opentelemetry-proto/blob/v1.9.0/opentelemetry/proto/trace/v1/trace.proto#L68 |
| 92 | type otlpScopeSpans struct { |
| 93 | Scope otlpScope `json:"scope"` |
| 94 | Spans []otlpSpan `json:"spans"` |
| 95 | } |
| 96 | |
| 97 | // otlpSpan corresponds to Span. |
| 98 | // https://github.com/open-telemetry/opentelemetry-proto/blob/v1.9.0/opentelemetry/proto/trace/v1/trace.proto#L89 |
| 99 | type otlpSpan struct { |
| 100 | TraceID string `json:"traceId"` |
| 101 | SpanID string `json:"spanId"` |
| 102 | ParentSpanID string `json:"parentSpanId,omitempty"` |
| 103 | Name string `json:"name"` |
| 104 | Kind int `json:"kind"` |
| 105 | StartTimeUnixNano intAsString `json:"startTimeUnixNano"` |
| 106 | EndTimeUnixNano intAsString `json:"endTimeUnixNano"` |
| 107 | Attributes []otlpAttribute `json:"attributes,omitempty"` |
| 108 | Status otlpSpanStatus `json:"status"` |
| 109 | } |
| 110 | |
| 111 | // otlpSpanStatus corresponds to Status. |
| 112 | // https://github.com/open-telemetry/opentelemetry-proto/blob/v1.9.0/opentelemetry/proto/trace/v1/trace.proto#L308 |
| 113 | type otlpSpanStatus struct { |
| 114 | Code int `json:"code"` |
| 115 | Message string `json:"message,omitempty"` |
| 116 | } |
| 117 | |
| 118 | // Metrics types |
| 119 | |
| 120 | // otlpMetricsRequest is the payload for ExportMetricsServiceRequest. |
| 121 | // https://github.com/open-telemetry/opentelemetry-proto/blob/v1.9.0/opentelemetry/proto/collector/metrics/v1/metrics_service.proto#L34 |
| 122 | type otlpMetricsRequest struct { |
| 123 | ResourceMetrics []otlpResourceMetrics `json:"resourceMetrics"` |
| 124 | } |
| 125 | |
| 126 | // otlpResourceMetrics corresponds to ResourceMetrics. |
| 127 | // https://github.com/open-telemetry/opentelemetry-proto/blob/v1.9.0/opentelemetry/proto/metrics/v1/metrics.proto#L66 |
| 128 | type otlpResourceMetrics struct { |
| 129 | Resource otlpResource `json:"resource"` |
| 130 | ScopeMetrics []otlpScopeMetrics `json:"scopeMetrics"` |
| 131 | } |
| 132 | |
| 133 | // otlpScopeMetrics corresponds to ScopeMetrics. |
| 134 | // https://github.com/open-telemetry/opentelemetry-proto/blob/v1.9.0/opentelemetry/proto/metrics/v1/metrics.proto#L86 |
| 135 | type otlpScopeMetrics struct { |
| 136 | Scope otlpScope `json:"scope"` |
| 137 | Metrics []otlpMetric `json:"metrics"` |
| 138 | } |
| 139 | |
| 140 | // otlpMetric corresponds to Metric. |
| 141 | // https://github.com/open-telemetry/opentelemetry-proto/blob/v1.9.0/opentelemetry/proto/metrics/v1/metrics.proto#L188 |
| 142 | type otlpMetric struct { |
| 143 | Name string `json:"name"` |
| 144 | Description string `json:"description,omitempty"` |
| 145 | Unit string `json:"unit,omitempty"` |
| 146 | Gauge *otlpGauge `json:"gauge,omitempty"` |
| 147 | Sum *otlpSum `json:"sum,omitempty"` |
| 148 | Histogram *otlpHistogram `json:"histogram,omitempty"` |
| 149 | } |
| 150 | |
| 151 | // otlpGauge corresponds to Gauge. |
| 152 | // https://github.com/open-telemetry/opentelemetry-proto/blob/v1.9.0/opentelemetry/proto/metrics/v1/metrics.proto#L232 |
| 153 | type otlpGauge struct { |
| 154 | DataPoints []otlpNumberDataPoint `json:"dataPoints"` |
| 155 | } |
| 156 | |
| 157 | // otlpSum corresponds to Sum. |
| 158 | // https://github.com/open-telemetry/opentelemetry-proto/blob/v1.9.0/opentelemetry/proto/metrics/v1/metrics.proto#L240 |
| 159 | type otlpSum struct { |
| 160 | DataPoints []otlpNumberDataPoint `json:"dataPoints"` |
| 161 | AggregationTemporality int `json:"aggregationTemporality"` // 1=delta, 2=cumulative |
| 162 | IsMonotonic bool `json:"isMonotonic"` |
| 163 | } |
| 164 | |
| 165 | // otlpHistogram corresponds to Histogram. |
| 166 | // https://github.com/open-telemetry/opentelemetry-proto/blob/v1.9.0/opentelemetry/proto/metrics/v1/metrics.proto#L255 |
| 167 | type otlpHistogram struct { |
| 168 | DataPoints []otlpHistogramDataPoint `json:"dataPoints"` |
| 169 | AggregationTemporality int `json:"aggregationTemporality"` |
| 170 | } |
| 171 | |
| 172 | // otlpNumberDataPoint corresponds to NumberDataPoint. |
| 173 | // https://github.com/open-telemetry/opentelemetry-proto/blob/v1.9.0/opentelemetry/proto/metrics/v1/metrics.proto#L385 |
| 174 | type otlpNumberDataPoint struct { |
| 175 | Attributes []otlpAttribute `json:"attributes,omitempty"` |
| 176 | TimeUnixNano intAsString `json:"timeUnixNano"` |
| 177 | AsInt *intAsString `json:"asInt,omitempty"` |
| 178 | AsDouble *float64 `json:"asDouble,omitempty"` |
| 179 | } |
| 180 | |
| 181 | // otlpHistogramDataPoint corresponds to HistogramDataPoint. |
| 182 | // https://github.com/open-telemetry/opentelemetry-proto/blob/v1.9.0/opentelemetry/proto/metrics/v1/metrics.proto#L434 |
| 183 | type otlpHistogramDataPoint struct { |
| 184 | Attributes []otlpAttribute `json:"attributes,omitempty"` |
| 185 | TimeUnixNano intAsString `json:"timeUnixNano"` |
| 186 | Count intAsString `json:"count"` |
| 187 | Sum *float64 `json:"sum,omitempty"` |
| 188 | BucketCounts []intAsString `json:"bucketCounts"` |
| 189 | ExplicitBounds []float64 `json:"explicitBounds"` |
| 190 | Min *float64 `json:"min,omitempty"` |
| 191 | Max *float64 `json:"max,omitempty"` |
| 192 | } |