span_context_shim_test.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright The OpenTelemetry Authors
  3. * SPDX-License-Identifier: Apache-2.0
  4. */
  5. #include <gtest/gtest.h>
  6. #include <algorithm>
  7. #include <initializer_list>
  8. #include <string>
  9. #include <utility>
  10. #include <vector>
  11. #include "opentracing/noop.h"
  12. #include "opentracing/span.h"
  13. #include "opentracing/tracer.h"
  14. #include "opentracing/value.h"
  15. #include "opentelemetry/baggage/baggage.h"
  16. #include "opentelemetry/nostd/shared_ptr.h"
  17. #include "opentelemetry/nostd/unique_ptr.h"
  18. #include "opentelemetry/opentracingshim/span_context_shim.h"
  19. #include "opentelemetry/trace/span_context.h"
  20. namespace trace_api = opentelemetry::trace;
  21. namespace baggage = opentelemetry::baggage;
  22. namespace nostd = opentelemetry::nostd;
  23. namespace shim = opentelemetry::opentracingshim;
  24. class SpanContextShimTest : public testing::Test
  25. {
  26. public:
  27. nostd::unique_ptr<shim::SpanContextShim> span_context_shim;
  28. protected:
  29. virtual void SetUp() override
  30. {
  31. auto span_context = trace_api::SpanContext::GetInvalid();
  32. auto baggage = baggage::Baggage::GetDefault()->Set("foo", "bar");
  33. span_context_shim =
  34. nostd::unique_ptr<shim::SpanContextShim>(new shim::SpanContextShim(span_context, baggage));
  35. }
  36. virtual void TearDown() override { span_context_shim.reset(); }
  37. };
  38. TEST_F(SpanContextShimTest, ExtractFrom)
  39. {
  40. ASSERT_TRUE(shim::SpanContextShim::extractFrom(nullptr) == nullptr);
  41. auto tracer = opentracing::MakeNoopTracer();
  42. auto span = tracer->StartSpanWithOptions("operation", {});
  43. ASSERT_TRUE(shim::SpanContextShim::extractFrom(&span->context()) == nullptr);
  44. auto span_context_shim = nostd::shared_ptr<shim::SpanContextShim>(new shim::SpanContextShim(
  45. trace_api::SpanContext::GetInvalid(), baggage::Baggage::GetDefault()));
  46. ASSERT_TRUE(shim::SpanContextShim::extractFrom(span_context_shim.get()) != nullptr);
  47. }
  48. TEST_F(SpanContextShimTest, BaggageItem)
  49. {
  50. std::string value;
  51. ASSERT_TRUE(span_context_shim->BaggageItem("foo", value));
  52. ASSERT_EQ(value, "bar");
  53. ASSERT_FALSE(span_context_shim->BaggageItem("", value));
  54. }
  55. TEST_F(SpanContextShimTest, NewWithKeyValue)
  56. {
  57. auto new_span_context_shim = span_context_shim->newWithKeyValue("test", "this");
  58. ASSERT_NE(span_context_shim.get(), &new_span_context_shim);
  59. ASSERT_EQ(span_context_shim->context(), new_span_context_shim.context());
  60. ASSERT_EQ(span_context_shim->context().IsValid(), new_span_context_shim.context().IsValid());
  61. ASSERT_EQ(span_context_shim->context().IsRemote(), new_span_context_shim.context().IsRemote());
  62. std::string value;
  63. ASSERT_TRUE(new_span_context_shim.BaggageItem("foo", value));
  64. ASSERT_EQ(value, "bar");
  65. ASSERT_TRUE(new_span_context_shim.BaggageItem("test", value));
  66. ASSERT_EQ(value, "this");
  67. }
  68. TEST_F(SpanContextShimTest, ForeachBaggageItem)
  69. {
  70. std::initializer_list<std::pair<std::string, std::string>> list{
  71. {"foo", "bar"}, {"bar", "baz"}, {"baz", "foo"}};
  72. nostd::shared_ptr<baggage::Baggage> baggage(new baggage::Baggage(list));
  73. shim::SpanContextShim new_span_context_shim(span_context_shim->context(), baggage);
  74. std::vector<std::string> concatenated;
  75. new_span_context_shim.ForeachBaggageItem(
  76. [&concatenated](const std::string &key, const std::string &value) {
  77. concatenated.emplace_back(key + ":" + value);
  78. return true;
  79. });
  80. ASSERT_EQ(concatenated.size(), 3);
  81. ASSERT_EQ(concatenated[0], "foo:bar");
  82. ASSERT_EQ(concatenated[1], "bar:baz");
  83. ASSERT_EQ(concatenated[2], "baz:foo");
  84. }
  85. TEST_F(SpanContextShimTest, Clone)
  86. {
  87. auto new_span_context = span_context_shim->Clone();
  88. auto new_span_context_shim = static_cast<shim::SpanContextShim *>(new_span_context.get());
  89. ASSERT_TRUE(new_span_context_shim != nullptr);
  90. ASSERT_NE(span_context_shim.get(), new_span_context_shim);
  91. ASSERT_EQ(span_context_shim->context(), new_span_context_shim->context());
  92. ASSERT_EQ(span_context_shim->context().IsValid(), new_span_context_shim->context().IsValid());
  93. ASSERT_EQ(span_context_shim->context().IsRemote(), new_span_context_shim->context().IsRemote());
  94. std::string value;
  95. std::string new_value;
  96. ASSERT_TRUE(span_context_shim->BaggageItem("foo", value));
  97. ASSERT_TRUE(new_span_context_shim->BaggageItem("foo", new_value));
  98. ASSERT_EQ(value, new_value);
  99. }