Skip to content

Commit dd70641

Browse files
committed
Add tests for Tracer
1 parent db8d879 commit dd70641

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

sdk/test/trace/tracer_test.cc

+49-4
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@
5454
#include "opentelemetry/trace/trace_state.h"
5555
#include "opentelemetry/trace/tracer.h"
5656

57+
#include <src/trace/span.h>
58+
5759
using namespace opentelemetry::sdk::trace;
5860
using namespace opentelemetry::sdk::resource;
61+
using namespace opentelemetry::sdk::instrumentationscope;
5962
using opentelemetry::common::SteadyTimestamp;
6063
using opentelemetry::common::SystemTimestamp;
6164
namespace nostd = opentelemetry::nostd;
@@ -142,15 +145,18 @@ std::shared_ptr<opentelemetry::trace::Tracer> initTracer(
142145
std::unique_ptr<SpanExporter> &&exporter,
143146
// For testing, just shove a pointer over, we'll take it over.
144147
Sampler *sampler,
145-
IdGenerator *id_generator = new RandomIdGenerator)
148+
IdGenerator *id_generator = new RandomIdGenerator,
149+
const ScopeConfigurator<TracerConfig> &tracer_configurator =
150+
TracerConfig::DefaultConfigurator())
146151
{
147152
auto processor = std::unique_ptr<SpanProcessor>(new SimpleSpanProcessor(std::move(exporter)));
148153
std::vector<std::unique_ptr<SpanProcessor>> processors;
149154
processors.push_back(std::move(processor));
150155
auto resource = Resource::Create({});
151-
auto context = std::make_shared<TracerContext>(std::move(processors), resource,
152-
std::unique_ptr<Sampler>(sampler),
153-
std::unique_ptr<IdGenerator>(id_generator));
156+
auto context = std::make_shared<TracerContext>(
157+
std::move(processors), resource, std::unique_ptr<Sampler>(sampler),
158+
std::unique_ptr<IdGenerator>(id_generator),
159+
std::make_unique<ScopeConfigurator<TracerConfig>>(tracer_configurator));
154160
return std::shared_ptr<opentelemetry::trace::Tracer>(new Tracer(context));
155161
}
156162

@@ -229,6 +235,7 @@ TEST(Tracer, StartSpanSampleOff)
229235

230236
TEST(Tracer, StartSpanCustomIdGenerator)
231237
{
238+
#ifdef OPENTELEMETRY_RTTI_ENABLED
232239
IdGenerator *id_generator = new MockIdGenerator();
233240
InMemorySpanExporter *exporter = new InMemorySpanExporter();
234241
std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
@@ -241,10 +248,14 @@ TEST(Tracer, StartSpanCustomIdGenerator)
241248

242249
EXPECT_EQ(cur_span_data->GetTraceId(), id_generator->GenerateTraceId());
243250
EXPECT_EQ(cur_span_data->GetSpanId(), id_generator->GenerateSpanId());
251+
#else
252+
GTEST_SKIP() << "cannot use 'typeid' with '-fno-rtti'"
253+
#endif
244254
}
245255

246256
TEST(Tracer, StartSpanWithOptionsTime)
247257
{
258+
#ifdef OPENTELEMETRY_RTTI_ENABLED
248259
InMemorySpanExporter *exporter = new InMemorySpanExporter();
249260
std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
250261
auto tracer = initTracer(std::unique_ptr<SpanExporter>{exporter});
@@ -264,6 +275,9 @@ TEST(Tracer, StartSpanWithOptionsTime)
264275
auto &cur_span_data = spans.at(0);
265276
ASSERT_EQ(std::chrono::nanoseconds(300), cur_span_data->GetStartTime().time_since_epoch());
266277
ASSERT_EQ(std::chrono::nanoseconds(30), cur_span_data->GetDuration());
278+
#else
279+
GTEST_SKIP() << "cannot use 'typeid' with '-fno-rtti'"
280+
#endif
267281
}
268282

269283
TEST(Tracer, StartSpanWithAttributes)
@@ -488,6 +502,37 @@ TEST(Tracer, SpanSetEvents)
488502
ASSERT_EQ(1, span_data_events[2].GetAttributes().size());
489503
}
490504

505+
TEST(Tracer, StartSpanWithDisabledConfig)
506+
{
507+
InMemorySpanExporter *exporter = new InMemorySpanExporter();
508+
std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
509+
ScopeConfigurator<TracerConfig> disable_tracer = [](const InstrumentationScope &) {
510+
return TracerConfig::Disabled();
511+
};
512+
auto tracer = initTracer(std::unique_ptr<SpanExporter>{exporter}, new AlwaysOnSampler(),
513+
new RandomIdGenerator(), disable_tracer);
514+
auto span = tracer->StartSpan("span 1");
515+
auto &span_ref = *span.get();
516+
517+
EXPECT_NE(typeid(span_ref), typeid(trace_api::Span));
518+
EXPECT_EQ(typeid(span_ref), typeid(trace_api::NoopSpan));
519+
}
520+
521+
TEST(Tracer, StartSpanWithEnabledConfig)
522+
{
523+
InMemorySpanExporter *exporter = new InMemorySpanExporter();
524+
std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
525+
ScopeConfigurator<TracerConfig> enable_tracer = [](const InstrumentationScope &) {
526+
return TracerConfig::Enabled();
527+
};
528+
auto tracer = initTracer(std::unique_ptr<SpanExporter>{exporter}, new AlwaysOnSampler(),
529+
new RandomIdGenerator(), enable_tracer);
530+
auto span = tracer->StartSpan("span 1");
531+
auto &span_ref = *span.get();
532+
533+
EXPECT_EQ(typeid(span_ref), typeid(Span));
534+
}
535+
491536
TEST(Tracer, SpanSetLinks)
492537
{
493538
InMemorySpanExporter *exporter = new InMemorySpanExporter();

0 commit comments

Comments
 (0)