1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
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"
13 #include <unordered_map>
15 #include <gtest/gtest.h>
18 # include "opentelemetry/sdk/common/env_variables.h"
19 using opentelemetry::sdk::common::setenv
;
20 using opentelemetry::sdk::common::unsetenv
;
23 using namespace opentelemetry::sdk::resource
;
24 namespace nostd
= opentelemetry::nostd
;
26 class TestResource
: public Resource
29 TestResource(ResourceAttributes attributes
= ResourceAttributes()) : Resource(attributes
) {}
32 TEST(ResourceTest
, create_without_servicename
)
34 ResourceAttributes expected_attributes
= {
35 {"service", "backend"},
36 {"version", (uint32_t)1},
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"}};
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
)
49 EXPECT_TRUE(expected_attributes
.find(e
.first
) != expected_attributes
.end());
50 if (expected_attributes
.find(e
.first
) != expected_attributes
.end())
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
));
59 EXPECT_EQ(opentelemetry::nostd::get
<std::string
>(expected_attributes
.find(e
.first
)->second
),
60 opentelemetry::nostd::get
<std::string
>(e
.second
));
63 EXPECT_EQ(received_attributes
.size(), expected_attributes
.size()); // for missing service.name
66 TEST(ResourceTest
, create_with_servicename
)
68 ResourceAttributes expected_attributes
= {
69 {"version", (uint32_t)1},
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"},
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
)
82 EXPECT_TRUE(expected_attributes
.find(e
.first
) != expected_attributes
.end());
83 if (expected_attributes
.find(e
.first
) != expected_attributes
.end())
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
));
92 EXPECT_EQ(nostd::get
<std::string
>(expected_attributes
.find(e
.first
)->second
),
93 nostd::get
<std::string
>(e
.second
));
96 EXPECT_EQ(received_attributes
.size(), expected_attributes
.size()); // for missing service.name
99 TEST(ResourceTest
, create_with_emptyatrributes
)
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"},
107 ResourceAttributes attributes
= {};
108 auto resource
= Resource::Create(attributes
);
109 auto received_attributes
= resource
.GetAttributes();
110 for (auto &e
: received_attributes
)
112 EXPECT_TRUE(expected_attributes
.find(e
.first
) != expected_attributes
.end());
113 if (expected_attributes
.find(e
.first
) != expected_attributes
.end())
115 EXPECT_EQ(opentelemetry::nostd::get
<std::string
>(expected_attributes
.find(e
.first
)->second
),
116 opentelemetry::nostd::get
<std::string
>(e
.second
));
119 EXPECT_EQ(received_attributes
.size(), expected_attributes
.size()); // for missing service.name
122 TEST(ResourceTest
, create_with_schemaurl
)
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();
129 EXPECT_EQ(received_schema_url
, schema_url
);
132 TEST(ResourceTest
, Merge
)
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"}};
139 auto merged_resource
= resource1
.Merge(resource2
);
140 auto received_attributes
= merged_resource
.GetAttributes();
141 for (auto &e
: received_attributes
)
143 EXPECT_TRUE(expected_attributes
.find(e
.first
) != expected_attributes
.end());
144 if (expected_attributes
.find(e
.first
) != expected_attributes
.end())
146 EXPECT_EQ(expected_attributes
.find(e
.first
)->second
, nostd::get
<std::string
>(e
.second
));
149 EXPECT_EQ(received_attributes
.size(), expected_attributes
.size());
152 TEST(ResourceTest
, MergeEmptyString
)
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"}};
159 auto merged_resource
= resource1
.Merge(resource2
);
160 auto received_attributes
= merged_resource
.GetAttributes();
162 for (auto &e
: received_attributes
)
164 EXPECT_TRUE(expected_attributes
.find(e
.first
) != expected_attributes
.end());
165 if (expected_attributes
.find(e
.first
) != expected_attributes
.end())
167 EXPECT_EQ(expected_attributes
.find(e
.first
)->second
, nostd::get
<std::string
>(e
.second
));
170 EXPECT_EQ(received_attributes
.size(), expected_attributes
.size());
174 TEST(ResourceTest
, OtelResourceDetector
)
176 std::map
<std::string
, std::string
> expected_attributes
= {{"k", "v"}};
178 setenv("OTEL_RESOURCE_ATTRIBUTES", "k=v", 1);
180 OTELResourceDetector detector
;
181 auto resource
= detector
.Detect();
182 auto received_attributes
= resource
.GetAttributes();
183 for (auto &e
: received_attributes
)
185 EXPECT_TRUE(expected_attributes
.find(e
.first
) != expected_attributes
.end());
186 if (expected_attributes
.find(e
.first
) != expected_attributes
.end())
188 EXPECT_EQ(expected_attributes
.find(e
.first
)->second
, nostd::get
<std::string
>(e
.second
));
191 EXPECT_EQ(received_attributes
.size(), expected_attributes
.size());
193 unsetenv("OTEL_RESOURCE_ATTRIBUTES");
196 TEST(ResourceTest
, OtelResourceDetectorEmptyEnv
)
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
)
205 EXPECT_TRUE(expected_attributes
.find(e
.first
) != expected_attributes
.end());
206 if (expected_attributes
.find(e
.first
) != expected_attributes
.end())
208 EXPECT_EQ(expected_attributes
.find(e
.first
)->second
, nostd::get
<std::string
>(e
.second
));
211 EXPECT_EQ(received_attributes
.size(), expected_attributes
.size());