Skip to content

feat(tracer): add SpanKind/StatusCode API and unify resource attribute merging#17

Merged
proffalken merged 2 commits into
mainfrom
feat/span-kind-status-and-resource-api
Jun 5, 2026
Merged

feat(tracer): add SpanKind/StatusCode API and unify resource attribute merging#17
proffalken merged 2 commits into
mainfrom
feat/span-kind-status-and-resource-api

Conversation

@proffalken
Copy link
Copy Markdown
Owner

@proffalken proffalken commented Jun 5, 2026

Summary

  • Span kind/status API: adds SpanKind::{INTERNAL,SERVER,CLIENT,PRODUCER,CONSUMER} and StatusCode::{UNSET,OK,ERROR} constant namespaces, plus Span::setKind(), setStatus(), setError(), setOk(). The JSON serialiser now writes the actual kind (defaulting to SERVER) and emits a status block only when it has been explicitly set, per the OTLP spec.
  • buildResourceAttributes(): new helper in OtelDefaults.h that merges runtime defaultResource() values with compile-time fallbacks. Runtime values win for any key that is set; compile-time fallbacks fill the rest. All three signal paths (traces, metrics, logs) now call this instead of three separate addResAttr() sites. Also replaces the all-or-nothing if (!res.empty()) approach introduced in feat(metrics): Use default resource labels for metrics. #14 for metrics.
  • addResAttr() deprecated: marked [[deprecated]] pointing to buildResourceAttributes().

Background

This work was developed on feat/runtime-resource-attrs-and-span-status but never merged. The ODR singleton fix from that branch was already landed in main via #16. This PR applies only the parts that are still missing, rebased cleanly onto current main with no conflicts.

Test plan

  • All 13 native Unity tests pass (pio test -e native)
  • Verify span.setKind(SpanKind::CLIENT) appears correctly in a collector backend
  • Verify span.setError("something went wrong") emits a status block with code=2 and message
  • Verify defaultResource().set("service.name", "my-device") overrides the compile-time default on all three signals

🤖 Generated with Claude Code

Summary by CodeRabbit

Release Notes

  • New Features

    • Spans now support configurable kind, status, and error information through new setter methods.
    • New public constants for span kinds (INTERNAL, SERVER, CLIENT, PRODUCER, CONSUMER) and status codes (UNSET, OK, ERROR).
  • Refactor

    • Improved resource attribute handling with unified API consolidation.

…e merging

