-
Notifications
You must be signed in to change notification settings - Fork 713
feat: update xecho access log with trace id #889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
pkg/server/xecho/middleware.go
Outdated
| var fields = make([]xlog.Field, 0, 8) | ||
|
|
||
| defer func() { | ||
| logger := xlog.FromContext(ctx.Request().Context()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里FromContext取的是default log,这个主要是用于业务日志,建议用Jupiter()日志,写入系统日志
Codecov Report
❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more. @@ Coverage Diff @@
## master #889 +/- ##
==========================================
- Coverage 53.05% 52.75% -0.30%
==========================================
Files 202 202
Lines 11336 11438 +102
==========================================
+ Hits 6014 6034 +20
- Misses 4866 4944 +78
- Partials 456 460 +4
|
|
This pull request has been marked as stale due to 60 days of inactivity. It will be closed in 4 weeks if no further activity occurs. If you think that's incorrect or this pull request should instead be reviewed, please simply write any comment. Even if closed, you can still revive the PR at any time. Thank you for your contributions. |
|
@hnlq715 麻烦看下,这个 PR 已经好久了 |
|
This issue is no longer marked as stale. |
| span.SetAttributes(semconv.HTTPServerAttributesFromHTTPRequest(pkg.Name(), c.Request().URL.Path, c.Request())...) | ||
|
|
||
| ctx = xlog.NewContext(ctx, xlog.Default(), span.SpanContext().TraceID().String()) | ||
| ctx = xlog.NewContext(ctx, xlog.Jupiter(), span.SpanContext().TraceID().String()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以考虑把jupiter和default日志都注入到ctx,然后框架用xlog.J(ctx),业务项目用xlog.L(ctx)来获取对应日志实例
pkg/xlog/context.go
Outdated
|
|
||
| func NewContext(ctx context.Context, l *Logger, traceID string) context.Context { | ||
| return context.WithValue(ctx, loggerKey{}, l.With(String(traceIDField, traceID))) | ||
| func NewContextWithDefaultLogger(ctx context.Context, l *Logger, traceID string) context.Context { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里尽量保证兼容,不改API
pkg/xlog/context.go
Outdated
| } | ||
|
|
||
| func FromContext(ctx context.Context) *Logger { | ||
| func NewContextWithJupiterLogger(ctx context.Context, l *Logger, traceID string) context.Context { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里新增的这个api可以不export出来,用newContextWithJupiterLogger
pkg/xlog/context.go
Outdated
| return context.WithValue(ctx, jupiterLoggerKey{}, l.With(String(traceIDField, traceID))) | ||
| } | ||
|
|
||
| func GetDefaultLoggerFromContext(ctx context.Context) *Logger { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上
pkg/xlog/context.go
Outdated
| return l | ||
| } | ||
|
|
||
| func GetJupiterLoggerFromContext(ctx context.Context) *Logger { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上
| } | ||
|
|
||
| func FromContext(ctx context.Context) *Logger { | ||
| func newContextWithJupiterLogger(ctx context.Context, l *Logger, traceID string) context.Context { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个FromContext还是可以继续保留API,可以加个Deprecated注释,推荐使用xlog.L
Describe what this PR does / why we need it
There is no trace id field in the
xecho access log, but we do need it.Describe how you did it
Just replace the default logger in the middleware
Describe how to verify it
The trace id field is included in the output access log