-
Notifications
You must be signed in to change notification settings - Fork 3
/
otel.go
71 lines (60 loc) · 1.67 KB
/
otel.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
package otel
import (
"context"
"net"
"github.com/alexfalkowski/go-service/os"
"github.com/alexfalkowski/go-service/version"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/jaeger"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
"go.opentelemetry.io/otel/trace"
"go.uber.org/fx"
)
// TracerParams for otel.
type TracerParams struct {
Lifecycle fx.Lifecycle
Name string
Version version.Version
Config *Config
}
// Register for otel.
func Register() {
otel.SetTextMapPropagator(propagation.TraceContext{})
}
// NewNoopTracer for otel.
func NewNoopTracer(name string) trace.Tracer {
return trace.NewNoopTracerProvider().Tracer(name)
}
// NewTracer for otel.
func NewTracer(params TracerParams) (trace.Tracer, error) {
if params.Config.IsJaeger() {
host, port, err := net.SplitHostPort(params.Config.Host)
if err != nil {
return nil, err
}
exp, err := jaeger.New(jaeger.WithAgentEndpoint(jaeger.WithAgentHost(host), jaeger.WithAgentPort(port)))
if err != nil {
return nil, err
}
p := tracesdk.NewTracerProvider(
tracesdk.WithBatcher(exp),
tracesdk.WithResource(resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceName(params.Name),
semconv.ServiceVersion(string(params.Version)),
attribute.String("name", os.ExecutableName()),
)),
)
params.Lifecycle.Append(fx.Hook{
OnStop: func(ctx context.Context) error {
return p.Shutdown(ctx)
},
})
return p.Tracer(params.Name), nil
}
return NewNoopTracer(params.Name), nil
}