Skip to content

otelfiber: Add LoggerProvider support#1731

Open
Copilot wants to merge 5 commits intomainfrom
copilot/add-logger-provider-to-otelfiber
Open

otelfiber: Add LoggerProvider support#1731
Copilot wants to merge 5 commits intomainfrom
copilot/add-logger-provider-to-otelfiber

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 3, 2026

OpenTelemetry defines a LoggerProvider interface but otelfiber had no support for it. This adds WithLoggerProvider config option and automatic per-request log record emission correlated with the active trace.

Changes

  • config.go: Added LoggerProvider otellog.LoggerProvider field + WithLoggerProvider(otellog.LoggerProvider) Option. Falls back to global logger provider when unset (consistent with tracer/meter behavior).
  • fiber.go: Creates a logger at middleware init; emits one structured log record per request after response processing. Severity is derived from HTTP status: Info (2xx/3xx), Warn (4xx), Error (5xx). Log record attributes: http.request.method, http.route, http.response.status_code, url.path.
  • otel_test/fiber_test.go: Tests for record emission, severity mapping (4xx/5xx), and skip-via-Next behavior using an in-memory sdklog.Processor.
  • README.md: Documents WithLoggerProvider in the config table and adds a usage example.
  • go.mod: Adds go.opentelemetry.io/otel/log v0.17.0 (runtime) and go.opentelemetry.io/otel/sdk/log v0.17.0 (test).

Usage

lp := sdklog.NewLoggerProvider(sdklog.WithProcessor(yourExporter))
app.Use(fiberotel.Middleware(fiberotel.WithLoggerProvider(lp)))

Each request emits a log record such as:

severity: INFO
body:     "GET /users/:id"
attributes:
  http.request.method:      GET
  http.route:               /users/:id
  http.response.status_code: 200
  url.path:                 /users/42
Original prompt

This section details on the original issue you should resolve

<issue_title>🚀 [Feature]: Add LoggerProvider to otelfiber</issue_title>
<issue_description>### Feature Description

OpenTelemetry has a logger provider interface, so I was wondering if otelfiber can support a LoggerProvider.

Additional Context (optional)

No response

Code Snippet (optional)

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my suggestion prior to opening this one.
  • I understand that improperly formatted feature requests may be closed without explanation.</issue_description>

<agent_instructions>- add support for otel pkg logger.

  • add unit-tests.
  • update documentation with examples.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits March 3, 2026 05:56
Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Copilot AI changed the title [WIP] Add LoggerProvider support to otelfiber otelfiber: Add LoggerProvider support Mar 3, 2026
Copilot finished work on behalf of gaby March 3, 2026 06:04
@gaby gaby marked this pull request as ready for review March 3, 2026 23:40
@gaby gaby requested a review from a team as a code owner March 3, 2026 23:40
@gaby gaby requested review from ReneWerner87, Copilot, efectn, gaby and sixcolors and removed request for a team March 3, 2026 23:40
Copy link
Copy Markdown
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

Adds OpenTelemetry logging support to the v3/otel Fiber middleware by allowing a configurable LoggerProvider and emitting a correlated log record per request.

Changes:

  • Add LoggerProvider to middleware config + WithLoggerProvider(...) option (defaults to global provider when unset).
  • Emit one structured log record per request with severity derived from HTTP status and standard HTTP/url attributes.
  • Add unit tests for log emission/severity/Next-skip behavior; update docs and module dependencies.

Reviewed changes

Copilot reviewed 6 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
v3/otel/config.go Adds LoggerProvider field and WithLoggerProvider option.
v3/otel/fiber.go Initializes OTel logger and emits a per-request log record with severity + attributes.
v3/otel/otel_test/fiber_test.go Adds in-memory log processor + tests validating emission and severity mapping.
v3/otel/README.md Documents WithLoggerProvider and adds a usage example.
v3/otel/go.mod Adds go.opentelemetry.io/otel/log and go.opentelemetry.io/otel/sdk/log deps.
v3/otel/go.sum Updates sums for new OTel log dependencies.
v3/otel/example/go.mod Pulls in otel/log as an indirect dependency for the example module.
v3/otel/example/go.sum Updates sums for the example module’s new indirect dependency.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +290 to +292
logSeverity := severityFromHTTPStatusCode(c.Response().StatusCode())
if logger.Enabled(ctx, otellog.EnabledParameters{Severity: logSeverity}) {
var logRecord otellog.Record
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

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

The log record uses the captured ctx variable for logger.Enabled/logger.Emit. Since downstream handlers can mutate the request context via c.SetContext(...), consider using c.Context() here (like the outbound propagator injection does) so emitted logs include any user-added values/baggage and reflect the final active context for the request.

Copilot uses AI. Check for mistakes.
Comment on lines +293 to +294
logRecord.SetTimestamp(start)
logRecord.SetObservedTimestamp(time.Now())
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

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

logRecord.SetTimestamp(start) sets the log timestamp to the beginning of the request even though the record is emitted after the response is processed. This can skew log ordering and make the record appear to occur before the response status is known; consider setting the timestamp to the emit time (and optionally recording the start time/duration as attributes if needed).

Suggested change
logRecord.SetTimestamp(start)
logRecord.SetObservedTimestamp(time.Now())
emitTime := time.Now()
logRecord.SetTimestamp(emitTime)
logRecord.SetObservedTimestamp(emitTime)

Copilot uses AI. Check for mistakes.
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]: Add LoggerProvider to otelfiber

4 participants