From 02cda5103f8b7eff5eca564038aa91b7a6a93297 Mon Sep 17 00:00:00 2001 From: Doug Barker Date: Thu, 16 Jan 2025 10:38:05 -0700 Subject: [PATCH] [SDK] enable deriving from ResourceDetector to create a Resource (#3247) --- CHANGELOG.md | 3 +++ .../opentelemetry/sdk/resource/resource.h | 2 +- .../sdk/resource/resource_detector.h | 4 ++++ sdk/src/resource/resource_detector.cc | 10 ++++++-- sdk/test/resource/resource_test.cc | 24 +++++++++++++++++++ 5 files changed, 40 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ca60c83e0..8aa23093bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,9 @@ Increment the: * [SDK] Better control of threads executed by opentelemetry-cpp [#3175](https://github.com/open-telemetry/opentelemetry-cpp/pull/3175) +* [SDK] Enable deriving from ResourceDetector to create a Resource + [#3247](https://github.com/open-telemetry/opentelemetry-cpp/pull/3247) + New features: * [SDK] Better control of threads executed by opentelemetry-cpp diff --git a/sdk/include/opentelemetry/sdk/resource/resource.h b/sdk/include/opentelemetry/sdk/resource/resource.h index d578491d78..5260b7c3f5 100644 --- a/sdk/include/opentelemetry/sdk/resource/resource.h +++ b/sdk/include/opentelemetry/sdk/resource/resource.h @@ -75,7 +75,7 @@ class Resource ResourceAttributes attributes_; std::string schema_url_; - friend class OTELResourceDetector; + friend class ResourceDetector; }; } // namespace resource diff --git a/sdk/include/opentelemetry/sdk/resource/resource_detector.h b/sdk/include/opentelemetry/sdk/resource/resource_detector.h index dcbd874847..2bce72a7c2 100644 --- a/sdk/include/opentelemetry/sdk/resource/resource_detector.h +++ b/sdk/include/opentelemetry/sdk/resource/resource_detector.h @@ -21,6 +21,10 @@ class ResourceDetector ResourceDetector() = default; virtual ~ResourceDetector() = default; virtual Resource Detect() = 0; + +protected: + static Resource Create(const ResourceAttributes &attributes, + const std::string &schema_url = std::string{}); }; /** diff --git a/sdk/src/resource/resource_detector.cc b/sdk/src/resource/resource_detector.cc index 2bd2fbe49b..3158f2a43d 100644 --- a/sdk/src/resource/resource_detector.cc +++ b/sdk/src/resource/resource_detector.cc @@ -22,6 +22,12 @@ namespace resource const char *OTEL_RESOURCE_ATTRIBUTES = "OTEL_RESOURCE_ATTRIBUTES"; const char *OTEL_SERVICE_NAME = "OTEL_SERVICE_NAME"; +Resource ResourceDetector::Create(const ResourceAttributes &attributes, + const std::string &schema_url) +{ + return Resource(attributes, schema_url); +} + Resource OTELResourceDetector::Detect() noexcept { std::string attributes_str, service_name; @@ -33,7 +39,7 @@ Resource OTELResourceDetector::Detect() noexcept if (!attributes_exists && !service_name_exists) { - return Resource(); + return ResourceDetector::Create({}); } ResourceAttributes attributes; @@ -59,7 +65,7 @@ Resource OTELResourceDetector::Detect() noexcept attributes[semconv::service::kServiceName] = service_name; } - return Resource(attributes); + return ResourceDetector::Create(attributes); } } // namespace resource diff --git a/sdk/test/resource/resource_test.cc b/sdk/test/resource/resource_test.cc index 7efc3688a8..73a097417b 100644 --- a/sdk/test/resource/resource_test.cc +++ b/sdk/test/resource/resource_test.cc @@ -35,6 +35,15 @@ class TestResource : public Resource {} }; +class TestResourceDetector : public ResourceDetector +{ +public: + TestResourceDetector() = default; + Resource Detect() noexcept override { return Create(attributes, schema_url); } + ResourceAttributes attributes; + std::string schema_url; +}; + TEST(ResourceTest, create_without_servicename) { ResourceAttributes expected_attributes = { @@ -266,4 +275,19 @@ TEST(ResourceTest, OtelResourceDetectorEmptyEnv) } EXPECT_EQ(received_attributes.size(), expected_attributes.size()); } + #endif + +TEST(ResourceTest, DerivedResourceDetector) +{ + TestResourceDetector detector; + + detector.attributes = {{"key", "value"}}; + detector.schema_url = "https://opentelemetry.io/schemas/v3.1.4"; + const auto resource = detector.Detect(); + const auto received_attributes = resource.GetAttributes(); + + EXPECT_EQ(received_attributes.size(), 1); + EXPECT_EQ(resource.GetSchemaURL(), detector.schema_url); + EXPECT_TRUE(received_attributes.find("key") != received_attributes.end()); +}