From f9c5ab2833acd6cb555e1fbbda94e0c9fdb1010d Mon Sep 17 00:00:00 2001 From: Vincent Free Date: Fri, 3 Jun 2022 15:17:12 +0200 Subject: [PATCH 1/2] Add examples for With functions --- otelmiddleware/doc.go | 9 +++-- otelmiddleware/example_test.go | 66 ++++++++++++++++++++++++++-------- 2 files changed, 55 insertions(+), 20 deletions(-) diff --git a/otelmiddleware/doc.go b/otelmiddleware/doc.go index 6d75fa1..76ddd74 100644 --- a/otelmiddleware/doc.go +++ b/otelmiddleware/doc.go @@ -15,7 +15,7 @@ When a span gets initialized it uses the following slice of trace.SpanStartOptio 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. -# Functions +Functions func TraceWithOptions(opt ...TraceOption) func(next http.Handler) http.Handler func Trace(next http.Handler) http.Handler func WithAttributes(attributes ...attribute.KeyValue) TraceOption @@ -23,10 +23,10 @@ After these options are applied a new span is created and the middleware will pa func WithServiceName(serviceName string) TraceOption func WithTracer(tracer trace.Tracer) TraceOption -# Types +Types type TraceOption func(*traceConfig) -# Structs +Structs type traceConfig struct { tracer trace.Tracer propagator propagation.TextMapPropagator @@ -34,8 +34,7 @@ After these options are applied a new span is created and the middleware will pa serviceName string } - -# Examples +Examples // create a new ServeMux serve := http.NewServeMux() // add a new route to the ServeMux diff --git a/otelmiddleware/example_test.go b/otelmiddleware/example_test.go index 775d17f..1e61219 100644 --- a/otelmiddleware/example_test.go +++ b/otelmiddleware/example_test.go @@ -1,23 +1,59 @@ package otelmiddleware_test import ( - "log" + "github.com/vincentfree/opentelemetry-http/otelmiddleware" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/attribute" "net/http" +) - "github.com/vincentfree/opentelemetry-http/otelmiddleware" +type exampleHandler func(http.ResponseWriter, *http.Request) + +func (th exampleHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + th(w, r) +} + +var ( + eh = exampleHandler(func(w http.ResponseWriter, r *http.Request) { + _, _ = w.Write([]byte("Hello World")) + }) ) -func Example() { - // create a new ServeMux - serve := http.NewServeMux() - // add a new route to the ServeMux - serve.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - _, err := w.Write([]byte("Hello World")) - if err != nil { - // handle error - } - })) - // create the Trace middleware and decorate the ServeMux routes with this middleware. - handler := otelmiddleware.TraceWithOptions(otelmiddleware.WithServiceName("ExampleService"))(serve) - log.Fatal(http.ListenAndServe(":8080", handler)) +func ExampleWithTracer() { + // returns a function that excepts a http.Handler. + handler := otelmiddleware.TraceWithOptions(otelmiddleware.WithTracer(otel.Tracer("example-tracer"))) + // pass a http.Handler to extend it with Tracing functionality. + http.Handle("/", handler(eh)) +} + +func ExampleWithServiceName() { + // returns a function that excepts a http.Handler. + handler := otelmiddleware.TraceWithOptions(otelmiddleware.WithServiceName("exampleService")) + // pass a http.Handler to extend it with Tracing functionality. + http.Handle("/", handler(eh)) +} + +func ExampleWithAttributes() { + // returns a function that excepts a http.Handler. + handler := otelmiddleware.TraceWithOptions(otelmiddleware.WithAttributes(attribute.String("example", "value"))) + // pass a http.Handler to extend it with Tracing functionality. + http.Handle("/", handler(eh)) +} + +func ExampleWithPropagator() { + // returns a function that excepts a http.Handler. + handler := otelmiddleware.TraceWithOptions(otelmiddleware.WithPropagator(otel.GetTextMapPropagator())) + // pass a http.Handler to extend it with Tracing functionality. + http.Handle("/", handler(eh)) +} + +func ExampleTraceWithOptions() { + // returns a function that excepts a http.Handler. + handler := otelmiddleware.TraceWithOptions(otelmiddleware.WithServiceName("exampleService")) + // pass a http.Handler to extend it with Tracing functionality. + http.Handle("/", handler(eh)) +} + +func ExampleTrace() { + http.Handle("/", otelmiddleware.Trace(eh)) } From 3238bddf6801450b80095994eec7c43c240ea198 Mon Sep 17 00:00:00 2001 From: Vincent Free Date: Fri, 3 Jun 2022 15:56:57 +0200 Subject: [PATCH 2/2] update docs, add badges to README.md --- README.md | 9 +++++++-- otelmiddleware/doc.go | 22 ++++++---------------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index f8e1ad9..4e718fe 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,12 @@ -# opentelemetry-http +# OpenTelemetry http + +[![Go](https://github.com/vincentfree/opentelemetry-http/actions/workflows/go.yml/badge.svg)](https://github.com/vincentfree/opentelemetry-http/actions/workflows/go.yml) +[![CodeQL](https://github.com/vincentfree/opentelemetry-http/actions/workflows/codeql.yml/badge.svg)](https://github.com/vincentfree/opentelemetry-http/actions/workflows/codeql.yml) +[![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) +[![Go Reference](https://pkg.go.dev/badge/github.com/vincentfree/opentelemetry-http/otelmiddleware.svg)](https://pkg.go.dev/github.com/vincentfree/opentelemetry-http/otelmiddleware) Open Telemetry http middleware. This package provides an instrumentation for middleware that can be used to trace HTTP requests. ## version -`0.0.4` +`0.0.5` diff --git a/otelmiddleware/doc.go b/otelmiddleware/doc.go index 76ddd74..92fcf99 100644 --- a/otelmiddleware/doc.go +++ b/otelmiddleware/doc.go @@ -7,12 +7,14 @@ the basic information is extracted using the OpenTelemetry semconv package. When a span gets initialized it uses the following slice of trace.SpanStartOption opts := []trace.SpanStartOption{ - trace.WithAttributes(semconv.NetAttributesFromHTTPRequest("tcp", request)...), - trace.WithAttributes(semconv.EndUserAttributesFromHTTPRequest(request)...), - trace.WithAttributes(semconv.HTTPServerAttributesFromHTTPRequest(request.Host, extractRoute(request.RequestURI), request)...), - trace.WithSpanKind(trace.SpanKindServer), + trace.WithAttributes(semconv.NetAttributesFromHTTPRequest("tcp", request)...), + trace.WithAttributes(semconv.EndUserAttributesFromHTTPRequest(request)...), + trace.WithAttributes(semconv.HTTPServerAttributesFromHTTPRequest(request.Host, extractRoute(request.RequestURI), request)...), + trace.WithSpanKind(trace.SpanKindServer), } +The slice can be extended using the WithAttributes TraceOption function. + 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. Functions @@ -33,17 +35,5 @@ Structs attributes []attribute.KeyValue serviceName string } - -Examples - // create a new ServeMux - serve := http.NewServeMux() - // add a new route to the ServeMux - serve.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("Hello World")) - })) - // create the Trace middleware and decorate the ServeMux routes with this middleware. - handler := TraceWithOptions(WithServiceName("ExampleService"))(serve) - http.ListenAndServe(":8080", handler) - */ package otelmiddleware