-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_test.go
More file actions
318 lines (279 loc) · 7.65 KB
/
example_test.go
File metadata and controls
318 lines (279 loc) · 7.65 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright 2025 The Rivaas Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build !integration
package tracing_test
import (
"context"
"fmt"
"net/http"
"go.opentelemetry.io/otel/attribute"
"rivaas.dev/tracing"
)
// ExampleNew demonstrates creating a new tracing configuration.
func ExampleNew() {
tracer, err := tracing.New(
tracing.WithServiceName("my-service"),
tracing.WithServiceVersion("1.0.0"),
tracing.WithStdout(),
)
if err != nil {
if _, printErr := fmt.Printf("Error: %v\n", err); printErr != nil {
panic(printErr)
}
return
}
defer func() {
if err = tracer.Shutdown(context.Background()); err != nil {
panic(err)
}
}()
if _, err = fmt.Printf("Tracing enabled: %v\n", tracer.IsEnabled()); err != nil {
panic(err)
}
// Output: Tracing enabled: true
}
// ExampleMustNew demonstrates creating tracing configuration that panics on error.
func ExampleMustNew() {
tracer := tracing.MustNew(
tracing.WithServiceName("my-service"),
tracing.WithStdout(),
)
defer func() {
if err := tracer.Shutdown(context.Background()); err != nil {
panic(err)
}
}()
if _, err := fmt.Printf("Service: %s\n", tracer.ServiceName()); err != nil {
panic(err)
}
// Output: Service: my-service
}
// ExampleStartSpan demonstrates creating and managing spans.
func ExampleTracer_StartSpan() {
tracer := tracing.MustNew(
tracing.WithServiceName("my-service"),
tracing.WithStdout(),
)
defer func() {
if err := tracer.Shutdown(context.Background()); err != nil {
panic(err)
}
}()
ctx := context.Background()
_, span := tracer.StartSpan(ctx, "database-query")
defer tracer.FinishSpan(span)
tracer.SetSpanAttribute(span, "db.query", "SELECT * FROM users")
tracer.SetSpanAttribute(span, "db.rows", 10)
// Output:
}
// ExampleTracer_AddSpanEvent demonstrates adding events to spans.
func ExampleTracer_AddSpanEvent() {
tracer := tracing.MustNew(
tracing.WithServiceName("my-service"),
tracing.WithStdout(),
)
defer func() {
if err := tracer.Shutdown(context.Background()); err != nil {
panic(err)
}
}()
ctx := context.Background()
ctx, span := tracer.StartSpan(ctx, "cache-operation")
defer tracer.FinishSpan(span)
tracer.AddSpanEvent(span, "cache_hit",
attribute.String("key", "user:123"),
attribute.Int("ttl", 3600),
)
_ = ctx // use ctx
// Output:
}
// ExampleTracer_FinishSpan demonstrates ending a span with success.
func ExampleTracer_FinishSpan() {
tracer := tracing.MustNew(
tracing.WithServiceName("my-service"),
tracing.WithStdout(),
)
defer func() {
if err := tracer.Shutdown(context.Background()); err != nil {
panic(err)
}
}()
ctx, span := tracer.StartSpan(context.Background(), "operation")
defer tracer.FinishSpan(span)
_ = ctx
// Output:
}
// ExampleTracer_FinishSpanWithError demonstrates ending a span with an error.
func ExampleTracer_FinishSpanWithError() {
tracer := tracing.MustNew(
tracing.WithServiceName("my-service"),
tracing.WithStdout(),
)
defer func() {
if err := tracer.Shutdown(context.Background()); err != nil {
panic(err)
}
}()
ctx, span := tracer.StartSpan(context.Background(), "operation")
err := doWork(ctx)
if err != nil {
tracer.FinishSpanWithError(span, err)
return
}
tracer.FinishSpan(span)
// Output:
}
func doWork(context.Context) error {
return nil
}
// ExampleTracer_WithSpan demonstrates running a function under a span.
func ExampleTracer_WithSpan() {
tracer := tracing.MustNew(
tracing.WithServiceName("my-service"),
tracing.WithStdout(),
)
defer func() {
if err := tracer.Shutdown(context.Background()); err != nil {
panic(err)
}
}()
err := tracer.WithSpan(context.Background(), "process-order", func(ctx context.Context) error {
// Simulate work
return nil
})
if err != nil {
panic(err)
}
// Output:
}
// ExampleCopyTraceContext demonstrates propagating trace context to a goroutine.
func ExampleCopyTraceContext() {
tracer := tracing.MustNew(
tracing.WithServiceName("my-service"),
tracing.WithStdout(),
)
defer func() {
if err := tracer.Shutdown(context.Background()); err != nil {
panic(err)
}
}()
ctx, span := tracer.StartSpan(context.Background(), "parent")
traceCtx := tracing.CopyTraceContext(ctx)
go func() {
_, childSpan := tracer.StartSpan(traceCtx, "async-job")
defer tracer.FinishSpan(childSpan)
}()
tracer.FinishSpan(span)
// Output:
}
// ExampleTracer_ExtractTraceContext demonstrates extracting trace context from headers.
func ExampleTracer_ExtractTraceContext() {
ctx := context.Background()
tracer := tracing.MustNew(
tracing.WithServiceName("my-service"),
tracing.WithStdout(),
)
defer func() {
if err := tracer.Shutdown(ctx); err != nil {
panic(err)
}
}()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/api/users", nil)
if err != nil {
panic(err)
}
req.Header.Set("traceparent", "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01")
ctx = tracer.ExtractTraceContext(ctx, req.Header)
ctx, span := tracer.StartSpan(ctx, "process-request")
defer tracer.FinishSpan(span)
// Output:
}
// ExampleWithSampleRate demonstrates configuring sampling rate.
func ExampleWithSampleRate() {
tracer := tracing.MustNew(
tracing.WithServiceName("my-service"),
tracing.WithStdout(),
tracing.WithSampleRate(0.1), // Sample 10% of requests
)
defer func() {
if err := tracer.Shutdown(context.Background()); err != nil {
panic(err)
}
}()
if _, err := fmt.Printf("Service: %s\n", tracer.ServiceName()); err != nil {
panic(err)
}
// Output: Service: my-service
}
// ExampleMiddleware demonstrates using middleware with path exclusion.
func ExampleMiddleware() {
tracer := tracing.MustNew(
tracing.WithServiceName("my-service"),
tracing.WithStdout(),
)
defer func() {
if err := tracer.Shutdown(context.Background()); err != nil {
panic(err)
}
}()
// Create handler with middleware options
mux := http.NewServeMux()
mux.HandleFunc("/api/users", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
// Use Middleware (returns error for invalid options; use MustMiddleware to panic)
mw, err := tracing.Middleware(tracer,
tracing.WithExcludePaths("/health", "/metrics", "/ready"),
tracing.WithHeaders("X-Request-ID", "X-Correlation-ID"),
)
if err != nil {
panic(err)
}
handler := mw(mux)
// Use handler...
_ = handler
// Output:
}
// ExampleWithOTLP demonstrates configuring OTLP provider.
func ExampleWithOTLP() {
tracer := tracing.MustNew(
tracing.WithServiceName("my-service"),
tracing.WithOTLP("localhost:4317", tracing.OTLPInsecure()),
)
defer func() {
if err := tracer.Shutdown(context.Background()); err != nil {
panic(err)
}
}()
if _, err := fmt.Printf("Service: %s\n", tracer.ServiceName()); err != nil {
panic(err)
}
// Output: Service: my-service
}
// ExampleWithOTLPHTTP demonstrates configuring OTLP HTTP provider.
func ExampleWithOTLPHTTP() {
tracer := tracing.MustNew(
tracing.WithServiceName("my-service"),
tracing.WithOTLPHTTP("http://localhost:4318"),
)
defer func() {
if err := tracer.Shutdown(context.Background()); err != nil {
panic(err)
}
}()
if _, err := fmt.Printf("Service: %s\n", tracer.ServiceName()); err != nil {
panic(err)
}
// Output: Service: my-service
}