This repository has been archived by the owner on Sep 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
64 lines (53 loc) · 1.57 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"fmt"
"log"
"net/http"
"time"
"math/rand"
"go.opentelemetry.io/otel/api/global"
"go.opentelemetry.io/otel/api/trace"
"go.opentelemetry.io/otel/exporters/stdout"
httptrace "go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
honeycombsamplers "github.com/honeycombio/opentelemetry-samplers-go/honeycombsamplers"
)
func main() {
exp, err := stdout.NewExporter([]stdout.Option{
stdout.WithQuantiles([]float64{0.5, 0.9, 0.99}),
stdout.WithPrettyPrint(),
}...)
if err != nil {
log.Fatal(err)
}
sample, err := honeycombsamplers.DeterministicSampler(2)
if err != nil {
log.Fatal(err)
}
config := sdktrace.Config{
DefaultSampler: sample,
}
tp := sdktrace.NewTracerProvider(sdktrace.WithConfig(config), sdktrace.WithSyncer(exp))
global.SetTracerProvider(tp)
tracer := global.Tracer("sampling-service")
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
attrs, _, spanCtx := httptrace.Extract(ctx, r)
ctx, span := tracer.Start(
trace.ContextWithRemoteSpanContext(ctx, spanCtx),
"parent-span",
trace.WithAttributes(attrs...),
)
upper := rand.Intn(10)
for i := 0; i < upper; i++ {
_, iSpan := tracer.Start(ctx, fmt.Sprintf("Sample-%d", i))
log.Printf("Doing really hard work (%d / %d)\n", i + 1, upper)
<-time.After(time.Duration(rand.Intn(1000)) * time.Millisecond)
iSpan.End()
}
defer span.End()
fmt.Fprintf(w, "Hello, World")
})
log.Fatal(http.ListenAndServe(":8000", mux))
}