Time passes, APIs change, things break (or not).
go.opentelemetry.io/otel v0.10.0
1package main
2
3import (
4 "context"
5 "net/http"
6
7 "go.opentelemetry.io/otel/api/global"
8 "go.opentelemetry.io/otel/api/propagation"
9 "go.opentelemetry.io/otel/exporters/trace/jaeger"
10 sdktrace "go.opentelemetry.io/otel/sdk/trace"
11)
12
13func ExampleTracing() {
14 // configure tracer
15 // alternatively:
16 // jaegerProvider, flush, err := jaeger.NewExportPipeline(...)
17 // global.SetTraceProvider(jaegerProvider)
18 flush, _ := jaeger.InstallNewPipeline(
19 jaeger.WithCollectorEndpoint(
20 jaeger.CollectorEndpointFromEnv(),
21 ),
22 jaeger.WithProcess(jaeger.Process{
23 ServiceName: "my-service",
24 }),
25 jaeger.WithSDK(&sdktrace.Config{
26 DefaultSampler: sdktrace.ProbabilitySampler(0.1),
27 }),
28 )
29 defer flush()
30 tracer := global.Tracer("my-tracer")
31
32 // configure propagation
33 // default should be usable?
34 // alternatively:
35 // b3Prop := trace.B3{}
36 // alternatively:
37 // w3cProp := trace.TraceContext{}
38 // global.SetPropagators(propagation.New(
39 // propagation.WithExtractors(w3cProp),
40 // propagation.WithInjectors(w3cProp),
41 // ))
42
43 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
44 // extract span from incoming request
45 ctx := propagation.ExtractHTTP(r.Context(), global.Propagators(), r.Header)
46 ctx, span := tracer.Start(ctx, "span")
47 defer span.End()
48
49 // example function
50 func(ctx context.Context) {
51 ctx, span := tracer.Start(ctx, "expensive function")
52 defer span.End()
53 // do stuff
54 _ = ctx
55 }(ctx)
56
57 // propagate trace into outgoing request
58 req, _ := http.NewRequest("GET", "https://opentelemetry.io/", nil)
59 propagation.InjectHTTP(ctx, global.Propagators(), req.Header)
60
61 http.DefaultClient.Do(req)
62 })
63}
Limitations:
1package main
2
3import (
4 "context"
5 "net/http"
6 "time"
7
8 "go.opentelemetry.io/otel/api/global"
9 "go.opentelemetry.io/otel/api/kv"
10 "go.opentelemetry.io/otel/api/metric"
11 "go.opentelemetry.io/otel/api/unit"
12 "go.opentelemetry.io/otel/exporters/metric/prometheus"
13)
14
15func ExampleMetric() {
16 promExporter, _ := prometheus.InstallNewPipeline(prometheus.Config{
17 DefaultHistogramBoundaries: []float64{1, 5, 10, 50, 100},
18 })
19 http.Handle("/metrics", promExporter)
20
21 meter := global.Meter("service_name")
22
23 counter0 := metric.Must(meter).NewFloat64Counter("counter0",
24 metric.WithDescription("hello world"),
25 metric.WithUnit(unit.Bytes),
26 )
27 counter1 := metric.Must(meter).NewFloat64Counter("counter1")
28
29 hist0 := metric.Must(meter).NewFloat64ValueRecorder("hist0")
30 hist1 := metric.Must(meter).NewFloat64ValueRecorder("hist1")
31
32 go func() {
33 var i int
34 for range time.NewTicker(500 * time.Millisecond).C {
35 i++
36 ctx := context.Background()
37 counter0.Add(ctx, float64(i))
38 counter1.Bind(kv.String("key1", "value1"), kv.Int("key2", i%10)).Add(ctx, float64(i))
39 hist0.Record(ctx, float64(i))
40 hist1.Record(ctx, float64(i), kv.String("key1", "value1"), kv.Int("key2", i%10))
41
42 // alternatively
43 // meter.RecordBatch(
44 // ctx,
45 // []kv.KeyValue{kv.String("key1", "value1"), kv.Int("key2", i%10)},
46 // counter1.Measurement(float64(i)),
47 // hist1.Measurement(float64(i)),
48 // )
49 }
50 }()
51
52 http.ListenAndServe(":8080", nil)
53}