-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add zerolog (#46) - remove unnecessary code, expose ExtraKey
- Loading branch information
Showing
4 changed files
with
39 additions
and
49 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Notes | ||
- If you're using the global logger in `github.com/rs/zerolog/log.Logger` to do log. You should add the middleware to pass the OTEL extra information (key, value) into the `context.Context`. | ||
- Example: | ||
```go | ||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/cloudwego/hertz/pkg/app" | ||
"github.com/rs/zerolog/log" | ||
"go.opentelemetry.io/otel/trace" | ||
"github.com/hertz-contrib/obs-opentelemetry/logging/zerolog" | ||
) | ||
|
||
func OtelZerologMiddleware() app.HandlerFunc { | ||
return func(ctx context.Context, reqCtx *app.RequestContext) { | ||
ctx = log.Logger.WithContext(ctx) | ||
|
||
span := trace.SpanFromContext(ctx) | ||
|
||
ctx = context.WithValue(ctx, zerolog.ExtraKey(zerolog.TraceIDKey), span.SpanContext().TraceID()) | ||
ctx = context.WithValue(ctx, zerolog.ExtraKey(zerolog.SpanIDKey), span.SpanContext().SpanID()) | ||
ctx = context.WithValue(ctx, zerolog.ExtraKey(zerolog.TraceFlagsKey), span.SpanContext().TraceFlags()) | ||
|
||
reqCtx.Next(ctx) | ||
} | ||
``` |
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
This file was deleted.
Oops, something went wrong.