Skip to content

Extract OTEL provider initialization out of runServeCmd#46421

Merged
getvictor merged 2 commits into
fleetdm:mainfrom
raju249:33370-otel-init-extraction
May 29, 2026
Merged

Extract OTEL provider initialization out of runServeCmd#46421
getvictor merged 2 commits into
fleetdm:mainfrom
raju249:33370-otel-init-extraction

Conversation

@raju249
Copy link
Copy Markdown
Contributor

@raju249 raju249 commented May 29, 2026

Extracts the OTEL trace, metric, and log provider setup out of runServeCmd and into initOTELProviders in a new cmd/fleet/otel.go. Same pattern as the prior extractions on this issue (#44929, #45343, #45583, #46166). Side effects (otel.SetTracerProvider, otel.SetMeterProvider) are preserved inside the extracted function, so runtime behavior is identical.

Three unit tests in cmd/fleet/otel_test.go:

  • OTEL disabled (the common production path) returns (nil, nil, nil) and never calls initFatal.
  • OTEL enabled without log export returns non-nil trace and meter providers; logger provider stays nil.
  • Log export enabled returns all three providers non-nil.

One honest note on coverage: the four initFatal sites inside the function are paranoid wrapping for OTEL SDK constructors that don't dial at construction time, so the error paths are hard to drive in tests without mocking the SDK. The tests above exercise the success paths and the disabled gate, which is the bulk of the realistic flow.

This continues the path toward serve.go >60% coverage per the discussion on #33370serve.go is now ~100 lines shorter and the OTEL phase is testable as a unit. Remaining slices per the broader plan: MDM Apple init, datastore init, Redis init.

Related issue: Refs #33370

Checklist for submitter

  • Added/updated automated tests
  • Changes file: not applicable — internal refactor with no user-visible behavior change

Summary by CodeRabbit

  • Refactor

    • Centralized OpenTelemetry provider initialization into a single setup path, simplifying startup and shutdown behavior and making observability configuration clearer.
  • Tests

    • Added unit tests covering disabled/enabled telemetry paths and optional log export, plus cleanup logic to ensure providers are shut down correctly.

Review Change Stack

Move the OTEL trace, metric, and log provider setup block out of runServeCmd into initOTELProviders in a new cmd/fleet/otel.go. Adds three unit tests covering the disabled path, the enabled trace+meter path, and the log-export-enabled path. Behavior is preserved — runServeCmd calls the new function and the global otel.SetTracerProvider/SetMeterProvider side effects are unchanged. Part of fleetdm#33370.
@raju249 raju249 requested a review from a team as a code owner May 29, 2026 04:51
Copy link
Copy Markdown

@claude claude Bot left a comment

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@raju249
Copy link
Copy Markdown
Contributor Author

raju249 commented May 29, 2026

Hello @getvictor - As per the discussion on the main issue, I raised this PR to extract the init of Otel provider.

Can you take a look, please?

Thanks! 🙏 🙇

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 29, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: b742bd4d-f2fd-4d52-82cd-b83c2c119452

📥 Commits

Reviewing files that changed from the base of the PR and between c661121 and b2abf26.

📒 Files selected for processing (2)
  • cmd/fleet/otel.go
  • cmd/fleet/otel_test.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • cmd/fleet/otel_test.go
  • cmd/fleet/otel.go

Walkthrough

This PR refactors OpenTelemetry provider initialization in Fleet's server startup. A new initOTELProviders function consolidates conditional construction of trace, metric, and log providers into a single testable entry point. The function reads Fleet configuration to determine whether OTEL is enabled and whether log export is configured, creates a shared OTEL resource with service identification, sets up OTLP exporters with gzip compression, applies metric view mappings for database connection instruments targeting SignOz compatibility, registers providers globally, and routes setup errors through a provided callback for fast failure. The change removes approximately 105 lines of inline OTEL setup logic from runServeCmd and replaces it with a cleaner call to the new function. Three unit tests verify behavior for disabled OTEL, enabled OTEL without log export, and fully enabled OTEL scenarios.

Possibly related PRs

  • fleetdm/fleet#45343: Both PRs refactor/use the same initFatal error-exit seam in cmd/fleet—main PR threads initFatal into the new OTEL provider initializer, while the retrieved PR changes initFatal into a swappable package-level var so tests can override it.
  • fleetdm/fleet#45583: Both PRs touch the fleet OTEL setup path in cmd/fleet/serve.go around logging.otel_logs_enabled/tracing—Extract early config validation from runServeCmd into testable helpers #45583 moves the otel-logs↔tracing compatibility check into config.Logging.Validate(initFatal), while the main PR refactors OTEL provider initialization into initOTELProviders that conditionally creates the OTLP logger provider based on cfg.Logging.OtelLogsEnabled.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately captures the main refactoring: extracting OTEL provider initialization from runServeCmd into a dedicated function.
Description check ✅ Passed The description comprehensively explains the refactoring, lists the three unit tests with their coverage, and acknowledges testing limitations with initFatal error paths.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
cmd/fleet/otel_test.go (1)

36-42: ⚡ Quick win

Add t.Cleanup to shut down OTEL providers created in cmd/fleet/otel_test.go tests. The tests call initOTELProviders(...) but this file has no corresponding Shutdown/cleanup, so shut down the returned non-nil lp/tp/mp in t.Cleanup to avoid leaks and cross-test state interference.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@cmd/fleet/otel_test.go` around lines 36 - 42, Tests calling initOTELProviders
in cmd/fleet/otel_test.go currently leak OTEL providers; add a t.Cleanup that
shuts down any non-nil providers returned (lp, tp, mp) to avoid cross-test
interference. In the Cleanup closure, for each of lp, tp, mp check for nil and
call the appropriate shutdown method (e.g., Shutdown or Close) with a short
context (context.Background() or context.WithTimeout) and ignore or log errors;
ensure the cleanup is registered immediately after the call to initOTELProviders
so providers are always torn down.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@cmd/fleet/otel.go`:
- Around line 49-51: The initOTELProviders function calls initFatal on error but
then continues, risking nil OTEL components; after each initFatal(...)
invocation inside initOTELProviders (the calls that follow error checks when
creating the OTEL resource, exporters, and providers—and the one near the
shutdown/setup), add an immediate return to stop further execution when
initFatal is invoked so res/exporters/providers aren't used when unset;
alternatively, if you prefer a single pattern, make initFatal always terminate
(or have initOTELProviders check a boolean result from initFatal and return on
failure) so the function exits whenever initFatal is called.

---

Nitpick comments:
In `@cmd/fleet/otel_test.go`:
- Around line 36-42: Tests calling initOTELProviders in cmd/fleet/otel_test.go
currently leak OTEL providers; add a t.Cleanup that shuts down any non-nil
providers returned (lp, tp, mp) to avoid cross-test interference. In the Cleanup
closure, for each of lp, tp, mp check for nil and call the appropriate shutdown
method (e.g., Shutdown or Close) with a short context (context.Background() or
context.WithTimeout) and ignore or log errors; ensure the cleanup is registered
immediately after the call to initOTELProviders so providers are always torn
down.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: d3d24bc2-a20d-40ae-9364-c9f42e6fb549

📥 Commits

Reviewing files that changed from the base of the PR and between 8e1cfcc and c661121.

📒 Files selected for processing (3)
  • cmd/fleet/otel.go
  • cmd/fleet/otel_test.go
  • cmd/fleet/serve.go

Comment thread cmd/fleet/otel.go
@codecov
Copy link
Copy Markdown

codecov Bot commented May 29, 2026

Codecov Report

❌ Patch coverage is 83.75000% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 66.90%. Comparing base (4d46b80) to head (b2abf26).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
cmd/fleet/otel.go 84.81% 8 Missing and 4 partials ⚠️
cmd/fleet/serve.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #46421      +/-   ##
==========================================
+ Coverage   66.87%   66.90%   +0.02%     
==========================================
  Files        2791     2792       +1     
  Lines      222293   222299       +6     
  Branches    11334    11334              
==========================================
+ Hits       148667   148730      +63     
+ Misses      60170    60111      -59     
- Partials    13456    13458       +2     
Flag Coverage Δ
backend 68.67% <83.75%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Add return nil, nil, nil after each initFatal call inside initOTELProviders so the function exits safely when the caller's initFatal does not terminate (the case in tests where it is swapped for a recorder). Adds a shutdownOTELProviders helper in otel_test.go that calls Shutdown on each non-nil provider via t.Cleanup, with a 100ms context timeout so cleanup stays fast even when no OTLP collector is listening. Per review feedback on fleetdm#46421.
Copy link
Copy Markdown
Member

@getvictor getvictor left a comment

Choose a reason for hiding this comment

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

Looks good. Thank you for the contribution.

@getvictor getvictor merged commit 06aba2c into fleetdm:main May 29, 2026
36 checks passed
@raju249 raju249 deleted the 33370-otel-init-extraction branch May 30, 2026 05:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants