Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jaeger: ThriftSender unit test #1162

Merged
merged 8 commits into from
Jan 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions exporters/jaeger/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ cc_test(
deps = [
":opentelemetry_exporter_jaeger_trace",
"//sdk/src/resource",
"//sdk/src/trace",
"@com_google_googletest//:gtest_main",
],
)
2 changes: 1 addition & 1 deletion exporters/jaeger/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ if(BUILD_TESTING)
endif()
target_link_libraries(
jaeger_exporter_test ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}
${GMOCK_LIB} opentelemetry_exporter_jaeger_trace)
${GMOCK_LIB} opentelemetry_trace opentelemetry_exporter_jaeger_trace)

target_include_directories(jaeger_exporter_test
PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src)
Expand Down
63 changes: 63 additions & 0 deletions exporters/jaeger/test/jaeger_exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <opentelemetry/exporters/jaeger/jaeger_exporter.h>
#include <memory>
#include <vector>
#include "opentelemetry/sdk/trace/batch_span_processor.h"
#include "opentelemetry/sdk/trace/tracer_provider.h"

#ifdef BAZEL_BUILD
# include "exporters/jaeger/src/thrift_sender.h"
Expand All @@ -28,6 +30,15 @@ namespace exporter
namespace jaeger
{

namespace trace_api = opentelemetry::trace;
namespace resource = opentelemetry::sdk::resource;

template <class T, size_t N>
static nostd::span<T, N> MakeSpan(T (&array)[N])
{
return nostd::span<T, N>(array);
}

class JaegerExporterTestPeer : public ::testing::Test
{
public:
Expand All @@ -49,6 +60,58 @@ class MockThriftSender : public ThriftSender
MOCK_METHOD(int, Append, (std::unique_ptr<JaegerRecordable> &&), (noexcept, override));
};

class MockTransport : public Transport
{
public:
MOCK_METHOD(int, EmitBatch, (const thrift::Batch &), (override));
MOCK_METHOD(uint32_t, MaxPacketSize, (), (const, override));
};

// Create spans, let processor call Export()
TEST_F(JaegerExporterTestPeer, ExportIntegrationTest)
{
auto mock_transport = new MockTransport;
auto mock_thrift_sender = new ThriftSender(std::unique_ptr<MockTransport>{mock_transport});
auto exporter = GetExporter(std::unique_ptr<ThriftSender>{mock_thrift_sender});

resource::ResourceAttributes resource_attributes = {{"service.name", "unit_test_service"},
{"tenant.id", "test_user"}};
resource_attributes["bool_value"] = true;
resource_attributes["int32_value"] = static_cast<int32_t>(1);
resource_attributes["uint32_value"] = static_cast<uint32_t>(2);
resource_attributes["int64_value"] = static_cast<int64_t>(0x1100000000LL);
resource_attributes["double_value"] = static_cast<double>(3.1);
auto resource = resource::Resource::Create(resource_attributes);

auto processor_opts = sdk::trace::BatchSpanProcessorOptions();
processor_opts.max_export_batch_size = 5;
processor_opts.max_queue_size = 5;
processor_opts.schedule_delay_millis = std::chrono::milliseconds(256);
auto processor = std::unique_ptr<sdk::trace::SpanProcessor>(
new sdk::trace::BatchSpanProcessor(std::move(exporter), processor_opts));
auto provider = nostd::shared_ptr<trace::TracerProvider>(
new sdk::trace::TracerProvider(std::move(processor), resource));

EXPECT_CALL(*mock_transport, EmitBatch(_)).Times(Exactly(1)).WillOnce(Return(1));

auto tracer = provider->GetTracer("test");
auto parent_span = tracer->StartSpan("Test parent span");

trace_api::StartSpanOptions child_span_opts = {};
child_span_opts.parent = parent_span->GetContext();
auto child_span = tracer->StartSpan("Test child span", child_span_opts);

child_span->End();
parent_span->End();

auto parent_ctx = parent_span->GetContext();
auto child_ctx = child_span->GetContext();
EXPECT_EQ(parent_ctx.trace_id(), child_ctx.trace_id());
EXPECT_EQ(parent_ctx.trace_state(), child_ctx.trace_state());
ASSERT_TRUE(parent_ctx.IsValid());
ASSERT_TRUE(child_ctx.IsValid());
}

TEST_F(JaegerExporterTestPeer, ShutdownTest)
{
auto mock_thrift_sender = new MockThriftSender;
Expand Down