Skip to content

Commit 4bf2bfb

Browse files
[sdk] minor changes in format, linking and var naming
1 parent c740ddb commit 4bf2bfb

File tree

10 files changed

+74
-49
lines changed

10 files changed

+74
-49
lines changed

sdk/include/opentelemetry/sdk/common/container.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ std::string GetContainerIDFromCgroup(const char *file_path);
2121

2222
/**
2323
Matches the line with the regex to find container.id
24-
@param line contains
24+
@param line a single line of text, typically from the /proc/self/cgroup file
2525
@return matched id or empty string
2626
*/
27-
std::string ExtractContainerIDFromLine(std::string &line);
27+
std::string ExtractContainerIDFromLine(const std::string &line);
2828
} // namespace common
2929
} // namespace sdk
3030
OPENTELEMETRY_END_NAMESPACE

sdk/include/opentelemetry/sdk/resource/resource_detector.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ class OTELResourceDetector : public ResourceDetector
4040
};
4141

4242
/**
43-
* ContainerResourceDetector to detect resource attributes when running inside a containerized environment.
44-
* This detector extracts metadata such as container ID from cgroup information
45-
* and sets attributes like container.id following the OpenTelemetry semantic conventions.
43+
* ContainerResourceDetector to detect resource attributes when running inside a containerized
44+
* environment. This detector extracts metadata such as container ID from cgroup information and
45+
* sets attributes like container.id following the OpenTelemetry semantic conventions.
4646
*/
4747
class ContainerResourceDetector : public ResourceDetector
4848
{

sdk/src/common/CMakeLists.txt

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

4-
set(COMMON_SRCS random.cc global_log_handler.cc env_variables.cc container.cc base64.cc
5-
disabled.cc)
4+
set(COMMON_SRCS random.cc global_log_handler.cc env_variables.cc container.cc
5+
base64.cc disabled.cc)
66
if(WIN32)
77
list(APPEND COMMON_SRCS platform/fork_windows.cc)
88
else()

sdk/src/common/container.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,23 @@ namespace sdk
2020
{
2121
namespace common
2222
{
23-
std::string GetContainerIDFromCgroup(const char* file_path)
23+
std::string GetContainerIDFromCgroup(const char *file_path)
2424
{
2525
std::ifstream cgroup_file(file_path);
2626
std::string line;
2727

28-
while(std::getline(cgroup_file, line))
28+
while (std::getline(cgroup_file, line))
2929
{
3030
std::string container_id = ExtractContainerIDFromLine(line);
31-
if(!container_id.empty())
31+
if (!container_id.empty())
3232
{
33-
return container_id;
33+
return container_id;
3434
}
3535
}
3636
return "";
3737
}
3838

39-
std::string ExtractContainerIDFromLine(std::string &line)
39+
std::string ExtractContainerIDFromLine(const std::string &line)
4040
{
4141
static const std::regex container_id_regex(R"(^.*/(?:.*[-:])?([0-9a-f]+)(?:\.|\s*$))");
4242
std::smatch match;

sdk/src/resource/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ cc_library(
1111
"//api",
1212
"//sdk:headers",
1313
"//sdk/src/common:env_variables",
14+
"//sdk/src/common:container",
1415
],
1516
)

sdk/src/resource/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright The OpenTelemetry Authors
22
# SPDX-License-Identifier: Apache-2.0
33

4-
add_library(opentelemetry_resources resource.cc resource_detector.cc)
4+
add_library(opentelemetry_resources resource.cc resource_detector.cc container_detector.cc)
55

66
set_target_properties(opentelemetry_resources PROPERTIES EXPORT_NAME resources)
77
set_target_version(opentelemetry_resources)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#include "opentelemetry/sdk/common/container.h"
5+
#include "opentelemetry/sdk/resource/resource.h"
6+
#include "opentelemetry/sdk/resource/resource_detector.h"
7+
#include "opentelemetry/semconv/incubating/container_attributes.h"
8+
#include "opentelemetry/version.h"
9+
10+
#include <string>
11+
12+
OPENTELEMETRY_BEGIN_NAMESPACE
13+
namespace sdk
14+
{
15+
namespace resource
16+
{
17+
18+
/**
19+
* This is the file path from where we can get container.id
20+
*/
21+
const char *KCGroupPath = "/proc/self/cgroup";
22+
23+
Resource ContainerResourceDetector::Detect() noexcept
24+
{
25+
std::string container_id = opentelemetry::sdk::common::GetContainerIDFromCgroup(KCGroupPath);
26+
if (container_id.empty())
27+
{
28+
return ResourceDetector::Create({});
29+
}
30+
31+
ResourceAttributes attributes;
32+
33+
attributes[semconv::container::kContainerId] = container_id;
34+
return ResourceDetector::Create(attributes);
35+
}
36+
37+
} // namespace resource
38+
} // namespace sdk
39+
OPENTELEMETRY_END_NAMESPACE

sdk/src/resource/resource.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ Resource Resource::Merge(const Resource &other) const noexcept
4040

4141
Resource Resource::Create(const ResourceAttributes &attributes, const std::string &schema_url)
4242
{
43-
static auto otel_resource = OTELResourceDetector().Detect();
43+
static auto otel_resource = OTELResourceDetector().Detect();
4444
static auto container_resource = ContainerResourceDetector().Detect();
45-
auto resource =
46-
Resource::GetDefault().Merge(otel_resource).Merge(container_resource).Merge(Resource{attributes, schema_url});
45+
auto resource = Resource::GetDefault()
46+
.Merge(otel_resource)
47+
.Merge(container_resource)
48+
.Merge(Resource{attributes, schema_url});
4749

4850
if (resource.attributes_.find(semconv::service::kServiceName) == resource.attributes_.end())
4951
{

sdk/src/resource/resource_detector.cc

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
#include "opentelemetry/sdk/resource/resource_detector.h"
55
#include "opentelemetry/nostd/variant.h"
6-
#include "opentelemetry/sdk/common/env_variables.h"
76
#include "opentelemetry/sdk/common/container.h"
7+
#include "opentelemetry/sdk/common/env_variables.h"
88
#include "opentelemetry/sdk/resource/resource.h"
9-
#include "opentelemetry/semconv/service_attributes.h"
109
#include "opentelemetry/semconv/incubating/container_attributes.h"
10+
#include "opentelemetry/semconv/service_attributes.h"
1111
#include "opentelemetry/version.h"
1212

1313
#include <stddef.h>
@@ -21,12 +21,8 @@ namespace sdk
2121
namespace resource
2222
{
2323

24-
const char *OTEL_RESOURCE_ATTRIBUTES = "OTEL_RESOURCE_ATTRIBUTES";
25-
const char *OTEL_SERVICE_NAME = "OTEL_SERVICE_NAME";
26-
/**
27-
* This is the file path from where we can get container.id
28-
*/
29-
const char *C_GROUP_PATH = "/proc/self/cgroup";
24+
const char *KOtelResourceAttributes = "OTEL_RESOURCE_ATTRIBUTES";
25+
const char *KOtelServiceName = "OTEL_SERVICE_NAME";
3026

3127
Resource ResourceDetector::Create(const ResourceAttributes &attributes,
3228
const std::string &schema_url)
@@ -39,9 +35,9 @@ Resource OTELResourceDetector::Detect() noexcept
3935
std::string attributes_str, service_name;
4036

4137
bool attributes_exists = opentelemetry::sdk::common::GetStringEnvironmentVariable(
42-
OTEL_RESOURCE_ATTRIBUTES, attributes_str);
38+
KOtelResourceAttributes, attributes_str);
4339
bool service_name_exists =
44-
opentelemetry::sdk::common::GetStringEnvironmentVariable(OTEL_SERVICE_NAME, service_name);
40+
opentelemetry::sdk::common::GetStringEnvironmentVariable(KOtelServiceName, service_name);
4541

4642
if (!attributes_exists && !service_name_exists)
4743
{
@@ -74,23 +70,6 @@ Resource OTELResourceDetector::Detect() noexcept
7470
return ResourceDetector::Create(attributes);
7571
}
7672

77-
Resource ContainerResourceDetector::Detect() noexcept
78-
{
79-
std::string container_id = opentelemetry::sdk::common::GetContainerIDFromCgroup(C_GROUP_PATH);
80-
if(container_id.empty())
81-
{
82-
return ResourceDetector::Create({});
83-
}
84-
85-
ResourceAttributes attributes;
86-
87-
if(!container_id.empty())
88-
{
89-
attributes[semconv::container::kContainerId] = container_id;
90-
}
91-
return ResourceDetector::Create(attributes);
92-
}
93-
9473
} // namespace resource
9574
} // namespace sdk
9675
OPENTELEMETRY_END_NAMESPACE

sdk/test/resource/resource_test.cc

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
#include <gtest/gtest.h>
55
#include <stdint.h>
66
#include <cstdlib>
7+
#include <fstream>
78
#include <map>
89
#include <string>
910
#include <unordered_map>
1011
#include <utility>
11-
#include<fstream>
1212

1313
#include "opentelemetry/nostd/variant.h"
1414
#include "opentelemetry/sdk/common/attribute_utils.h"
@@ -295,9 +295,10 @@ TEST(ResourceTest, DerivedResourceDetector)
295295
EXPECT_TRUE(received_attributes.find("key") != received_attributes.end());
296296
}
297297

298-
TEST(ResourceTest, ExctractValidContainerId)
298+
TEST(ResourceTest, ExtractValidContainerId)
299299
{
300-
std::string line = "13:name=systemd:/podruntime/docker/kubepods/ac679f8a8319c8cf7d38e1adf263bc08d23.aaaa";
300+
std::string line =
301+
"13:name=systemd:/podruntime/docker/kubepods/ac679f8a8319c8cf7d38e1adf263bc08d23.aaaa";
301302
std::string extracted_id = opentelemetry::sdk::common::ExtractContainerIDFromLine(line);
302303
EXPECT_EQ(std::string{"ac679f8a8319c8cf7d38e1adf263bc08d23"}, extracted_id);
303304
}
@@ -308,20 +309,23 @@ TEST(ResourceTest, ExtractIdFromMockUpCGroupFile)
308309

309310
{
310311
std::ofstream outfile(filename);
311-
outfile << "13:name=systemd:/kuberuntime/containerd/kubepods-pod872d2066_00ef_48ea_a7d8_51b18b72d739:cri-containerd:e857a4bf05a69080a759574949d7a0e69572e27647800fa7faff6a05a8332aa1\n";
312+
outfile << "13:name=systemd:/kuberuntime/containerd"
313+
"/kubepods-pod872d2066_00ef_48ea_a7d8_51b18b72d739:cri-containerd:"
314+
"e857a4bf05a69080a759574949d7a0e69572e27647800fa7faff6a05a8332aa1\n";
312315
outfile << "9:cpu:/not-a-container\n";
313316
}
314317

315318
std::string container_id = opentelemetry::sdk::common::GetContainerIDFromCgroup(filename);
316-
EXPECT_EQ(container_id, std::string{"e857a4bf05a69080a759574949d7a0e69572e27647800fa7faff6a05a8332aa1"});
319+
EXPECT_EQ(container_id,
320+
std::string{"e857a4bf05a69080a759574949d7a0e69572e27647800fa7faff6a05a8332aa1"});
317321

318322
std::remove(filename);
319323
}
320324

321325
TEST(ResourceTest, DoesNotExtractInvalidLine)
322326
{
323327
std::string line = "this line does not contain a container id";
324-
std::string id = opentelemetry::sdk::common::ExtractContainerIDFromLine(line);
328+
std::string id = opentelemetry::sdk::common::ExtractContainerIDFromLine(line);
325329
EXPECT_EQ(id, std::string{""});
326330
}
327331

0 commit comments

Comments
 (0)