-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcode.go
216 lines (171 loc) · 5.1 KB
/
code.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"context"
"log"
"net/http"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
"go.opentelemetry.io/otel/trace"
)
func main() {
// start_create_span1 OMIT
tracer := otel.Tracer("ex.com/my/application")
ctx, span := tracer.Start(context.Background(), "my span name") // HL
defer span.End()
// end_create_span1 OMIT
// start-span-from-ctx OMIT
span := trace.SpanFromContext(ctx)
// end-span-from-ctx OMIT
// start-span-attribute OMIT
fooKey := attribute.Key("foo") // HL
bar42KeyValue := attribute.Int("bar", 42) // HL
ctx, span := tracer.Start(
ctx,
"my span name",
trace.WithAttributes( // HL
fooKey.String("fooValue"),
bar42KeyValue,
attribute.Bool("admin", false)))
defer span.End()
// end-span-attribute OMIT
// start-attribute-1 OMIT
span.SetAttributes(
fooKey.String("fooValue"),
bar42KeyValue,
attribute.Bool("admin", false))
// end-attribute-1 OMIT
// start-spevent OMIT
span.AddEvent("some-event", // HL
trace.WithStackTrace(true),
trace.WithAttributes(attribute.String("event-attr-key", "event-ettr-value")))
// end-spevent OMIT
// start-propagate-http OMIT
r, _ := http.NewRequest(http.MethodGet, "https://ex.com", nil)
otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(r.Header)) // HL
// end-propagate-http OMIT
}
type Handler struct{}
// start-receive-http OMIT
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := otel.GetTextMapPropagator().Extract(r.Context(), propagation.HeaderCarrier(r.Header)) // HL
ctx, span := otel.Tracer("ex.com/my/app/http/handler").
Start(ctx, "handler name", trace.WithAttributes( /* ... */ ))
defer span.End()
// ...
return
}
// end-receive-http OMIT
type EventsHandler struct{}
const AttrKeyEventName = attribute.Key("events.name")
// start-receive-events OMIT
func (h *EventsHandler) Handle(ctx context.Context, e Event) error {
tr := otel.Tracer("ex.com/my/app/events/handler")
ctx = otel.GetTextMapPropagator().Extract(ctx, e.Headers) // HL
ctx, sp := tr.Start(
ctx,
"events name",
trace.WithSpanKind(trace.SpanKindConsumer),
trace.WithAttributes(AttrKeyEventName.String("events name")),
)
defer sp.End()
// ...
return nil
}
// end-receive-events OMIT
// start-events OMIT
type Header map[string]string // HL
type Event struct {
Headers Header
Key []byte
Payload []byte
}
// end-events OMIT
// start-events-propagation OMIT
// Ensure Handler implements OTel propagation.TextMapCarrier
var _ = propagation.TextMapCarrier(Header{})
func (h Header) Get(key string) string {
return h[key]
}
func (h Header) Set(key, value string) {
h[key] = value
}
func (h Header) Keys() []string {
keys := make([]string, len(h))
for k := range h {
keys = append(keys, k)
}
return keys
}
// end-events-propagation OMIT
func export() {
// start-resource OMIT
res, err := resource.New(context.TODO(), // HL
resource.WithAttributes( // HL
semconv.ServiceNameKey.String("my awesome service"),
semconv.ServiceVersionKey.String("develop"),
semconv.DeploymentEnvironmentKey.String("local"),
attribute.String("resource_attribute", "added as resource"),
),
)
if err != nil {
log.Fatalf("failed to create otel sdk/resource: %v", err)
}
// end-resource OMIT
// start-client OMIT
otlpClient := otlptracegrpc.NewClient( // HL
otlptracegrpc.WithInsecure(),
otlptracegrpc.WithEndpoint("localhost:55680"))
// end-client OMIT
// start-exporter OMIT
otlpExporter, err := otlptrace.New(context.TODO(), otlpClient) // HL
if err != nil {
log.Fatalf("failed to create OTel exporter, disabling OTel: %v", err)
return
}
// end-exporter OMIT
// start-stdoutexporter OMIT
stdoutExporter, err := stdouttrace.New()
if err != nil {
log.Fatalf("failed to create stdouttrace exporter: %v", err)
}
// end-stdoutexporter OMIT
// start-traceprovider OMIT
tracerProvider := trace.NewTracerProvider( // HL_provider
trace.WithSampler(trace.AlwaysSample()), // HL_Sampler
trace.WithResource(res), // HL_resource
trace.WithSyncer(otlpExporter), // HL_exporter
)
// end-traceprovider OMIT
// start-traceprovider-2 OMIT
tracerProvider := trace.NewTracerProvider(
trace.WithSampler(trace.AlwaysSample()),
trace.WithResource(res),
trace.WithBatcher(otlpExporter), // HL
)
// end-traceprovider-2 OMIT
// start-debug-exporter OMIT
if debug {
tracerProvider.RegisterSpanProcessor(
trace.NewSimpleSpanProcessor(stdoutExporter))
}
// end-debug-exporter OMIT
// start-register OMIT
otel.SetTracerProvider(tracerProvider) // HL
otel.SetTextMapPropagator( // HL
propagation.NewCompositeTextMapPropagator(
propagation.Baggage{},
propagation.TraceContext{},
),
)
// end-register OMIT
// start-getglobals OMIT
otel.GetTracerProvider()
otel.GetTextMapPropagator()
// end-getglobals OMIT
}