Skip to content

feat(logger): add CtxLogger interface for OpenTelemetry trace integration#3195

Open
MrSibe wants to merge 15 commits intoapache:developfrom
MrSibe:feat/ctxlogger-interface
Open

feat(logger): add CtxLogger interface for OpenTelemetry trace integration#3195
MrSibe wants to merge 15 commits intoapache:developfrom
MrSibe:feat/ctxlogger-interface

Conversation

@MrSibe
Copy link
Contributor

@MrSibe MrSibe commented Feb 4, 2026

Summary

Implements context-aware logging with automatic OpenTelemetry trace correlation for distributed tracing.

Related issue: #3182

What's New

  • CtxLogger interface: Extends Logger with context-aware methods (CtxInfof, CtxErrorf, etc.)
  • Automatic trace injection: Logs automatically include trace_id, span_id, trace_flags from context
  • Dual logger support: Works with both Zap and Logrus
  • Optional error recording: Can record error logs to OpenTelemetry spans
  • Backward compatible: Existing code works without changes
  • Opt-in: Disabled by default, enabled via configuration

Configuration

dubbo:
  logger:
    driver: zap
    level: info
    format: json
    trace-integration:
      enabled: true
      record-error-to-span: true

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)
@MrSibe MrSibe force-pushed the feat/ctxlogger-interface branch from 6337c06 to 7d57f4f Compare February 4, 2026 09:08
@codecov-commenter
Copy link

codecov-commenter commented Feb 4, 2026

Codecov Report

❌ Patch coverage is 73.93162% with 61 lines in your changes missing coverage. Please review.
✅ Project coverage is 47.96%. Comparing base (60d1c2a) to head (52048eb).
⚠️ Report is 728 commits behind head on develop.

Files with missing lines Patch % Lines
logger/core/logrus/ctx_logger.go 68.11% 11 Missing and 11 partials ⚠️
logger/core/zap/ctx_logger.go 68.11% 11 Missing and 11 partials ⚠️
config/logger_config.go 53.33% 5 Missing and 2 partials ⚠️
logger/trace_extractor.go 73.33% 2 Missing and 2 partials ⚠️
logger/core/logrus/logrus.go 50.00% 2 Missing and 1 partial ⚠️
logger/core/zap/zap.go 50.00% 2 Missing and 1 partial ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Alanxtl Alanxtl linked an issue Feb 4, 2026 that may be closed by this pull request
2 tasks
@Alanxtl Alanxtl added ✏️ Feature 3.3.2 version 3.3.2 labels Feb 4, 2026
@Rinai-R
Copy link
Contributor

Rinai-R commented Feb 4, 2026

合并之后可以在samples里面加个示例

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 CtxLogger interface 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.
Copy link
Contributor

@Alanxtl Alanxtl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 这个新的api兼容new api吗
  2. dubbo-website 里面写一下文档,介绍一下使用方式
  3. 写 samples

@MrSibe
Copy link
Contributor Author

MrSibe commented Feb 5, 2026

  1. 这个新的api兼容new api吗
  2. dubbo-website 里面写一下文档,介绍一下使用方式
  3. 写 samples

目前仅支持 Old API, 我把 New API 的加上

Add TraceIntegration support to global.LoggerConfig to enable
trace integration features in New API.
@MrSibe MrSibe requested a review from Alanxtl February 5, 2026 09:06
Copy link
Contributor

@Alanxtl Alanxtl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

现在还是没有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).
@sonarqubecloud
Copy link

sonarqubecloud bot commented Feb 6, 2026

@MrSibe
Copy link
Contributor Author

MrSibe commented Feb 6, 2026

Usage

Old API

config.NewLoggerConfigBuilder().
    SetDriver("zap").
    SetTraceIntegrationEnabled(true).
    SetRecordErrorToSpan(true).
    Build()

New API

logger.NewOptions(
    logger.WithZap(),
    logger.WithTraceIntegration(true),
    logger.WithRecordErrorToSpan(true),
)

YAML config

dubbo:
  logger:
    driver: zap
    trace-integration:
      enabled: true
      record-error-to-span: true

Logging 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)

@MrSibe MrSibe requested a review from Alanxtl February 6, 2026 11:16
Copy link
Contributor

@Alanxtl Alanxtl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool work lgtm
don't forget to write doc in dubbo-website

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] 提供 ctxLogger 接口

4 participants