-
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 21 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.cc", | ||
"container_detector.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,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) | ||
dbarker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
target_include_directories( | ||
opentelemetry_resource_detectors | ||
PUBLIC "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/resource_detectors/include>" | ||
PUBLIC "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/sdk/include>") | ||
dbarker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
if(OPENTELEMETRY_INSTALL) | ||
dbarker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
opentelemetry_add_pkgconfig( | ||
resource_detectors "OpenTelemetry - Resource detectors" | ||
"Components for resource detection in the OpenTelemetry resource detectors." | ||
) | ||
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,47 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "opentelemetry/resource_detectors/container_resource_detector.h" | ||
|
||
#include <fstream> | ||
#include <regex> | ||
#include <string> | ||
|
||
#include "opentelemetry/version.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace sdk | ||
{ | ||
namespace resource | ||
{ | ||
std::string GetContainerIDFromCgroup(const char *file_path) | ||
{ | ||
std::ifstream cgroup_file(file_path); | ||
nikhilbhatia08 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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(const std::string &line) | ||
nikhilbhatia08 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
{ | ||
static const std::regex container_id_regex(R"(^.*/(?:.*[-:])?([0-9a-f]+)(?:\.|\s*$))"); | ||
nikhilbhatia08 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
dbarker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
std::smatch match; | ||
|
||
if (std::regex_search(line, match, container_id_regex)) | ||
{ | ||
return match.str(1); | ||
} | ||
|
||
return ""; | ||
nikhilbhatia08 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} // namespace resource | ||
} // namespace sdk | ||
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,41 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "opentelemetry/nostd/variant.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" | ||
#include "opentelemetry/version.h" | ||
|
||
#include <string> | ||
#include <unordered_map> | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace sdk | ||
dbarker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
{ | ||
namespace resource | ||
dbarker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
{ | ||
|
||
/** | ||
* This is the file path from where we can get container.id | ||
*/ | ||
const char *KCGroupPath = "/proc/self/cgroup"; | ||
nikhilbhatia08 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
Resource ContainerResourceDetector::Detect() noexcept | ||
{ | ||
std::string container_id = opentelemetry::sdk::resource::GetContainerIDFromCgroup(KCGroupPath); | ||
if (container_id.empty()) | ||
{ | ||
return ResourceDetector::Create({}); | ||
} | ||
|
||
ResourceAttributes attributes; | ||
|
||
attributes[semconv::container::kContainerId] = container_id; | ||
nikhilbhatia08 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return ResourceDetector::Create(attributes); | ||
} | ||
|
||
} // namespace resource | ||
} // namespace sdk | ||
OPENTELEMETRY_END_NAMESPACE |
43 changes: 43 additions & 0 deletions
43
resource_detectors/include/opentelemetry/resource_detectors/container_resource_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,43 @@ | ||
// Copyright The OpenTelemetry Authors | ||
dbarker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 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 | ||
nikhilbhatia08 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
*/ | ||
std::string GetContainerIDFromCgroup(const char *file_path); | ||
nikhilbhatia08 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
dbarker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* 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); | ||
nikhilbhatia08 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
} // namespace resource | ||
} // namespace sdk | ||
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
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
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 | ||||
---|---|---|---|---|---|---|
|
@@ -3,13 +3,16 @@ | |||||
|
||||||
#include <gtest/gtest.h> | ||||||
#include <stdint.h> | ||||||
#include <cstdio> | ||||||
#include <cstdlib> | ||||||
#include <fstream> | ||||||
#include <map> | ||||||
#include <string> | ||||||
#include <unordered_map> | ||||||
#include <utility> | ||||||
|
||||||
#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" | ||||||
|
@@ -292,3 +295,52 @@ 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); | ||||||
|
std::remove(filename); |
Copilot uses AI. Check for mistakes.
Positive FeedbackNegative Feedback
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.