-
Notifications
You must be signed in to change notification settings - Fork 501
[SDK] Implementation of container resource as per semconv #3572
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lalitb
merged 31 commits into
open-telemetry:main
from
nikhilbhatia08:implement_resource_detectors
Aug 10, 2025
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
c740ddb
[SDK] Implementation of container resource as per semconv
nikhilbhatia08 4bf2bfb
[sdk] minor changes in format, linking and var naming
nikhilbhatia08 f075c93
changes for ci
nikhilbhatia08 86cebd6
Merge branch 'main' into implement_resource_detectors
lalitb 00decf9
ci and requested changes
nikhilbhatia08 b84840f
Merge branch 'implement_resource_detectors' of https://github.com/nik…
nikhilbhatia08 edef26e
iwyu-fix
nikhilbhatia08 3742932
newline-fix
nikhilbhatia08 a495294
newline-fix
nikhilbhatia08 72565ec
Merge branch 'implement_resource_detectors' of https://github.com/nik…
nikhilbhatia08 f7b5e94
[EXPORTER] Fixes tsan warnings (#3531)
owent a836349
[DOC] Document minimum required versions (#3562)
markus456 5ec872c
Bump github/codeql-action from 3.29.4 to 3.29.5 (#3574)
dependabot[bot] 81624a3
Add subscript to issue templates (#3576)
opentelemetrybot 4cebe35
iwyu-fix
nikhilbhatia08 a1778b9
newline-fix
nikhilbhatia08 926517f
Restore ci.yml to match main
nikhilbhatia08 bd7a71c
Merge branch 'implement_resource_detectors' of https://github.com/nik…
nikhilbhatia08 2546661
new folder for resource detectors
nikhilbhatia08 75c495c
BUILD file for resource_detectors
nikhilbhatia08 220a5ad
BUILD file(resource_detectors) minor changes
nikhilbhatia08 439e92c
added comments and changes as required
nikhilbhatia08 9e5bc55
changed to nostd::string_view
nikhilbhatia08 9bc07e1
regex fix based on nostd::string_view
nikhilbhatia08 047e35c
Merge branch 'main' into implement_resource_detectors
lalitb a404c80
constants naming fix
nikhilbhatia08 7cbcc59
namespace change and tests
nikhilbhatia08 0e7d011
iwyu and BUILD fix
nikhilbhatia08 fd93b32
namespace change
nikhilbhatia08 b030cc9
cmakelists flags addition and fix
nikhilbhatia08 018a1f8
resource detectors preview-options
nikhilbhatia08 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Copyright The OpenTelemetry Authors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
cc_library( | ||
name = "headers", | ||
hdrs = glob(["include/**/*.h"]), | ||
strip_include_prefix = "include", | ||
) | ||
|
||
cc_library( | ||
name = "resource_detectors", | ||
srcs = [ | ||
"container_detector.cc", | ||
"container_detector_utils.cc", | ||
], | ||
deps = [ | ||
"//api", | ||
"//resource_detectors:headers", | ||
"//sdk:headers", | ||
"//sdk/src/resource", | ||
], | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Copyright The OpenTelemetry Authors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
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_resources) | ||
target_include_directories( | ||
opentelemetry_resource_detectors | ||
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>" | ||
"$<INSTALL_INTERFACE:include>") | ||
|
||
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(BUILD_TESTING) | ||
add_subdirectory(test) | ||
endif() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// 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_detector_utils.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 <string> | ||
#include <unordered_map> | ||
#include <utility> | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace resource_detector | ||
{ | ||
|
||
/** | ||
* This is the file path from where we can get container.id | ||
*/ | ||
constexpr const char *kCGroupPath = "/proc/self/cgroup"; | ||
|
||
opentelemetry::sdk::resource::Resource ContainerResourceDetector::Detect() noexcept | ||
{ | ||
std::string container_id = | ||
opentelemetry::resource_detector::detail::GetContainerIDFromCgroup(kCGroupPath); | ||
if (container_id.empty()) | ||
{ | ||
return ResourceDetector::Create({}); | ||
} | ||
|
||
opentelemetry::sdk::resource::ResourceAttributes attributes; | ||
|
||
attributes[semconv::container::kContainerId] = std::move(container_id); | ||
return ResourceDetector::Create(attributes); | ||
} | ||
|
||
} // namespace resource_detector | ||
OPENTELEMETRY_END_NAMESPACE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// 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 <fstream> | ||
#include <regex> | ||
#include <string> | ||
|
||
#include "opentelemetry/version.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace resource_detector | ||
{ | ||
namespace detail | ||
{ | ||
|
||
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(); | ||
} | ||
|
||
std::string ExtractContainerIDFromLine(nostd::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::match_results<const char *> match; | ||
|
||
if (std::regex_search(line.data(), line.data() + line.size(), match, container_id_regex)) | ||
{ | ||
return match.str(1); | ||
} | ||
|
||
return std::string(); | ||
} | ||
|
||
} // namespace detail | ||
} // namespace resource_detector | ||
OPENTELEMETRY_END_NAMESPACE |
26 changes: 26 additions & 0 deletions
26
resource_detectors/include/opentelemetry/resource_detectors/container_detector.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// 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 resource_detector | ||
{ | ||
|
||
/** | ||
* 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_detector | ||
OPENTELEMETRY_END_NAMESPACE |
33 changes: 33 additions & 0 deletions
33
resource_detectors/include/opentelemetry/resource_detectors/container_detector_utils.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
|
||
#include "opentelemetry/nostd/string_view.h" | ||
#include "opentelemetry/version.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace resource_detector | ||
{ | ||
namespace detail | ||
{ | ||
|
||
/** | ||
* Reads the container.id from /proc/self/cgroup file. | ||
* @param file_path file path of cgroup | ||
* @return container.id as string or an empty string if not found on error | ||
*/ | ||
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(nostd::string_view line); | ||
|
||
} // namespace detail | ||
} // namespace resource_detector | ||
OPENTELEMETRY_END_NAMESPACE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", | ||
], | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include <gtest/gtest.h> | ||
#include <cstdio> | ||
#include <fstream> | ||
#include <string> | ||
|
||
#include "opentelemetry/nostd/string_view.h" | ||
#include "opentelemetry/resource_detectors/container_detector_utils.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{""}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.