From ca069e136428b77a01b33ec337a25498e2a02f68 Mon Sep 17 00:00:00 2001 From: Mateen Anjum Date: Thu, 13 Aug 2026 03:57:23 -0400 Subject: [PATCH 1/4] [CONFIGURATION] Add a resource detector extension example Signed-off-by: Mateen Anjum --- CHANGELOG.md | 3 ++ examples/configuration/CMakeLists.txt | 4 ++- .../configuration/custom_resource_detector.cc | 14 +++++++++ .../configuration/custom_resource_detector.h | 25 +++++++++++++++ .../custom_resource_detector_builder.cc | 31 +++++++++++++++++++ .../custom_resource_detector_builder.h | 18 +++++++++++ examples/configuration/extensions.yaml | 2 +- examples/configuration/main.cc | 2 ++ 8 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 examples/configuration/custom_resource_detector.cc create mode 100644 examples/configuration/custom_resource_detector.h create mode 100644 examples/configuration/custom_resource_detector_builder.cc create mode 100644 examples/configuration/custom_resource_detector_builder.h diff --git a/CHANGELOG.md b/CHANGELOG.md index 0390e0cd8f..48ee646a6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,9 @@ Increment the: ## [Unreleased] +* [CONFIGURATION] Add a resource detector extension example + [#4419](https://github.com/open-telemetry/opentelemetry-cpp/issues/4419) + * [RESOURCE DETECTOR] Add the host resource detector [#4413](https://github.com/open-telemetry/opentelemetry-cpp/issues/4413) * [CONFIGURATION] Add configuration builders for the container and process diff --git a/examples/configuration/CMakeLists.txt b/examples/configuration/CMakeLists.txt index 5beea87914..490a86a7c0 100644 --- a/examples/configuration/CMakeLists.txt +++ b/examples/configuration/CMakeLists.txt @@ -17,7 +17,9 @@ add_executable( custom_log_record_exporter.cc custom_log_record_exporter_builder.cc custom_log_record_processor.cc - custom_log_record_processor_builder.cc) + custom_log_record_processor_builder.cc + custom_resource_detector.cc + custom_resource_detector_builder.cc) target_link_libraries( example_yaml diff --git a/examples/configuration/custom_resource_detector.cc b/examples/configuration/custom_resource_detector.cc new file mode 100644 index 0000000000..01de9a96d2 --- /dev/null +++ b/examples/configuration/custom_resource_detector.cc @@ -0,0 +1,14 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/sdk/common/global_log_handler.h" +#include "opentelemetry/sdk/resource/resource.h" +#include "opentelemetry/sdk/resource/resource_detector.h" + +#include "custom_resource_detector.h" + +opentelemetry::sdk::resource::Resource CustomResourceDetector::Detect() noexcept +{ + OTEL_INTERNAL_LOG_ERROR("CustomResourceDetector::Detect(): YOUR CODE HERE"); + return ResourceDetector::Create({}); +} diff --git a/examples/configuration/custom_resource_detector.h b/examples/configuration/custom_resource_detector.h new file mode 100644 index 0000000000..94935036bf --- /dev/null +++ b/examples/configuration/custom_resource_detector.h @@ -0,0 +1,25 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include + +#include "opentelemetry/sdk/resource/resource.h" +#include "opentelemetry/sdk/resource/resource_detector.h" + +class CustomResourceDetector : public opentelemetry::sdk::resource::ResourceDetector +{ +public: + CustomResourceDetector(const std::string &comment) : comment_(comment) {} + CustomResourceDetector(CustomResourceDetector &&) = delete; + CustomResourceDetector(const CustomResourceDetector &) = delete; + CustomResourceDetector &operator=(CustomResourceDetector &&) = delete; + CustomResourceDetector &operator=(const CustomResourceDetector &other) = delete; + ~CustomResourceDetector() override = default; + + opentelemetry::sdk::resource::Resource Detect() noexcept override; + +private: + std::string comment_; +}; diff --git a/examples/configuration/custom_resource_detector_builder.cc b/examples/configuration/custom_resource_detector_builder.cc new file mode 100644 index 0000000000..f9127a6c0e --- /dev/null +++ b/examples/configuration/custom_resource_detector_builder.cc @@ -0,0 +1,31 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include +#include +#include + +#include "opentelemetry/sdk/configuration/document_node.h" +#include "opentelemetry/sdk/configuration/extension_resource_detector_configuration.h" +#include "opentelemetry/sdk/configuration/registry.h" + +#include "custom_resource_detector.h" +#include "custom_resource_detector_builder.h" + +std::unique_ptr +CustomResourceDetectorBuilder::Build( + const opentelemetry::sdk::configuration::ExtensionResourceDetectorConfiguration *model) const +{ + // Read yaml attributes + std::string comment = model->node->GetRequiredString("comment"); + + auto sdk = std::make_unique(comment); + + return sdk; +} + +void CustomResourceDetectorBuilder::Register(opentelemetry::sdk::configuration::Registry *registry) +{ + auto builder = std::make_unique(); + registry->SetExtensionResourceDetectorBuilder("my_custom_detector", std::move(builder)); +} diff --git a/examples/configuration/custom_resource_detector_builder.h b/examples/configuration/custom_resource_detector_builder.h new file mode 100644 index 0000000000..c9900bf016 --- /dev/null +++ b/examples/configuration/custom_resource_detector_builder.h @@ -0,0 +1,18 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "opentelemetry/sdk/configuration/extension_resource_detector_builder.h" +#include "opentelemetry/sdk/configuration/registry.h" + +class CustomResourceDetectorBuilder + : public opentelemetry::sdk::configuration::ExtensionResourceDetectorBuilder +{ +public: + static void Register(opentelemetry::sdk::configuration::Registry *registry); + + std::unique_ptr Build( + const opentelemetry::sdk::configuration::ExtensionResourceDetectorConfiguration *model) + const override; +}; diff --git a/examples/configuration/extensions.yaml b/examples/configuration/extensions.yaml index 7f75513e54..59fd399237 100644 --- a/examples/configuration/extensions.yaml +++ b/examples/configuration/extensions.yaml @@ -74,6 +74,6 @@ logger_provider: resource: detection/development: detectors: - # Registry::SetExtensionResourceDetectorBuilder. No example implementation in this directory. + # Registry::SetExtensionResourceDetectorBuilder, see custom_resource_detector.cc. - my_custom_detector: comment: "This is a resource detector extension point, with properties." diff --git a/examples/configuration/main.cc b/examples/configuration/main.cc index b58be26991..a14cfc5544 100644 --- a/examples/configuration/main.cc +++ b/examples/configuration/main.cc @@ -21,6 +21,7 @@ #include "custom_log_record_processor_builder.h" #include "custom_pull_metric_exporter_builder.h" #include "custom_push_metric_exporter_builder.h" +#include "custom_resource_detector_builder.h" #include "custom_sampler_builder.h" #include "custom_span_exporter_builder.h" #include "custom_span_processor_builder.h" @@ -235,6 +236,7 @@ void InitOtel(const std::string &config_file) CustomPullMetricExporterBuilder::Register(registry.get()); CustomLogRecordExporterBuilder::Register(registry.get()); CustomLogRecordProcessorBuilder::Register(registry.get()); + CustomResourceDetectorBuilder::Register(registry.get()); /* 4 - Parse a config.yaml */ From ad58d373fa90c8c502a8340c2d5f3b3c85929843 Mon Sep 17 00:00:00 2001 From: Mateen Anjum Date: Thu, 13 Aug 2026 06:10:58 -0400 Subject: [PATCH 2/4] [CONFIGURATION] Fix iwyu finding in the detector extension example Signed-off-by: Mateen Anjum --- examples/configuration/custom_resource_detector.h | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/configuration/custom_resource_detector.h b/examples/configuration/custom_resource_detector.h index 94935036bf..9b8ff52bf1 100644 --- a/examples/configuration/custom_resource_detector.h +++ b/examples/configuration/custom_resource_detector.h @@ -5,7 +5,6 @@ #include -#include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/resource/resource_detector.h" class CustomResourceDetector : public opentelemetry::sdk::resource::ResourceDetector From 9398691133e8ac172225b0386b08745149501a44 Mon Sep 17 00:00:00 2001 From: Mateen Anjum Date: Thu, 13 Aug 2026 10:13:59 -0400 Subject: [PATCH 3/4] [CONFIGURATION] Set the parsed comment as a resource attribute in the example Signed-off-by: Mateen Anjum --- examples/configuration/custom_resource_detector.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/configuration/custom_resource_detector.cc b/examples/configuration/custom_resource_detector.cc index 01de9a96d2..143af1903b 100644 --- a/examples/configuration/custom_resource_detector.cc +++ b/examples/configuration/custom_resource_detector.cc @@ -10,5 +10,5 @@ opentelemetry::sdk::resource::Resource CustomResourceDetector::Detect() noexcept { OTEL_INTERNAL_LOG_ERROR("CustomResourceDetector::Detect(): YOUR CODE HERE"); - return ResourceDetector::Create({}); + return ResourceDetector::Create({{"custom.comment", comment_}}); } From 77de6a7a8e454041b9fc9781c4bd479ae9d4e8a0 Mon Sep 17 00:00:00 2001 From: Mateen Anjum Date: Thu, 13 Aug 2026 14:43:22 -0400 Subject: [PATCH 4/4] [CONFIGURATION] Fix iwyu finding Signed-off-by: Mateen Anjum --- examples/configuration/custom_resource_detector.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/configuration/custom_resource_detector.cc b/examples/configuration/custom_resource_detector.cc index 143af1903b..e78728ccc5 100644 --- a/examples/configuration/custom_resource_detector.cc +++ b/examples/configuration/custom_resource_detector.cc @@ -1,6 +1,8 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +#include + #include "opentelemetry/sdk/common/global_log_handler.h" #include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/resource/resource_detector.h"