Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/origin/main' into RetryableErro…
Browse files Browse the repository at this point in the history
…rHttp
  • Loading branch information
chusitoo committed Jan 17, 2025
2 parents facc19d + 02cda51 commit d3c52fe
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ Increment the:
* [SDK] Better control of threads executed by opentelemetry-cpp
[#3175](https://github.com/open-telemetry/opentelemetry-cpp/pull/3175)

[EXPORTER] Support handling retry-able errors for OTLP/HTTP
* [SDK] Enable deriving from ResourceDetector to create a Resource
[#3247](https://github.com/open-telemetry/opentelemetry-cpp/pull/3247)

* [EXPORTER] Support handling retry-able errors for OTLP/HTTP
[#3223](https://github.com/open-telemetry/opentelemetry-cpp/pull/3223)

New features:
Expand Down
2 changes: 1 addition & 1 deletion sdk/include/opentelemetry/sdk/resource/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Resource
ResourceAttributes attributes_;
std::string schema_url_;

friend class OTELResourceDetector;
friend class ResourceDetector;
};

} // namespace resource
Expand Down
4 changes: 4 additions & 0 deletions sdk/include/opentelemetry/sdk/resource/resource_detector.h
Original file line number Diff line number Diff line change
Expand Up @@ -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{});
};

/**
Expand Down
10 changes: 8 additions & 2 deletions sdk/src/resource/resource_detector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,7 +39,7 @@ Resource OTELResourceDetector::Detect() noexcept

if (!attributes_exists && !service_name_exists)
{
return Resource();
return ResourceDetector::Create({});
}

ResourceAttributes attributes;
Expand All @@ -59,7 +65,7 @@ Resource OTELResourceDetector::Detect() noexcept
attributes[semconv::service::kServiceName] = service_name;
}

return Resource(attributes);
return ResourceDetector::Create(attributes);
}

} // namespace resource
Expand Down
24 changes: 24 additions & 0 deletions sdk/test/resource/resource_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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());
}

0 comments on commit d3c52fe

Please sign in to comment.