| 1 |
// Copyright 2019-2025 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
// Package otel supports OpenTelemetry tracing and metrics for the Go Cloud Development Kit. |
| 16 |
package otel |
| 17 |
|
| 18 |
import ( |
| 19 |
"fmt" |
| 20 |
"go.opentelemetry.io/otel" |
| 21 |
"go.opentelemetry.io/otel/attribute" |
| 22 |
"go.opentelemetry.io/otel/metric" |
| 23 |
sdkmetric "go.opentelemetry.io/otel/sdk/metric" |
| 24 |
"strings" |
| 25 |
) |
| 26 |
|
| 27 |
// Units are encoded according to the case-sensitive abbreviations from the |
| 28 |
// Unified Code for Units of Measure: http://unitsofmeasure.org/ucum.html. |
| 29 |
const ( |
| 30 |
unitDimensionless = "1" |
| 31 |
unitMilliseconds = "ms" |
| 32 |
unitBytes = "By" |
| 33 |
) |
| 34 |
|
| 35 |
var ( |
| 36 |
defaultMillisecondsBoundaries = []float64{ |
| 37 |
0.0, 0.1, 0.2, 0.4, 0.6, 0.8, 1.0, |
| 38 |
2.0, 3.0, 4.0, 5.0, 6.0, 8.0, 10.0, |
| 39 |
13.0, 16.0, 20.0, 25.0, 30.0, 40.0, |
| 40 |
50.0, 65.0, 80.0, 100.0, 130.0, 160.0, |
| 41 |
200.0, 250.0, 300.0, 400.0, 500.0, |
| 42 |
650.0, 800.0, 1000.0, 2000.0, 5000.0, 10000.0, |
| 43 |
} |
| 44 |
) |
| 45 |
|
| 46 |
func Views(pkg string) []sdkmetric.View { |
| 47 |
|
| 48 |
return []sdkmetric.View{ |
| 49 |
|
| 50 |
// View for latency histogram. |
| 51 |
func(inst sdkmetric.Instrument) (sdkmetric.Stream, bool) { |
| 52 |
if inst.Kind == sdkmetric.InstrumentKindHistogram { |
| 53 |
if inst.Name == pkg+"/latency" { |
| 54 |
return sdkmetric.Stream{ |
| 55 |
Name: inst.Name, |
| 56 |
Description: "Distribution of method latency, by provider and method.", |
| 57 |
Aggregation: sdkmetric.AggregationExplicitBucketHistogram{ |
| 58 |
Boundaries: defaultMillisecondsBoundaries, |
| 59 |
}, |
| 60 |
AttributeFilter: func(kv attribute.KeyValue) bool { |
| 61 |
return kv.Key == packageKey || kv.Key == methodKey |
| 62 |
}, |
| 63 |
}, true |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
return sdkmetric.Stream{}, false |
| 68 |
}, |
| 69 |
|
| 70 |
// View for completed_calls count. |
| 71 |
func(inst sdkmetric.Instrument) (sdkmetric.Stream, bool) { |
| 72 |
if inst.Kind == sdkmetric.InstrumentKindHistogram { |
| 73 |
if inst.Name == pkg+"/latency" { |
| 74 |
return sdkmetric.Stream{ |
| 75 |
Name: strings.Replace(inst.Name, "/latency", "/completed_calls", 1), |
| 76 |
Description: "Count of method calls by provider, method and status.", |
| 77 |
Aggregation: sdkmetric.DefaultAggregationSelector(sdkmetric.InstrumentKindCounter), |
| 78 |
AttributeFilter: func(kv attribute.KeyValue) bool { |
| 79 |
return kv.Key == methodKey || kv.Key == statusKey |
| 80 |
}, |
| 81 |
}, true |
| 82 |
} |
| 83 |
} |
| 84 |
return sdkmetric.Stream{}, false |
| 85 |
}, |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
// CounterView returns summation views that add up individual measurements the counter takes. |
| 90 |
func CounterView(pkg string, meterName string, description string) []sdkmetric.View { |
| 91 |
return []sdkmetric.View{ |
| 92 |
// View for gauge counts. |
| 93 |
func(inst sdkmetric.Instrument) (sdkmetric.Stream, bool) { |
| 94 |
if inst.Kind == sdkmetric.InstrumentKindCounter { |
| 95 |
if inst.Name == pkg+meterName { |
| 96 |
return sdkmetric.Stream{ |
| 97 |
Name: inst.Name, |
| 98 |
Description: description, |
| 99 |
Aggregation: sdkmetric.DefaultAggregationSelector(sdkmetric.InstrumentKindCounter), |
| 100 |
}, true |
| 101 |
} |
| 102 |
} |
| 103 |
return sdkmetric.Stream{}, false |
| 104 |
}, |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
// LatencyMeasure returns the measure for method call latency used by Go CDK APIs. |
| 109 |
func LatencyMeasure(pkg string, provider string) metric.Float64Histogram { |
| 110 |
|
| 111 |
attrs := []attribute.KeyValue{ |
| 112 |
packageKey.String(pkg), |
| 113 |
providerKey.String(provider), |
| 114 |
} |
| 115 |
|
| 116 |
pkgMeter := otel.Meter(pkg, metric.WithInstrumentationAttributes(attrs...)) |
| 117 |
|
| 118 |
m, err := pkgMeter.Float64Histogram( |
| 119 |
pkg+"/latency", |
| 120 |
metric.WithDescription("Latency distribution of method calls"), |
| 121 |
metric.WithUnit(unitMilliseconds), |
| 122 |
) |
| 123 |
|
| 124 |
if err != nil { |
| 125 |
// The only possible errors are from invalid key or value names, and those are programming |
| 126 |
// errors that will be found during testing. |
| 127 |
panic(fmt.Sprintf("fullName=%q, provider=%q: %v", pkg, pkgMeter, err)) |
| 128 |
} |
| 129 |
|
| 130 |
return m |
| 131 |
} |
| 132 |
|
| 133 |
// DimensionlessMeasure creates a simple counter specifically for dimensionless measurements. |
| 134 |
func DimensionlessMeasure(pkg string, provider string, meterName string, description string) metric.Int64Counter { |
| 135 |
|
| 136 |
attrs := []attribute.KeyValue{ |
| 137 |
packageKey.String(pkg), |
| 138 |
providerKey.String(provider), |
| 139 |
} |
| 140 |
|
| 141 |
pkgMeter := otel.Meter(pkg, metric.WithInstrumentationAttributes(attrs...)) |
| 142 |
|
| 143 |
m, err := pkgMeter.Int64Counter(pkg+meterName, metric.WithDescription(description), metric.WithUnit(unitDimensionless)) |
| 144 |
|
| 145 |
if err != nil { |
| 146 |
// The only possible errors are from invalid key or value names, |
| 147 |
// and those are programming errors that will be found during testing. |
| 148 |
panic(fmt.Sprintf("fullName=%q, provider=%q: %v", pkg, pkgMeter, err)) |
| 149 |
} |
| 150 |
return m |
| 151 |
} |
| 152 |
|
| 153 |
// BytesMeasure creates a counter for bytes measurements. |
| 154 |
func BytesMeasure(pkg string, provider string, meterName string, description string) metric.Int64Counter { |
| 155 |
|
| 156 |
attrs := []attribute.KeyValue{ |
| 157 |
packageKey.String(pkg), |
| 158 |
providerKey.String(provider), |
| 159 |
} |
| 160 |
|
| 161 |
pkgMeter := otel.Meter(pkg, metric.WithInstrumentationAttributes(attrs...)) |
| 162 |
m, err := pkgMeter.Int64Counter(pkg+meterName, metric.WithDescription(description), metric.WithUnit(unitBytes)) |
| 163 |
|
| 164 |
if err != nil { |
| 165 |
// The only possible errors are from invalid key or value names, and those are programming |
| 166 |
// errors that will be found during testing. |
| 167 |
panic(fmt.Sprintf("fullName=%q, provider=%q: %v", pkg, pkgMeter, err)) |
| 168 |
} |
| 169 |
return m |
| 170 |
} |