-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.go
138 lines (118 loc) · 3.71 KB
/
simple.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/GuanceCloud/oteldatadogtie"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
"gopkg.in/DataDog/dd-trace-go.v1/profiler"
)
var OtelTracer trace.Tracer
func httpExporter(ctx context.Context) (sdktrace.SpanExporter, error) {
exporter, err := otlptracehttp.New(ctx,
otlptracehttp.WithEndpoint("127.0.0.1:9529"),
otlptracehttp.WithURLPath("/otel/v1/trace"),
otlptracehttp.WithInsecure(),
otlptracehttp.WithCompression(otlptracehttp.GzipCompression),
otlptracehttp.WithTimeout(time.Second*15),
otlptracehttp.WithRetry(otlptracehttp.RetryConfig{
Enabled: true,
InitialInterval: time.Second,
MaxInterval: time.Second * 15,
MaxElapsedTime: time.Minute * 2,
}),
)
return exporter, err
}
func stdoutExporter(_ context.Context) (sdktrace.SpanExporter, error) {
return stdouttrace.New(stdouttrace.WithPrettyPrint())
}
func main() {
ctx := context.Background()
exporter, err := httpExporter(ctx)
if err != nil {
log.Fatalf("unable to init exporter: %v", err)
}
tp := oteldatadogtie.NewTracerProvider(
sdktrace.WithBatcher(exporter,
sdktrace.WithBatchTimeout(time.Second),
sdktrace.WithExportTimeout(time.Second*15),
),
)
defer tp.Shutdown(ctx)
otel.SetTracerProvider(tp)
// If you need to use custom Resource, you should add attribute
// oteldatadogtie.AttributeRuntimeID to it, then you can call the
// Open Telemetry original sdktrace.NewTracerProvider function as usual,
// at last use oteldatadogtie.Wrap function to wrap your TracerProvider
// instance, for example:
//
// res, err := resource.New(ctx,
// resource.WithProcess(),
// resource.WithAttributes(
// attribute.String("foo", "bar"),
// oteldatadogtie.AttributeRuntimeID,
// ),
// )
// if err != nil {
// log.Fatalf("unable to build resource: %v", err)
// }
// if res, err = resource.Merge(resource.Default(), res); err != nil {
// log.Fatalf("unable to merge resource: %v", err)
// }
//
// tp2 := sdktrace.NewTracerProvider(
// sdktrace.WithBatcher(
// exporter,
// sdktrace.WithBatchTimeout(time.Second),
// sdktrace.WithExportTimeout(time.Second*15),
// ),
// sdktrace.WithResource(res),
// )
//
// defer tp2.Shutdown(ctx)
// otel.SetTracerProvider(oteldatadogtie.Wrap(tp2))
OtelTracer = otel.Tracer("oteldatadogtie")
// Use our oteldatadogtie.Start wrapper to start Datadog profiler,
// or use the original Datadog profiler.Start and combine setting
// tag oteldatadogtie.TagRuntimeID, see oteldatadogtie.StartDDProfiler
// for details.
err = oteldatadogtie.StartDDProfiler(
profiler.WithProfileTypes(profiler.CPUProfile, profiler.HeapProfile, profiler.MutexProfile, profiler.GoroutineProfile),
profiler.WithService("oteldatadogtie-demo"),
profiler.WithEnv("testing"),
profiler.WithVersion("v0.0.1"),
profiler.WithAgentAddr("127.0.0.1:9529"),
)
if err != nil {
log.Fatalf("unable to start dd profiler: %v", err)
}
defer oteldatadogtie.StopDDProfiler()
n := 27
for {
func() {
newCtx, span := OtelTracer.Start(ctx, "main", trace.WithAttributes(attribute.Int("n", n)))
defer span.End()
fmt.Printf("fibonacci(%d) = %d\n", n, fibonacci(newCtx, n))
time.Sleep(time.Second * 5)
}()
}
}
func fibonacci(ctx context.Context, n int) int {
newCtx := ctx
if n%6 == 5 {
var span trace.Span
newCtx, span = OtelTracer.Start(ctx, "fibonacci", trace.WithAttributes(attribute.Int("n", n)))
defer span.End()
}
if n < 2 {
return 1
}
return fibonacci(newCtx, n-1) + fibonacci(newCtx, n-2)
}