Skip to content

Commit e3c1f67

Browse files
authored
Merge pull request #7 from vincentfree/add-examples
Add examples
2 parents 5606f01 + 9de3f99 commit e3c1f67

File tree

3 files changed

+67
-37
lines changed

3 files changed

+67
-37
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
# opentelemetry-http
1+
# OpenTelemetry http
2+
3+
[![Go](https://github.com/vincentfree/opentelemetry-http/actions/workflows/go.yml/badge.svg)](https://github.com/vincentfree/opentelemetry-http/actions/workflows/go.yml)
4+
[![CodeQL](https://github.com/vincentfree/opentelemetry-http/actions/workflows/codeql.yml/badge.svg)](https://github.com/vincentfree/opentelemetry-http/actions/workflows/codeql.yml)
5+
[![Dependency Review](https://github.com/vincentfree/opentelemetry-http/actions/workflows/dependency-review.yml/badge.svg)](https://github.com/vincentfree/opentelemetry-http/actions/workflows/dependency-review.yml)
6+
[![Go Reference](https://pkg.go.dev/badge/github.com/vincentfree/opentelemetry-http/otelmiddleware.svg)](https://pkg.go.dev/github.com/vincentfree/opentelemetry-http/otelmiddleware)
27

38
Open Telemetry http middleware. This package provides an instrumentation for middleware that can be used to trace HTTP requests.
49

510
## version
611

7-
`0.0.4`
12+
`0.0.5`

otelmiddleware/doc.go

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,33 @@ the basic information is extracted using the OpenTelemetry semconv package.
77
When a span gets initialized it uses the following slice of trace.SpanStartOption
88
99
opts := []trace.SpanStartOption{
10-
trace.WithAttributes(semconv.NetAttributesFromHTTPRequest("tcp", request)...),
11-
trace.WithAttributes(semconv.EndUserAttributesFromHTTPRequest(request)...),
12-
trace.WithAttributes(semconv.HTTPServerAttributesFromHTTPRequest(request.Host, extractRoute(request.RequestURI), request)...),
13-
trace.WithSpanKind(trace.SpanKindServer),
10+
trace.WithAttributes(semconv.NetAttributesFromHTTPRequest("tcp", request)...),
11+
trace.WithAttributes(semconv.EndUserAttributesFromHTTPRequest(request)...),
12+
trace.WithAttributes(semconv.HTTPServerAttributesFromHTTPRequest(request.Host, extractRoute(request.RequestURI), request)...),
13+
trace.WithSpanKind(trace.SpanKindServer),
1414
}
1515
16+
The slice can be extended using the WithAttributes TraceOption function.
17+
1618
After these options are applied a new span is created and the middleware will pass the http.ResponseWriter and http.Request to the next http.Handler.
1719
18-
# Functions
20+
Functions
1921
func TraceWithOptions(opt ...TraceOption) func(next http.Handler) http.Handler
2022
func Trace(next http.Handler) http.Handler
2123
func WithAttributes(attributes ...attribute.KeyValue) TraceOption
2224
func WithPropagator(p propagation.TextMapPropagator) TraceOption
2325
func WithServiceName(serviceName string) TraceOption
2426
func WithTracer(tracer trace.Tracer) TraceOption
2527
26-
# Types
28+
Types
2729
type TraceOption func(*traceConfig)
2830
29-
# Structs
31+
Structs
3032
type traceConfig struct {
3133
tracer trace.Tracer
3234
propagator propagation.TextMapPropagator
3335
attributes []attribute.KeyValue
3436
serviceName string
3537
}
36-
37-
38-
# Examples
39-
// create a new ServeMux
40-
serve := http.NewServeMux()
41-
// add a new route to the ServeMux
42-
serve.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
43-
w.Write([]byte("Hello World"))
44-
}))
45-
// create the Trace middleware and decorate the ServeMux routes with this middleware.
46-
handler := TraceWithOptions(WithServiceName("ExampleService"))(serve)
47-
http.ListenAndServe(":8080", handler)
48-
4938
*/
5039
package otelmiddleware

otelmiddleware/example_test.go

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,59 @@
11
package otelmiddleware_test
22

33
import (
4-
"log"
4+
"github.com/vincentfree/opentelemetry-http/otelmiddleware"
5+
"go.opentelemetry.io/otel"
6+
"go.opentelemetry.io/otel/attribute"
57
"net/http"
8+
)
69

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+
})
820
)
921

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))
2359
}

0 commit comments

Comments
 (0)