-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
259 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "span_shim.h" | ||
#include "tracer_shim.h" | ||
#include "shim_mocks.h" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
namespace trace_api = opentelemetry::trace; | ||
namespace baggage = opentelemetry::baggage; | ||
namespace nostd = opentelemetry::nostd; | ||
namespace shim = opentelemetry::opentracingshim; | ||
|
||
class SpanShimTest : public testing::Test | ||
{ | ||
public: | ||
nostd::unique_ptr<shim::SpanShim> span_shim; | ||
MockSpan* mock_span; | ||
|
||
protected: | ||
virtual void SetUp() | ||
{ | ||
mock_span = new MockSpan(); | ||
auto span = nostd::shared_ptr<trace_api::Span>(mock_span); | ||
auto tracer = shim::TracerShim::createTracerShim(); | ||
auto tracer_shim = dynamic_cast<shim::TracerShim*>(tracer.get()); | ||
auto baggage = baggage::Baggage::GetDefault()->Set("baggage", "item"); | ||
span_shim = nostd::unique_ptr<shim::SpanShim>( | ||
new shim::SpanShim(*tracer_shim, span, baggage)); | ||
} | ||
|
||
virtual void TearDown() | ||
{ | ||
span_shim.reset(); | ||
} | ||
}; | ||
|
||
TEST_F(SpanShimTest, HandleError) | ||
{ | ||
span_shim->handleError(true); | ||
ASSERT_EQ(mock_span->status_.first, trace_api::StatusCode::kError); | ||
span_shim->handleError("true"); | ||
ASSERT_EQ(mock_span->status_.first, trace_api::StatusCode::kError); | ||
span_shim->handleError(false); | ||
ASSERT_EQ(mock_span->status_.first, trace_api::StatusCode::kOk); | ||
span_shim->handleError("false"); | ||
ASSERT_EQ(mock_span->status_.first, trace_api::StatusCode::kOk); | ||
span_shim->handleError(nullptr); | ||
ASSERT_EQ(mock_span->status_.first, trace_api::StatusCode::kUnset); | ||
} | ||
|
||
TEST_F(SpanShimTest, FinishWithOptions) | ||
{ | ||
opentracing::FinishSpanOptions options; | ||
options.finish_steady_timestamp = opentracing::SteadyTime::clock::now(); | ||
ASSERT_NE(mock_span->options_.end_steady_time, options.finish_steady_timestamp); | ||
span_shim->FinishWithOptions(options); | ||
ASSERT_EQ(mock_span->options_.end_steady_time, options.finish_steady_timestamp); | ||
} | ||
|
||
TEST_F(SpanShimTest, SetOperationName) | ||
{ | ||
ASSERT_NE(mock_span->name_, "foo"); | ||
span_shim->SetOperationName("foo"); | ||
ASSERT_EQ(mock_span->name_, "foo"); | ||
} | ||
|
||
TEST_F(SpanShimTest, SetTag_Normal) | ||
{ | ||
ASSERT_NE(mock_span->attribute_.first, "foo"); | ||
span_shim->SetTag("foo", "bar"); | ||
ASSERT_EQ(mock_span->attribute_.first, "foo"); | ||
ASSERT_STREQ(nostd::get<const char *>(mock_span->attribute_.second), "bar"); | ||
} | ||
|
||
TEST_F(SpanShimTest, SetTag_Error) | ||
{ | ||
ASSERT_NE(mock_span->attribute_.first, "error"); | ||
span_shim->SetTag("error", true); | ||
ASSERT_NE(mock_span->attribute_.first, "error"); | ||
span_shim->SetTag("error", "false"); | ||
ASSERT_NE(mock_span->attribute_.first, "error"); | ||
span_shim->SetTag("error", nullptr); | ||
ASSERT_NE(mock_span->attribute_.first, "error"); | ||
} | ||
|
||
TEST_F(SpanShimTest, BaggageItem) | ||
{ | ||
ASSERT_EQ(span_shim->BaggageItem({}), ""); | ||
ASSERT_EQ(span_shim->BaggageItem(""), ""); | ||
ASSERT_EQ(span_shim->BaggageItem("invalid"), ""); | ||
ASSERT_EQ(span_shim->BaggageItem("baggage"), "item"); | ||
} | ||
|
||
TEST_F(SpanShimTest, SetBaggageItem) | ||
{ | ||
span_shim->SetBaggageItem("new", "entry"); | ||
ASSERT_EQ(span_shim->BaggageItem("new"), "entry"); | ||
ASSERT_EQ(span_shim->BaggageItem("baggage"), "item"); | ||
span_shim->SetBaggageItem("empty", ""); | ||
ASSERT_EQ(span_shim->BaggageItem("empty"), ""); | ||
span_shim->SetBaggageItem("no value", {}); | ||
ASSERT_EQ(span_shim->BaggageItem("no value"), ""); | ||
} | ||
|
||
TEST_F(SpanShimTest, Log_NoEvent) | ||
{ | ||
std::string name; | ||
common::SystemTimestamp timestamp; | ||
std::unordered_map<std::string, common::AttributeValue> attributes; | ||
|
||
span_shim->Log({{"test", 42}}); | ||
std::tie(name, timestamp, attributes) = mock_span->event_; | ||
|
||
ASSERT_EQ(name, "log"); | ||
ASSERT_EQ(timestamp, common::SystemTimestamp{}); | ||
ASSERT_EQ(attributes.size(), 1); | ||
ASSERT_EQ(nostd::get<int64_t>(attributes["test"]), 42); | ||
} | ||
|
||
TEST_F(SpanShimTest, Log_NoEvent_Timestamp) | ||
{ | ||
std::string name; | ||
common::SystemTimestamp timestamp; | ||
std::unordered_map<std::string, common::AttributeValue> attributes; | ||
|
||
auto logtime = opentracing::SystemTime::time_point::clock::now(); | ||
span_shim->Log(logtime, {{"foo", "bar"}}); | ||
std::tie(name, timestamp, attributes) = mock_span->event_; | ||
|
||
ASSERT_EQ(name, "log"); | ||
ASSERT_EQ(timestamp, common::SystemTimestamp{logtime}); | ||
ASSERT_EQ(attributes.size(), 1); | ||
ASSERT_STREQ(nostd::get<const char *>(attributes["foo"]), "bar"); | ||
} | ||
|
||
TEST_F(SpanShimTest, Log_Event) | ||
{ | ||
std::string name; | ||
common::SystemTimestamp timestamp; | ||
std::unordered_map<std::string, common::AttributeValue> attributes; | ||
|
||
auto logtime = opentracing::SystemTime::time_point::clock::now(); | ||
std::initializer_list<std::pair<opentracing::string_view, opentracing::Value>> fields{ | ||
{"event", "test!"}, {"foo", opentracing::string_view{"bar"}}, | ||
{"error.kind", 42}, {"message","hello"}, {"stack","overflow"}}; | ||
span_shim->Log(logtime, fields); | ||
std::tie(name, timestamp, attributes) = mock_span->event_; | ||
|
||
ASSERT_EQ(name, "test!"); | ||
ASSERT_EQ(timestamp, common::SystemTimestamp{logtime}); | ||
ASSERT_EQ(attributes.size(), 5); | ||
ASSERT_STREQ(nostd::get<const char *>(attributes["event"]), "test!"); | ||
ASSERT_EQ(nostd::get<nostd::string_view>(attributes["foo"]), nostd::string_view{"bar"}); | ||
ASSERT_EQ(nostd::get<int64_t>(attributes["error.kind"]), 42); | ||
ASSERT_STREQ(nostd::get<const char *>(attributes["message"]), "hello"); | ||
ASSERT_STREQ(nostd::get<const char *>(attributes["stack"]), "overflow"); | ||
} | ||
|
||
TEST_F(SpanShimTest, Log_Error) | ||
{ | ||
std::string name; | ||
common::SystemTimestamp timestamp; | ||
std::unordered_map<std::string, common::AttributeValue> attributes; | ||
|
||
auto logtime = opentracing::SystemTime::time_point::clock::now(); | ||
std::vector<std::pair<opentracing::string_view, opentracing::Value>> fields{ | ||
{"event", "error"}, {"foo", opentracing::string_view{"bar"}}, | ||
{"error.kind", 42}, {"message","hello"}, {"stack","overflow"}}; | ||
span_shim->Log(logtime, fields); | ||
std::tie(name, timestamp, attributes) = mock_span->event_; | ||
|
||
ASSERT_EQ(name, "exception"); | ||
ASSERT_EQ(timestamp, common::SystemTimestamp{logtime}); | ||
ASSERT_EQ(attributes.size(), 5); | ||
ASSERT_STREQ(nostd::get<const char *>(attributes["event"]), "error"); | ||
ASSERT_EQ(nostd::get<nostd::string_view>(attributes["foo"]), nostd::string_view{"bar"}); | ||
ASSERT_EQ(nostd::get<int64_t>(attributes["exception.type"]), 42); | ||
ASSERT_STREQ(nostd::get<const char *>(attributes["exception.message"]), "hello"); | ||
ASSERT_STREQ(nostd::get<const char *>(attributes["exception.stacktrace"]), "overflow"); | ||
} |