diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
commit | e6918187568dbd01842d8d1d2c808ce16a894239 (patch) | |
tree | 64f88b554b444a49f656b6c656111a145cbbaa28 /src/jaegertracing/opentelemetry-cpp/sdk/test/resource | |
parent | Initial commit. (diff) | |
download | ceph-e6918187568dbd01842d8d1d2c808ce16a894239.tar.xz ceph-e6918187568dbd01842d8d1d2c808ce16a894239.zip |
Adding upstream version 18.2.2.upstream/18.2.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/jaegertracing/opentelemetry-cpp/sdk/test/resource')
3 files changed, 234 insertions, 0 deletions
diff --git a/src/jaegertracing/opentelemetry-cpp/sdk/test/resource/BUILD b/src/jaegertracing/opentelemetry-cpp/sdk/test/resource/BUILD new file mode 100644 index 000000000..e70f16ee7 --- /dev/null +++ b/src/jaegertracing/opentelemetry-cpp/sdk/test/resource/BUILD @@ -0,0 +1,12 @@ +cc_test( + name = "resource_test", + srcs = [ + "resource_test.cc", + ], + tags = ["test"], + deps = [ + "//api", + "//sdk/src/resource", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/src/jaegertracing/opentelemetry-cpp/sdk/test/resource/CMakeLists.txt b/src/jaegertracing/opentelemetry-cpp/sdk/test/resource/CMakeLists.txt new file mode 100644 index 000000000..514b98680 --- /dev/null +++ b/src/jaegertracing/opentelemetry-cpp/sdk/test/resource/CMakeLists.txt @@ -0,0 +1,9 @@ +foreach(testname resource_test) + add_executable(${testname} "${testname}.cc") + target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} opentelemetry_resources) + gtest_add_tests( + TARGET ${testname} + TEST_PREFIX resources. + TEST_LIST ${testname}) +endforeach() diff --git a/src/jaegertracing/opentelemetry-cpp/sdk/test/resource/resource_test.cc b/src/jaegertracing/opentelemetry-cpp/sdk/test/resource/resource_test.cc new file mode 100644 index 000000000..5f058eafb --- /dev/null +++ b/src/jaegertracing/opentelemetry-cpp/sdk/test/resource/resource_test.cc @@ -0,0 +1,213 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/sdk/resource/resource.h" +#include "opentelemetry/common/key_value_iterable_view.h" +#include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/sdk/common/attribute_utils.h" +#include "opentelemetry/sdk/resource/experimental_semantic_conventions.h" +#include "opentelemetry/sdk/resource/resource_detector.h" + +#include <cstdlib> +#include <string> +#include <unordered_map> + +#include <gtest/gtest.h> + +#if defined(_MSC_VER) +# include "opentelemetry/sdk/common/env_variables.h" +using opentelemetry::sdk::common::setenv; +using opentelemetry::sdk::common::unsetenv; +#endif + +using namespace opentelemetry::sdk::resource; +namespace nostd = opentelemetry::nostd; + +class TestResource : public Resource +{ +public: + TestResource(ResourceAttributes attributes = ResourceAttributes()) : Resource(attributes) {} +}; + +TEST(ResourceTest, create_without_servicename) +{ + ResourceAttributes expected_attributes = { + {"service", "backend"}, + {"version", (uint32_t)1}, + {"cost", 234.23}, + {OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkLanguage), "cpp"}, + {OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkName), "opentelemetry"}, + {OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkVersion), OPENTELEMETRY_SDK_VERSION}, + {OTEL_GET_RESOURCE_ATTR(AttrServiceName), "unknown_service"}}; + + ResourceAttributes attributes = { + {"service", "backend"}, {"version", (uint32_t)1}, {"cost", 234.23}}; + auto resource = Resource::Create(attributes); + auto received_attributes = resource.GetAttributes(); + for (auto &e : received_attributes) + { + EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end()); + if (expected_attributes.find(e.first) != expected_attributes.end()) + { + if (e.first == "version") + EXPECT_EQ(nostd::get<uint32_t>(expected_attributes.find(e.first)->second), + nostd::get<uint32_t>(e.second)); + else if (e.first == "cost") + EXPECT_EQ(nostd::get<double>(expected_attributes.find(e.first)->second), + nostd::get<double>(e.second)); + else + EXPECT_EQ(opentelemetry::nostd::get<std::string>(expected_attributes.find(e.first)->second), + opentelemetry::nostd::get<std::string>(e.second)); + } + } + EXPECT_EQ(received_attributes.size(), expected_attributes.size()); // for missing service.name +} + +TEST(ResourceTest, create_with_servicename) +{ + ResourceAttributes expected_attributes = { + {"version", (uint32_t)1}, + {"cost", 234.23}, + {OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkLanguage), "cpp"}, + {OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkName), "opentelemetry"}, + {OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkVersion), OPENTELEMETRY_SDK_VERSION}, + {OTEL_GET_RESOURCE_ATTR(AttrServiceName), "backend"}, + }; + ResourceAttributes attributes = { + {"service.name", "backend"}, {"version", (uint32_t)1}, {"cost", 234.23}}; + auto resource = Resource::Create(attributes); + auto received_attributes = resource.GetAttributes(); + for (auto &e : received_attributes) + { + EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end()); + if (expected_attributes.find(e.first) != expected_attributes.end()) + { + if (e.first == "version") + EXPECT_EQ(nostd::get<uint32_t>(expected_attributes.find(e.first)->second), + nostd::get<uint32_t>(e.second)); + else if (e.first == "cost") + EXPECT_EQ(nostd::get<double>(expected_attributes.find(e.first)->second), + nostd::get<double>(e.second)); + else + EXPECT_EQ(nostd::get<std::string>(expected_attributes.find(e.first)->second), + nostd::get<std::string>(e.second)); + } + } + EXPECT_EQ(received_attributes.size(), expected_attributes.size()); // for missing service.name +} + +TEST(ResourceTest, create_with_emptyatrributes) +{ + ResourceAttributes expected_attributes = { + {OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkLanguage), "cpp"}, + {OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkName), "opentelemetry"}, + {OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkVersion), OPENTELEMETRY_SDK_VERSION}, + {OTEL_GET_RESOURCE_ATTR(AttrServiceName), "unknown_service"}, + }; + ResourceAttributes attributes = {}; + auto resource = Resource::Create(attributes); + auto received_attributes = resource.GetAttributes(); + for (auto &e : received_attributes) + { + EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end()); + if (expected_attributes.find(e.first) != expected_attributes.end()) + { + EXPECT_EQ(opentelemetry::nostd::get<std::string>(expected_attributes.find(e.first)->second), + opentelemetry::nostd::get<std::string>(e.second)); + } + } + EXPECT_EQ(received_attributes.size(), expected_attributes.size()); // for missing service.name +} + +TEST(ResourceTest, create_with_schemaurl) +{ + const std::string schema_url = "https://opentelemetry.io/schemas/1.2.0"; + ResourceAttributes attributes = {}; + auto resource = Resource::Create(attributes, schema_url); + auto received_schema_url = resource.GetSchemaURL(); + + EXPECT_EQ(received_schema_url, schema_url); +} + +TEST(ResourceTest, Merge) +{ + TestResource resource1(ResourceAttributes({{"service", "backend"}})); + TestResource resource2(ResourceAttributes({{"host", "service-host"}})); + std::map<std::string, std::string> expected_attributes = {{"service", "backend"}, + {"host", "service-host"}}; + + auto merged_resource = resource1.Merge(resource2); + auto received_attributes = merged_resource.GetAttributes(); + for (auto &e : received_attributes) + { + EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end()); + if (expected_attributes.find(e.first) != expected_attributes.end()) + { + EXPECT_EQ(expected_attributes.find(e.first)->second, nostd::get<std::string>(e.second)); + } + } + EXPECT_EQ(received_attributes.size(), expected_attributes.size()); +} + +TEST(ResourceTest, MergeEmptyString) +{ + TestResource resource1({{"service", "backend"}, {"host", "service-host"}}); + TestResource resource2({{"service", ""}, {"host", "another-service-host"}}); + std::map<std::string, std::string> expected_attributes = {{"service", ""}, + {"host", "another-service-host"}}; + + auto merged_resource = resource1.Merge(resource2); + auto received_attributes = merged_resource.GetAttributes(); + + for (auto &e : received_attributes) + { + EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end()); + if (expected_attributes.find(e.first) != expected_attributes.end()) + { + EXPECT_EQ(expected_attributes.find(e.first)->second, nostd::get<std::string>(e.second)); + } + } + EXPECT_EQ(received_attributes.size(), expected_attributes.size()); +} + +#ifndef NO_GETENV +TEST(ResourceTest, OtelResourceDetector) +{ + std::map<std::string, std::string> expected_attributes = {{"k", "v"}}; + + setenv("OTEL_RESOURCE_ATTRIBUTES", "k=v", 1); + + OTELResourceDetector detector; + auto resource = detector.Detect(); + auto received_attributes = resource.GetAttributes(); + for (auto &e : received_attributes) + { + EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end()); + if (expected_attributes.find(e.first) != expected_attributes.end()) + { + EXPECT_EQ(expected_attributes.find(e.first)->second, nostd::get<std::string>(e.second)); + } + } + EXPECT_EQ(received_attributes.size(), expected_attributes.size()); + + unsetenv("OTEL_RESOURCE_ATTRIBUTES"); +} + +TEST(ResourceTest, OtelResourceDetectorEmptyEnv) +{ + std::map<std::string, std::string> expected_attributes = {}; + unsetenv("OTEL_RESOURCE_ATTRIBUTES"); + OTELResourceDetector detector; + auto resource = detector.Detect(); + auto received_attributes = resource.GetAttributes(); + for (auto &e : received_attributes) + { + EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end()); + if (expected_attributes.find(e.first) != expected_attributes.end()) + { + EXPECT_EQ(expected_attributes.find(e.first)->second, nostd::get<std::string>(e.second)); + } + } + EXPECT_EQ(received_attributes.size(), expected_attributes.size()); +} +#endif |