-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from vincentfree/add-examples
Add examples
- Loading branch information
Showing
3 changed files
with
67 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |