/
setup_grpc.go
51 lines (44 loc) · 1.41 KB
/
setup_grpc.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
package otelKit
import (
"github.com/richelieu-yang/chimera/v3/src/core/strKit"
"github.com/richelieu-yang/chimera/v3/src/log/logrusKit"
"github.com/sirupsen/logrus"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/propagation"
"time"
)
// MustSetUpWithGrpc
/*
@param opts e.g. otlptracegrpc.WithInsecure(), otlptracegrpc.WithDialOption(grpc.WithBlock())
*/
func MustSetUpWithGrpc(grpcEndpoint, serviceName string, attributeMap map[string]string, opts ...otlptracegrpc.Option) {
err := SetUpWithGrpc(grpcEndpoint, serviceName, attributeMap, opts...)
if err != nil {
logrusKit.DisableQuote(nil)
logrus.Fatalf("%+v", err)
}
}
func SetUpWithGrpc(grpcEndpoint, serviceName string, attributeMap map[string]string, opts ...otlptracegrpc.Option) (err error) {
defer func() {
if err == nil {
setupFlag.Store(true)
}
}()
if err = strKit.AssertNotEmpty(serviceName, "serviceName"); err != nil {
return
}
/* TracerProvider */
tp, err := NewGrpcTracerProvider(grpcEndpoint, serviceName, attributeMap, opts...)
if err != nil {
return
}
otel.SetTracerProvider(tp)
/* TextMapPropagator */
textMapPropagator := propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})
otel.SetTextMapPropagator(textMapPropagator)
logrus.RegisterExitHandler(func() {
ShutdownTracerProvider(tp, time.Second*3)
})
return
}