-
Notifications
You must be signed in to change notification settings - Fork 52
/
opentelemetry.go
57 lines (51 loc) · 1.33 KB
/
opentelemetry.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
package main
import (
"log"
"os"
"go.opentelemetry.io/otel/exporters/jaeger"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
)
var tp *trace.TracerProvider
func initTracer() (*trace.TracerProvider, error) {
url := os.Getenv("JAEGER_ENDPOINT")
if len(url) > 0 {
return initJaegerTracer(url)
} else {
return initFileTracer()
}
}
func initFileTracer() (*trace.TracerProvider, error) {
log.Println("Initializing tracing to traces.json")
f, err := os.Create("traces.json")
if err != nil {
return nil, err
}
exporter, err := stdouttrace.New(
stdouttrace.WithWriter(f),
stdouttrace.WithPrettyPrint(),
)
if err != nil {
return nil, err
}
return trace.NewTracerProvider(
trace.WithBatcher(exporter),
trace.WithSampler(trace.AlwaysSample()),
), nil
}
func initJaegerTracer(url string) (*trace.TracerProvider, error) {
log.Printf("Initializing tracing to jaeger at %s\n", url)
exporter, err := jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(url)))
if err != nil {
return nil, err
}
return trace.NewTracerProvider(
trace.WithBatcher(exporter),
trace.WithResource(resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String(serviceName),
)),
), nil
}