1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package metric_test
16
17 import (
18 "context"
19 "database/sql"
20 "fmt"
21 "net/http"
22 "runtime"
23 "time"
24
25 "go.opentelemetry.io/otel"
26 "go.opentelemetry.io/otel/attribute"
27 "go.opentelemetry.io/otel/metric"
28 semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
29 )
30
31 var meter = otel.Meter("my-service-meter")
32
33 func ExampleMeter_synchronous() {
34
35 workDuration, err := meter.Int64Histogram(
36 "workDuration",
37 metric.WithUnit("ms"))
38 if err != nil {
39 fmt.Println("Failed to register instrument")
40 panic(err)
41 }
42
43 startTime := time.Now()
44 ctx := context.Background()
45
46
47 workDuration.Record(ctx, time.Since(startTime).Milliseconds())
48 }
49
50 func ExampleMeter_asynchronous_single() {
51 _, err := meter.Int64ObservableGauge(
52 "DiskUsage",
53 metric.WithUnit("By"),
54 metric.WithInt64Callback(func(_ context.Context, obsrv metric.Int64Observer) error {
55
56
57
58
59
60
61
62
63
64
65
66
67 usage := 75000
68 obsrv.Observe(int64(usage), metric.WithAttributes(attribute.Int("disk.id", 3)))
69 return nil
70 }),
71 )
72 if err != nil {
73 fmt.Println("failed to register instrument")
74 panic(err)
75 }
76 }
77
78 func ExampleMeter_asynchronous_multiple() {
79
80 heapAlloc, err := meter.Int64ObservableUpDownCounter("heapAllocs")
81 if err != nil {
82 fmt.Println("failed to register updown counter for heapAllocs")
83 panic(err)
84 }
85 gcCount, err := meter.Int64ObservableCounter("gcCount")
86 if err != nil {
87 fmt.Println("failed to register counter for gcCount")
88 panic(err)
89 }
90
91 _, err = meter.RegisterCallback(
92 func(_ context.Context, o metric.Observer) error {
93 memStats := &runtime.MemStats{}
94
95 runtime.ReadMemStats(memStats)
96
97 o.ObserveInt64(heapAlloc, int64(memStats.HeapAlloc))
98 o.ObserveInt64(gcCount, int64(memStats.NumGC))
99
100 return nil
101 },
102 heapAlloc,
103 gcCount,
104 )
105 if err != nil {
106 fmt.Println("Failed to register callback")
107 panic(err)
108 }
109 }
110
111
112
113
114 func ExampleMeter_counter() {
115 apiCounter, err := meter.Int64Counter(
116 "api.counter",
117 metric.WithDescription("Number of API calls."),
118 metric.WithUnit("{call}"),
119 )
120 if err != nil {
121 panic(err)
122 }
123 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
124 apiCounter.Add(r.Context(), 1)
125
126
127 })
128 }
129
130
131
132
133
134 func ExampleMeter_upDownCounter() {
135 var err error
136 itemsCounter, err := meter.Int64UpDownCounter(
137 "items.counter",
138 metric.WithDescription("Number of items."),
139 metric.WithUnit("{item}"),
140 )
141 if err != nil {
142 panic(err)
143 }
144
145 _ = func() {
146
147 itemsCounter.Add(context.Background(), 1)
148 }
149
150 _ = func() {
151
152 itemsCounter.Add(context.Background(), -1)
153 }
154 }
155
156
157
158
159 func ExampleMeter_histogram() {
160 histogram, err := meter.Float64Histogram(
161 "task.duration",
162 metric.WithDescription("The duration of task execution."),
163 metric.WithUnit("s"),
164 metric.WithExplicitBucketBoundaries(.005, .01, .025, .05, .075, .1, .25, .5, .75, 1, 2.5, 5, 7.5, 10),
165 )
166 if err != nil {
167 panic(err)
168 }
169 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
170 start := time.Now()
171
172
173
174 duration := time.Since(start)
175 histogram.Record(r.Context(), duration.Seconds())
176 })
177 }
178
179
180
181
182
183 func ExampleMeter_observableCounter() {
184 start := time.Now()
185 if _, err := meter.Float64ObservableCounter(
186 "uptime",
187 metric.WithDescription("The duration since the application started."),
188 metric.WithUnit("s"),
189 metric.WithFloat64Callback(func(_ context.Context, o metric.Float64Observer) error {
190 o.Observe(float64(time.Since(start).Seconds()))
191 return nil
192 }),
193 ); err != nil {
194 panic(err)
195 }
196 }
197
198
199
200
201
202 func ExampleMeter_observableUpDownCounter() {
203
204
205 _ = func(db *sql.DB, meter metric.Meter, poolName string) (metric.Registration, error) {
206 max, err := meter.Int64ObservableUpDownCounter(
207 "db.client.connections.max",
208 metric.WithDescription("The maximum number of open connections allowed."),
209 metric.WithUnit("{connection}"),
210 )
211 if err != nil {
212 return nil, err
213 }
214
215 waitTime, err := meter.Int64ObservableUpDownCounter(
216 "db.client.connections.wait_time",
217 metric.WithDescription("The time it took to obtain an open connection from the pool."),
218 metric.WithUnit("ms"),
219 )
220 if err != nil {
221 return nil, err
222 }
223
224 reg, err := meter.RegisterCallback(
225 func(_ context.Context, o metric.Observer) error {
226 stats := db.Stats()
227 o.ObserveInt64(max, int64(stats.MaxOpenConnections))
228 o.ObserveInt64(waitTime, int64(stats.WaitDuration))
229 return nil
230 },
231 max,
232 waitTime,
233 )
234 if err != nil {
235 return nil, err
236 }
237 return reg, nil
238 }
239 }
240
241
242
243
244
245 func ExampleMeter_observableGauge() {
246 if _, err := meter.Int64ObservableGauge(
247 "memory.heap",
248 metric.WithDescription(
249 "Memory usage of the allocated heap objects.",
250 ),
251 metric.WithUnit("By"),
252 metric.WithInt64Callback(func(_ context.Context, o metric.Int64Observer) error {
253 var m runtime.MemStats
254 runtime.ReadMemStats(&m)
255 o.Observe(int64(m.HeapAlloc))
256 return nil
257 }),
258 ); err != nil {
259 panic(err)
260 }
261 }
262
263
264
265
266 func ExampleMeter_attributes() {
267 apiCounter, err := meter.Int64UpDownCounter(
268 "api.finished.counter",
269 metric.WithDescription("Number of finished API calls."),
270 metric.WithUnit("{call}"),
271 )
272 if err != nil {
273 panic(err)
274 }
275 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
276
277 statusCode := http.StatusOK
278
279 apiCounter.Add(r.Context(), 1,
280 metric.WithAttributes(semconv.HTTPStatusCode(statusCode)))
281 })
282 }
283
View as plain text