From c740ddb4b04d6e50937e16b6da73be0f08704077 Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Tue, 29 Jul 2025 17:05:37 +0000 Subject: [PATCH 01/26] [SDK] Implementation of container resource as per semconv --- .../opentelemetry/sdk/common/container.h | 30 +++++++++++ .../sdk/resource/resource_detector.h | 11 ++++ sdk/src/common/BUILD | 11 ++++ sdk/src/common/CMakeLists.txt | 2 +- sdk/src/common/container.cc | 53 +++++++++++++++++++ sdk/src/resource/resource.cc | 3 +- sdk/src/resource/resource_detector.cc | 23 ++++++++ sdk/test/resource/resource_test.cc | 47 ++++++++++++++++ 8 files changed, 178 insertions(+), 2 deletions(-) create mode 100644 sdk/include/opentelemetry/sdk/common/container.h create mode 100644 sdk/src/common/container.cc diff --git a/sdk/include/opentelemetry/sdk/common/container.h b/sdk/include/opentelemetry/sdk/common/container.h new file mode 100644 index 0000000000..97ddb35d53 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/common/container.h @@ -0,0 +1,30 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include + +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace common +{ +/** + Reads the container.id from /proc/self/cgroup file. + @param file_path file path of cgroup + @return container.id as string or empty string +*/ +std::string GetContainerIDFromCgroup(const char *file_path); + +/** + Matches the line with the regex to find container.id + @param line contains + @return matched id or empty string +*/ +std::string ExtractContainerIDFromLine(std::string &line); +} // namespace common +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/resource/resource_detector.h b/sdk/include/opentelemetry/sdk/resource/resource_detector.h index f129d3d1bc..cdc3056bbc 100644 --- a/sdk/include/opentelemetry/sdk/resource/resource_detector.h +++ b/sdk/include/opentelemetry/sdk/resource/resource_detector.h @@ -39,6 +39,17 @@ class OTELResourceDetector : public ResourceDetector Resource Detect() noexcept override; }; +/** + * ContainerResourceDetector to detect resource attributes when running inside a containerized environment. + * This detector extracts metadata such as container ID from cgroup information + * and sets attributes like container.id following the OpenTelemetry semantic conventions. + */ +class ContainerResourceDetector : public ResourceDetector +{ +public: + Resource Detect() noexcept override; +}; + } // namespace resource } // namespace sdk OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/src/common/BUILD b/sdk/src/common/BUILD index 19b47034f7..47981f169c 100644 --- a/sdk/src/common/BUILD +++ b/sdk/src/common/BUILD @@ -57,6 +57,17 @@ cc_library( ], ) +cc_library( + name = "container", + srcs = [ + "container.cc", + ], + deps = [ + "//api", + "//sdk:headers", + ], +) + cc_library( name = "global_log_handler", srcs = [ diff --git a/sdk/src/common/CMakeLists.txt b/sdk/src/common/CMakeLists.txt index 4a3b59aefa..9c341c04ff 100644 --- a/sdk/src/common/CMakeLists.txt +++ b/sdk/src/common/CMakeLists.txt @@ -1,7 +1,7 @@ # Copyright The OpenTelemetry Authors # SPDX-License-Identifier: Apache-2.0 -set(COMMON_SRCS random.cc global_log_handler.cc env_variables.cc base64.cc +set(COMMON_SRCS random.cc global_log_handler.cc env_variables.cc container.cc base64.cc disabled.cc) if(WIN32) list(APPEND COMMON_SRCS platform/fork_windows.cc) diff --git a/sdk/src/common/container.cc b/sdk/src/common/container.cc new file mode 100644 index 0000000000..7e3ffd33c8 --- /dev/null +++ b/sdk/src/common/container.cc @@ -0,0 +1,53 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/sdk/common/container.h" + +#ifdef _MSC_VER +# include +# define strcasecmp _stricmp +#else +# include +#endif + +#include +#include + +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace common +{ +std::string GetContainerIDFromCgroup(const char* file_path) +{ + std::ifstream cgroup_file(file_path); + std::string line; + + while(std::getline(cgroup_file, line)) + { + std::string container_id = ExtractContainerIDFromLine(line); + if(!container_id.empty()) + { + return container_id; + } + } + return ""; +} + +std::string ExtractContainerIDFromLine(std::string &line) +{ + static const std::regex container_id_regex(R"(^.*/(?:.*[-:])?([0-9a-f]+)(?:\.|\s*$))"); + std::smatch match; + + if (std::regex_search(line, match, container_id_regex)) + { + return match.str(1); + } + + return ""; +} +} // namespace common +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE \ No newline at end of file diff --git a/sdk/src/resource/resource.cc b/sdk/src/resource/resource.cc index 95b41498f8..80a6080d37 100644 --- a/sdk/src/resource/resource.cc +++ b/sdk/src/resource/resource.cc @@ -41,8 +41,9 @@ Resource Resource::Merge(const Resource &other) const noexcept Resource Resource::Create(const ResourceAttributes &attributes, const std::string &schema_url) { static auto otel_resource = OTELResourceDetector().Detect(); + static auto container_resource = ContainerResourceDetector().Detect(); auto resource = - Resource::GetDefault().Merge(otel_resource).Merge(Resource{attributes, schema_url}); + Resource::GetDefault().Merge(otel_resource).Merge(container_resource).Merge(Resource{attributes, schema_url}); if (resource.attributes_.find(semconv::service::kServiceName) == resource.attributes_.end()) { diff --git a/sdk/src/resource/resource_detector.cc b/sdk/src/resource/resource_detector.cc index 3158f2a43d..f7c5c3e599 100644 --- a/sdk/src/resource/resource_detector.cc +++ b/sdk/src/resource/resource_detector.cc @@ -4,8 +4,10 @@ #include "opentelemetry/sdk/resource/resource_detector.h" #include "opentelemetry/nostd/variant.h" #include "opentelemetry/sdk/common/env_variables.h" +#include "opentelemetry/sdk/common/container.h" #include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/semconv/service_attributes.h" +#include "opentelemetry/semconv/incubating/container_attributes.h" #include "opentelemetry/version.h" #include @@ -21,6 +23,10 @@ namespace resource const char *OTEL_RESOURCE_ATTRIBUTES = "OTEL_RESOURCE_ATTRIBUTES"; const char *OTEL_SERVICE_NAME = "OTEL_SERVICE_NAME"; +/** + * This is the file path from where we can get container.id +*/ +const char *C_GROUP_PATH = "/proc/self/cgroup"; Resource ResourceDetector::Create(const ResourceAttributes &attributes, const std::string &schema_url) @@ -68,6 +74,23 @@ Resource OTELResourceDetector::Detect() noexcept return ResourceDetector::Create(attributes); } +Resource ContainerResourceDetector::Detect() noexcept +{ + std::string container_id = opentelemetry::sdk::common::GetContainerIDFromCgroup(C_GROUP_PATH); + if(container_id.empty()) + { + return ResourceDetector::Create({}); + } + + ResourceAttributes attributes; + + if(!container_id.empty()) + { + attributes[semconv::container::kContainerId] = container_id; + } + return ResourceDetector::Create(attributes); +} + } // namespace resource } // namespace sdk OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/test/resource/resource_test.cc b/sdk/test/resource/resource_test.cc index 696509f892..d620786929 100644 --- a/sdk/test/resource/resource_test.cc +++ b/sdk/test/resource/resource_test.cc @@ -8,9 +8,11 @@ #include #include #include +#include #include "opentelemetry/nostd/variant.h" #include "opentelemetry/sdk/common/attribute_utils.h" +#include "opentelemetry/sdk/common/container.h" #include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/resource/resource_detector.h" #include "opentelemetry/sdk/version/version.h" @@ -292,3 +294,48 @@ TEST(ResourceTest, DerivedResourceDetector) EXPECT_EQ(resource.GetSchemaURL(), detector.schema_url); EXPECT_TRUE(received_attributes.find("key") != received_attributes.end()); } + +TEST(ResourceTest, ExctractValidContainerId) +{ + std::string line = "13:name=systemd:/podruntime/docker/kubepods/ac679f8a8319c8cf7d38e1adf263bc08d23.aaaa"; + std::string extracted_id = opentelemetry::sdk::common::ExtractContainerIDFromLine(line); + EXPECT_EQ(std::string{"ac679f8a8319c8cf7d38e1adf263bc08d23"}, extracted_id); +} + +TEST(ResourceTest, ExtractIdFromMockUpCGroupFile) +{ + const char *filename = "test_cgroup.txt"; + + { + std::ofstream outfile(filename); + outfile << "13:name=systemd:/kuberuntime/containerd/kubepods-pod872d2066_00ef_48ea_a7d8_51b18b72d739:cri-containerd:e857a4bf05a69080a759574949d7a0e69572e27647800fa7faff6a05a8332aa1\n"; + outfile << "9:cpu:/not-a-container\n"; + } + + std::string container_id = opentelemetry::sdk::common::GetContainerIDFromCgroup(filename); + EXPECT_EQ(container_id, std::string{"e857a4bf05a69080a759574949d7a0e69572e27647800fa7faff6a05a8332aa1"}); + + std::remove(filename); +} + +TEST(ResourceTest, DoesNotExtractInvalidLine) +{ + std::string line = "this line does not contain a container id"; + std::string id = opentelemetry::sdk::common::ExtractContainerIDFromLine(line); + EXPECT_EQ(id, std::string{""}); +} + +TEST(ContainerIdDetectorTest, ReturnsEmptyOnNoMatch) +{ + const char *filename = "test_empty_cgroup.txt"; + + { + std::ofstream outfile(filename); + outfile << "no container id here\n"; + } + + std::string id = opentelemetry::sdk::common::GetContainerIDFromCgroup(filename); + EXPECT_EQ(id, std::string{""}); + + std::remove(filename); // cleanup +} From 4bf2bfb281cbeae320739d93eb27b63073bc90e9 Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Wed, 30 Jul 2025 12:23:37 +0000 Subject: [PATCH 02/26] [sdk] minor changes in format, linking and var naming --- .../opentelemetry/sdk/common/container.h | 4 +- .../sdk/resource/resource_detector.h | 6 +-- sdk/src/common/CMakeLists.txt | 4 +- sdk/src/common/container.cc | 10 ++--- sdk/src/resource/BUILD | 1 + sdk/src/resource/CMakeLists.txt | 2 +- sdk/src/resource/container_detector.cc | 39 +++++++++++++++++++ sdk/src/resource/resource.cc | 8 ++-- sdk/src/resource/resource_detector.cc | 33 +++------------- sdk/test/resource/resource_test.cc | 16 +++++--- 10 files changed, 74 insertions(+), 49 deletions(-) create mode 100644 sdk/src/resource/container_detector.cc diff --git a/sdk/include/opentelemetry/sdk/common/container.h b/sdk/include/opentelemetry/sdk/common/container.h index 97ddb35d53..ef3091f165 100644 --- a/sdk/include/opentelemetry/sdk/common/container.h +++ b/sdk/include/opentelemetry/sdk/common/container.h @@ -21,10 +21,10 @@ std::string GetContainerIDFromCgroup(const char *file_path); /** Matches the line with the regex to find container.id - @param line contains + @param line a single line of text, typically from the /proc/self/cgroup file @return matched id or empty string */ -std::string ExtractContainerIDFromLine(std::string &line); +std::string ExtractContainerIDFromLine(const std::string &line); } // namespace common } // namespace sdk OPENTELEMETRY_END_NAMESPACE \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/resource/resource_detector.h b/sdk/include/opentelemetry/sdk/resource/resource_detector.h index cdc3056bbc..f4a3f126da 100644 --- a/sdk/include/opentelemetry/sdk/resource/resource_detector.h +++ b/sdk/include/opentelemetry/sdk/resource/resource_detector.h @@ -40,9 +40,9 @@ class OTELResourceDetector : public ResourceDetector }; /** - * ContainerResourceDetector to detect resource attributes when running inside a containerized environment. - * This detector extracts metadata such as container ID from cgroup information - * and sets attributes like container.id following the OpenTelemetry semantic conventions. + * ContainerResourceDetector to detect resource attributes when running inside a containerized + * environment. This detector extracts metadata such as container ID from cgroup information and + * sets attributes like container.id following the OpenTelemetry semantic conventions. */ class ContainerResourceDetector : public ResourceDetector { diff --git a/sdk/src/common/CMakeLists.txt b/sdk/src/common/CMakeLists.txt index 9c341c04ff..dcb33d1e5f 100644 --- a/sdk/src/common/CMakeLists.txt +++ b/sdk/src/common/CMakeLists.txt @@ -1,8 +1,8 @@ # Copyright The OpenTelemetry Authors # SPDX-License-Identifier: Apache-2.0 -set(COMMON_SRCS random.cc global_log_handler.cc env_variables.cc container.cc base64.cc - disabled.cc) +set(COMMON_SRCS random.cc global_log_handler.cc env_variables.cc container.cc + base64.cc disabled.cc) if(WIN32) list(APPEND COMMON_SRCS platform/fork_windows.cc) else() diff --git a/sdk/src/common/container.cc b/sdk/src/common/container.cc index 7e3ffd33c8..0ca1e9bbe1 100644 --- a/sdk/src/common/container.cc +++ b/sdk/src/common/container.cc @@ -20,23 +20,23 @@ namespace sdk { namespace common { -std::string GetContainerIDFromCgroup(const char* file_path) +std::string GetContainerIDFromCgroup(const char *file_path) { std::ifstream cgroup_file(file_path); std::string line; - while(std::getline(cgroup_file, line)) + while (std::getline(cgroup_file, line)) { std::string container_id = ExtractContainerIDFromLine(line); - if(!container_id.empty()) + if (!container_id.empty()) { - return container_id; + return container_id; } } return ""; } -std::string ExtractContainerIDFromLine(std::string &line) +std::string ExtractContainerIDFromLine(const std::string &line) { static const std::regex container_id_regex(R"(^.*/(?:.*[-:])?([0-9a-f]+)(?:\.|\s*$))"); std::smatch match; diff --git a/sdk/src/resource/BUILD b/sdk/src/resource/BUILD index 8845629990..0e50aee9c3 100644 --- a/sdk/src/resource/BUILD +++ b/sdk/src/resource/BUILD @@ -11,5 +11,6 @@ cc_library( "//api", "//sdk:headers", "//sdk/src/common:env_variables", + "//sdk/src/common:container", ], ) diff --git a/sdk/src/resource/CMakeLists.txt b/sdk/src/resource/CMakeLists.txt index 48b647ec41..b9f11ea0a5 100644 --- a/sdk/src/resource/CMakeLists.txt +++ b/sdk/src/resource/CMakeLists.txt @@ -1,7 +1,7 @@ # Copyright The OpenTelemetry Authors # SPDX-License-Identifier: Apache-2.0 -add_library(opentelemetry_resources resource.cc resource_detector.cc) +add_library(opentelemetry_resources resource.cc resource_detector.cc container_detector.cc) set_target_properties(opentelemetry_resources PROPERTIES EXPORT_NAME resources) set_target_version(opentelemetry_resources) diff --git a/sdk/src/resource/container_detector.cc b/sdk/src/resource/container_detector.cc new file mode 100644 index 0000000000..d6ef2e3e11 --- /dev/null +++ b/sdk/src/resource/container_detector.cc @@ -0,0 +1,39 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/sdk/common/container.h" +#include "opentelemetry/sdk/resource/resource.h" +#include "opentelemetry/sdk/resource/resource_detector.h" +#include "opentelemetry/semconv/incubating/container_attributes.h" +#include "opentelemetry/version.h" + +#include + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace resource +{ + +/** + * This is the file path from where we can get container.id + */ +const char *KCGroupPath = "/proc/self/cgroup"; + +Resource ContainerResourceDetector::Detect() noexcept +{ + std::string container_id = opentelemetry::sdk::common::GetContainerIDFromCgroup(KCGroupPath); + if (container_id.empty()) + { + return ResourceDetector::Create({}); + } + + ResourceAttributes attributes; + + attributes[semconv::container::kContainerId] = container_id; + return ResourceDetector::Create(attributes); +} + +} // namespace resource +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE \ No newline at end of file diff --git a/sdk/src/resource/resource.cc b/sdk/src/resource/resource.cc index 80a6080d37..cfc900a6e1 100644 --- a/sdk/src/resource/resource.cc +++ b/sdk/src/resource/resource.cc @@ -40,10 +40,12 @@ Resource Resource::Merge(const Resource &other) const noexcept Resource Resource::Create(const ResourceAttributes &attributes, const std::string &schema_url) { - static auto otel_resource = OTELResourceDetector().Detect(); + static auto otel_resource = OTELResourceDetector().Detect(); static auto container_resource = ContainerResourceDetector().Detect(); - auto resource = - Resource::GetDefault().Merge(otel_resource).Merge(container_resource).Merge(Resource{attributes, schema_url}); + auto resource = Resource::GetDefault() + .Merge(otel_resource) + .Merge(container_resource) + .Merge(Resource{attributes, schema_url}); if (resource.attributes_.find(semconv::service::kServiceName) == resource.attributes_.end()) { diff --git a/sdk/src/resource/resource_detector.cc b/sdk/src/resource/resource_detector.cc index f7c5c3e599..10ae4c3d68 100644 --- a/sdk/src/resource/resource_detector.cc +++ b/sdk/src/resource/resource_detector.cc @@ -3,11 +3,11 @@ #include "opentelemetry/sdk/resource/resource_detector.h" #include "opentelemetry/nostd/variant.h" -#include "opentelemetry/sdk/common/env_variables.h" #include "opentelemetry/sdk/common/container.h" +#include "opentelemetry/sdk/common/env_variables.h" #include "opentelemetry/sdk/resource/resource.h" -#include "opentelemetry/semconv/service_attributes.h" #include "opentelemetry/semconv/incubating/container_attributes.h" +#include "opentelemetry/semconv/service_attributes.h" #include "opentelemetry/version.h" #include @@ -21,12 +21,8 @@ namespace sdk namespace resource { -const char *OTEL_RESOURCE_ATTRIBUTES = "OTEL_RESOURCE_ATTRIBUTES"; -const char *OTEL_SERVICE_NAME = "OTEL_SERVICE_NAME"; -/** - * This is the file path from where we can get container.id -*/ -const char *C_GROUP_PATH = "/proc/self/cgroup"; +const char *KOtelResourceAttributes = "OTEL_RESOURCE_ATTRIBUTES"; +const char *KOtelServiceName = "OTEL_SERVICE_NAME"; Resource ResourceDetector::Create(const ResourceAttributes &attributes, const std::string &schema_url) @@ -39,9 +35,9 @@ Resource OTELResourceDetector::Detect() noexcept std::string attributes_str, service_name; bool attributes_exists = opentelemetry::sdk::common::GetStringEnvironmentVariable( - OTEL_RESOURCE_ATTRIBUTES, attributes_str); + KOtelResourceAttributes, attributes_str); bool service_name_exists = - opentelemetry::sdk::common::GetStringEnvironmentVariable(OTEL_SERVICE_NAME, service_name); + opentelemetry::sdk::common::GetStringEnvironmentVariable(KOtelServiceName, service_name); if (!attributes_exists && !service_name_exists) { @@ -74,23 +70,6 @@ Resource OTELResourceDetector::Detect() noexcept return ResourceDetector::Create(attributes); } -Resource ContainerResourceDetector::Detect() noexcept -{ - std::string container_id = opentelemetry::sdk::common::GetContainerIDFromCgroup(C_GROUP_PATH); - if(container_id.empty()) - { - return ResourceDetector::Create({}); - } - - ResourceAttributes attributes; - - if(!container_id.empty()) - { - attributes[semconv::container::kContainerId] = container_id; - } - return ResourceDetector::Create(attributes); -} - } // namespace resource } // namespace sdk OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/test/resource/resource_test.cc b/sdk/test/resource/resource_test.cc index d620786929..2ab00d48ba 100644 --- a/sdk/test/resource/resource_test.cc +++ b/sdk/test/resource/resource_test.cc @@ -4,11 +4,11 @@ #include #include #include +#include #include #include #include #include -#include #include "opentelemetry/nostd/variant.h" #include "opentelemetry/sdk/common/attribute_utils.h" @@ -295,9 +295,10 @@ TEST(ResourceTest, DerivedResourceDetector) EXPECT_TRUE(received_attributes.find("key") != received_attributes.end()); } -TEST(ResourceTest, ExctractValidContainerId) +TEST(ResourceTest, ExtractValidContainerId) { - std::string line = "13:name=systemd:/podruntime/docker/kubepods/ac679f8a8319c8cf7d38e1adf263bc08d23.aaaa"; + std::string line = + "13:name=systemd:/podruntime/docker/kubepods/ac679f8a8319c8cf7d38e1adf263bc08d23.aaaa"; std::string extracted_id = opentelemetry::sdk::common::ExtractContainerIDFromLine(line); EXPECT_EQ(std::string{"ac679f8a8319c8cf7d38e1adf263bc08d23"}, extracted_id); } @@ -308,12 +309,15 @@ TEST(ResourceTest, ExtractIdFromMockUpCGroupFile) { std::ofstream outfile(filename); - outfile << "13:name=systemd:/kuberuntime/containerd/kubepods-pod872d2066_00ef_48ea_a7d8_51b18b72d739:cri-containerd:e857a4bf05a69080a759574949d7a0e69572e27647800fa7faff6a05a8332aa1\n"; + outfile << "13:name=systemd:/kuberuntime/containerd" + "/kubepods-pod872d2066_00ef_48ea_a7d8_51b18b72d739:cri-containerd:" + "e857a4bf05a69080a759574949d7a0e69572e27647800fa7faff6a05a8332aa1\n"; outfile << "9:cpu:/not-a-container\n"; } std::string container_id = opentelemetry::sdk::common::GetContainerIDFromCgroup(filename); - EXPECT_EQ(container_id, std::string{"e857a4bf05a69080a759574949d7a0e69572e27647800fa7faff6a05a8332aa1"}); + EXPECT_EQ(container_id, + std::string{"e857a4bf05a69080a759574949d7a0e69572e27647800fa7faff6a05a8332aa1"}); std::remove(filename); } @@ -321,7 +325,7 @@ TEST(ResourceTest, ExtractIdFromMockUpCGroupFile) TEST(ResourceTest, DoesNotExtractInvalidLine) { std::string line = "this line does not contain a container id"; - std::string id = opentelemetry::sdk::common::ExtractContainerIDFromLine(line); + std::string id = opentelemetry::sdk::common::ExtractContainerIDFromLine(line); EXPECT_EQ(id, std::string{""}); } From f075c93a7bcf5d95f71cd9f15ac4c2b0ef7ae749 Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Thu, 31 Jul 2025 19:20:46 +0530 Subject: [PATCH 03/26] changes for ci --- sdk/src/common/container.cc | 7 ------- sdk/src/resource/BUILD | 2 +- sdk/src/resource/CMakeLists.txt | 3 ++- sdk/src/resource/container_detector.cc | 2 ++ sdk/src/resource/resource_detector.cc | 2 -- sdk/test/resource/resource_test.cc | 1 + 6 files changed, 6 insertions(+), 11 deletions(-) diff --git a/sdk/src/common/container.cc b/sdk/src/common/container.cc index 0ca1e9bbe1..5a317ab4b9 100644 --- a/sdk/src/common/container.cc +++ b/sdk/src/common/container.cc @@ -3,13 +3,6 @@ #include "opentelemetry/sdk/common/container.h" -#ifdef _MSC_VER -# include -# define strcasecmp _stricmp -#else -# include -#endif - #include #include diff --git a/sdk/src/resource/BUILD b/sdk/src/resource/BUILD index 0e50aee9c3..3cef2c6338 100644 --- a/sdk/src/resource/BUILD +++ b/sdk/src/resource/BUILD @@ -10,7 +10,7 @@ cc_library( deps = [ "//api", "//sdk:headers", - "//sdk/src/common:env_variables", "//sdk/src/common:container", + "//sdk/src/common:env_variables", ], ) diff --git a/sdk/src/resource/CMakeLists.txt b/sdk/src/resource/CMakeLists.txt index b9f11ea0a5..91384fa88a 100644 --- a/sdk/src/resource/CMakeLists.txt +++ b/sdk/src/resource/CMakeLists.txt @@ -1,7 +1,8 @@ # Copyright The OpenTelemetry Authors # SPDX-License-Identifier: Apache-2.0 -add_library(opentelemetry_resources resource.cc resource_detector.cc container_detector.cc) +add_library(opentelemetry_resources resource.cc resource_detector.cc + container_detector.cc) set_target_properties(opentelemetry_resources PROPERTIES EXPORT_NAME resources) set_target_version(opentelemetry_resources) diff --git a/sdk/src/resource/container_detector.cc b/sdk/src/resource/container_detector.cc index d6ef2e3e11..f16cfd83e1 100644 --- a/sdk/src/resource/container_detector.cc +++ b/sdk/src/resource/container_detector.cc @@ -1,6 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +#include "opentelemetry/nostd/variant.h" #include "opentelemetry/sdk/common/container.h" #include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/resource/resource_detector.h" @@ -8,6 +9,7 @@ #include "opentelemetry/version.h" #include +#include OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk diff --git a/sdk/src/resource/resource_detector.cc b/sdk/src/resource/resource_detector.cc index 10ae4c3d68..4c5dcd7c0f 100644 --- a/sdk/src/resource/resource_detector.cc +++ b/sdk/src/resource/resource_detector.cc @@ -3,10 +3,8 @@ #include "opentelemetry/sdk/resource/resource_detector.h" #include "opentelemetry/nostd/variant.h" -#include "opentelemetry/sdk/common/container.h" #include "opentelemetry/sdk/common/env_variables.h" #include "opentelemetry/sdk/resource/resource.h" -#include "opentelemetry/semconv/incubating/container_attributes.h" #include "opentelemetry/semconv/service_attributes.h" #include "opentelemetry/version.h" diff --git a/sdk/test/resource/resource_test.cc b/sdk/test/resource/resource_test.cc index 2ab00d48ba..48932eabcd 100644 --- a/sdk/test/resource/resource_test.cc +++ b/sdk/test/resource/resource_test.cc @@ -3,6 +3,7 @@ #include #include +#include #include #include #include From 00decf9e0871b80adf62ffe39710214b5a20a7c6 Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Sat, 2 Aug 2025 15:04:05 +0530 Subject: [PATCH 04/26] ci and requested changes --- .../opentelemetry/sdk/common/container.h | 30 ------------- .../resource/container_resource_detector.h | 43 +++++++++++++++++++ .../sdk/resource/resource_detector.h | 11 ----- sdk/src/common/BUILD | 11 ----- sdk/src/common/CMakeLists.txt | 4 +- sdk/src/resource/BUILD | 1 - sdk/src/resource/CMakeLists.txt | 4 +- sdk/src/{common => resource}/container.cc | 6 +-- sdk/src/resource/container_detector.cc | 5 +-- sdk/src/resource/resource.cc | 1 + sdk/test/resource/resource_test.cc | 10 ++--- 11 files changed, 58 insertions(+), 68 deletions(-) delete mode 100644 sdk/include/opentelemetry/sdk/common/container.h create mode 100644 sdk/include/opentelemetry/sdk/resource/container_resource_detector.h rename sdk/src/{common => resource}/container.cc (88%) diff --git a/sdk/include/opentelemetry/sdk/common/container.h b/sdk/include/opentelemetry/sdk/common/container.h deleted file mode 100644 index ef3091f165..0000000000 --- a/sdk/include/opentelemetry/sdk/common/container.h +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -#pragma once - -#include - -#include "opentelemetry/version.h" - -OPENTELEMETRY_BEGIN_NAMESPACE -namespace sdk -{ -namespace common -{ -/** - Reads the container.id from /proc/self/cgroup file. - @param file_path file path of cgroup - @return container.id as string or empty string -*/ -std::string GetContainerIDFromCgroup(const char *file_path); - -/** - Matches the line with the regex to find container.id - @param line a single line of text, typically from the /proc/self/cgroup file - @return matched id or empty string -*/ -std::string ExtractContainerIDFromLine(const std::string &line); -} // namespace common -} // namespace sdk -OPENTELEMETRY_END_NAMESPACE \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/resource/container_resource_detector.h b/sdk/include/opentelemetry/sdk/resource/container_resource_detector.h new file mode 100644 index 0000000000..386b9fc2da --- /dev/null +++ b/sdk/include/opentelemetry/sdk/resource/container_resource_detector.h @@ -0,0 +1,43 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "opentelemetry/sdk/resource/resource.h" +#include "opentelemetry/sdk/resource/resource_detector.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace resource +{ + +/** + * ContainerResourceDetector to detect resource attributes when running inside a containerized + * environment. This detector extracts metadata such as container ID from cgroup information and + * sets attributes like container.id following the OpenTelemetry semantic conventions. + */ +class ContainerResourceDetector : public ResourceDetector +{ +public: + Resource Detect() noexcept override; +}; + +/** + * Reads the container.id from /proc/self/cgroup file. + * @param file_path file path of cgroup + * @return container.id as string or empty string + */ +std::string GetContainerIDFromCgroup(const char *file_path); + +/** + * Matches the line with the regex to find container.id + * @param line a single line of text, typically from the /proc/self/cgroup file + * @return matched id or empty string + */ +std::string ExtractContainerIDFromLine(const std::string &line); + +} // namespace resource +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/resource/resource_detector.h b/sdk/include/opentelemetry/sdk/resource/resource_detector.h index f4a3f126da..f129d3d1bc 100644 --- a/sdk/include/opentelemetry/sdk/resource/resource_detector.h +++ b/sdk/include/opentelemetry/sdk/resource/resource_detector.h @@ -39,17 +39,6 @@ class OTELResourceDetector : public ResourceDetector Resource Detect() noexcept override; }; -/** - * ContainerResourceDetector to detect resource attributes when running inside a containerized - * environment. This detector extracts metadata such as container ID from cgroup information and - * sets attributes like container.id following the OpenTelemetry semantic conventions. - */ -class ContainerResourceDetector : public ResourceDetector -{ -public: - Resource Detect() noexcept override; -}; - } // namespace resource } // namespace sdk OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/src/common/BUILD b/sdk/src/common/BUILD index 47981f169c..19b47034f7 100644 --- a/sdk/src/common/BUILD +++ b/sdk/src/common/BUILD @@ -57,17 +57,6 @@ cc_library( ], ) -cc_library( - name = "container", - srcs = [ - "container.cc", - ], - deps = [ - "//api", - "//sdk:headers", - ], -) - cc_library( name = "global_log_handler", srcs = [ diff --git a/sdk/src/common/CMakeLists.txt b/sdk/src/common/CMakeLists.txt index dcb33d1e5f..4a3b59aefa 100644 --- a/sdk/src/common/CMakeLists.txt +++ b/sdk/src/common/CMakeLists.txt @@ -1,8 +1,8 @@ # Copyright The OpenTelemetry Authors # SPDX-License-Identifier: Apache-2.0 -set(COMMON_SRCS random.cc global_log_handler.cc env_variables.cc container.cc - base64.cc disabled.cc) +set(COMMON_SRCS random.cc global_log_handler.cc env_variables.cc base64.cc + disabled.cc) if(WIN32) list(APPEND COMMON_SRCS platform/fork_windows.cc) else() diff --git a/sdk/src/resource/BUILD b/sdk/src/resource/BUILD index 3cef2c6338..8845629990 100644 --- a/sdk/src/resource/BUILD +++ b/sdk/src/resource/BUILD @@ -10,7 +10,6 @@ cc_library( deps = [ "//api", "//sdk:headers", - "//sdk/src/common:container", "//sdk/src/common:env_variables", ], ) diff --git a/sdk/src/resource/CMakeLists.txt b/sdk/src/resource/CMakeLists.txt index 91384fa88a..de4f47ac05 100644 --- a/sdk/src/resource/CMakeLists.txt +++ b/sdk/src/resource/CMakeLists.txt @@ -1,8 +1,8 @@ # Copyright The OpenTelemetry Authors # SPDX-License-Identifier: Apache-2.0 -add_library(opentelemetry_resources resource.cc resource_detector.cc - container_detector.cc) +add_library(opentelemetry_resources resource.cc resource_detector.cc + container.cc container_detector.cc) set_target_properties(opentelemetry_resources PROPERTIES EXPORT_NAME resources) set_target_version(opentelemetry_resources) diff --git a/sdk/src/common/container.cc b/sdk/src/resource/container.cc similarity index 88% rename from sdk/src/common/container.cc rename to sdk/src/resource/container.cc index 5a317ab4b9..0a7677b94c 100644 --- a/sdk/src/common/container.cc +++ b/sdk/src/resource/container.cc @@ -1,7 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -#include "opentelemetry/sdk/common/container.h" +#include "opentelemetry/sdk/resource/container_resource_detector.h" #include #include @@ -11,7 +11,7 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk { -namespace common +namespace resource { std::string GetContainerIDFromCgroup(const char *file_path) { @@ -41,6 +41,6 @@ std::string ExtractContainerIDFromLine(const std::string &line) return ""; } -} // namespace common +} // namespace resource } // namespace sdk OPENTELEMETRY_END_NAMESPACE \ No newline at end of file diff --git a/sdk/src/resource/container_detector.cc b/sdk/src/resource/container_detector.cc index f16cfd83e1..330f12a417 100644 --- a/sdk/src/resource/container_detector.cc +++ b/sdk/src/resource/container_detector.cc @@ -2,9 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 #include "opentelemetry/nostd/variant.h" -#include "opentelemetry/sdk/common/container.h" +#include "opentelemetry/sdk/resource/container_resource_detector.h" #include "opentelemetry/sdk/resource/resource.h" -#include "opentelemetry/sdk/resource/resource_detector.h" #include "opentelemetry/semconv/incubating/container_attributes.h" #include "opentelemetry/version.h" @@ -24,7 +23,7 @@ const char *KCGroupPath = "/proc/self/cgroup"; Resource ContainerResourceDetector::Detect() noexcept { - std::string container_id = opentelemetry::sdk::common::GetContainerIDFromCgroup(KCGroupPath); + std::string container_id = opentelemetry::sdk::resource::GetContainerIDFromCgroup(KCGroupPath); if (container_id.empty()) { return ResourceDetector::Create({}); diff --git a/sdk/src/resource/resource.cc b/sdk/src/resource/resource.cc index cfc900a6e1..6f67f0517c 100644 --- a/sdk/src/resource/resource.cc +++ b/sdk/src/resource/resource.cc @@ -6,6 +6,7 @@ #include #include "opentelemetry/nostd/variant.h" +#include "opentelemetry/sdk/resource/container_resource_detector.h" #include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/resource/resource_detector.h" #include "opentelemetry/sdk/version/version.h" diff --git a/sdk/test/resource/resource_test.cc b/sdk/test/resource/resource_test.cc index 48932eabcd..2cfac57a79 100644 --- a/sdk/test/resource/resource_test.cc +++ b/sdk/test/resource/resource_test.cc @@ -13,7 +13,7 @@ #include "opentelemetry/nostd/variant.h" #include "opentelemetry/sdk/common/attribute_utils.h" -#include "opentelemetry/sdk/common/container.h" +#include "opentelemetry/sdk/resource/container_resource_detector.h" #include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/resource/resource_detector.h" #include "opentelemetry/sdk/version/version.h" @@ -300,7 +300,7 @@ TEST(ResourceTest, ExtractValidContainerId) { std::string line = "13:name=systemd:/podruntime/docker/kubepods/ac679f8a8319c8cf7d38e1adf263bc08d23.aaaa"; - std::string extracted_id = opentelemetry::sdk::common::ExtractContainerIDFromLine(line); + std::string extracted_id = opentelemetry::sdk::resource::ExtractContainerIDFromLine(line); EXPECT_EQ(std::string{"ac679f8a8319c8cf7d38e1adf263bc08d23"}, extracted_id); } @@ -316,7 +316,7 @@ TEST(ResourceTest, ExtractIdFromMockUpCGroupFile) outfile << "9:cpu:/not-a-container\n"; } - std::string container_id = opentelemetry::sdk::common::GetContainerIDFromCgroup(filename); + std::string container_id = opentelemetry::sdk::resource::GetContainerIDFromCgroup(filename); EXPECT_EQ(container_id, std::string{"e857a4bf05a69080a759574949d7a0e69572e27647800fa7faff6a05a8332aa1"}); @@ -326,7 +326,7 @@ TEST(ResourceTest, ExtractIdFromMockUpCGroupFile) TEST(ResourceTest, DoesNotExtractInvalidLine) { std::string line = "this line does not contain a container id"; - std::string id = opentelemetry::sdk::common::ExtractContainerIDFromLine(line); + std::string id = opentelemetry::sdk::resource::ExtractContainerIDFromLine(line); EXPECT_EQ(id, std::string{""}); } @@ -339,7 +339,7 @@ TEST(ContainerIdDetectorTest, ReturnsEmptyOnNoMatch) outfile << "no container id here\n"; } - std::string id = opentelemetry::sdk::common::GetContainerIDFromCgroup(filename); + std::string id = opentelemetry::sdk::resource::GetContainerIDFromCgroup(filename); EXPECT_EQ(id, std::string{""}); std::remove(filename); // cleanup From edef26e5b3f7311db15f02f2d49ca4ec26deaf2d Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Sat, 2 Aug 2025 21:13:01 +0530 Subject: [PATCH 05/26] iwyu-fix --- sdk/src/resource/container.cc | 1 + sdk/src/resource/container_detector.cc | 1 + 2 files changed, 2 insertions(+) diff --git a/sdk/src/resource/container.cc b/sdk/src/resource/container.cc index 0a7677b94c..8b4ee269a4 100644 --- a/sdk/src/resource/container.cc +++ b/sdk/src/resource/container.cc @@ -5,6 +5,7 @@ #include #include +#include #include "opentelemetry/version.h" diff --git a/sdk/src/resource/container_detector.cc b/sdk/src/resource/container_detector.cc index 330f12a417..50fbc297bc 100644 --- a/sdk/src/resource/container_detector.cc +++ b/sdk/src/resource/container_detector.cc @@ -4,6 +4,7 @@ #include "opentelemetry/nostd/variant.h" #include "opentelemetry/sdk/resource/container_resource_detector.h" #include "opentelemetry/sdk/resource/resource.h" +#include "opentelemetry/sdk/resource/resource_detector.h" #include "opentelemetry/semconv/incubating/container_attributes.h" #include "opentelemetry/version.h" From 37429326b1d0b8b8ad5e7eb3202cdfecaf25f749 Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Sun, 3 Aug 2025 05:00:00 +0530 Subject: [PATCH 06/26] newline-fix --- sdk/src/resource/container.cc | 2 +- sdk/src/resource/container_detector.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/src/resource/container.cc b/sdk/src/resource/container.cc index 8b4ee269a4..a0b845e2cb 100644 --- a/sdk/src/resource/container.cc +++ b/sdk/src/resource/container.cc @@ -44,4 +44,4 @@ std::string ExtractContainerIDFromLine(const std::string &line) } } // namespace resource } // namespace sdk -OPENTELEMETRY_END_NAMESPACE \ No newline at end of file +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/src/resource/container_detector.cc b/sdk/src/resource/container_detector.cc index 50fbc297bc..9c9c238bad 100644 --- a/sdk/src/resource/container_detector.cc +++ b/sdk/src/resource/container_detector.cc @@ -38,4 +38,4 @@ Resource ContainerResourceDetector::Detect() noexcept } // namespace resource } // namespace sdk -OPENTELEMETRY_END_NAMESPACE \ No newline at end of file +OPENTELEMETRY_END_NAMESPACE From a4952948ece17f499ba409922149c5234e7ba4ce Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Sun, 3 Aug 2025 05:00:00 +0530 Subject: [PATCH 07/26] newline-fix hello changes some changes --- .github/workflows/ci.yml | 5 ++-- .github/workflows/iwyu.yml | 3 ++- CMakeLists.txt | 1 + resource_detectors/CMakeLists.txt | 23 +++++++++++++++++++ .../container.cc | 4 ++-- .../container_detector.cc | 4 ++-- .../container_resource_detector.h | 0 sdk/src/resource/CMakeLists.txt | 3 +-- sdk/src/resource/resource.cc | 10 +++----- sdk/test/resource/BUILD | 1 + sdk/test/resource/CMakeLists.txt | 5 ++-- sdk/test/resource/resource_test.cc | 2 +- 12 files changed, 42 insertions(+), 19 deletions(-) create mode 100644 resource_detectors/CMakeLists.txt rename {sdk/src/resource => resource_detectors}/container.cc (89%) rename {sdk/src/resource => resource_detectors}/container_detector.cc (90%) rename {sdk/include/opentelemetry/sdk/resource => resource_detectors/include/opentelemetry/resource_detectors}/container_resource_detector.h (100%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 33078816f6..19116dad76 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,9 +2,10 @@ name: CI on: push: - branches: [ main ] + branches: [ main, nb ] pull_request: - branches: [ main ] + branches: [ main, nb ] + workflow_dispatch: permissions: contents: read diff --git a/.github/workflows/iwyu.yml b/.github/workflows/iwyu.yml index 76c9c93bc7..5bced6a74c 100644 --- a/.github/workflows/iwyu.yml +++ b/.github/workflows/iwyu.yml @@ -3,9 +3,10 @@ name: include-what-you-use on: push: - branches: [ main ] + branches: [ main, nb ] pull_request: branches: [ main ] + workflow_dispatch: permissions: contents: read diff --git a/CMakeLists.txt b/CMakeLists.txt index 005717c46d..e4424fc5e9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -634,6 +634,7 @@ if(DEFINED OPENTELEMETRY_BUILD_DLL) endif() add_subdirectory(api) +add_subdirectory(resource_detectors) if(WITH_OPENTRACING) add_subdirectory(opentracing-shim) diff --git a/resource_detectors/CMakeLists.txt b/resource_detectors/CMakeLists.txt new file mode 100644 index 0000000000..8e1c832fb7 --- /dev/null +++ b/resource_detectors/CMakeLists.txt @@ -0,0 +1,23 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +add_library(opentelemetry_resource_detectors container.cc container_detector.cc) + +set_target_properties(opentelemetry_resource_detectors + PROPERTIES EXPORT_NAME resource_detectors) +set_target_version(opentelemetry_resource_detectors) + +target_link_libraries(opentelemetry_resource_detectors + PUBLIC opentelemetry_common) + +target_include_directories( + opentelemetry_resource_detectors + PUBLIC "$" + PUBLIC "$") + +if(OPENTELEMETRY_INSTALL) + opentelemetry_add_pkgconfig( + resource_detectors "OpenTelemetry - Resource detectors" + "Components for resource detection in the OpenTelemetry resource detectors." + ) +endif() diff --git a/sdk/src/resource/container.cc b/resource_detectors/container.cc similarity index 89% rename from sdk/src/resource/container.cc rename to resource_detectors/container.cc index 8b4ee269a4..7e69b818a0 100644 --- a/sdk/src/resource/container.cc +++ b/resource_detectors/container.cc @@ -1,7 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -#include "opentelemetry/sdk/resource/container_resource_detector.h" +#include "opentelemetry/resource_detectors/container_resource_detector.h" #include #include @@ -44,4 +44,4 @@ std::string ExtractContainerIDFromLine(const std::string &line) } } // namespace resource } // namespace sdk -OPENTELEMETRY_END_NAMESPACE \ No newline at end of file +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/src/resource/container_detector.cc b/resource_detectors/container_detector.cc similarity index 90% rename from sdk/src/resource/container_detector.cc rename to resource_detectors/container_detector.cc index 50fbc297bc..cd8ec0f323 100644 --- a/sdk/src/resource/container_detector.cc +++ b/resource_detectors/container_detector.cc @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 #include "opentelemetry/nostd/variant.h" -#include "opentelemetry/sdk/resource/container_resource_detector.h" +#include "opentelemetry/resource_detectors/container_resource_detector.h" #include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/resource/resource_detector.h" #include "opentelemetry/semconv/incubating/container_attributes.h" @@ -38,4 +38,4 @@ Resource ContainerResourceDetector::Detect() noexcept } // namespace resource } // namespace sdk -OPENTELEMETRY_END_NAMESPACE \ No newline at end of file +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/resource/container_resource_detector.h b/resource_detectors/include/opentelemetry/resource_detectors/container_resource_detector.h similarity index 100% rename from sdk/include/opentelemetry/sdk/resource/container_resource_detector.h rename to resource_detectors/include/opentelemetry/resource_detectors/container_resource_detector.h diff --git a/sdk/src/resource/CMakeLists.txt b/sdk/src/resource/CMakeLists.txt index de4f47ac05..48b647ec41 100644 --- a/sdk/src/resource/CMakeLists.txt +++ b/sdk/src/resource/CMakeLists.txt @@ -1,8 +1,7 @@ # Copyright The OpenTelemetry Authors # SPDX-License-Identifier: Apache-2.0 -add_library(opentelemetry_resources resource.cc resource_detector.cc - container.cc container_detector.cc) +add_library(opentelemetry_resources resource.cc resource_detector.cc) set_target_properties(opentelemetry_resources PROPERTIES EXPORT_NAME resources) set_target_version(opentelemetry_resources) diff --git a/sdk/src/resource/resource.cc b/sdk/src/resource/resource.cc index 6f67f0517c..95b41498f8 100644 --- a/sdk/src/resource/resource.cc +++ b/sdk/src/resource/resource.cc @@ -6,7 +6,6 @@ #include #include "opentelemetry/nostd/variant.h" -#include "opentelemetry/sdk/resource/container_resource_detector.h" #include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/resource/resource_detector.h" #include "opentelemetry/sdk/version/version.h" @@ -41,12 +40,9 @@ Resource Resource::Merge(const Resource &other) const noexcept Resource Resource::Create(const ResourceAttributes &attributes, const std::string &schema_url) { - static auto otel_resource = OTELResourceDetector().Detect(); - static auto container_resource = ContainerResourceDetector().Detect(); - auto resource = Resource::GetDefault() - .Merge(otel_resource) - .Merge(container_resource) - .Merge(Resource{attributes, schema_url}); + static auto otel_resource = OTELResourceDetector().Detect(); + auto resource = + Resource::GetDefault().Merge(otel_resource).Merge(Resource{attributes, schema_url}); if (resource.attributes_.find(semconv::service::kServiceName) == resource.attributes_.end()) { diff --git a/sdk/test/resource/BUILD b/sdk/test/resource/BUILD index 145ecf0b56..4f403a20c5 100644 --- a/sdk/test/resource/BUILD +++ b/sdk/test/resource/BUILD @@ -9,6 +9,7 @@ cc_test( tags = ["test"], deps = [ "//api", + "//resource_detectors", "//sdk/src/resource", "@com_google_googletest//:gtest_main", ], diff --git a/sdk/test/resource/CMakeLists.txt b/sdk/test/resource/CMakeLists.txt index dfe78589f8..b606e94fad 100644 --- a/sdk/test/resource/CMakeLists.txt +++ b/sdk/test/resource/CMakeLists.txt @@ -3,8 +3,9 @@ foreach(testname resource_test) add_executable(${testname} "${testname}.cc") - target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES} - ${CMAKE_THREAD_LIBS_INIT} opentelemetry_resources) + target_link_libraries( + ${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} + opentelemetry_resources opentelemetry_resource_detectors) gtest_add_tests( TARGET ${testname} TEST_PREFIX resources. diff --git a/sdk/test/resource/resource_test.cc b/sdk/test/resource/resource_test.cc index 2cfac57a79..74463b4dee 100644 --- a/sdk/test/resource/resource_test.cc +++ b/sdk/test/resource/resource_test.cc @@ -12,8 +12,8 @@ #include #include "opentelemetry/nostd/variant.h" +#include "opentelemetry/resource_detectors/container_resource_detector.h" #include "opentelemetry/sdk/common/attribute_utils.h" -#include "opentelemetry/sdk/resource/container_resource_detector.h" #include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/resource/resource_detector.h" #include "opentelemetry/sdk/version/version.h" From f7b5e94bb9455d5348d3468869689fe4cc824ce5 Mon Sep 17 00:00:00 2001 From: WenTao Ou Date: Tue, 29 Jul 2025 18:05:22 +0800 Subject: [PATCH 08/26] [EXPORTER] Fixes tsan warnings (#3531) --- .../http/client/curl/http_operation_curl.h | 1 + .../http/client/curl/http_operation_curl.cc | 30 ++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/ext/include/opentelemetry/ext/http/client/curl/http_operation_curl.h b/ext/include/opentelemetry/ext/http/client/curl/http_operation_curl.h index d180134cae..76879fc1df 100644 --- a/ext/include/opentelemetry/ext/http/client/curl/http_operation_curl.h +++ b/ext/include/opentelemetry/ext/http/client/curl/http_operation_curl.h @@ -338,6 +338,7 @@ class HttpOperation std::promise result_promise; std::future result_future; }; + friend class HttpOperationAccessor; std::unique_ptr async_data_; }; } // namespace curl diff --git a/ext/src/http/client/curl/http_operation_curl.cc b/ext/src/http/client/curl/http_operation_curl.cc index 042ae4fe63..3fd27c9634 100644 --- a/ext/src/http/client/curl/http_operation_curl.cc +++ b/ext/src/http/client/curl/http_operation_curl.cc @@ -47,6 +47,28 @@ namespace client namespace curl { +class HttpOperationAccessor +{ +public: + OPENTELEMETRY_SANITIZER_NO_THREAD static std::thread::id GetThreadId( + const HttpOperation::AsyncData &async_data) + { +#if !(defined(OPENTELEMETRY_HAVE_THREAD_SANITIZER) && OPENTELEMETRY_HAVE_THREAD_SANITIZER) + std::atomic_thread_fence(std::memory_order_acquire); +#endif + return async_data.callback_thread; + } + + OPENTELEMETRY_SANITIZER_NO_THREAD static void SetThreadId(HttpOperation::AsyncData &async_data, + std::thread::id thread_id) + { + async_data.callback_thread = thread_id; +#if !(defined(OPENTELEMETRY_HAVE_THREAD_SANITIZER) && OPENTELEMETRY_HAVE_THREAD_SANITIZER) + std::atomic_thread_fence(std::memory_order_release); +#endif + } +}; + size_t HttpOperation::WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) { HttpOperation *self = reinterpret_cast(userp); @@ -335,7 +357,7 @@ HttpOperation::~HttpOperation() case opentelemetry::ext::http::client::SessionState::Sending: { if (async_data_ && async_data_->result_future.valid()) { - if (async_data_->callback_thread != std::this_thread::get_id()) + if (HttpOperationAccessor::GetThreadId(*async_data_) != std::this_thread::get_id()) { async_data_->result_future.wait(); last_curl_result_ = async_data_->result_future.get(); @@ -360,7 +382,7 @@ void HttpOperation::Finish() if (async_data_ && async_data_->result_future.valid()) { // We should not wait in callback from Cleanup() - if (async_data_->callback_thread != std::this_thread::get_id()) + if (HttpOperationAccessor::GetThreadId(*async_data_) != std::this_thread::get_id()) { async_data_->result_future.wait(); last_curl_result_ = async_data_->result_future.get(); @@ -412,9 +434,9 @@ void HttpOperation::Cleanup() callback.swap(async_data_->callback); if (callback) { - async_data_->callback_thread = std::this_thread::get_id(); + HttpOperationAccessor::SetThreadId(*async_data_, std::this_thread::get_id()); callback(*this); - async_data_->callback_thread = std::thread::id(); + HttpOperationAccessor::SetThreadId(*async_data_, std::thread::id()); } // Set value to promise to continue Finish() From a836349d6ff7732dd909f07745927b6a583b3d16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Tue, 29 Jul 2025 17:32:00 +0300 Subject: [PATCH 09/26] [DOC] Document minimum required versions (#3562) --- docs/dependencies.md | 28 +++++++++++++++++----------- install/cmake/third_party_minimum | 2 ++ 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/docs/dependencies.md b/docs/dependencies.md index 8d19b6490b..78dbc4bb4d 100644 --- a/docs/dependencies.md +++ b/docs/dependencies.md @@ -6,6 +6,11 @@ There are - Internal dependencies as the part of code from external libraries backported/copied in main repo. +The minimum required versions of the third-party libraries that are required can +be found in the [third_party_minimum](/install/cmake/third_party_minimum) file. +The minimum supported versions are listed here for convenience but the +authoritative source is always the linked file. + Both these dependencies are listed here: ## Internal dependencies @@ -37,8 +42,8 @@ Both these dependencies are listed here: - [OTLP/HTTP+JSON](/exporters/otlp) exporter: - - [protobuf](https://github.com/protocolbuffers/protobuf): Library to - serialize structured data. + - [protobuf](https://github.com/protocolbuffers/protobuf) (v3.21.6 or later): + Library to serialize structured data. - OTLP messages are constructed as protobuf payloads. - `protoc` compiler is used to generate C++ stubs for proto files provided by `opentelemetry-proto`. @@ -48,18 +53,19 @@ Both these dependencies are listed here: [here](https://github.com/protocolbuffers/protobuf/blob/master/LICENSE). The code generated by protoc compiler is owned by the owner of `.proto` file. - - [libcurl](https://curl.se/libcurl/) : the multiprotocol file transfer - library. + - [libcurl](https://curl.se/libcurl/) (curl-7_81_0 or later): + the multiprotocol file transfer library. - Export connects with opentelemetry collector over HTTP protocol using libcurl library - License: Inspired by `MIT/X` but not same. - - [nlohmann/json](https://github.com/nlohmann/json): JSON for Modern C++. + - [nlohmann/json](https://github.com/nlohmann/json) (v3.10.5 or later): + JSON for Modern C++. - protobuf serialized otlp messages are encoded in JSON format using this library. - License: `MIT License` - - [zlib](https://www.zlib.net/): A Massively Spiffy Yet Delicately - Unobtrusive Compression Library. + - [zlib](https://www.zlib.net/) (v1.2.11 or later): + A Massively Spiffy Yet Delicately Unobtrusive Compression Library. - The `http_client` utilizes zlib to compress the message body and send it in gzip format. - License: The library is licensed @@ -68,7 +74,7 @@ Both these dependencies are listed here: - [OTLP/gRPC](/exporters/otlp) exporter: - `protobuf` OTLP messages are constructed as protobuf payloads. - - [gRPC](https://github.com/grpc/grpc): An RPC library and framework + - [gRPC](https://github.com/grpc/grpc) (v1.49.2 or later): An RPC library and framework - Exporter communicates with OTLP collector using gRPC transport mechanism. - License: `Apache License 2.0` @@ -84,8 +90,8 @@ Both these dependencies are listed here: - [Prometheus](/exporters/prometheus) exporter: - - [`prometheus-cpp`](https://github.com/jupp0r/prometheus-cpp) Prometheus - Client Library for Modern C++ + - [`prometheus-cpp`](https://github.com/jupp0r/prometheus-cpp) + (v1.1.0 or later): Prometheus Client Library for Modern C++ - License: `MIT License` - [ElasticSearch](/exporters/elasticsearch) @@ -96,6 +102,6 @@ Both these dependencies are listed here: - [Opentracing](/opentracing-shim) shim: - [`opentracing-cpp`](https://github.com/opentracing/opentracing-cpp) - OpenTracing API for C++ + (v1.6.0 or later): OpenTracing API for C++ - A bridge layer implementing the OpenTracing API using the OpenTelemetry API - License: `Apache License 2.0` diff --git a/install/cmake/third_party_minimum b/install/cmake/third_party_minimum index 004c8e597a..15470c56ac 100644 --- a/install/cmake/third_party_minimum +++ b/install/cmake/third_party_minimum @@ -4,6 +4,8 @@ # Minimum supported version git tags of third-party dependencies # Format: = +# Update docs/dependencies.md whenever the minimum version of a library is modified. + abseil=20220623.2 zlib=v1.2.11 curl=curl-7_81_0 From 5ec872c315d73a2e363d361783b79458787939d7 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 30 Jul 2025 07:43:34 +0200 Subject: [PATCH 10/26] Bump github/codeql-action from 3.29.4 to 3.29.5 (#3574) Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.29.4 to 3.29.5. - [Release notes](https://github.com/github/codeql-action/releases) - [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/github/codeql-action/compare/4e828ff8d448a8a6e532957b1811f387a63867e8...51f77329afa6477de8c49fc9c7046c15b9a4e79d) --- updated-dependencies: - dependency-name: github/codeql-action dependency-version: 3.29.5 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/codeql-analysis.yml | 6 +++--- .github/workflows/ossf-scorecard.yml | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index f0774c0494..c81ce7e5f3 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -37,10 +37,10 @@ jobs: run: | sudo -E ./ci/setup_ci_environment.sh - name: Initialize CodeQL - uses: github/codeql-action/init@4e828ff8d448a8a6e532957b1811f387a63867e8 # v3.29.4 + uses: github/codeql-action/init@51f77329afa6477de8c49fc9c7046c15b9a4e79d # v3.29.5 with: languages: cpp - name: Autobuild - uses: github/codeql-action/autobuild@4e828ff8d448a8a6e532957b1811f387a63867e8 # v3.29.4 + uses: github/codeql-action/autobuild@51f77329afa6477de8c49fc9c7046c15b9a4e79d # v3.29.5 - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@4e828ff8d448a8a6e532957b1811f387a63867e8 # v3.29.4 + uses: github/codeql-action/analyze@51f77329afa6477de8c49fc9c7046c15b9a4e79d # v3.29.5 diff --git a/.github/workflows/ossf-scorecard.yml b/.github/workflows/ossf-scorecard.yml index 35b39ec0cb..55a846d9c3 100644 --- a/.github/workflows/ossf-scorecard.yml +++ b/.github/workflows/ossf-scorecard.yml @@ -47,6 +47,6 @@ jobs: # Upload the results to GitHub's code scanning dashboard (optional). # Commenting out will disable upload of results to your repo's Code Scanning dashboard - name: "Upload to code-scanning" - uses: github/codeql-action/upload-sarif@4e828ff8d448a8a6e532957b1811f387a63867e8 # v3.29.4 + uses: github/codeql-action/upload-sarif@51f77329afa6477de8c49fc9c7046c15b9a4e79d # v3.29.5 with: sarif_file: results.sarif From 81624a34712056e3c1cb07c139bf9372052943c6 Mon Sep 17 00:00:00 2001 From: OpenTelemetry Bot <107717825+opentelemetrybot@users.noreply.github.com> Date: Fri, 1 Aug 2025 09:15:10 -0700 Subject: [PATCH 11/26] Add subscript to issue templates (#3576) Co-authored-by: otelbot <197425009+otelbot@users.noreply.github.com> --- .github/ISSUE_TEMPLATE/bug_report.md | 2 ++ .github/ISSUE_TEMPLATE/feature_request.md | 2 ++ 2 files changed, 4 insertions(+) diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index e0bb4cca27..7f45b9f5b0 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -17,3 +17,5 @@ What did you see instead? **Additional context** Add any other context about the problem here. + +**Tip**: [React](https://github.blog/news-insights/product-news/add-reactions-to-pull-requests-issues-and-comments/) with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding `+1` or `me too`, to help us triage it. Learn more [here](https://opentelemetry.io/community/end-user/issue-participation/). diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md index 973549ab2d..95ec00965b 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.md +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -17,3 +17,5 @@ Which alternative solutions or features have you considered? **Additional context** Add any other context about the feature request here. + +**Tip**: [React](https://github.blog/news-insights/product-news/add-reactions-to-pull-requests-issues-and-comments/) with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding `+1` or `me too`, to help us triage it. Learn more [here](https://opentelemetry.io/community/end-user/issue-participation/). From 4cebe359a058a30a5e0fb596475d581dcceac212 Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Sat, 2 Aug 2025 21:13:01 +0530 Subject: [PATCH 12/26] iwyu-fix --- sdk/src/resource/container.cc | 1 + sdk/src/resource/container_detector.cc | 1 + 2 files changed, 2 insertions(+) diff --git a/sdk/src/resource/container.cc b/sdk/src/resource/container.cc index 0a7677b94c..8b4ee269a4 100644 --- a/sdk/src/resource/container.cc +++ b/sdk/src/resource/container.cc @@ -5,6 +5,7 @@ #include #include +#include #include "opentelemetry/version.h" diff --git a/sdk/src/resource/container_detector.cc b/sdk/src/resource/container_detector.cc index 330f12a417..50fbc297bc 100644 --- a/sdk/src/resource/container_detector.cc +++ b/sdk/src/resource/container_detector.cc @@ -4,6 +4,7 @@ #include "opentelemetry/nostd/variant.h" #include "opentelemetry/sdk/resource/container_resource_detector.h" #include "opentelemetry/sdk/resource/resource.h" +#include "opentelemetry/sdk/resource/resource_detector.h" #include "opentelemetry/semconv/incubating/container_attributes.h" #include "opentelemetry/version.h" From a1778b9aaeca202d2e6022e667b46da369d368e0 Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Sun, 3 Aug 2025 05:00:00 +0530 Subject: [PATCH 13/26] newline-fix hello changes some changes --- .github/workflows/ci.yml | 5 ++-- .github/workflows/iwyu.yml | 3 ++- CMakeLists.txt | 1 + resource_detectors/CMakeLists.txt | 23 +++++++++++++++++++ .../container.cc | 4 ++-- .../container_detector.cc | 4 ++-- .../container_resource_detector.h | 0 sdk/src/resource/CMakeLists.txt | 3 +-- sdk/src/resource/resource.cc | 10 +++----- sdk/test/resource/BUILD | 1 + sdk/test/resource/CMakeLists.txt | 5 ++-- sdk/test/resource/resource_test.cc | 2 +- 12 files changed, 42 insertions(+), 19 deletions(-) create mode 100644 resource_detectors/CMakeLists.txt rename {sdk/src/resource => resource_detectors}/container.cc (89%) rename {sdk/src/resource => resource_detectors}/container_detector.cc (90%) rename {sdk/include/opentelemetry/sdk/resource => resource_detectors/include/opentelemetry/resource_detectors}/container_resource_detector.h (100%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 33078816f6..19116dad76 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,9 +2,10 @@ name: CI on: push: - branches: [ main ] + branches: [ main, nb ] pull_request: - branches: [ main ] + branches: [ main, nb ] + workflow_dispatch: permissions: contents: read diff --git a/.github/workflows/iwyu.yml b/.github/workflows/iwyu.yml index 76c9c93bc7..5bced6a74c 100644 --- a/.github/workflows/iwyu.yml +++ b/.github/workflows/iwyu.yml @@ -3,9 +3,10 @@ name: include-what-you-use on: push: - branches: [ main ] + branches: [ main, nb ] pull_request: branches: [ main ] + workflow_dispatch: permissions: contents: read diff --git a/CMakeLists.txt b/CMakeLists.txt index 005717c46d..e4424fc5e9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -634,6 +634,7 @@ if(DEFINED OPENTELEMETRY_BUILD_DLL) endif() add_subdirectory(api) +add_subdirectory(resource_detectors) if(WITH_OPENTRACING) add_subdirectory(opentracing-shim) diff --git a/resource_detectors/CMakeLists.txt b/resource_detectors/CMakeLists.txt new file mode 100644 index 0000000000..8e1c832fb7 --- /dev/null +++ b/resource_detectors/CMakeLists.txt @@ -0,0 +1,23 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +add_library(opentelemetry_resource_detectors container.cc container_detector.cc) + +set_target_properties(opentelemetry_resource_detectors + PROPERTIES EXPORT_NAME resource_detectors) +set_target_version(opentelemetry_resource_detectors) + +target_link_libraries(opentelemetry_resource_detectors + PUBLIC opentelemetry_common) + +target_include_directories( + opentelemetry_resource_detectors + PUBLIC "$" + PUBLIC "$") + +if(OPENTELEMETRY_INSTALL) + opentelemetry_add_pkgconfig( + resource_detectors "OpenTelemetry - Resource detectors" + "Components for resource detection in the OpenTelemetry resource detectors." + ) +endif() diff --git a/sdk/src/resource/container.cc b/resource_detectors/container.cc similarity index 89% rename from sdk/src/resource/container.cc rename to resource_detectors/container.cc index 8b4ee269a4..7e69b818a0 100644 --- a/sdk/src/resource/container.cc +++ b/resource_detectors/container.cc @@ -1,7 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -#include "opentelemetry/sdk/resource/container_resource_detector.h" +#include "opentelemetry/resource_detectors/container_resource_detector.h" #include #include @@ -44,4 +44,4 @@ std::string ExtractContainerIDFromLine(const std::string &line) } } // namespace resource } // namespace sdk -OPENTELEMETRY_END_NAMESPACE \ No newline at end of file +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/src/resource/container_detector.cc b/resource_detectors/container_detector.cc similarity index 90% rename from sdk/src/resource/container_detector.cc rename to resource_detectors/container_detector.cc index 50fbc297bc..cd8ec0f323 100644 --- a/sdk/src/resource/container_detector.cc +++ b/resource_detectors/container_detector.cc @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 #include "opentelemetry/nostd/variant.h" -#include "opentelemetry/sdk/resource/container_resource_detector.h" +#include "opentelemetry/resource_detectors/container_resource_detector.h" #include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/resource/resource_detector.h" #include "opentelemetry/semconv/incubating/container_attributes.h" @@ -38,4 +38,4 @@ Resource ContainerResourceDetector::Detect() noexcept } // namespace resource } // namespace sdk -OPENTELEMETRY_END_NAMESPACE \ No newline at end of file +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/resource/container_resource_detector.h b/resource_detectors/include/opentelemetry/resource_detectors/container_resource_detector.h similarity index 100% rename from sdk/include/opentelemetry/sdk/resource/container_resource_detector.h rename to resource_detectors/include/opentelemetry/resource_detectors/container_resource_detector.h diff --git a/sdk/src/resource/CMakeLists.txt b/sdk/src/resource/CMakeLists.txt index de4f47ac05..48b647ec41 100644 --- a/sdk/src/resource/CMakeLists.txt +++ b/sdk/src/resource/CMakeLists.txt @@ -1,8 +1,7 @@ # Copyright The OpenTelemetry Authors # SPDX-License-Identifier: Apache-2.0 -add_library(opentelemetry_resources resource.cc resource_detector.cc - container.cc container_detector.cc) +add_library(opentelemetry_resources resource.cc resource_detector.cc) set_target_properties(opentelemetry_resources PROPERTIES EXPORT_NAME resources) set_target_version(opentelemetry_resources) diff --git a/sdk/src/resource/resource.cc b/sdk/src/resource/resource.cc index 6f67f0517c..95b41498f8 100644 --- a/sdk/src/resource/resource.cc +++ b/sdk/src/resource/resource.cc @@ -6,7 +6,6 @@ #include #include "opentelemetry/nostd/variant.h" -#include "opentelemetry/sdk/resource/container_resource_detector.h" #include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/resource/resource_detector.h" #include "opentelemetry/sdk/version/version.h" @@ -41,12 +40,9 @@ Resource Resource::Merge(const Resource &other) const noexcept Resource Resource::Create(const ResourceAttributes &attributes, const std::string &schema_url) { - static auto otel_resource = OTELResourceDetector().Detect(); - static auto container_resource = ContainerResourceDetector().Detect(); - auto resource = Resource::GetDefault() - .Merge(otel_resource) - .Merge(container_resource) - .Merge(Resource{attributes, schema_url}); + static auto otel_resource = OTELResourceDetector().Detect(); + auto resource = + Resource::GetDefault().Merge(otel_resource).Merge(Resource{attributes, schema_url}); if (resource.attributes_.find(semconv::service::kServiceName) == resource.attributes_.end()) { diff --git a/sdk/test/resource/BUILD b/sdk/test/resource/BUILD index 145ecf0b56..4f403a20c5 100644 --- a/sdk/test/resource/BUILD +++ b/sdk/test/resource/BUILD @@ -9,6 +9,7 @@ cc_test( tags = ["test"], deps = [ "//api", + "//resource_detectors", "//sdk/src/resource", "@com_google_googletest//:gtest_main", ], diff --git a/sdk/test/resource/CMakeLists.txt b/sdk/test/resource/CMakeLists.txt index dfe78589f8..b606e94fad 100644 --- a/sdk/test/resource/CMakeLists.txt +++ b/sdk/test/resource/CMakeLists.txt @@ -3,8 +3,9 @@ foreach(testname resource_test) add_executable(${testname} "${testname}.cc") - target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES} - ${CMAKE_THREAD_LIBS_INIT} opentelemetry_resources) + target_link_libraries( + ${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} + opentelemetry_resources opentelemetry_resource_detectors) gtest_add_tests( TARGET ${testname} TEST_PREFIX resources. diff --git a/sdk/test/resource/resource_test.cc b/sdk/test/resource/resource_test.cc index 2cfac57a79..74463b4dee 100644 --- a/sdk/test/resource/resource_test.cc +++ b/sdk/test/resource/resource_test.cc @@ -12,8 +12,8 @@ #include #include "opentelemetry/nostd/variant.h" +#include "opentelemetry/resource_detectors/container_resource_detector.h" #include "opentelemetry/sdk/common/attribute_utils.h" -#include "opentelemetry/sdk/resource/container_resource_detector.h" #include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/resource/resource_detector.h" #include "opentelemetry/sdk/version/version.h" From 926517f3f3e781ac133e0d1226f4b0cdf1f3520b Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Sun, 3 Aug 2025 05:00:00 +0530 Subject: [PATCH 14/26] Restore ci.yml to match main --- .github/workflows/ci.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 19116dad76..33078816f6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,10 +2,9 @@ name: CI on: push: - branches: [ main, nb ] + branches: [ main ] pull_request: - branches: [ main, nb ] - workflow_dispatch: + branches: [ main ] permissions: contents: read From 25466618cf3c8d0095ad905707f2617330590b2a Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Thu, 7 Aug 2025 16:08:12 +0530 Subject: [PATCH 15/26] new folder for resource detectors --- .github/workflows/ci.yml | 5 ++--- .github/workflows/iwyu.yml | 3 +-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 19116dad76..33078816f6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,10 +2,9 @@ name: CI on: push: - branches: [ main, nb ] + branches: [ main ] pull_request: - branches: [ main, nb ] - workflow_dispatch: + branches: [ main ] permissions: contents: read diff --git a/.github/workflows/iwyu.yml b/.github/workflows/iwyu.yml index 5bced6a74c..76c9c93bc7 100644 --- a/.github/workflows/iwyu.yml +++ b/.github/workflows/iwyu.yml @@ -3,10 +3,9 @@ name: include-what-you-use on: push: - branches: [ main, nb ] + branches: [ main ] pull_request: branches: [ main ] - workflow_dispatch: permissions: contents: read From 75c495cec088eb19098923edd6111a4991dd97b4 Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Thu, 7 Aug 2025 17:47:00 +0530 Subject: [PATCH 16/26] BUILD file for resource_detectors --- resource_detectors/BUILD | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 resource_detectors/BUILD diff --git a/resource_detectors/BUILD b/resource_detectors/BUILD new file mode 100644 index 0000000000..d56a537ba1 --- /dev/null +++ b/resource_detectors/BUILD @@ -0,0 +1,17 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +package(default_visibility = ["//visibility:public"]) + +cc_library( + name = "resource_detectors", + srcs = glob(["**/*.cc"]), + hdrs = glob(["include/opentelemetry/resource_detectors/*.h"]), + include_prefix = "opentelemetry/resource_detectors", + strip_include_prefix = "include", + deps = [ + "//api", + "//sdk:headers", + "//sdk/src/resource", + ], +) From 220a5adc1363924f8ed7ccdb5889bec925e8bc04 Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Thu, 7 Aug 2025 18:58:53 +0530 Subject: [PATCH 17/26] BUILD file(resource_detectors) minor changes --- resource_detectors/BUILD | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/resource_detectors/BUILD b/resource_detectors/BUILD index d56a537ba1..adee772f0e 100644 --- a/resource_detectors/BUILD +++ b/resource_detectors/BUILD @@ -4,14 +4,21 @@ package(default_visibility = ["//visibility:public"]) cc_library( - name = "resource_detectors", - srcs = glob(["**/*.cc"]), - hdrs = glob(["include/opentelemetry/resource_detectors/*.h"]), - include_prefix = "opentelemetry/resource_detectors", + name = "headers", + hdrs = glob(["include/**/*.h"]), strip_include_prefix = "include", +) + +cc_library( + name = "resource_detectors", + srcs = [ + "container.cc", + "container_detector.cc", + ], deps = [ "//api", + "//resource_detectors:headers", "//sdk:headers", - "//sdk/src/resource", + "//sdk/src/resource", ], ) From 439e92cd43ff4a2dd8319b0e26fcff4901445a8e Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Fri, 8 Aug 2025 00:01:43 +0530 Subject: [PATCH 18/26] added comments and changes as required --- resource_detectors/container.cc | 22 ++++++++++++++----- resource_detectors/container_detector.cc | 4 ++-- .../container_resource_detector.h | 2 +- sdk/src/resource/resource_detector.cc | 4 ++-- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/resource_detectors/container.cc b/resource_detectors/container.cc index 7e69b818a0..4b2e758560 100644 --- a/resource_detectors/container.cc +++ b/resource_detectors/container.cc @@ -27,20 +27,30 @@ std::string GetContainerIDFromCgroup(const char *file_path) return container_id; } } - return ""; + return std::string(); } -std::string ExtractContainerIDFromLine(const std::string &line) +std::string ExtractContainerIDFromLine(std::string_view line) { + /** + * This regex is designed to extract container IDs from cgroup file lines. + * It matches hexadecimal container IDs used by container runtimes like Docker, containerd, and + * cri-o. + * Examples of matching lines: + * - 0::/docker/3fae9b2c6d7e8f90123456789abcdef0123456789abcdef0123456789abcdef0 + * - "13:name=systemd:/podruntime/docker/kubepods/ac679f8a8319c8cf7d38e1adf263bc08d23.aaaa" + * - "e857a4bf05a69080a759574949d7a0e69572e27647800fa7faff6a05a8332aa1" + * Please see the test cases in resource_test.cc for more examples. + */ static const std::regex container_id_regex(R"(^.*/(?:.*[-:])?([0-9a-f]+)(?:\.|\s*$))"); - std::smatch match; + std::match_results match; - if (std::regex_search(line, match, container_id_regex)) + if (std::regex_search(line.begin(), line.end(), match, container_id_regex)) { - return match.str(1); + return std::string(match[1].first, match[1].second); } - return ""; + return std::string(); } } // namespace resource } // namespace sdk diff --git a/resource_detectors/container_detector.cc b/resource_detectors/container_detector.cc index cd8ec0f323..abbf11e8f1 100644 --- a/resource_detectors/container_detector.cc +++ b/resource_detectors/container_detector.cc @@ -20,7 +20,7 @@ namespace resource /** * This is the file path from where we can get container.id */ -const char *KCGroupPath = "/proc/self/cgroup"; +constexpr const char *KCGroupPath = "/proc/self/cgroup"; Resource ContainerResourceDetector::Detect() noexcept { @@ -32,7 +32,7 @@ Resource ContainerResourceDetector::Detect() noexcept ResourceAttributes attributes; - attributes[semconv::container::kContainerId] = container_id; + attributes[semconv::container::kContainerId] = std::move(container_id); return ResourceDetector::Create(attributes); } diff --git a/resource_detectors/include/opentelemetry/resource_detectors/container_resource_detector.h b/resource_detectors/include/opentelemetry/resource_detectors/container_resource_detector.h index 386b9fc2da..6b21acf44c 100644 --- a/resource_detectors/include/opentelemetry/resource_detectors/container_resource_detector.h +++ b/resource_detectors/include/opentelemetry/resource_detectors/container_resource_detector.h @@ -36,7 +36,7 @@ std::string GetContainerIDFromCgroup(const char *file_path); * @param line a single line of text, typically from the /proc/self/cgroup file * @return matched id or empty string */ -std::string ExtractContainerIDFromLine(const std::string &line); +std::string ExtractContainerIDFromLine(std::string_view line); } // namespace resource } // namespace sdk diff --git a/sdk/src/resource/resource_detector.cc b/sdk/src/resource/resource_detector.cc index 4c5dcd7c0f..089697476c 100644 --- a/sdk/src/resource/resource_detector.cc +++ b/sdk/src/resource/resource_detector.cc @@ -19,8 +19,8 @@ namespace sdk namespace resource { -const char *KOtelResourceAttributes = "OTEL_RESOURCE_ATTRIBUTES"; -const char *KOtelServiceName = "OTEL_SERVICE_NAME"; +constexpr const char *KOtelResourceAttributes = "OTEL_RESOURCE_ATTRIBUTES"; +constexpr const char *KOtelServiceName = "OTEL_SERVICE_NAME"; Resource ResourceDetector::Create(const ResourceAttributes &attributes, const std::string &schema_url) From 9e5bc5554bfd0d19548331436cc6d5b961ec2ecd Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Fri, 8 Aug 2025 01:08:16 +0530 Subject: [PATCH 19/26] changed to nostd::string_view --- resource_detectors/container.cc | 5 +++-- .../resource_detectors/container_resource_detector.h | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/resource_detectors/container.cc b/resource_detectors/container.cc index 4b2e758560..00ddbcd9fa 100644 --- a/resource_detectors/container.cc +++ b/resource_detectors/container.cc @@ -1,6 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +#include "opentelemetry/nostd/string_view.h" #include "opentelemetry/resource_detectors/container_resource_detector.h" #include @@ -30,7 +31,7 @@ std::string GetContainerIDFromCgroup(const char *file_path) return std::string(); } -std::string ExtractContainerIDFromLine(std::string_view line) +std::string ExtractContainerIDFromLine(nostd::string_view line) { /** * This regex is designed to extract container IDs from cgroup file lines. @@ -47,7 +48,7 @@ std::string ExtractContainerIDFromLine(std::string_view line) if (std::regex_search(line.begin(), line.end(), match, container_id_regex)) { - return std::string(match[1].first, match[1].second); + return match.str(1); } return std::string(); diff --git a/resource_detectors/include/opentelemetry/resource_detectors/container_resource_detector.h b/resource_detectors/include/opentelemetry/resource_detectors/container_resource_detector.h index 6b21acf44c..fd7f1a977f 100644 --- a/resource_detectors/include/opentelemetry/resource_detectors/container_resource_detector.h +++ b/resource_detectors/include/opentelemetry/resource_detectors/container_resource_detector.h @@ -3,6 +3,7 @@ #pragma once +#include "opentelemetry/nostd/string_view.h" #include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/resource/resource_detector.h" #include "opentelemetry/version.h" @@ -36,7 +37,7 @@ std::string GetContainerIDFromCgroup(const char *file_path); * @param line a single line of text, typically from the /proc/self/cgroup file * @return matched id or empty string */ -std::string ExtractContainerIDFromLine(std::string_view line); +std::string ExtractContainerIDFromLine(nostd::string_view line); } // namespace resource } // namespace sdk From 9bc07e1f0b8d80e63ab03265d75ff1d5f01a407f Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Fri, 8 Aug 2025 11:13:35 +0530 Subject: [PATCH 20/26] regex fix based on nostd::string_view --- resource_detectors/container.cc | 4 ++-- resource_detectors/container_detector.cc | 1 + sdk/test/resource/resource_test.cc | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/resource_detectors/container.cc b/resource_detectors/container.cc index 00ddbcd9fa..453dcffaf0 100644 --- a/resource_detectors/container.cc +++ b/resource_detectors/container.cc @@ -44,9 +44,9 @@ std::string ExtractContainerIDFromLine(nostd::string_view line) * Please see the test cases in resource_test.cc for more examples. */ static const std::regex container_id_regex(R"(^.*/(?:.*[-:])?([0-9a-f]+)(?:\.|\s*$))"); - std::match_results match; + std::match_results match; - if (std::regex_search(line.begin(), line.end(), match, container_id_regex)) + if (std::regex_search(line.data(), line.data() + line.size(), match, container_id_regex)) { return match.str(1); } diff --git a/resource_detectors/container_detector.cc b/resource_detectors/container_detector.cc index abbf11e8f1..5a29adafed 100644 --- a/resource_detectors/container_detector.cc +++ b/resource_detectors/container_detector.cc @@ -10,6 +10,7 @@ #include #include +#include OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk diff --git a/sdk/test/resource/resource_test.cc b/sdk/test/resource/resource_test.cc index 74463b4dee..9db1398c37 100644 --- a/sdk/test/resource/resource_test.cc +++ b/sdk/test/resource/resource_test.cc @@ -11,6 +11,7 @@ #include #include +#include "opentelemetry/nostd/string_view.h" #include "opentelemetry/nostd/variant.h" #include "opentelemetry/resource_detectors/container_resource_detector.h" #include "opentelemetry/sdk/common/attribute_utils.h" From a404c80b7e204ce5991c6626d7bb182da73705ab Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Fri, 8 Aug 2025 23:41:22 +0530 Subject: [PATCH 21/26] constants naming fix --- resource_detectors/container_detector.cc | 4 ++-- sdk/src/resource/resource_detector.cc | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/resource_detectors/container_detector.cc b/resource_detectors/container_detector.cc index 5a29adafed..95449d5f1a 100644 --- a/resource_detectors/container_detector.cc +++ b/resource_detectors/container_detector.cc @@ -21,11 +21,11 @@ namespace resource /** * This is the file path from where we can get container.id */ -constexpr const char *KCGroupPath = "/proc/self/cgroup"; +constexpr const char *kCGroupPath = "/proc/self/cgroup"; Resource ContainerResourceDetector::Detect() noexcept { - std::string container_id = opentelemetry::sdk::resource::GetContainerIDFromCgroup(KCGroupPath); + std::string container_id = opentelemetry::sdk::resource::GetContainerIDFromCgroup(kCGroupPath); if (container_id.empty()) { return ResourceDetector::Create({}); diff --git a/sdk/src/resource/resource_detector.cc b/sdk/src/resource/resource_detector.cc index 089697476c..f51d9a2a70 100644 --- a/sdk/src/resource/resource_detector.cc +++ b/sdk/src/resource/resource_detector.cc @@ -19,8 +19,8 @@ namespace sdk namespace resource { -constexpr const char *KOtelResourceAttributes = "OTEL_RESOURCE_ATTRIBUTES"; -constexpr const char *KOtelServiceName = "OTEL_SERVICE_NAME"; +constexpr const char *kOtelResourceAttributes = "OTEL_RESOURCE_ATTRIBUTES"; +constexpr const char *kOtelServiceName = "OTEL_SERVICE_NAME"; Resource ResourceDetector::Create(const ResourceAttributes &attributes, const std::string &schema_url) @@ -33,9 +33,9 @@ Resource OTELResourceDetector::Detect() noexcept std::string attributes_str, service_name; bool attributes_exists = opentelemetry::sdk::common::GetStringEnvironmentVariable( - KOtelResourceAttributes, attributes_str); + kOtelResourceAttributes, attributes_str); bool service_name_exists = - opentelemetry::sdk::common::GetStringEnvironmentVariable(KOtelServiceName, service_name); + opentelemetry::sdk::common::GetStringEnvironmentVariable(kOtelServiceName, service_name); if (!attributes_exists && !service_name_exists) { From 7cbcc59998709ec5664448a4d5369362df4a7907 Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Sat, 9 Aug 2025 17:40:28 +0530 Subject: [PATCH 22/26] namespace change and tests --- CMakeLists.txt | 2 +- resource_detectors/BUILD | 10 ++- resource_detectors/CMakeLists.txt | 31 +++++--- resource_detectors/container_detector.cc | 14 ++-- ...ntainer.cc => container_detector_utils.cc} | 13 ++-- .../resource_detectors/container_detector.h | 30 ++++++++ ..._detector.h => container_detector_utils.h} | 21 ++---- resource_detectors/test/BUILD | 15 ++++ resource_detectors/test/CMakeLists.txt | 14 ++++ .../test/container_detector_test.cc | 72 +++++++++++++++++++ sdk/test/resource/BUILD | 1 - sdk/test/resource/CMakeLists.txt | 5 +- sdk/test/resource/resource_test.cc | 51 ------------- 13 files changed, 185 insertions(+), 94 deletions(-) rename resource_detectors/{container.cc => container_detector_utils.cc} (86%) create mode 100644 resource_detectors/include/opentelemetry/resource_detectors/container_detector.h rename resource_detectors/include/opentelemetry/resource_detectors/{container_resource_detector.h => container_detector_utils.h} (59%) create mode 100644 resource_detectors/test/BUILD create mode 100644 resource_detectors/test/CMakeLists.txt create mode 100644 resource_detectors/test/container_detector_test.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index e4424fc5e9..e5be3bcf54 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -634,7 +634,6 @@ if(DEFINED OPENTELEMETRY_BUILD_DLL) endif() add_subdirectory(api) -add_subdirectory(resource_detectors) if(WITH_OPENTRACING) add_subdirectory(opentracing-shim) @@ -646,6 +645,7 @@ if(NOT WITH_API_ONLY) add_subdirectory(sdk) add_subdirectory(ext) add_subdirectory(exporters) + add_subdirectory(resource_detectors) if(BUILD_TESTING) add_subdirectory(test_common) diff --git a/resource_detectors/BUILD b/resource_detectors/BUILD index adee772f0e..499bc9cbad 100644 --- a/resource_detectors/BUILD +++ b/resource_detectors/BUILD @@ -5,15 +5,21 @@ package(default_visibility = ["//visibility:public"]) cc_library( name = "headers", - hdrs = glob(["include/**/*.h"]), + hdrs = glob( + ["include/**/*.h"], + exclude = ["include/opentelemetry/resource_detectors/container_detector_utils.h"], + ), strip_include_prefix = "include", ) cc_library( name = "resource_detectors", srcs = [ - "container.cc", "container_detector.cc", + "container_detector_utils.cc", + ], + hdrs = [ + "include/opentelemetry/resource_detectors/container_detector_utils.h", ], deps = [ "//api", diff --git a/resource_detectors/CMakeLists.txt b/resource_detectors/CMakeLists.txt index 8e1c832fb7..101f277409 100644 --- a/resource_detectors/CMakeLists.txt +++ b/resource_detectors/CMakeLists.txt @@ -1,23 +1,36 @@ # Copyright The OpenTelemetry Authors # SPDX-License-Identifier: Apache-2.0 -add_library(opentelemetry_resource_detectors container.cc container_detector.cc) +add_library(opentelemetry_resource_detectors container_detector_utils.cc + container_detector.cc) set_target_properties(opentelemetry_resource_detectors PROPERTIES EXPORT_NAME resource_detectors) set_target_version(opentelemetry_resource_detectors) target_link_libraries(opentelemetry_resource_detectors - PUBLIC opentelemetry_common) - + PUBLIC opentelemetry_resources) target_include_directories( opentelemetry_resource_detectors PUBLIC "$" - PUBLIC "$") + "$") + +otel_add_component( + COMPONENT + resource_detectors + TARGETS + opentelemetry_resource_detectors + FILES_DIRECTORY + "include/opentelemetry/" + FILES_DESTINATION + "include/opentelemetry" + FILES_MATCHING + PATTERN + "*.h" + PATTERN + "container_detector_utils.h" + EXCLUDE) -if(OPENTELEMETRY_INSTALL) - opentelemetry_add_pkgconfig( - resource_detectors "OpenTelemetry - Resource detectors" - "Components for resource detection in the OpenTelemetry resource detectors." - ) +if(BUILD_TESTING) + add_subdirectory(test) endif() diff --git a/resource_detectors/container_detector.cc b/resource_detectors/container_detector.cc index 95449d5f1a..af152e88a6 100644 --- a/resource_detectors/container_detector.cc +++ b/resource_detectors/container_detector.cc @@ -1,8 +1,9 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +#include "opentelemetry/resource_detectors/container_detector.h" #include "opentelemetry/nostd/variant.h" -#include "opentelemetry/resource_detectors/container_resource_detector.h" +#include "opentelemetry/resource_detectors/container_detector_utils.h" #include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/resource/resource_detector.h" #include "opentelemetry/semconv/incubating/container_attributes.h" @@ -13,7 +14,7 @@ #include OPENTELEMETRY_BEGIN_NAMESPACE -namespace sdk +namespace resource_detector { namespace resource { @@ -23,20 +24,21 @@ namespace resource */ constexpr const char *kCGroupPath = "/proc/self/cgroup"; -Resource ContainerResourceDetector::Detect() noexcept +opentelemetry::sdk::resource::Resource ContainerResourceDetector::Detect() noexcept { - std::string container_id = opentelemetry::sdk::resource::GetContainerIDFromCgroup(kCGroupPath); + std::string container_id = + opentelemetry::resource_detector::detail::GetContainerIDFromCgroup(kCGroupPath); if (container_id.empty()) { return ResourceDetector::Create({}); } - ResourceAttributes attributes; + opentelemetry::sdk::resource::ResourceAttributes attributes; attributes[semconv::container::kContainerId] = std::move(container_id); return ResourceDetector::Create(attributes); } } // namespace resource -} // namespace sdk +} // namespace resource_detector OPENTELEMETRY_END_NAMESPACE diff --git a/resource_detectors/container.cc b/resource_detectors/container_detector_utils.cc similarity index 86% rename from resource_detectors/container.cc rename to resource_detectors/container_detector_utils.cc index 453dcffaf0..a2f3063cec 100644 --- a/resource_detectors/container.cc +++ b/resource_detectors/container_detector_utils.cc @@ -1,8 +1,9 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +#include "opentelemetry/resource_detectors/container_detector_utils.h" #include "opentelemetry/nostd/string_view.h" -#include "opentelemetry/resource_detectors/container_resource_detector.h" +#include "opentelemetry/resource_detectors/container_detector.h" #include #include @@ -11,10 +12,11 @@ #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE -namespace sdk +namespace resource_detector { -namespace resource +namespace detail { + std::string GetContainerIDFromCgroup(const char *file_path) { std::ifstream cgroup_file(file_path); @@ -53,6 +55,7 @@ std::string ExtractContainerIDFromLine(nostd::string_view line) return std::string(); } -} // namespace resource -} // namespace sdk + +} // namespace detail +} // namespace resource_detector OPENTELEMETRY_END_NAMESPACE diff --git a/resource_detectors/include/opentelemetry/resource_detectors/container_detector.h b/resource_detectors/include/opentelemetry/resource_detectors/container_detector.h new file mode 100644 index 0000000000..39215f715d --- /dev/null +++ b/resource_detectors/include/opentelemetry/resource_detectors/container_detector.h @@ -0,0 +1,30 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/sdk/resource/resource.h" +#include "opentelemetry/sdk/resource/resource_detector.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace resource_detector +{ +namespace resource +{ + +/** + * ContainerResourceDetector to detect resource attributes when running inside a containerized + * environment. This detector extracts metadata such as container ID from cgroup information and + * sets attributes like container.id following the OpenTelemetry semantic conventions. + */ +class ContainerResourceDetector : public opentelemetry::sdk::resource::ResourceDetector +{ +public: + opentelemetry::sdk::resource::Resource Detect() noexcept override; +}; + +} // namespace resource +} // namespace resource_detector +OPENTELEMETRY_END_NAMESPACE diff --git a/resource_detectors/include/opentelemetry/resource_detectors/container_resource_detector.h b/resource_detectors/include/opentelemetry/resource_detectors/container_detector_utils.h similarity index 59% rename from resource_detectors/include/opentelemetry/resource_detectors/container_resource_detector.h rename to resource_detectors/include/opentelemetry/resource_detectors/container_detector_utils.h index fd7f1a977f..185657e303 100644 --- a/resource_detectors/include/opentelemetry/resource_detectors/container_resource_detector.h +++ b/resource_detectors/include/opentelemetry/resource_detectors/container_detector_utils.h @@ -9,26 +9,15 @@ #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE -namespace sdk +namespace resource_detector { -namespace resource +namespace detail { -/** - * ContainerResourceDetector to detect resource attributes when running inside a containerized - * environment. This detector extracts metadata such as container ID from cgroup information and - * sets attributes like container.id following the OpenTelemetry semantic conventions. - */ -class ContainerResourceDetector : public ResourceDetector -{ -public: - Resource Detect() noexcept override; -}; - /** * Reads the container.id from /proc/self/cgroup file. * @param file_path file path of cgroup - * @return container.id as string or empty string + * @return container.id as string or an empty string if not found on error */ std::string GetContainerIDFromCgroup(const char *file_path); @@ -39,6 +28,6 @@ std::string GetContainerIDFromCgroup(const char *file_path); */ std::string ExtractContainerIDFromLine(nostd::string_view line); -} // namespace resource -} // namespace sdk +} // namespace detail +} // namespace resource_detector OPENTELEMETRY_END_NAMESPACE diff --git a/resource_detectors/test/BUILD b/resource_detectors/test/BUILD new file mode 100644 index 0000000000..2cac8174dc --- /dev/null +++ b/resource_detectors/test/BUILD @@ -0,0 +1,15 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +cc_test( + name = "resource_detector_test", + srcs = [ + "container_detector_test.cc", + ], + tags = ["test"], + deps = [ + "//api", + "//resource_detectors", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/resource_detectors/test/CMakeLists.txt b/resource_detectors/test/CMakeLists.txt new file mode 100644 index 0000000000..9414e64212 --- /dev/null +++ b/resource_detectors/test/CMakeLists.txt @@ -0,0 +1,14 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +add_executable(resource_detector_test container_detector_test.cc) + +# Link the required dependencies +target_link_libraries( + resource_detector_test PRIVATE opentelemetry_resource_detectors + opentelemetry_api GTest::gtest_main) + +gtest_add_tests( + TARGET resource_detector_test + TEST_PREFIX resource_detector. + TEST_LIST resource_detector_test) diff --git a/resource_detectors/test/container_detector_test.cc b/resource_detectors/test/container_detector_test.cc new file mode 100644 index 0000000000..30c7b35e84 --- /dev/null +++ b/resource_detectors/test/container_detector_test.cc @@ -0,0 +1,72 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include +#include +#include +#include + +#include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/nostd/variant.h" +#include "opentelemetry/resource_detectors/container_detector.h" +#include "opentelemetry/resource_detectors/container_detector_utils.h" +#include "opentelemetry/sdk/resource/resource.h" + +TEST(ContainerIdDetectorTest, ExtractValidContainerIdFromLine) +{ + std::string line = + "13:name=systemd:/podruntime/docker/kubepods/ac679f8a8319c8cf7d38e1adf263bc08d23.aaaa"; + std::string extracted_id = + opentelemetry::resource_detector::detail::ExtractContainerIDFromLine(line); + EXPECT_EQ(std::string{"ac679f8a8319c8cf7d38e1adf263bc08d23"}, extracted_id); +} + +TEST(ContainerIdDetectorTest, ExtractIdFromMockUpCGroupFile) +{ + const char *filename = "test_cgroup.txt"; + + { + std::ofstream outfile(filename); + outfile << "13:name=systemd:/kuberuntime/containerd" + "/kubepods-pod872d2066_00ef_48ea_a7d8_51b18b72d739:cri-containerd:" + "e857a4bf05a69080a759574949d7a0e69572e27647800fa7faff6a05a8332aa1\n"; + outfile << "9:cpu:/not-a-container\n"; + } + + std::string container_id = + opentelemetry::resource_detector::detail::GetContainerIDFromCgroup(filename); + EXPECT_EQ(container_id, + std::string{"e857a4bf05a69080a759574949d7a0e69572e27647800fa7faff6a05a8332aa1"}); + + std::remove(filename); +} + +TEST(ContainerIdDetectorTest, DoesNotExtractInvalidLine) +{ + std::string line = "this line does not contain a container id"; + std::string id = opentelemetry::resource_detector::detail::ExtractContainerIDFromLine(line); + EXPECT_EQ(id, std::string{""}); +} + +TEST(ContainerIdDetectorTest, ReturnsEmptyOnNoMatch) +{ + const char *filename = "test_empty_cgroup.txt"; + + { + std::ofstream outfile(filename); + outfile << "no container id here\n"; + } + + std::string id = opentelemetry::resource_detector::detail::GetContainerIDFromCgroup(filename); + EXPECT_EQ(id, std::string{""}); + + std::remove(filename); // cleanup +} + +TEST(ContainerIdDetectorTest, ReturnsEmptyOnFileFailingToOpen) +{ + const char *filename = "test_invalid_cgroup.txt"; + + std::string id = opentelemetry::resource_detector::detail::GetContainerIDFromCgroup(filename); + EXPECT_EQ(id, std::string{""}); +} diff --git a/sdk/test/resource/BUILD b/sdk/test/resource/BUILD index 4f403a20c5..145ecf0b56 100644 --- a/sdk/test/resource/BUILD +++ b/sdk/test/resource/BUILD @@ -9,7 +9,6 @@ cc_test( tags = ["test"], deps = [ "//api", - "//resource_detectors", "//sdk/src/resource", "@com_google_googletest//:gtest_main", ], diff --git a/sdk/test/resource/CMakeLists.txt b/sdk/test/resource/CMakeLists.txt index b606e94fad..dfe78589f8 100644 --- a/sdk/test/resource/CMakeLists.txt +++ b/sdk/test/resource/CMakeLists.txt @@ -3,9 +3,8 @@ foreach(testname resource_test) add_executable(${testname} "${testname}.cc") - target_link_libraries( - ${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} - opentelemetry_resources opentelemetry_resource_detectors) + target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} opentelemetry_resources) gtest_add_tests( TARGET ${testname} TEST_PREFIX resources. diff --git a/sdk/test/resource/resource_test.cc b/sdk/test/resource/resource_test.cc index 9db1398c37..caefd5e22a 100644 --- a/sdk/test/resource/resource_test.cc +++ b/sdk/test/resource/resource_test.cc @@ -11,9 +11,7 @@ #include #include -#include "opentelemetry/nostd/string_view.h" #include "opentelemetry/nostd/variant.h" -#include "opentelemetry/resource_detectors/container_resource_detector.h" #include "opentelemetry/sdk/common/attribute_utils.h" #include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/resource/resource_detector.h" @@ -296,52 +294,3 @@ TEST(ResourceTest, DerivedResourceDetector) EXPECT_EQ(resource.GetSchemaURL(), detector.schema_url); EXPECT_TRUE(received_attributes.find("key") != received_attributes.end()); } - -TEST(ResourceTest, ExtractValidContainerId) -{ - std::string line = - "13:name=systemd:/podruntime/docker/kubepods/ac679f8a8319c8cf7d38e1adf263bc08d23.aaaa"; - std::string extracted_id = opentelemetry::sdk::resource::ExtractContainerIDFromLine(line); - EXPECT_EQ(std::string{"ac679f8a8319c8cf7d38e1adf263bc08d23"}, extracted_id); -} - -TEST(ResourceTest, ExtractIdFromMockUpCGroupFile) -{ - const char *filename = "test_cgroup.txt"; - - { - std::ofstream outfile(filename); - outfile << "13:name=systemd:/kuberuntime/containerd" - "/kubepods-pod872d2066_00ef_48ea_a7d8_51b18b72d739:cri-containerd:" - "e857a4bf05a69080a759574949d7a0e69572e27647800fa7faff6a05a8332aa1\n"; - outfile << "9:cpu:/not-a-container\n"; - } - - std::string container_id = opentelemetry::sdk::resource::GetContainerIDFromCgroup(filename); - EXPECT_EQ(container_id, - std::string{"e857a4bf05a69080a759574949d7a0e69572e27647800fa7faff6a05a8332aa1"}); - - std::remove(filename); -} - -TEST(ResourceTest, DoesNotExtractInvalidLine) -{ - std::string line = "this line does not contain a container id"; - std::string id = opentelemetry::sdk::resource::ExtractContainerIDFromLine(line); - EXPECT_EQ(id, std::string{""}); -} - -TEST(ContainerIdDetectorTest, ReturnsEmptyOnNoMatch) -{ - const char *filename = "test_empty_cgroup.txt"; - - { - std::ofstream outfile(filename); - outfile << "no container id here\n"; - } - - std::string id = opentelemetry::sdk::resource::GetContainerIDFromCgroup(filename); - EXPECT_EQ(id, std::string{""}); - - std::remove(filename); // cleanup -} From 0e7d011383042d5cc3ae5f41d58ecc995eb6bbad Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Sat, 9 Aug 2025 18:40:00 +0530 Subject: [PATCH 23/26] iwyu and BUILD fix --- resource_detectors/BUILD | 8 +------- resource_detectors/container_detector_utils.cc | 1 - .../opentelemetry/resource_detectors/container_detector.h | 1 - .../resource_detectors/container_detector_utils.h | 4 ++-- resource_detectors/test/container_detector_test.cc | 5 +---- sdk/test/resource/resource_test.cc | 2 -- 6 files changed, 4 insertions(+), 17 deletions(-) diff --git a/resource_detectors/BUILD b/resource_detectors/BUILD index 499bc9cbad..503949ca28 100644 --- a/resource_detectors/BUILD +++ b/resource_detectors/BUILD @@ -5,10 +5,7 @@ package(default_visibility = ["//visibility:public"]) cc_library( name = "headers", - hdrs = glob( - ["include/**/*.h"], - exclude = ["include/opentelemetry/resource_detectors/container_detector_utils.h"], - ), + hdrs = glob(["include/**/*.h"]), strip_include_prefix = "include", ) @@ -18,9 +15,6 @@ cc_library( "container_detector.cc", "container_detector_utils.cc", ], - hdrs = [ - "include/opentelemetry/resource_detectors/container_detector_utils.h", - ], deps = [ "//api", "//resource_detectors:headers", diff --git a/resource_detectors/container_detector_utils.cc b/resource_detectors/container_detector_utils.cc index a2f3063cec..a1c54e2f47 100644 --- a/resource_detectors/container_detector_utils.cc +++ b/resource_detectors/container_detector_utils.cc @@ -3,7 +3,6 @@ #include "opentelemetry/resource_detectors/container_detector_utils.h" #include "opentelemetry/nostd/string_view.h" -#include "opentelemetry/resource_detectors/container_detector.h" #include #include diff --git a/resource_detectors/include/opentelemetry/resource_detectors/container_detector.h b/resource_detectors/include/opentelemetry/resource_detectors/container_detector.h index 39215f715d..109e10195c 100644 --- a/resource_detectors/include/opentelemetry/resource_detectors/container_detector.h +++ b/resource_detectors/include/opentelemetry/resource_detectors/container_detector.h @@ -3,7 +3,6 @@ #pragma once -#include "opentelemetry/nostd/string_view.h" #include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/resource/resource_detector.h" #include "opentelemetry/version.h" diff --git a/resource_detectors/include/opentelemetry/resource_detectors/container_detector_utils.h b/resource_detectors/include/opentelemetry/resource_detectors/container_detector_utils.h index 185657e303..666bd95b7c 100644 --- a/resource_detectors/include/opentelemetry/resource_detectors/container_detector_utils.h +++ b/resource_detectors/include/opentelemetry/resource_detectors/container_detector_utils.h @@ -3,9 +3,9 @@ #pragma once +#include + #include "opentelemetry/nostd/string_view.h" -#include "opentelemetry/sdk/resource/resource.h" -#include "opentelemetry/sdk/resource/resource_detector.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE diff --git a/resource_detectors/test/container_detector_test.cc b/resource_detectors/test/container_detector_test.cc index 30c7b35e84..0243973c46 100644 --- a/resource_detectors/test/container_detector_test.cc +++ b/resource_detectors/test/container_detector_test.cc @@ -2,15 +2,12 @@ // SPDX-License-Identifier: Apache-2.0 #include +#include #include #include -#include #include "opentelemetry/nostd/string_view.h" -#include "opentelemetry/nostd/variant.h" -#include "opentelemetry/resource_detectors/container_detector.h" #include "opentelemetry/resource_detectors/container_detector_utils.h" -#include "opentelemetry/sdk/resource/resource.h" TEST(ContainerIdDetectorTest, ExtractValidContainerIdFromLine) { diff --git a/sdk/test/resource/resource_test.cc b/sdk/test/resource/resource_test.cc index caefd5e22a..696509f892 100644 --- a/sdk/test/resource/resource_test.cc +++ b/sdk/test/resource/resource_test.cc @@ -3,9 +3,7 @@ #include #include -#include #include -#include #include #include #include From fd93b32dcb4107e942ba9bcbdce0d1325fa28663 Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Sat, 9 Aug 2025 20:41:40 +0530 Subject: [PATCH 24/26] namespace change --- resource_detectors/container_detector.cc | 3 --- .../opentelemetry/resource_detectors/container_detector.h | 3 --- 2 files changed, 6 deletions(-) diff --git a/resource_detectors/container_detector.cc b/resource_detectors/container_detector.cc index af152e88a6..3cf27d4b9e 100644 --- a/resource_detectors/container_detector.cc +++ b/resource_detectors/container_detector.cc @@ -16,8 +16,6 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace resource_detector { -namespace resource -{ /** * This is the file path from where we can get container.id @@ -39,6 +37,5 @@ opentelemetry::sdk::resource::Resource ContainerResourceDetector::Detect() noexc return ResourceDetector::Create(attributes); } -} // namespace resource } // namespace resource_detector OPENTELEMETRY_END_NAMESPACE diff --git a/resource_detectors/include/opentelemetry/resource_detectors/container_detector.h b/resource_detectors/include/opentelemetry/resource_detectors/container_detector.h index 109e10195c..24ac6b3298 100644 --- a/resource_detectors/include/opentelemetry/resource_detectors/container_detector.h +++ b/resource_detectors/include/opentelemetry/resource_detectors/container_detector.h @@ -10,8 +10,6 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace resource_detector { -namespace resource -{ /** * ContainerResourceDetector to detect resource attributes when running inside a containerized @@ -24,6 +22,5 @@ class ContainerResourceDetector : public opentelemetry::sdk::resource::ResourceD opentelemetry::sdk::resource::Resource Detect() noexcept override; }; -} // namespace resource } // namespace resource_detector OPENTELEMETRY_END_NAMESPACE From b030cc980f0d74f46b4b3ee0bcd0735481d7d9b8 Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Sun, 10 Aug 2025 17:44:34 +0530 Subject: [PATCH 25/26] cmakelists flags addition and fix --- CMakeLists.txt | 7 ++++++- resource_detectors/CMakeLists.txt | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e5be3bcf54..1a6f9cf99a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -261,6 +261,9 @@ option(WITH_METRICS_EXEMPLAR_PREVIEW option(WITH_THREAD_INSTRUMENTATION_PREVIEW "Whether to enable thread instrumentation" OFF) +option(WITH_RESOURCE_DETECTORS_PREVIEW + "Whether to enable inbuilt resource detectors" OFF) + option(OPENTELEMETRY_SKIP_DYNAMIC_LOADING_TESTS "Whether to build test libraries that are always linked as shared libs" OFF) @@ -645,7 +648,9 @@ if(NOT WITH_API_ONLY) add_subdirectory(sdk) add_subdirectory(ext) add_subdirectory(exporters) - add_subdirectory(resource_detectors) + if(WITH_RESOURCE_DETECTORS_PREVIEW) + add_subdirectory(resource_detectors) + endif() if(BUILD_TESTING) add_subdirectory(test_common) diff --git a/resource_detectors/CMakeLists.txt b/resource_detectors/CMakeLists.txt index 101f277409..108ca00bad 100644 --- a/resource_detectors/CMakeLists.txt +++ b/resource_detectors/CMakeLists.txt @@ -12,8 +12,8 @@ target_link_libraries(opentelemetry_resource_detectors PUBLIC opentelemetry_resources) target_include_directories( opentelemetry_resource_detectors - PUBLIC "$" - "$") + PUBLIC "$" + "$") otel_add_component( COMPONENT From 018a1f813243181bc893ef5730aebd8459bd87d6 Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Sun, 10 Aug 2025 22:33:01 +0530 Subject: [PATCH 26/26] resource detectors preview-options --- test_common/cmake/preview-options.cmake | 1 + 1 file changed, 1 insertion(+) diff --git a/test_common/cmake/preview-options.cmake b/test_common/cmake/preview-options.cmake index 88acf7a187..41a35aa55b 100644 --- a/test_common/cmake/preview-options.cmake +++ b/test_common/cmake/preview-options.cmake @@ -13,5 +13,6 @@ set(WITH_THREAD_INSTRUMENTATION_PREVIEW ${ENABLE_PREVIEW} CACHE BOOL "" FORCE) set(WITH_OTLP_GRPC_SSL_MTLS_PREVIEW ${ENABLE_PREVIEW} CACHE BOOL "" FORCE) set(WITH_OTLP_GRPC_CREDENTIAL_PREVIEW ${ENABLE_PREVIEW} CACHE BOOL "" FORCE) set(WITH_OTLP_RETRY_PREVIEW ${ENABLE_PREVIEW} CACHE BOOL "" FORCE) +set(WITH_RESOURCE_DETECTORS_PREVIEW ${ENABLE_PREVIEW} CACHE BOOL "" FORCE) set(WITH_OTLP_HTTP_COMPRESSION ${ENABLE_PREVIEW} CACHE BOOL "" FORCE) set(WITH_CURL_LOGGING ${ENABLE_PREVIEW} CACHE BOOL "" FORCE)