-
Notifications
You must be signed in to change notification settings - Fork 328
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
[log] better trace log #3914
[log] better trace log #3914
Conversation
Codecov Report
@@ Coverage Diff @@
## master #3914 +/- ##
==========================================
+ Coverage 75.38% 76.16% +0.78%
==========================================
Files 303 328 +25
Lines 25923 27971 +2048
==========================================
+ Hits 19541 21303 +1762
- Misses 5360 5573 +213
- Partials 1022 1095 +73
... and 3 files with indirect coverage changes 📣 Codecov offers a browser extension for seamless coverage viewing on GitHub. Try it in Chrome or Firefox today! |
@@ -25,7 +25,7 @@ jobs: | |||
- name: Set up Go | |||
uses: actions/setup-go@v3 | |||
with: | |||
go-version: 1.18.5 |
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.
revert if this is not required
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.
after updating go.mod, need to upgrade to go.1.19
# go.uber.org/multierr
Error: ../../../go/pkg/mod/go.uber.org/[email protected]/error.go:209:20: undefined: atomic.Bool
note: module requires Go 1.19
# go.opentelemetry.io/otel/sdk/trace
Error: ../../../go/pkg/mod/go.opentelemetry.io/otel/[email protected]/trace/provider.go:78:24: undefined: atomic.Pointer
Error: ../../../go/pkg/mod/go.opentelemetry.io/otel/[email protected]/trace/provider.go:80:20: undefined: atomic.Bool
note: module requires Go 1.19
Error: Process completed with exit code 2.
@@ -28,7 +28,7 @@ jobs: | |||
- name: Set up Go | |||
uses: actions/setup-go@v2 | |||
with: | |||
go-version: 1.18.5 |
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.
revert if this is not required
@@ -64,14 +63,12 @@ func newHTTPHandler(web3Handler Web3Handler) *hTTPHandler { | |||
} | |||
|
|||
func (handler *hTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { | |||
ctx, span := tracer.NewSpan(req.Context(), "handler.ServeHTTP") | |||
defer span.End() |
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.
there are other places calling tracer.NewSpan()
, should same changes be applied there as well?
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.
Yes, can remove this later and use log.Ctx
instead. But it can be used when there are special needs (such as funcs spent time)
actually log.Ctx
is an event in the span
pkg/log/tracelog.go
Outdated
) | ||
|
||
// Ctx is a wrapper of otelzap.Ctx, returns a logger with context. | ||
func Ctx(ctx context.Context) otelzap.LoggerWithCtx { |
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.
The name Ctx
may be misleading. Using T(ctx)
may be a better alternative as it denotes tracing, and is consistent with the L()
and S()
methods in log pkg.
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
@@ -115,6 +126,9 @@ func InitLoggers(globalCfg GlobalConfig, subCfgs map[string]GlobalConfig, opts . | |||
zap.RedirectStdLog(logger) | |||
} | |||
zap.ReplaceGlobals(logger) | |||
if err := initTraceLogger(logger, cfg.Trace); err != nil { |
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.
why is it initiated here?
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.
trace logger needs to wrap a global zap logger, here is global zap logger initiated
) | ||
|
||
// T is a wrapper of otelzap.Ctx, returns a logger with context. | ||
func T(ctx context.Context) otelzap.LoggerWithCtx { |
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.
listen logs from L()
, etc.
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.
after initTraceLogger
, T(ctx) and L() use the same zap logger.
In different , T(ctx) will additionally attach some trace fields and send them to the trace provider(jaeger/signoz)
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.
ok. Does the tracer work as before with this pr?
server/main.go
Outdated
@@ -73,6 +73,8 @@ func init() { | |||
} | |||
|
|||
func main() { | |||
// set max number of CPUs, disable log printing | |||
maxprocs.Set(maxprocs.Logger(nil)) |
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.
only for json format?
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.
yes, Its log is not controlled by zap and will break the format of zap json log
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.
this line will be executed even if for console format
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
Description
Due to the requirement of structured JSON logs for Signoz log collection, the current format of console logs cannot be parsed by Signoz. Additionally, the logs do not contain a trace_id, making it difficult to combine tracing and logs effectively.
This PR addresses these issues by implementing the following changes:
Disable logging in automaxprocs that is not in JSON format.
Wrap the zap.Logger to print the trace_id when there is a api trace log. This requires using log.Ctx(ctx), these logs can be sent to signoz by configuring whether.
Fixes #(issue)
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
Test Configuration:
Checklist: