Skip to content

[RESOURCE DETECTOR] Add the service resource detector and builder - #4450

Open
shashankxrm wants to merge 12 commits into
open-telemetry:mainfrom
shashankxrm:fix/service-resource-detector-4414
Open

[RESOURCE DETECTOR] Add the service resource detector and builder#4450
shashankxrm wants to merge 12 commits into
open-telemetry:mainfrom
shashankxrm:fix/service-resource-detector-4414

Conversation

@shashankxrm

Copy link
Copy Markdown
Contributor

Fixes #4414

Summary

Adds the built-in service resource detector for declarative configuration and direct use. The detector populates service.name and service.instance.id according to the OpenTelemetry semantic conventions, with a builder integrated into the existing Registry / SdkBuilder path.

Implementation

  • Service detector utilities: Adds GetServiceName() and GenerateServiceInstanceId(), reusing GetExecutableName() from the process detector utilities.
  • ServiceResourceDetector: Sets service.name, service.instance.id, and the service semantic-conventions schema URL.
  • ServiceDetectorBuilder: Registers through Registry::SetServiceResourceDetectorBuilder() and builds ServiceResourceDetector. Integrated into the resource-detector aggregate targets and installation exports.
  • CMake / Bazel: Adds targets for the service detector utilities, detector, and builder, including aggregate and test targets.
  • Example configuration: Registers ServiceDetectorBuilder in examples/configuration/main.cc and enables service: in kitchen-sink.yaml. sdk-default.yaml remains unchanged, with built-in detectors commented out by design.
  • Install tests: Adds smoke tests for the installed service detector and builder.
  • Documentation: Documents service detector behavior, platform considerations, registration, and build targets in resource_detectors/README.md. Adds a CHANGELOG entry.

Behavior

service.name

  1. Uses OTEL_SERVICE_NAME when it is set and non-empty.
  2. Otherwise falls back to unknown_service:<process.executable.name> when the executable name is available.
  3. Otherwise returns unknown_service.

The detector does not read OTEL_RESOURCE_ATTRIBUTES.

service.instance.id

Generates an RFC 4122 UUID version 4 using the SDK random utility. The value is cached for the current process ID and regenerated if the PID changes, such as after fork().

Platform notes

  • Executable-name fallback uses the existing process detector utilities.
  • On macOS, the executable name is available for the current process only; the service detector always uses the current PID.
  • Linux and Windows executable-name fallback is supported.

Testing

Passed locally:

  • service_resource_detector_utils_test (4/4)
  • service_resource_detector_test (3/3)
  • service_resource_detector_builder_test (3/3)
  • registry_testRegistry.ServiceResourceDetectorBuilder (1/1)
  • process_resource_detector_builder_test (2/2)
  • host_resource_detector_test (6/6)
  • example_yaml --test --yaml sdk-default.yaml
  • C++14 build and relevant service detector tests
  • clang-format-18 --Werror -n on changed C++ files

Not validated locally:

  • Bazel targets/tests (Bazel was not available locally)
  • Full install-tree resource_detectors_test via cmake.install.test (the full install was blocked by an unrelated ryml install issue; service headers/libraries were verified in a partial install)

Environment note:

  • example_yaml --test --yaml kitchen-sink.yaml could not be completed locally because the local build did not include the OTLP HTTP and related exporters required by that configuration. CI runs this test with the full feature set.

Checklist

  • CHANGELOG.md updated for non-trivial changes
  • Unit tests have been added
  • Changes in public API reviewed

@shashankxrm
shashankxrm requested a review from a team as a code owner August 19, 2026 02:23
@codecov

codecov Bot commented Aug 19, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.42857% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.66%. Comparing base (68a575f) to head (90ea114).

Files with missing lines Patch % Lines
resource_detectors/src/service_detector_utils.cc 95.46% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4450      +/-   ##
==========================================
+ Coverage   82.62%   82.66%   +0.04%     
==========================================
  Files         512      515       +3     
  Lines       20139    20195      +56     
