Skip to content

Commit 7cbcc59

Browse files
namespace change and tests
1 parent a404c80 commit 7cbcc59

File tree

13 files changed

+185
-94
lines changed

13 files changed

+185
-94
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,6 @@ if(DEFINED OPENTELEMETRY_BUILD_DLL)
634634
endif()
635635

636636
add_subdirectory(api)
637-
add_subdirectory(resource_detectors)
638637

639638
if(WITH_OPENTRACING)
640639
add_subdirectory(opentracing-shim)
@@ -646,6 +645,7 @@ if(NOT WITH_API_ONLY)
646645
add_subdirectory(sdk)
647646
add_subdirectory(ext)
648647
add_subdirectory(exporters)
648+
add_subdirectory(resource_detectors)
649649

650650
if(BUILD_TESTING)
651651
add_subdirectory(test_common)

resource_detectors/BUILD

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@ package(default_visibility = ["//visibility:public"])
55

66
cc_library(
77
name = "headers",
8-
hdrs = glob(["include/**/*.h"]),
8+
hdrs = glob(
9+
["include/**/*.h"],
10+
exclude = ["include/opentelemetry/resource_detectors/container_detector_utils.h"],
11+
),
912
strip_include_prefix = "include",
1013
)
1114

1215
cc_library(
1316
name = "resource_detectors",
1417
srcs = [
15-
"container.cc",
1618
"container_detector.cc",
19+
"container_detector_utils.cc",
20+
],
21+
hdrs = [
22+
"include/opentelemetry/resource_detectors/container_detector_utils.h",
1723
],
1824
deps = [
1925
"//api",

resource_detectors/CMakeLists.txt

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
11
# Copyright The OpenTelemetry Authors
22
# SPDX-License-Identifier: Apache-2.0
33

4-
add_library(opentelemetry_resource_detectors container.cc container_detector.cc)
4+
add_library(opentelemetry_resource_detectors container_detector_utils.cc
5+
container_detector.cc)
56

67
set_target_properties(opentelemetry_resource_detectors
78
PROPERTIES EXPORT_NAME resource_detectors)
89
set_target_version(opentelemetry_resource_detectors)
910

1011
target_link_libraries(opentelemetry_resource_detectors
11-
PUBLIC opentelemetry_common)
12-
12+
PUBLIC opentelemetry_resources)
1313
target_include_directories(
1414
opentelemetry_resource_detectors
1515
PUBLIC "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/resource_detectors/include>"
16-
PUBLIC "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/sdk/include>")
16+
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/sdk/include>")
17+
18+
otel_add_component(
19+
COMPONENT
20+
resource_detectors
21+
TARGETS
22+
opentelemetry_resource_detectors
23+
FILES_DIRECTORY
24+
"include/opentelemetry/"
25+
FILES_DESTINATION
26+
"include/opentelemetry"
27+
FILES_MATCHING
28+
PATTERN
29+
"*.h"
30+
PATTERN
31+
"container_detector_utils.h"
32+
EXCLUDE)
1733

18-
if(OPENTELEMETRY_INSTALL)
19-
opentelemetry_add_pkgconfig(
20-
resource_detectors "OpenTelemetry - Resource detectors"
21-
"Components for resource detection in the OpenTelemetry resource detectors."
22-
)
34+
if(BUILD_TESTING)
35+
add_subdirectory(test)
2336
endif()
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
#include "opentelemetry/resource_detectors/container_detector.h"
45
#include "opentelemetry/nostd/variant.h"
5-
#include "opentelemetry/resource_detectors/container_resource_detector.h"
6+
#include "opentelemetry/resource_detectors/container_detector_utils.h"
67
#include "opentelemetry/sdk/resource/resource.h"
78
#include "opentelemetry/sdk/resource/resource_detector.h"
89
#include "opentelemetry/semconv/incubating/container_attributes.h"
@@ -13,7 +14,7 @@
1314
#include <utility>
1415

1516
OPENTELEMETRY_BEGIN_NAMESPACE
16-
namespace sdk
17+
namespace resource_detector
1718
{
1819
namespace resource
1920
{
@@ -23,20 +24,21 @@ namespace resource
2324
*/
2425
constexpr const char *kCGroupPath = "/proc/self/cgroup";
2526

26-
Resource ContainerResourceDetector::Detect() noexcept
27+
opentelemetry::sdk::resource::Resource ContainerResourceDetector::Detect() noexcept
2728
{
28-
std::string container_id = opentelemetry::sdk::resource::GetContainerIDFromCgroup(kCGroupPath);
29+
std::string container_id =
30+
opentelemetry::resource_detector::detail::GetContainerIDFromCgroup(kCGroupPath);
2931
if (container_id.empty())
3032
{
3133
return ResourceDetector::Create({});
3234
}
3335

34-
ResourceAttributes attributes;
36+
opentelemetry::sdk::resource::ResourceAttributes attributes;
3537

3638
attributes[semconv::container::kContainerId] = std::move(container_id);
3739
return ResourceDetector::Create(attributes);
3840
}
3941

4042
} // namespace resource
41-
} // namespace sdk
43+
} // namespace resource_detector
4244
OPENTELEMETRY_END_NAMESPACE