Three related improvements that were developed on feat/runtime-resource-attrs-and-span-status
but never merged, now applied cleanly on top of main (which already has the ODR
singleton fix from #16).

1. Span kind and status API
   Add SpanKind::{INTERNAL,SERVER,CLIENT,PRODUCER,CONSUMER} and
   StatusCode::{UNSET,OK,ERROR} constant namespaces.
   Add Span::setKind(), setStatus(), setError(), setOk().
   The JSON serialiser now writes kind_ (defaulting to SERVER) and emits
   a status block only when statusCode_ != UNSET, per the OTLP spec.
   Move constructor and move-assignment updated to transfer the new fields.

2. buildResourceAttributes() — unified resource attribute merging
   Add buildResourceAttributes() to OtelDefaults.h. It merges runtime
   defaultResource() values with compile-time fallbacks (service.name,
   service.instance.id, host.name): runtime wins for any key that is set,
   compile-time fallback fills the rest.
   All three signal paths (traces, metrics, logs) now use this helper
   instead of three separate addResAttr() call sites. Replaces the
   all-or-nothing if/else approach introduced in #14 for metrics.

3. addResAttr() deprecated
   Mark the global addResAttr() helper [[deprecated]] pointing to
   buildResourceAttributes(). The private Span::addResAttr() copy is
   unchanged — it is only referenced by its own class.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 5, 2026 12:32
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 5, 2026

Review Change Stack

Warning

Review limit reached

@proffalken, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 47 minutes and 34 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 39e386f1-5a90-4812-ac07-8af9633a41df

📥 Commits

Reviewing files that changed from the base of the PR and between b717c3c and c999e86.

📒 Files selected for processing (2)
  • include/OtelTracer.h
  • test/test_otlp/test_otlp.cpp

Walkthrough

This PR refactors OpenTelemetry resource attribute handling and extends the Span API with metadata support. A new buildResourceAttributes() helper consolidates resource population logic across logger and metrics components. The Span class gains configurable kind and status metadata with setter methods, alongside numeric constant namespaces for OTLP compliance, and updated serialisation to emit status objects conditionally.

Changes

Span Metadata and Resource Attributes

Layer / File(s) Summary
Resource attributes consolidation helper
include/OtelDefaults.h
New inline buildResourceAttributes() merges runtime attributes from defaultResource() with compile-time fallback keys (service.name, service.instance.id, host.name) only when not already set.
Span metadata fields and setter API
include/OtelTracer.h
Span gains kind_, statusCode_, statusMessage_ instance state. New public setters setKind(), setStatus(), setError(), setOk() are added with full move construction/assignment support. Deprecated addResAttr global helper and new SpanKind and StatusCode numeric constant namespaces introduced.
Span::end() serialisation with metadata
include/OtelTracer.h
Resource attributes now populated via buildResourceAttributes(); span JSON uses configurable kind_ field instead of hardcoded value; status object conditionally serialised with code and message (when ERROR).
Apply buildResourceAttributes across codebase
include/OtelLogger.h, src/OtelMetrics.cpp
Logger and metrics components replaced with single buildResourceAttributes() call using default service/host getters instead of manual attribute insertion.
Documentation comment updates
include/OtelLogger.h, include/OtelMetrics.h
Include comments updated to list buildResourceAttributes and remove deprecated addResAttr reference.

Sequence Diagram

sequenceDiagram
    participant App as Application
    participant Span as Span class
    participant BuildRes as buildResourceAttributes
    participant JSON as JSON Serialisation
    
    App->>Span: setKind(SpanKind::CLIENT)
    App->>Span: setStatus(StatusCode::OK)
    App->>Span: end()
    Span->>BuildRes: merge resource attrs
    BuildRes->>JSON: populate resource.attributes
    Span->>JSON: emit kind from kind_ field
    Span->>JSON: conditionally emit status object
    JSON-->>App: completed span OTLP
Loading

🎯 3 (Moderate) | ⏱️ ~20 minutes

🐰 A span now takes its shape with pride,
With kind and status as its guide.
Resource attrs merge nice and clean,
The finest OTLP ever seen!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the two main features: adding SpanKind/StatusCode API and unifying resource attribute merging via buildResourceAttributes().
Docstring Coverage ✅ Passed Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%.
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
  • Commit unit tests in branch feat/span-kind-status-and-resource-api

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

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 extends the tracer API to support explicit OTLP span kind and status, and centralizes resource attribute construction so runtime defaultResource() values are merged with compile-time fallbacks consistently across traces, metrics, and logs.

Changes:

  • Add SpanKind::* / StatusCode::* constants and Span::{setKind,setStatus,setError,setOk}; update JSON span serialization to emit kind and optionally a status block.
  • Introduce buildResourceAttributes() to merge runtime resource attributes with compile-time defaults (runtime wins per key).
  • Replace per-signal resource attribute logic with the shared helper and deprecate addResAttr().

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/OtelMetrics.cpp Switch metrics resource construction to buildResourceAttributes() (per-key merge instead of all-or-nothing).
include/OtelTracer.h Add SpanKind/StatusCode APIs, serialize kind/status, and deprecate addResAttr().
include/OtelMetrics.h Update header comment to reflect removal of addResAttr() usage.
include/OtelLogger.h Switch logs resource attributes to buildResourceAttributes().
include/OtelDefaults.h Add buildResourceAttributes() helper implementing runtime+fallback merging.

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

Comment thread include/OtelTracer.h
Comment thread include/OtelDefaults.h
Comment thread include/OtelTracer.h
1. Fix Span move-assignment TraceContext corruption
   If the RHS span was the currently active context, calling end() on the
   LHS would restore currentTraceContext() to the LHS parent's IDs, leaving
   subsequent spans/logs linking to the wrong parent. Now the RHS active
   state is checked before end(), and if it was active the context is
   reinstalled after the field move.

2. Add tests for span kind and status serialisation
   - default kind is SERVER (2)
   - setKind(CLIENT) → kind field is 3
   - status block absent when UNSET (default)
   - setError("msg") → status.code=2 + status.message present
   - setOk() → status.code=1, no status.message

3. Add tests for buildResourceAttributes() merge behaviour
   - runtime service.name override appears in span resource attributes
   - partial runtime override (only service.name set) still yields
     compile-time fallback for service.instance.id and host.name

All 20 native tests pass.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@proffalken proffalken merged commit a762afb into main Jun 5, 2026
3 checks passed
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