==========================================
+ Hits        16638    16692      +54     
- Misses       3501     3503       +2     
Files with missing lines Coverage Δ
resource_detectors/src/process_detector_utils.cc 89.29% <ø> (ø)
resource_detectors/src/service_detector.cc 100.00% <100.00%> (ø)
resource_detectors/src/service_detector_builder.cc 100.00% <100.00%> (ø)
resource_detectors/src/service_detector_utils.cc 95.46% <95.46%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds a built-in service resource detector to OpenTelemetry C++’s resource_detectors component, plus a declarative-configuration builder that plugs into the SDK Registry/SdkBuilder flow. The detector populates service.name (from OTEL_SERVICE_NAME or an executable-name fallback) and service.instance.id (UUIDv4).

Changes:

  • Introduces ServiceResourceDetector, supporting utilities, and a ServiceDetectorBuilder with CMake/Bazel targets.
  • Adds unit tests and install-tree smoke tests validating detector/builder availability and basic behavior.
  • Updates documentation, examples, and changelog to reflect the new built-in detector and configuration usage.

Reviewed changes

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

Show a summary per file
File Description
resource_detectors/test/service_detector_utils_test.cc Adds unit tests for service detector utilities (env var + UUIDv4 format/stability).
resource_detectors/test/service_detector_test.cc Adds unit tests validating detected service.* attributes and schema URL.
resource_detectors/test/service_detector_builder_test.cc Adds tests for builder registration and building the detector instance.
resource_detectors/test/process_detector_test.cc Adds a test for GetExecutableName() behavior.
resource_detectors/test/CMakeLists.txt Wires new service detector tests into the CMake test build.
resource_detectors/test/BUILD Wires new service detector tests into Bazel.
resource_detectors/src/service_detector.cc Implements ServiceResourceDetector::Detect().
resource_detectors/src/service_detector_utils.cc Implements GetServiceName() + cached UUIDv4 instance ID generation.
resource_detectors/src/service_detector_builder.cc Implements builder registration and construction of ServiceResourceDetector.
resource_detectors/src/process_detector_utils.cc Adds GetExecutableName() with macOS-specific behavior.
resource_detectors/README.md Documents service detector behavior, limitations, and link targets.
resource_detectors/include/opentelemetry/resource_detectors/service_detector.h Public header for the new service resource detector.
resource_detectors/include/opentelemetry/resource_detectors/service_detector_builder.h Public header for the service detector builder.
resource_detectors/include/opentelemetry/resource_detectors/detail/service_detector_utils.h Declares new service detector utility functions.
resource_detectors/include/opentelemetry/resource_detectors/detail/process_detector_utils.h Declares GetExecutableName() API and platform behavior.
resource_detectors/CMakeLists.txt Adds service detector libraries and aggregates them into component exports.
resource_detectors/BUILD Adds Bazel libraries for service detector/utils/builder and aggregates.
install/test/src/test_resource_detectors.cc Adds install-tree tests for the service detector and builder.
install/test/cmake/fetch_content_test/CMakeLists.txt Links installed service detector targets in FetchContent install tests.
install/test/cmake/component_tests/resource_detectors/CMakeLists.txt Links installed service detector targets in component install tests.
examples/configuration/main.cc Registers ServiceDetectorBuilder in the configuration example.
examples/configuration/kitchen-sink.yaml Enables the service: detector in the kitchen-sink example config.
CHANGELOG.md Adds changelog entry for the service resource detector + builder.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +84 to +88
std::string GenerateServiceInstanceId() noexcept
{
static std::mutex mutex;
static int32_t cached_pid = -1;
static std::string cached_id;
Comment thread resource_detectors/README.md Outdated
@shashankxrm
shashankxrm force-pushed the fix/service-resource-detector-4414 branch 3 times, most recently from 39795d0 to 0ab3b6a Compare August 20, 2026 16:39

@dbarker dbarker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR! Please see minor feedback below.

Comment thread resource_detectors/src/process_detector_utils.cc Outdated
Comment thread resource_detectors/README.md Outdated
@shashankxrm
shashankxrm force-pushed the fix/service-resource-detector-4414 branch 2 times, most recently from 4a0073b to 3de4502 Compare August 21, 2026 13:27
@shashankxrm

Copy link
Copy Markdown
Contributor Author

Thanks for the PR! Please see minor feedback below.

Thanks for the review! Sure, I'll check the feedback and update the PR.

@dbarker dbarker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good. Thanks for the new detector!

@shashankxrm

Copy link
Copy Markdown
Contributor Author

Looks good. Thanks for the new detector!

Glad to hear it! Appreciate the feedback.

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.

[RESOURCE DETECTOR] Service resource detector

3 participants