@@ -5,6 +5,9 @@ weight: 10
5
5
cSpell:ignore : chan fatalln funcs intn itoa khtml otelhttp rolldice stdouttrace strconv
6
6
---
7
7
8
+ <!-- markdownlint-disable blanks-around-fences -->
9
+ <? code-excerpt path-base="examples/go/dice"?>
10
+
8
11
This page will show you how to get started with OpenTelemetry in Go.
9
12
10
13
You will learn how you can instrument a simple application manually, in such a
@@ -121,6 +124,8 @@ application that exports telemetry.
121
124
122
125
Create ` otel.go ` with OpenTelemetry SDK bootstrapping code:
123
126
127
+ <!-- prettier-ignore-start -->
128
+ <? code-excerpt "otel.go" from="package main"?>
124
129
``` go
125
130
package main
126
131
@@ -220,6 +225,7 @@ func newMeterProvider() (*metric.MeterProvider, error) {
220
225
return meterProvider, nil
221
226
}
222
227
```
228
+ <!-- prettier-ignore-end -->
223
229
224
230
If you're only using tracing or metrics, you can omit the code the corresponding
225
231
TracerProvider or MeterProvider initialization code.
@@ -232,6 +238,8 @@ server.
232
238
Modify ` main.go ` to include code that sets up OpenTelemetry SDK and instruments
233
239
the HTTP server using the ` otelhttp ` instrumentation library:
234
240
241
+ <!-- prettier-ignore-start -->
242
+ <? code-excerpt "main.go" from="package main"?>
235
243
``` go
236
244
package main
237
245
@@ -317,6 +325,7 @@ func newHTTPHandler() http.Handler {
317
325
return handler
318
326
}
319
327
```
328
+ <!-- prettier-ignore-end -->
320
329
321
330
### Add Custom Instrumentation
322
331
@@ -327,6 +336,8 @@ your application. For that you'll need to write some custom
327
336
328
337
Modify ` rolldice.go ` to include custom instrumentation using OpenTelemetry API:
329
338
339
+ <!-- prettier-ignore-start -->
340
+ <? code-excerpt "rolldice.go" from="package main"?>
330
341
``` go
331
342
package main
332
343
@@ -364,7 +375,6 @@ func rolldice(w http.ResponseWriter, r *http.Request) {
364
375
365
376
roll := 1 + rand.Intn (6 )
366
377
367
- // Add the custom attribute to the span and counter.
368
378
rollValueAttr := attribute.Int (" roll.value" , roll)
369
379
span.SetAttributes (rollValueAttr)
370
380
rollCnt.Add (ctx, 1 , metric.WithAttributes (rollValueAttr))
@@ -375,6 +385,7 @@ func rolldice(w http.ResponseWriter, r *http.Request) {
375
385
}
376
386
}
377
387
```
388
+ <!-- prettier-ignore-end -->
378
389
379
390
Note that if you're only using tracing or metrics, you can omit the
380
391
corresponding code that instruments the other telemetry type.
0 commit comments