resource_detectors/container.cc renamed to resource_detectors/container_detector_utils.cc

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
#include "opentelemetry/resource_detectors/container_detector_utils.h"
45
#include "opentelemetry/nostd/string_view.h"
5-
#include "opentelemetry/resource_detectors/container_resource_detector.h"
6+
#include "opentelemetry/resource_detectors/container_detector.h"
67

78
#include <fstream>
89
#include <regex>
@@ -11,10 +12,11 @@
1112
#include "opentelemetry/version.h"
1213

1314
OPENTELEMETRY_BEGIN_NAMESPACE
14-
namespace sdk
15+
namespace resource_detector
1516
{
16-
namespace resource
17+
namespace detail
1718
{
19+
1820
std::string GetContainerIDFromCgroup(const char *file_path)
1921
{
2022
std::ifstream cgroup_file(file_path);
@@ -53,6 +55,7 @@ std::string ExtractContainerIDFromLine(nostd::string_view line)
5355

5456
return std::string();
5557
}
56-
} // namespace resource
57-
} // namespace sdk
58+
59+
} // namespace detail
60+
} // namespace resource_detector
5861
OPENTELEMETRY_END_NAMESPACE
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#pragma once
5+
6+
#include "opentelemetry/nostd/string_view.h"
7+
#include "opentelemetry/sdk/resource/resource.h"
8+
#include "opentelemetry/sdk/resource/resource_detector.h"
9+
#include "opentelemetry/version.h"
10+
11+
OPENTELEMETRY_BEGIN_NAMESPACE
12+
namespace resource_detector
13+
{
14+
namespace resource
15+
{
16+
17+
/**
18+
* ContainerResourceDetector to detect resource attributes when running inside a containerized
19+
* environment. This detector extracts metadata such as container ID from cgroup information and
20+
* sets attributes like container.id following the OpenTelemetry semantic conventions.
21+
*/
22+
class ContainerResourceDetector : public opentelemetry::sdk::resource::ResourceDetector
23+
{
24+
public:
25+
opentelemetry::sdk::resource::Resource Detect() noexcept override;
26+
};
27+
28+
} // namespace resource
29+
} // namespace resource_detector
30+
OPENTELEMETRY_END_NAMESPACE

resource_detectors/include/opentelemetry/resource_detectors/container_resource_detector.h renamed to resource_detectors/include/opentelemetry/resource_detectors/container_detector_utils.h

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,15 @@
99
#include "opentelemetry/version.h"
1010

1111
OPENTELEMETRY_BEGIN_NAMESPACE
12-
namespace sdk
12+
namespace resource_detector
1313
{
14-
namespace resource
14+
namespace detail
1515
{
1616

17-
/**
18-
* ContainerResourceDetector to detect resource attributes when running inside a containerized
19-
* environment. This detector extracts metadata such as container ID from cgroup information and
20-
* sets attributes like container.id following the OpenTelemetry semantic conventions.
21-
*/
22-
class ContainerResourceDetector : public ResourceDetector
23-
{
24-
public:
25-
Resource Detect() noexcept override;
26-
};
27-
2817
/**
2918
* Reads the container.id from /proc/self/cgroup file.
3019
* @param file_path file path of cgroup
31-
* @return container.id as string or empty string
20+
* @return container.id as string or an empty string if not found on error
3221
*/
3322
std::string GetContainerIDFromCgroup(const char *file_path);
3423

@@ -39,6 +28,6 @@ std::string GetContainerIDFromCgroup(const char *file_path);
3928
*/
4029
std::string ExtractContainerIDFromLine(nostd::string_view line);
4130

42-
} // namespace resource
43-
} // namespace sdk
31+
} // namespace detail
32+
} // namespace resource_detector
4433
OPENTELEMETRY_END_NAMESPACE

