Skip to content

Commit

Permalink
Add tests for Tracer
Browse files Browse the repository at this point in the history
  • Loading branch information
psx95 committed Nov 16, 2024
1 parent db8d879 commit b82ea68
Showing 1 changed file with 49 additions and 4 deletions.
53 changes: 49 additions & 4 deletions sdk/test/trace/tracer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@
#include "opentelemetry/trace/trace_state.h"
#include "opentelemetry/trace/tracer.h"

#include <src/trace/span.h>

using namespace opentelemetry::sdk::trace;
using namespace opentelemetry::sdk::resource;
using namespace opentelemetry::sdk::instrumentationscope;
using opentelemetry::common::SteadyTimestamp;
using opentelemetry::common::SystemTimestamp;
namespace nostd = opentelemetry::nostd;
Expand Down Expand Up @@ -142,15 +145,18 @@ std::shared_ptr<opentelemetry::trace::Tracer> initTracer(
std::unique_ptr<SpanExporter> &&exporter,
// For testing, just shove a pointer over, we'll take it over.
Sampler *sampler,
IdGenerator *id_generator = new RandomIdGenerator)
IdGenerator *id_generator = new RandomIdGenerator,
const ScopeConfigurator<TracerConfig> &tracer_configurator =
TracerConfig::DefaultConfigurator())
{
auto processor = std::unique_ptr<SpanProcessor>(new SimpleSpanProcessor(std::move(exporter)));
std::vector<std::unique_ptr<SpanProcessor>> processors;
processors.push_back(std::move(processor));
auto resource = Resource::Create({});
auto context = std::make_shared<TracerContext>(std::move(processors), resource,
std::unique_ptr<Sampler>(sampler),
std::unique_ptr<IdGenerator>(id_generator));
auto context = std::make_shared<TracerContext>(
std::move(processors), resource, std::unique_ptr<Sampler>(sampler),
std::unique_ptr<IdGenerator>(id_generator),
std::make_unique<ScopeConfigurator<TracerConfig>>(tracer_configurator));
return std::shared_ptr<opentelemetry::trace::Tracer>(new Tracer(context));
}

Expand Down Expand Up @@ -229,6 +235,7 @@ TEST(Tracer, StartSpanSampleOff)

TEST(Tracer, StartSpanCustomIdGenerator)
{
#ifdef OPENTELEMETRY_RTTI_ENABLED
IdGenerator *id_generator = new MockIdGenerator();
InMemorySpanExporter *exporter = new InMemorySpanExporter();
std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
Expand All @@ -241,10 +248,14 @@ TEST(Tracer, StartSpanCustomIdGenerator)

EXPECT_EQ(cur_span_data->GetTraceId(), id_generator->GenerateTraceId());
EXPECT_EQ(cur_span_data->GetSpanId(), id_generator->GenerateSpanId());
#else
GTEST_SKIP() << "cannot use 'typeid' with '-fno-rtti'";
#endif
}

TEST(Tracer, StartSpanWithOptionsTime)
{
#ifdef OPENTELEMETRY_RTTI_ENABLED
InMemorySpanExporter *exporter = new InMemorySpanExporter();
std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
auto tracer = initTracer(std::unique_ptr<SpanExporter>{exporter});
Expand All @@ -264,6 +275,9 @@ TEST(Tracer, StartSpanWithOptionsTime)
auto &cur_span_data = spans.at(0);
ASSERT_EQ(std::chrono::nanoseconds(300), cur_span_data->GetStartTime().time_since_epoch());
ASSERT_EQ(std::chrono::nanoseconds(30), cur_span_data->GetDuration());
#else
GTEST_SKIP() << "cannot use 'typeid' with '-fno-rtti'";
#endif
}

TEST(Tracer, StartSpanWithAttributes)
Expand Down Expand Up @@ -488,6 +502,37 @@ TEST(Tracer, SpanSetEvents)
ASSERT_EQ(1, span_data_events[2].GetAttributes().size());
}

TEST(Tracer, StartSpanWithDisabledConfig)
{
InMemorySpanExporter *exporter = new InMemorySpanExporter();
std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
ScopeConfigurator<TracerConfig> disable_tracer = [](const InstrumentationScope &) {
return TracerConfig::Disabled();
};
auto tracer = initTracer(std::unique_ptr<SpanExporter>{exporter}, new AlwaysOnSampler(),
new RandomIdGenerator(), disable_tracer);
auto span = tracer->StartSpan("span 1");
auto &span_ref = *span.get();

EXPECT_NE(typeid(span_ref), typeid(trace_api::Span));
EXPECT_EQ(typeid(span_ref), typeid(trace_api::NoopSpan));
}

TEST(Tracer, StartSpanWithEnabledConfig)
{
InMemorySpanExporter *exporter = new InMemorySpanExporter();
std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
ScopeConfigurator<TracerConfig> enable_tracer = [](const InstrumentationScope &) {
return TracerConfig::Enabled();
};
auto tracer = initTracer(std::unique_ptr<SpanExporter>{exporter}, new AlwaysOnSampler(),
new RandomIdGenerator(), enable_tracer);
auto span = tracer->StartSpan("span 1");
auto &span_ref = *span.get();

EXPECT_EQ(typeid(span_ref), typeid(Span));
}

TEST(Tracer, SpanSetLinks)
{
InMemorySpanExporter *exporter = new InMemorySpanExporter();
Expand Down

0 comments on commit b82ea68

Please sign in to comment.