|
1 | 1 | package otelmiddleware_test
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "log" |
| 4 | + "github.com/vincentfree/opentelemetry-http/otelmiddleware" |
| 5 | + "go.opentelemetry.io/otel" |
| 6 | + "go.opentelemetry.io/otel/attribute" |
5 | 7 | "net/http"
|
| 8 | +) |
6 | 9 |
|
7 |
| - "github.com/vincentfree/opentelemetry-http/otelmiddleware" |
| 10 | +type exampleHandler func(http.ResponseWriter, *http.Request) |
| 11 | + |
| 12 | +func (th exampleHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 13 | + th(w, r) |
| 14 | +} |
| 15 | + |
| 16 | +var ( |
| 17 | + eh = exampleHandler(func(w http.ResponseWriter, r *http.Request) { |
| 18 | + _, _ = w.Write([]byte("Hello World")) |
| 19 | + }) |
8 | 20 | )
|
9 | 21 |
|
10 |
| -func Example() { |
11 |
| - // create a new ServeMux |
12 |
| - serve := http.NewServeMux() |
13 |
| - // add a new route to the ServeMux |
14 |
| - serve.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
15 |
| - _, err := w.Write([]byte("Hello World")) |
16 |
| - if err != nil { |
17 |
| - // handle error |
18 |
| - } |
19 |
| - })) |
20 |
| - // create the Trace middleware and decorate the ServeMux routes with this middleware. |
21 |
| - handler := otelmiddleware.TraceWithOptions(otelmiddleware.WithServiceName("ExampleService"))(serve) |
22 |
| - log.Fatal(http.ListenAndServe(":8080", handler)) |
| 22 | +func ExampleWithTracer() { |
| 23 | + // returns a function that excepts a http.Handler. |
| 24 | + handler := otelmiddleware.TraceWithOptions(otelmiddleware.WithTracer(otel.Tracer("example-tracer"))) |
| 25 | + // pass a http.Handler to extend it with Tracing functionality. |
| 26 | + http.Handle("/", handler(eh)) |
| 27 | +} |
| 28 | + |
| 29 | +func ExampleWithServiceName() { |
| 30 | + // returns a function that excepts a http.Handler. |
| 31 | + handler := otelmiddleware.TraceWithOptions(otelmiddleware.WithServiceName("exampleService")) |
| 32 | + // pass a http.Handler to extend it with Tracing functionality. |
| 33 | + http.Handle("/", handler(eh)) |
| 34 | +} |
| 35 | + |
| 36 | +func ExampleWithAttributes() { |
| 37 | + // returns a function that excepts a http.Handler. |
| 38 | + handler := otelmiddleware.TraceWithOptions(otelmiddleware.WithAttributes(attribute.String("example", "value"))) |
| 39 | + // pass a http.Handler to extend it with Tracing functionality. |
| 40 | + http.Handle("/", handler(eh)) |
| 41 | +} |
| 42 | + |
| 43 | +func ExampleWithPropagator() { |
| 44 | + // returns a function that excepts a http.Handler. |
| 45 | + handler := otelmiddleware.TraceWithOptions(otelmiddleware.WithPropagator(otel.GetTextMapPropagator())) |
| 46 | + // pass a http.Handler to extend it with Tracing functionality. |
| 47 | + http.Handle("/", handler(eh)) |
| 48 | +} |
| 49 | + |
| 50 | +func ExampleTraceWithOptions() { |
| 51 | + // returns a function that excepts a http.Handler. |
| 52 | + handler := otelmiddleware.TraceWithOptions(otelmiddleware.WithServiceName("exampleService")) |
| 53 | + // pass a http.Handler to extend it with Tracing functionality. |
| 54 | + http.Handle("/", handler(eh)) |
| 55 | +} |
| 56 | + |
| 57 | +func ExampleTrace() { |
| 58 | + http.Handle("/", otelmiddleware.Trace(eh)) |
23 | 59 | }
|
0 commit comments