feat(logger): add CtxLogger interface for OpenTelemetry trace integration#3195
feat(logger): add CtxLogger interface for OpenTelemetry trace integration#3195MrSibe wants to merge 15 commits intoapache:developfrom
Conversation
Add context-aware logging support to integrate with OpenTelemetry traces: - Add CtxLogger interface extending Logger with context-aware methods - Create trace_extractor.go to extract traceId, spanId, and trace_flags from context - Add configuration constants for trace integration (enabled, record-error-to-span) This is the foundation for issue apache#3182 - providing ctxLogger interface to correlate logs with distributed traces.
Implement context-aware logging adapter for Zap: - Add ZapCtxLogger that wraps DubboLogger - Automatically inject trace_id, span_id, trace_flags from context - Support optional error recording to span - Integrate with existing Zap instantiation logic
Implement context-aware logging adapter for Logrus: - Add LogrusCtxLogger that wraps DubboLogger - Automatically inject trace_id, span_id, trace_flags from context - Support optional error recording to span - Integrate with existing Logrus instantiation logic
Extend LoggerConfig to support trace integration: - Add TraceIntegrationConfig struct with enabled and record-error-to-span options - Update toURL() method to pass trace integration parameters - Enable users to configure trace logging via YAML
Add comprehensive unit tests: - trace_extractor_test.go: test trace field extraction from context - zap/ctx_logger_test.go: test Zap adapter with trace injection - logrus/ctx_logger_test.go: test Logrus adapter with trace injection - Fix import error in logrus/ctx_logger.go (move errors import to top) All tests pass successfully, verifying: - Trace fields are correctly extracted from valid span context - Logs include trace_id, span_id, trace_flags when context has trace - Logs work normally without trace context (backward compatible)
6337c06 to
7d57f4f
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## develop #3195 +/- ##
===========================================
+ Coverage 46.76% 47.96% +1.20%
===========================================
Files 295 464 +169
Lines 17172 33659 +16487
===========================================
+ Hits 8031 16146 +8115
- Misses 8287 16210 +7923
- Partials 854 1303 +449 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
合并之后可以在samples里面加个示例 |
There was a problem hiding this comment.
Pull request overview
This PR implements context-aware logging with automatic OpenTelemetry trace correlation to enable distributed tracing support in Dubbo-Go. The implementation adds a new CtxLogger interface that extends the base Logger interface with context-aware methods, allowing logs to automatically include trace_id, span_id, and trace_flags from the OpenTelemetry context.
Changes:
- Added
CtxLoggerinterface with context-aware logging methods (CtxDebug, CtxInfo, CtxWarn, CtxError and their formatted variants) - Implemented trace extraction utilities to extract OpenTelemetry trace information from context
- Provided implementations for both Zap and Logrus logging frameworks with optional error recording to spans
- Added configuration support for trace integration via YAML with opt-in enablement
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| logger/base.go | Added CtxLogger interface extending Logger with context-aware methods |
| logger/trace_extractor.go | Implemented trace field extraction from OpenTelemetry context |
| logger/trace_extractor_test.go | Comprehensive tests for trace extraction and validation logic |
| logger/core/zap/ctx_logger.go | Zap implementation of CtxLogger with trace field injection and optional span error recording |
| logger/core/zap/ctx_logger_test.go | Tests for Zap context logger covering various logging scenarios |
| logger/core/zap/zap.go | Integration of CtxLogger into Zap logger instantiation based on configuration |
| logger/core/logrus/ctx_logger.go | Logrus implementation of CtxLogger with trace field injection and optional span error recording |
| logger/core/logrus/ctx_logger_test.go | Tests for Logrus context logger covering various logging scenarios |
| logger/core/logrus/logrus.go | Integration of CtxLogger into Logrus logger instantiation based on configuration |
| config/logger_config.go | Added TraceIntegrationConfig structure and configuration mapping to URL parameters |
| common/constant/key.go | Added configuration keys for trace integration settings |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Add comprehensive unit tests for recordErrorToSpan functionality in both Zap and Logrus CtxLogger implementations - Test coverage includes: enabled/disabled states, span status verification, and error event recording - Fix error creation inconsistency in Zap implementation to use errors.New instead of fmt.Errorf for consistency with Logrus
Fix testifylint errors by replacing assert.Len(t, events, 0) with assert.Empty(t, events) for better readability.
Alanxtl
left a comment
There was a problem hiding this comment.
- 这个新的api兼容new api吗
- dubbo-website 里面写一下文档,介绍一下使用方式
- 写 samples
目前仅支持 Old API, 我把 New API 的加上 |
Add TraceIntegration support to global.LoggerConfig to enable trace integration features in New API.
Add WithTraceIntegration and WithRecordErrorToSpan options to enable CtxLogger configuration through New API.
Use idiomatic Go pattern for creating pointer copies.
Add SetTraceIntegrationEnabled() and SetRecordErrorToSpan() methods to LoggerConfigBuilder to support trace integration configuration via the Old API (Builder pattern).
|
UsageOld APIconfig.NewLoggerConfigBuilder().
SetDriver("zap").
SetTraceIntegrationEnabled(true).
SetRecordErrorToSpan(true).
Build()New APIlogger.NewOptions(
logger.WithZap(),
logger.WithTraceIntegration(true),
logger.WithRecordErrorToSpan(true),
)YAML configdubbo:
logger:
driver: zap
trace-integration:
enabled: true
record-error-to-span: trueLogging with CtxLogger// get logger
log := logger.GetLogger().(logger.CtxLogger)
// log with context
log.CtxInfo(ctx, "message")
log.CtxInfof(ctx, "user: %s", name)
log.CtxError(ctx, "error")
log.CtxErrorf(ctx, "failed: %v", err) |
Alanxtl
left a comment
There was a problem hiding this comment.
cool work lgtm
don't forget to write doc in dubbo-website



Summary
Implements context-aware logging with automatic OpenTelemetry trace correlation for distributed tracing.
Related issue: #3182
What's New
Loggerwith context-aware methods (CtxInfof,CtxErrorf, etc.)trace_id,span_id,trace_flagsfrom contextConfiguration