-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
77 lines (59 loc) · 1.74 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
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"context"
"log"
"github.com/gin-gonic/gin"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
)
var tracer trace.Tracer
func main() {
// init tracer and ensure cleanup after app ends
ctx := context.Background()
exp := newExporter(ctx)
tp := newTraceProvider(exp)
defer func() { _ = tp.Shutdown(ctx) }()
otel.SetTracerProvider(tp)
tracer = tp.Tracer("otel-texample")
r := gin.Default()
r.Use(otelgin.Middleware("otlp-example"))
r.GET("/reverse/:str", reverseStrHandler())
r.Run() // listen on :8080
}
func newExporter(ctx context.Context) *otlptrace.Exporter {
secureOption := otlptracegrpc.WithInsecure() // TODO Use WithTLSCredentials for prod
exporter, err := otlptrace.New(
context.Background(),
otlptracegrpc.NewClient(
secureOption,
otlptracegrpc.WithEndpoint("localhost:4317"),
),
)
if err != nil {
log.Fatalf("failed to init exporter: %v", err)
}
return exporter
}
func newTraceProvider(exp sdktrace.SpanExporter) *sdktrace.TracerProvider {
r, err := resource.New(
context.Background(),
resource.WithAttributes(
semconv.ServiceNameKey.String("otlp-example"),
semconv.ServiceNamespaceKey.String("localsvc"),
),
)
if err != nil {
panic(err)
}
return sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()), // TODO Set sane sampling rates when deployed
sdktrace.WithBatcher(exp),
sdktrace.WithResource(r),
)
}