Skip to content

feat(metrics): Use default resource labels for metrics.#14

Open
octo wants to merge 2 commits intoproffalken:mainfrom
octo:resource-labels
Open

feat(metrics): Use default resource labels for metrics.#14
octo wants to merge 2 commits intoproffalken:mainfrom
octo:resource-labels

Conversation

@octo
Copy link
Copy Markdown

@octo octo commented Apr 7, 2026

Previously, metrics were reported without the resource labels set via defaultResource().set(), using a set of built-in defaults (service.name, service.instance.id, and host.name) instead.

This changes the behavior if default resource labels are set and uses those resource labels instead. If no default resource labels are set, the previous behavior is retained for (limited) backwards compatibility.

Note on implementation: The static keyword was removed from defaultResource() to enable correct linking. With static, each compilation unit allocates its own set of resource labels, which is not intended.

Summary by CodeRabbit

  • Bug Fixes
    • Resource handling now correctly prioritises configured default resource attributes during initialisation. When defaults are present they are merged into the resource; when not present the system falls back to the standard service name, instance ID and hostname attributes to preserve prior behaviour.

Previously, metrics were reported without the resource labels set via
`defaultResource().set()`, using a set of built-in defaults
(`service.name`, `service.instance.id`, and `host.name`) instead.

This changes the behavior if default resource labels are set and uses
those resource labels instead. If no default resource labels are set,
the previous behavior is retained for (limited) backwards compatibility.

Note on implementation: The `static` keyword was removed from
`defaultResource()` to enable correct linking. With `static`, each
compilation unit allocates its own set of resource labels, which is not
intended.
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 7, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0477cc80-3770-45ef-ae57-dedc076147b9

📥 Commits

Reviewing files that changed from the base of the PR and between 86ece0d and dddd74f.

📒 Files selected for processing (1)
  • src/OtelMetrics.cpp
✅ Files skipped from review due to trivial changes (1)
  • src/OtelMetrics.cpp

Walkthrough

The defaultResource() function linkage is changed from static inline to inline in the header, and addCommonResource() now prefers attributes from OTel::defaultResource() when present, falling back to hardcoded service and host defaults only if the default resource is empty.

Changes

Cohort / File(s) Summary
Header linkage modification
include/OtelDefaults.h
Changed defaultResource() declaration from static inline to inline, altering linkage from internal to external while keeping the local-static singleton OTelResourceConfig rc.
Resource attribute logic
src/OtelMetrics.cpp
addCommonResource() now fetches OTel::defaultResource() and merges its attributes into the provided resource when non-empty; otherwise it populates resource["attributes"] with the previous hardcoded service.name, service.instance.id, and host.name defaults.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A hop through headers, tidy and fine,
Inline now sings where static did shine,
Defaults first heard, then fallbacks dance,
Attributes merge with a careful glance,
Rabbit applauds this small, neat advance 🌿

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: using default resource labels for metrics. It directly reflects the primary objective of the PR.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ 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

@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.

🧹 Nitpick comments (1)
src/OtelMetrics.cpp (1)

22-33: Logic is correct; consider using the empty() accessor.

The new early-return path correctly prioritises user-configured resource attributes over the built-in fallbacks. One minor encapsulation improvement: OTelResourceConfig already exposes bool empty() const (OtelDefaults.h line 103), so prefer that over direct member access.

♻️ Suggested change
 static void addCommonResource(JsonObject& resource) {
   auto &res = OTel::defaultResource();
-  if (!res.attrs.empty()) {
+  if (!res.empty()) {
     res.addResourceAttributes(resource);
     return;
   }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/OtelMetrics.cpp` around lines 22 - 33, The function addCommonResource
currently checks res.attrs.empty() on the OTel::defaultResource() return value;
change this to use the public accessor by calling res.empty() instead (i.e., use
OTelResourceConfig::empty()) so the implementation relies on the type's
encapsulated API; update the conditional in addCommonResource that now reads if
(!res.attrs.empty()) to if (!res.empty()) and leave the rest of the early-return
logic unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/OtelMetrics.cpp`:
- Around line 22-33: The function addCommonResource currently checks
res.attrs.empty() on the OTel::defaultResource() return value; change this to
use the public accessor by calling res.empty() instead (i.e., use
OTelResourceConfig::empty()) so the implementation relies on the type's
encapsulated API; update the conditional in addCommonResource that now reads if
(!res.attrs.empty()) to if (!res.empty()) and leave the rest of the early-return
logic unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f1eceec2-3476-478b-ae22-9ce983e73955

📥 Commits

Reviewing files that changed from the base of the PR and between 5a47813 and 86ece0d.

📒 Files selected for processing (2)
  • include/OtelDefaults.h
  • src/OtelMetrics.cpp

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.

1 participant