tracer_config_test.cc 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include "opentelemetry/sdk/trace/tracer_config.h"
  4. #include <gtest/gtest.h>
  5. #include <array>
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include "opentelemetry/common/attribute_value.h"
  10. #include "opentelemetry/nostd/string_view.h"
  11. #include "opentelemetry/nostd/unique_ptr.h"
  12. #include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h"
  13. #include "opentelemetry/sdk/instrumentationscope/scope_configurator.h"
  14. namespace trace_sdk = opentelemetry::sdk::trace;
  15. namespace instrumentation_scope = opentelemetry::sdk::instrumentationscope;
  16. /** Tests to verify the basic behavior of trace_sdk::TracerConfig */
  17. TEST(TracerConfig, CheckDisabledWorksAsExpected)
  18. {
  19. trace_sdk::TracerConfig disabled_config = trace_sdk::TracerConfig::Disabled();
  20. ASSERT_FALSE(disabled_config.IsEnabled());
  21. }
  22. TEST(TracerConfig, CheckEnabledWorksAsExpected)
  23. {
  24. trace_sdk::TracerConfig enabled_config = trace_sdk::TracerConfig::Enabled();
  25. ASSERT_TRUE(enabled_config.IsEnabled());
  26. }
  27. TEST(TracerConfig, CheckDefaultConfigWorksAccToSpec)
  28. {
  29. trace_sdk::TracerConfig default_config = trace_sdk::TracerConfig::Default();
  30. ASSERT_TRUE(default_config.IsEnabled());
  31. }
  32. /** Tests to verify the behavior of trace_sdk::TracerConfig::DefaultConfigurator */
  33. static std::pair<opentelemetry::nostd::string_view, opentelemetry::common::AttributeValue> attr1 = {
  34. "accept_single_attr", true};
  35. static std::pair<opentelemetry::nostd::string_view, opentelemetry::common::AttributeValue> attr2 = {
  36. "accept_second_attr", "some other attr"};
  37. static std::pair<opentelemetry::nostd::string_view, opentelemetry::common::AttributeValue> attr3 = {
  38. "accept_third_attr", 3};
  39. static instrumentation_scope::InstrumentationScope test_scope_1 =
  40. *instrumentation_scope::InstrumentationScope::Create("test_scope_1");
  41. static instrumentation_scope::InstrumentationScope test_scope_2 =
  42. *instrumentation_scope::InstrumentationScope::Create("test_scope_2", "1.0");
  43. static instrumentation_scope::InstrumentationScope test_scope_3 =
  44. *instrumentation_scope::InstrumentationScope::Create(
  45. "test_scope_3",
  46. "0",
  47. "https://opentelemetry.io/schemas/v1.18.0");
  48. static instrumentation_scope::InstrumentationScope test_scope_4 =
  49. *instrumentation_scope::InstrumentationScope::Create("test_scope_4",
  50. "0",
  51. "https://opentelemetry.io/schemas/v1.18.0",
  52. {attr1});
  53. static instrumentation_scope::InstrumentationScope test_scope_5 =
  54. *instrumentation_scope::InstrumentationScope::Create("test_scope_5",
  55. "0",
  56. "https://opentelemetry.io/schemas/v1.18.0",
  57. {attr1, attr2, attr3});
  58. // This array could also directly contain the reference types, but that leads to 'uninitialized
  59. // value was created by heap allocation' errors in Valgrind memcheck. This is a bug in Googletest
  60. // library, see https://github.com/google/googletest/issues/3805#issuecomment-1397301790 for more
  61. // details. Using pointers is a workaround to prevent the Valgrind warnings.
  62. const std::array<instrumentation_scope::InstrumentationScope *, 5> instrumentation_scopes = {
  63. &test_scope_1, &test_scope_2, &test_scope_3, &test_scope_4, &test_scope_5,
  64. };
  65. // Test fixture for VerifyDefaultConfiguratorBehavior
  66. class DefaultTracerConfiguratorTestFixture
  67. : public ::testing::TestWithParam<instrumentation_scope::InstrumentationScope *>
  68. {};
  69. // verifies that the default configurator always returns the default tracer config
  70. TEST_P(DefaultTracerConfiguratorTestFixture, VerifyDefaultConfiguratorBehavior)
  71. {
  72. instrumentation_scope::InstrumentationScope *scope = GetParam();
  73. instrumentation_scope::ScopeConfigurator<trace_sdk::TracerConfig> default_configurator =
  74. instrumentation_scope::ScopeConfigurator<trace_sdk::TracerConfig>::Builder(
  75. trace_sdk::TracerConfig::Default())
  76. .Build();
  77. ASSERT_EQ(default_configurator.ComputeConfig(*scope), trace_sdk::TracerConfig::Default());
  78. }
  79. INSTANTIATE_TEST_SUITE_P(InstrumentationScopes,
  80. DefaultTracerConfiguratorTestFixture,
  81. ::testing::ValuesIn(instrumentation_scopes));