resource_detectors/test/BUILD

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cc_test(
5+
name = "resource_detector_test",
6+
srcs = [
7+
"container_detector_test.cc",
8+
],
9+
tags = ["test"],
10+
deps = [
11+
"//api",
12+
"//resource_detectors",
13+
"@com_google_googletest//:gtest_main",
14+
],
15+
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
add_executable(resource_detector_test container_detector_test.cc)
5+
6+
# Link the required dependencies
7+
target_link_libraries(
8+
resource_detector_test PRIVATE opentelemetry_resource_detectors
9+
opentelemetry_api GTest::gtest_main)
10+
11+
gtest_add_tests(
12+
TARGET resource_detector_test
13+
TEST_PREFIX resource_detector.
14+
TEST_LIST resource_detector_test)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#include <gtest/gtest.h>
5+
#include <fstream>
6+
#include <string>
7+
#include <utility>
8+
9+
#include "opentelemetry/nostd/string_view.h"
10+
#include "opentelemetry/nostd/variant.h"
11+
#include "opentelemetry/resource_detectors/container_detector.h"
12+
#include "opentelemetry/resource_detectors/container_detector_utils.h"
13+
#include "opentelemetry/sdk/resource/resource.h"
14+
15+
TEST(ContainerIdDetectorTest, ExtractValidContainerIdFromLine)
16+
{
17+
std::string line =
18+
"13:name=systemd:/podruntime/docker/kubepods/ac679f8a8319c8cf7d38e1adf263bc08d23.aaaa";
19+
std::string extracted_id =
20+
opentelemetry::resource_detector::detail::ExtractContainerIDFromLine(line);
21+
EXPECT_EQ(std::string{"ac679f8a8319c8cf7d38e1adf263bc08d23"}, extracted_id);
22+
}
23+
24+
TEST(ContainerIdDetectorTest, ExtractIdFromMockUpCGroupFile)
25+
{
26+
const char *filename = "test_cgroup.txt";
27+
28+
{
29+
std::ofstream outfile(filename);
30+
outfile << "13:name=systemd:/kuberuntime/containerd"
31+
"/kubepods-pod872d2066_00ef_48ea_a7d8_51b18b72d739:cri-containerd:"
32+
"e857a4bf05a69080a759574949d7a0e69572e27647800fa7faff6a05a8332aa1\n";
33+
outfile << "9:cpu:/not-a-container\n";
34+
}
35+
36+
std::string container_id =
37+
opentelemetry::resource_detector::detail::GetContainerIDFromCgroup(filename);
38+
EXPECT_EQ(container_id,
39+
std::string{"e857a4bf05a69080a759574949d7a0e69572e27647800fa7faff6a05a8332aa1"});
40+
41+
std::remove(filename);
42+
}
43+
44+
TEST(ContainerIdDetectorTest, DoesNotExtractInvalidLine)
45+
{
46+
std::string line = "this line does not contain a container id";
47+
std::string id = opentelemetry::resource_detector::detail::ExtractContainerIDFromLine(line);
48+
EXPECT_EQ(id, std::string{""});
49+
}
50+
51+
TEST(ContainerIdDetectorTest, ReturnsEmptyOnNoMatch)
52+
{
53+
const char *filename = "test_empty_cgroup.txt";
54+
55+
{
56+
std::ofstream outfile(filename);
57+
outfile << "no container id here\n";
58+
}
59+
60+
std::string id = opentelemetry::resource_detector::detail::GetContainerIDFromCgroup(filename);
61+
EXPECT_EQ(id, std::string{""});
62+
63+
std::remove(filename); // cleanup
64+
}
65+
66+
TEST(ContainerIdDetectorTest, ReturnsEmptyOnFileFailingToOpen)
67+
{
68+
const char *filename = "test_invalid_cgroup.txt";
69+
70+
std::string id = opentelemetry::resource_detector::detail::GetContainerIDFromCgroup(filename);
71+
EXPECT_EQ(id, std::string{""});
72+
}

0 commit comments

Comments
 (0)