-
Notifications
You must be signed in to change notification settings - Fork 2
/
tracer.go
79 lines (64 loc) · 1.88 KB
/
tracer.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
78
79
package appx
import (
"context"
"io"
texporter "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace"
"github.com/reearth/reearthx/log"
"github.com/uber/jaeger-client-go"
jaegercfg "github.com/uber/jaeger-client-go/config"
jaegerlog "github.com/uber/jaeger-client-go/log"
"github.com/uber/jaeger-lib/metrics"
"go.opentelemetry.io/otel"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
type Tracer string
const TRACER_GCP = Tracer("gcp")
const TRACER_JAEGER = Tracer("jaeger")
type TracerConfig struct {
Name string
Tracer Tracer
TracerSample float64
}
func InitTracer(ctx context.Context, conf TracerConfig) io.Closer {
if conf.Tracer == TRACER_GCP {
initGCPTracer(ctx, conf)
} else if conf.Tracer == TRACER_JAEGER {
return initJaegerTracer(conf)
}
return nil
}
func initGCPTracer(ctx context.Context, conf TracerConfig) {
exporter, err := texporter.New()
if err != nil {
log.Fatalc(ctx, err)
}
tp := sdktrace.NewTracerProvider(sdktrace.WithSyncer(exporter), sdktrace.WithSampler(sdktrace.TraceIDRatioBased(conf.TracerSample)))
defer func() {
_ = tp.ForceFlush(ctx)
}()
otel.SetTracerProvider(tp)
log.Infofc(ctx, "tracer: initialized cloud trace with sample fraction: %g", conf.TracerSample)
}
func initJaegerTracer(conf TracerConfig) io.Closer {
cfg := jaegercfg.Configuration{
Sampler: &jaegercfg.SamplerConfig{
Type: jaeger.SamplerTypeConst,
Param: conf.TracerSample,
},
Reporter: &jaegercfg.ReporterConfig{
LogSpans: true,
},
}
jLogger := jaegerlog.StdLogger
jMetricsFactory := metrics.NullFactory
closer, err := cfg.InitGlobalTracer(
conf.Name,
jaegercfg.Logger(jLogger),
jaegercfg.Metrics(jMetricsFactory),
)
if err != nil {
log.Fatalf("Could not initialize jaeger tracer: %s\n", err.Error())
}
log.Infof("tracer: initialized jaeger tracer with sample fraction: %g\n", conf.TracerSample)
return closer
}