[RESOURCE DETECTOR] Add the service resource detector and builder - #4450
[RESOURCE DETECTOR] Add the service resource detector and builder#4450shashankxrm wants to merge 12 commits into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ 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
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
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 aServiceDetectorBuilderwith 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.
| std::string GenerateServiceInstanceId() noexcept | ||
| { | ||
| static std::mutex mutex; | ||
| static int32_t cached_pid = -1; | ||
| static std::string cached_id; |
39795d0 to
0ab3b6a
Compare
Removed unnecessary inclusion of stddef.h.
dbarker
left a comment
There was a problem hiding this comment.
Thanks for the PR! Please see minor feedback below.
4a0073b to
3de4502
Compare
Thanks for the review! Sure, I'll check the feedback and update the PR. |
dbarker
left a comment
There was a problem hiding this comment.
Looks good. Thanks for the new detector!
Glad to hear it! Appreciate the feedback. |
Fixes #4414
Summary
Adds the built-in service resource detector for declarative configuration and direct use. The detector populates
service.nameandservice.instance.idaccording to the OpenTelemetry semantic conventions, with a builder integrated into the existingRegistry/SdkBuilderpath.Implementation
GetServiceName()andGenerateServiceInstanceId(), reusingGetExecutableName()from the process detector utilities.ServiceResourceDetector: Setsservice.name,service.instance.id, and the service semantic-conventions schema URL.ServiceDetectorBuilder: Registers throughRegistry::SetServiceResourceDetectorBuilder()and buildsServiceResourceDetector. Integrated into the resource-detector aggregate targets and installation exports.ServiceDetectorBuilderinexamples/configuration/main.ccand enablesservice:inkitchen-sink.yaml.sdk-default.yamlremains unchanged, with built-in detectors commented out by design.resource_detectors/README.md. Adds a CHANGELOG entry.Behavior
service.nameOTEL_SERVICE_NAMEwhen it is set and non-empty.unknown_service:<process.executable.name>when the executable name is available.unknown_service.The detector does not read
OTEL_RESOURCE_ATTRIBUTES.service.instance.idGenerates 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
Testing
Passed locally:
service_resource_detector_utils_test(4/4)service_resource_detector_test(3/3)service_resource_detector_builder_test(3/3)registry_test—Registry.ServiceResourceDetectorBuilder(1/1)process_resource_detector_builder_test(2/2)host_resource_detector_test(6/6)example_yaml --test --yaml sdk-default.yamlclang-format-18 --Werror -non changed C++ filesNot validated locally:
resource_detectors_testviacmake.install.test(the full install was blocked by an unrelatedrymlinstall issue; service headers/libraries were verified in a partial install)Environment note:
example_yaml --test --yaml kitchen-sink.yamlcould 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.mdupdated for non-trivial changes