]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/sdk/test/resource/resource_test.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / sdk / test / resource / resource_test.cc
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include "opentelemetry/sdk/resource/resource.h"
5 #include "opentelemetry/common/key_value_iterable_view.h"
6 #include "opentelemetry/nostd/string_view.h"
7 #include "opentelemetry/sdk/common/attribute_utils.h"
8 #include "opentelemetry/sdk/resource/experimental_semantic_conventions.h"
9 #include "opentelemetry/sdk/resource/resource_detector.h"
10
11 #include <cstdlib>
12 #include <string>
13 #include <unordered_map>
14
15 #include <gtest/gtest.h>
16
17 #if defined(_MSC_VER)
18 # include "opentelemetry/sdk/common/env_variables.h"
19 using opentelemetry::sdk::common::setenv;
20 using opentelemetry::sdk::common::unsetenv;
21 #endif
22
23 using namespace opentelemetry::sdk::resource;
24 namespace nostd = opentelemetry::nostd;
25
26 class TestResource : public Resource
27 {
28 public:
29 TestResource(ResourceAttributes attributes = ResourceAttributes()) : Resource(attributes) {}
30 };
31
32 TEST(ResourceTest, create_without_servicename)
33 {
34 ResourceAttributes expected_attributes = {
35 {"service", "backend"},
36 {"version", (uint32_t)1},
37 {"cost", 234.23},
38 {OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkLanguage), "cpp"},
39 {OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkName), "opentelemetry"},
40 {OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkVersion), OPENTELEMETRY_SDK_VERSION},
41 {OTEL_GET_RESOURCE_ATTR(AttrServiceName), "unknown_service"}};
42
43 ResourceAttributes attributes = {
44 {"service", "backend"}, {"version", (uint32_t)1}, {"cost", 234.23}};
45 auto resource = Resource::Create(attributes);
46 auto received_attributes = resource.GetAttributes();
47 for (auto &e : received_attributes)
48 {
49 EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end());
50 if (expected_attributes.find(e.first) != expected_attributes.end())
51 {
52 if (e.first == "version")
53 EXPECT_EQ(nostd::get<uint32_t>(expected_attributes.find(e.first)->second),
54 nostd::get<uint32_t>(e.second));
55 else if (e.first == "cost")
56 EXPECT_EQ(nostd::get<double>(expected_attributes.find(e.first)->second),
57 nostd::get<double>(e.second));
58 else
59 EXPECT_EQ(opentelemetry::nostd::get<std::string>(expected_attributes.find(e.first)->second),
60 opentelemetry::nostd::get<std::string>(e.second));
61 }
62 }
63 EXPECT_EQ(received_attributes.size(), expected_attributes.size()); // for missing service.name
64 }
65
66 TEST(ResourceTest, create_with_servicename)
67 {
68 ResourceAttributes expected_attributes = {
69 {"version", (uint32_t)1},
70 {"cost", 234.23},
71 {OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkLanguage), "cpp"},
72 {OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkName), "opentelemetry"},
73 {OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkVersion), OPENTELEMETRY_SDK_VERSION},
74 {OTEL_GET_RESOURCE_ATTR(AttrServiceName), "backend"},
75 };
76 ResourceAttributes attributes = {
77 {"service.name", "backend"}, {"version", (uint32_t)1}, {"cost", 234.23}};
78 auto resource = Resource::Create(attributes);
79 auto received_attributes = resource.GetAttributes();
80 for (auto &e : received_attributes)
81 {
82 EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end());
83 if (expected_attributes.find(e.first) != expected_attributes.end())
84 {
85 if (e.first == "version")
86 EXPECT_EQ(nostd::get<uint32_t>(expected_attributes.find(e.first)->second),
87 nostd::get<uint32_t>(e.second));
88 else if (e.first == "cost")
89 EXPECT_EQ(nostd::get<double>(expected_attributes.find(e.first)->second),
90 nostd::get<double>(e.second));
91 else
92 EXPECT_EQ(nostd::get<std::string>(expected_attributes.find(e.first)->second),
93 nostd::get<std::string>(e.second));
94 }
95 }
96 EXPECT_EQ(received_attributes.size(), expected_attributes.size()); // for missing service.name
97 }
98
99 TEST(ResourceTest, create_with_emptyatrributes)
100 {
101 ResourceAttributes expected_attributes = {
102 {OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkLanguage), "cpp"},
103 {OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkName), "opentelemetry"},
104 {OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkVersion), OPENTELEMETRY_SDK_VERSION},
105 {OTEL_GET_RESOURCE_ATTR(AttrServiceName), "unknown_service"},
106 };
107 ResourceAttributes attributes = {};
108 auto resource = Resource::Create(attributes);
109 auto received_attributes = resource.GetAttributes();
110 for (auto &e : received_attributes)
111 {
112 EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end());
113 if (expected_attributes.find(e.first) != expected_attributes.end())
114 {
115 EXPECT_EQ(opentelemetry::nostd::get<std::string>(expected_attributes.find(e.first)->second),
116 opentelemetry::nostd::get<std::string>(e.second));
117 }
118 }
119 EXPECT_EQ(received_attributes.size(), expected_attributes.size()); // for missing service.name
120 }
121
122 TEST(ResourceTest, create_with_schemaurl)
123 {
124 const std::string schema_url = "https://opentelemetry.io/schemas/1.2.0";
125 ResourceAttributes attributes = {};
126 auto resource = Resource::Create(attributes, schema_url);
127 auto received_schema_url = resource.GetSchemaURL();
128
129 EXPECT_EQ(received_schema_url, schema_url);
130 }
131
132 TEST(ResourceTest, Merge)
133 {
134 TestResource resource1(ResourceAttributes({{"service", "backend"}}));
135 TestResource resource2(ResourceAttributes({{"host", "service-host"}}));
136 std::map<std::string, std::string> expected_attributes = {{"service", "backend"},
137 {"host", "service-host"}};
138
139 auto merged_resource = resource1.Merge(resource2);
140 auto received_attributes = merged_resource.GetAttributes();
141 for (auto &e : received_attributes)
142 {
143 EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end());
144 if (expected_attributes.find(e.first) != expected_attributes.end())
145 {
146 EXPECT_EQ(expected_attributes.find(e.first)->second, nostd::get<std::string>(e.second));
147 }
148 }
149 EXPECT_EQ(received_attributes.size(), expected_attributes.size());
150 }
151
152 TEST(ResourceTest, MergeEmptyString)
153 {
154 TestResource resource1({{"service", "backend"}, {"host", "service-host"}});
155 TestResource resource2({{"service", ""}, {"host", "another-service-host"}});
156 std::map<std::string, std::string> expected_attributes = {{"service", ""},
157 {"host", "another-service-host"}};
158
159 auto merged_resource = resource1.Merge(resource2);
160 auto received_attributes = merged_resource.GetAttributes();
161
162 for (auto &e : received_attributes)
163 {
164 EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end());
165 if (expected_attributes.find(e.first) != expected_attributes.end())
166 {
167 EXPECT_EQ(expected_attributes.find(e.first)->second, nostd::get<std::string>(e.second));
168 }
169 }
170 EXPECT_EQ(received_attributes.size(), expected_attributes.size());
171 }
172
173 #ifndef NO_GETENV
174 TEST(ResourceTest, OtelResourceDetector)
175 {
176 std::map<std::string, std::string> expected_attributes = {{"k", "v"}};
177
178 setenv("OTEL_RESOURCE_ATTRIBUTES", "k=v", 1);
179
180 OTELResourceDetector detector;
181 auto resource = detector.Detect();
182 auto received_attributes = resource.GetAttributes();
183 for (auto &e : received_attributes)
184 {
185 EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end());
186 if (expected_attributes.find(e.first) != expected_attributes.end())
187 {
188 EXPECT_EQ(expected_attributes.find(e.first)->second, nostd::get<std::string>(e.second));
189 }
190 }
191 EXPECT_EQ(received_attributes.size(), expected_attributes.size());
192
193 unsetenv("OTEL_RESOURCE_ATTRIBUTES");
194 }
195
196 TEST(ResourceTest, OtelResourceDetectorEmptyEnv)
197 {
198 std::map<std::string, std::string> expected_attributes = {};
199 unsetenv("OTEL_RESOURCE_ATTRIBUTES");
200 OTELResourceDetector detector;
201 auto resource = detector.Detect();
202 auto received_attributes = resource.GetAttributes();
203 for (auto &e : received_attributes)
204 {
205 EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end());
206 if (expected_attributes.find(e.first) != expected_attributes.end())
207 {
208 EXPECT_EQ(expected_attributes.find(e.first)->second, nostd::get<std::string>(e.second));
209 }
210 }
211 EXPECT_EQ(received_attributes.size(), expected_attributes.size());
212 }
213 #endif