https://github.com/dmathieu/owe
OpenTelemetry Wide Events for Go
https://github.com/dmathieu/owe
Last synced: 9 months ago
JSON representation
OpenTelemetry Wide Events for Go
- Host: GitHub
- URL: https://github.com/dmathieu/owe
- Owner: dmathieu
- License: mit
- Created: 2024-07-04T09:30:56.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-03-10T08:21:50.000Z (over 1 year ago)
- Last Synced: 2025-03-10T09:35:09.571Z (over 1 year ago)
- Language: Go
- Size: 71.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# OpenTelemetry Wide Events for Go
OpenTelemetry makes it easy to retrieve the current span stored in context.
```golang
span := trace.SpanFromContext(ctx)
```
With the notion of [Wide
Events](https://isburmistrov.substack.com/p/all-you-need-is-wide-events-not-metrics)
originally introduces by Charity Majors, you will also want to retrieve that
main span all the time.
And yet, if a child span has been created, you will retrieve that new span and
not the wide event you expected.
This package allows you to define a wide event for a context, which will not
change unless you manually do so. You can then retrieve the proper main span
all the time, even if child spans were created.
```golang
span := owe.SpanFromContext(ctx)
```

## Usage
In order to have a span to retrieve, one needs to be added to the context.
You can do that manually with the `ContextWithSpan` method:
```golang
ctx := owe.ContextWithSpan(ctx, span)
```
Or you can use one of the provided handlers that will automatically create a
wide event span.
### net/http
The `owehttp` package allows setting wide events for HTTP servers.
```golang
handler := func(http.ResponseWriter, *http.Request) {
// the HTTP handler sets my app's behavior
event := owe.SpanFromContext(r.Context())
}
endpoint := otelhttp.NewHandler(owe.NewHandler(handler), "otelhttp")
server := &http.Server{
Handler: endpoint,
}
log.Fatal(server.ListenAndServe())
```
### gRPC
The `owegrpc` package allows setting wide events for gRPC servers.
```golang
s := grpc.NewServer(
grpc.StatsHandler(otelgrpc.NewServerHandler()),
grpc.StatsHandler(owegrpc.NewHandler()),
)
// Register your gRPC services
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
```