-
Notifications
You must be signed in to change notification settings - Fork 9
/
tracing.go
52 lines (42 loc) · 1.4 KB
/
tracing.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package actiontracing
import (
"context"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/jaeger"
"go.opentelemetry.io/otel/sdk/resource"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
)
func SetupTracing(ctx context.Context, jaegerEndpoint string) (context.Context, func()) {
tp, err := createTracer(jaegerEndpoint)
if err != nil {
panic(err)
}
// Register our TracerProvider as the global so any imported
// instrumentation in the future will default to using it.
otel.SetTracerProvider(tp)
spanCtx, span := tp.Tracer("ns").Start(ctx, "ns (cli invocation)")
return spanCtx, func() {
span.End()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
_ = tp.Shutdown(ctx)
}
}
func createTracer(url string) (*tracesdk.TracerProvider, error) {
exp, err := jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(url)))
if err != nil {
return nil, err
}
return tracesdk.NewTracerProvider(
tracesdk.WithBatcher(exp, tracesdk.WithMaxExportBatchSize(1)),
tracesdk.WithResource(resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String("foundation"),
)),
), nil
}