From d027b019e92885d4e1f1b3231a8c3c0ab88fe34a Mon Sep 17 00:00:00 2001 From: Karen Xu Date: Sat, 17 Oct 2020 18:30:17 -0400 Subject: [PATCH] Remove trace_api and rebase --- api/include/opentelemetry/metrics/noop.h | 131 +++++----- api/test/metrics/noop_metrics_test.cc | 2 - .../ext/zpages/threadsafe_span_data.h | 7 +- .../opentelemetry/sdk/trace/recordable.h | 5 +- sdk/include/opentelemetry/sdk/trace/sampler.h | 10 +- .../sdk/trace/samplers/always_off.h | 11 +- .../opentelemetry/sdk/trace/samplers/parent.h | 11 +- .../sdk/trace/samplers/trace_id_ratio.h | 11 +- .../opentelemetry/sdk/trace/span_data.h | 3 +- sdk/src/trace/span.cc | 3 +- sdk/test/trace/always_on_sampler_test.cc | 7 +- sdk/test/trace/parent_or_else_sampler_test.cc | 51 ---- sdk/test/trace/parent_sampler_test.cc | 2 +- sdk/test/trace/probability_sampler_test.cc | 226 ------------------ sdk/test/trace/trace_id_ratio_sampler_test.cc | 6 +- sdk/test/trace/tracer_test.cc | 10 +- 16 files changed, 100 insertions(+), 396 deletions(-) delete mode 100644 sdk/test/trace/parent_or_else_sampler_test.cc delete mode 100644 sdk/test/trace/probability_sampler_test.cc diff --git a/api/include/opentelemetry/metrics/noop.h b/api/include/opentelemetry/metrics/noop.h index 8711d1675f..552e125dd0 100644 --- a/api/include/opentelemetry/metrics/noop.h +++ b/api/include/opentelemetry/metrics/noop.h @@ -15,6 +15,9 @@ #include +// TODO: Create generic short pattern for opentelemetry::common and opentelemetry::trace and others +// as necessary + OPENTELEMETRY_BEGIN_NAMESPACE namespace metrics { @@ -297,36 +300,36 @@ class NoopMeter : public Meter * @param enabled a boolean that turns the metric instrument on and off. * @return */ - opentelemetry::nostd::shared_ptr> NewShortCounter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - const bool enabled) override + nostd::shared_ptr> NewShortCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + const bool enabled) override { return nostd::shared_ptr>{ new NoopCounter(name, description, unit, enabled)}; } - opentelemetry::nostd::shared_ptr> NewIntCounter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - const bool enabled) override + nostd::shared_ptr> NewIntCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + const bool enabled) override { return nostd::shared_ptr>{new NoopCounter(name, description, unit, enabled)}; } - opentelemetry::nostd::shared_ptr> NewFloatCounter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - const bool enabled) override + nostd::shared_ptr> NewFloatCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + const bool enabled) override { return nostd::shared_ptr>{ new NoopCounter(name, description, unit, enabled)}; } - opentelemetry::nostd::shared_ptr> NewDoubleCounter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - const bool enabled) override + nostd::shared_ptr> NewDoubleCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + const bool enabled) override { return nostd::shared_ptr>{ new NoopCounter(name, description, unit, enabled)}; @@ -342,41 +345,37 @@ class NoopMeter : public Meter * @param enabled a boolean that turns the metric instrument on and off. * @return */ - opentelemetry::nostd::shared_ptr> NewShortUpDownCounter( - nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - const bool enabled) override + nostd::shared_ptr> NewShortUpDownCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + const bool enabled) override { return nostd::shared_ptr>{ new NoopUpDownCounter(name, description, unit, enabled)}; } - opentelemetry::nostd::shared_ptr> NewIntUpDownCounter( - nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - const bool enabled) override + nostd::shared_ptr> NewIntUpDownCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + const bool enabled) override { return nostd::shared_ptr>{ new NoopUpDownCounter(name, description, unit, enabled)}; } - opentelemetry::nostd::shared_ptr> NewFloatUpDownCounter( - nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - const bool enabled) override + nostd::shared_ptr> NewFloatUpDownCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + const bool enabled) override { return nostd::shared_ptr>{ new NoopUpDownCounter(name, description, unit, enabled)}; } - opentelemetry::nostd::shared_ptr> NewDoubleUpDownCounter( - nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - const bool enabled) override + nostd::shared_ptr> NewDoubleUpDownCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + const bool enabled) override { return nostd::shared_ptr>{ new NoopUpDownCounter(name, description, unit, enabled)}; @@ -392,41 +391,37 @@ class NoopMeter : public Meter * @param enabled a boolean that turns the metric instrument on and off. * @return */ - opentelemetry::nostd::shared_ptr> NewShortValueRecorder( - nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - const bool enabled) override + nostd::shared_ptr> NewShortValueRecorder(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + const bool enabled) override { return nostd::shared_ptr>{ new NoopValueRecorder(name, description, unit, enabled)}; } - opentelemetry::nostd::shared_ptr> NewIntValueRecorder( - nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - const bool enabled) override + nostd::shared_ptr> NewIntValueRecorder(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + const bool enabled) override { return nostd::shared_ptr>{ new NoopValueRecorder(name, description, unit, enabled)}; } - opentelemetry::nostd::shared_ptr> NewFloatValueRecorder( - nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - const bool enabled) override + nostd::shared_ptr> NewFloatValueRecorder(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + const bool enabled) override { return nostd::shared_ptr>{ new NoopValueRecorder(name, description, unit, enabled)}; } - opentelemetry::nostd::shared_ptr> NewDoubleValueRecorder( - nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - const bool enabled) override + nostd::shared_ptr> NewDoubleValueRecorder(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + const bool enabled) override { return nostd::shared_ptr>{ new NoopValueRecorder(name, description, unit, enabled)}; @@ -442,7 +437,7 @@ class NoopMeter : public Meter * @param enabled a boolean that turns the metric instrument on and off. * @return */ - opentelemetry::nostd::shared_ptr> NewShortSumObserver( + nostd::shared_ptr> NewShortSumObserver( nostd::string_view name, nostd::string_view description, nostd::string_view unit, @@ -453,7 +448,7 @@ class NoopMeter : public Meter new NoopSumObserver(name, description, unit, enabled, callback)}; } - opentelemetry::nostd::shared_ptr> NewIntSumObserver( + nostd::shared_ptr> NewIntSumObserver( nostd::string_view name, nostd::string_view description, nostd::string_view unit, @@ -464,7 +459,7 @@ class NoopMeter : public Meter new NoopSumObserver(name, description, unit, enabled, callback)}; } - opentelemetry::nostd::shared_ptr> NewFloatSumObserver( + nostd::shared_ptr> NewFloatSumObserver( nostd::string_view name, nostd::string_view description, nostd::string_view unit, @@ -475,7 +470,7 @@ class NoopMeter : public Meter new NoopSumObserver(name, description, unit, enabled, callback)}; } - opentelemetry::nostd::shared_ptr> NewDoubleSumObserver( + nostd::shared_ptr> NewDoubleSumObserver( nostd::string_view name, nostd::string_view description, nostd::string_view unit, @@ -496,7 +491,7 @@ class NoopMeter : public Meter * @param enabled a boolean that turns the metric instrument on and off. * @return */ - opentelemetry::nostd::shared_ptr> NewShortUpDownSumObserver( + nostd::shared_ptr> NewShortUpDownSumObserver( nostd::string_view name, nostd::string_view description, nostd::string_view unit, @@ -507,7 +502,7 @@ class NoopMeter : public Meter new NoopUpDownSumObserver(name, description, unit, enabled, callback)}; } - opentelemetry::nostd::shared_ptr> NewIntUpDownSumObserver( + nostd::shared_ptr> NewIntUpDownSumObserver( nostd::string_view name, nostd::string_view description, nostd::string_view unit, @@ -518,7 +513,7 @@ class NoopMeter : public Meter new NoopUpDownSumObserver(name, description, unit, enabled, callback)}; } - opentelemetry::nostd::shared_ptr> NewFloatUpDownSumObserver( + nostd::shared_ptr> NewFloatUpDownSumObserver( nostd::string_view name, nostd::string_view description, nostd::string_view unit, @@ -529,7 +524,7 @@ class NoopMeter : public Meter new NoopUpDownSumObserver(name, description, unit, enabled, callback)}; } - opentelemetry::nostd::shared_ptr> NewDoubleUpDownSumObserver( + nostd::shared_ptr> NewDoubleUpDownSumObserver( nostd::string_view name, nostd::string_view description, nostd::string_view unit, @@ -550,7 +545,7 @@ class NoopMeter : public Meter * @param enabled a boolean that turns the metric instrument on and off. * @return */ - opentelemetry::nostd::shared_ptr> NewShortValueObserver( + nostd::shared_ptr> NewShortValueObserver( nostd::string_view name, nostd::string_view description, nostd::string_view unit, @@ -561,7 +556,7 @@ class NoopMeter : public Meter new NoopValueObserver(name, description, unit, enabled, callback)}; } - opentelemetry::nostd::shared_ptr> NewIntValueObserver( + nostd::shared_ptr> NewIntValueObserver( nostd::string_view name, nostd::string_view description, nostd::string_view unit, @@ -572,7 +567,7 @@ class NoopMeter : public Meter new NoopValueObserver(name, description, unit, enabled, callback)}; } - opentelemetry::nostd::shared_ptr> NewFloatValueObserver( + nostd::shared_ptr> NewFloatValueObserver( nostd::string_view name, nostd::string_view description, nostd::string_view unit, @@ -583,7 +578,7 @@ class NoopMeter : public Meter new NoopValueObserver(name, description, unit, enabled, callback)}; } - opentelemetry::nostd::shared_ptr> NewDoubleValueObserver( + nostd::shared_ptr> NewDoubleValueObserver( nostd::string_view name, nostd::string_view description, nostd::string_view unit, diff --git a/api/test/metrics/noop_metrics_test.cc b/api/test/metrics/noop_metrics_test.cc index 31f464398d..8e43f78180 100644 --- a/api/test/metrics/noop_metrics_test.cc +++ b/api/test/metrics/noop_metrics_test.cc @@ -12,8 +12,6 @@ OPENTELEMETRY_BEGIN_NAMESPACE using opentelemetry::metrics::Meter; using opentelemetry::metrics::NoopMeter; -namespace metrics_api = opentelemetry::metrics; - void Callback(opentelemetry::metrics::ObserverResult result) { std::map labels = {{"key", "value"}}; diff --git a/ext/include/opentelemetry/ext/zpages/threadsafe_span_data.h b/ext/include/opentelemetry/ext/zpages/threadsafe_span_data.h index 75df283b05..4d937bd8b7 100644 --- a/ext/include/opentelemetry/ext/zpages/threadsafe_span_data.h +++ b/ext/include/opentelemetry/ext/zpages/threadsafe_span_data.h @@ -17,7 +17,9 @@ using opentelemetry::sdk::trace::AttributeConverter; using opentelemetry::sdk::trace::SpanDataAttributeValue; using opentelemetry::sdk::trace::SpanDataEvent; -namespace trace_api = opentelemetry::trace; + +// TODO: Create generic short pattern for opentelemetry::common and opentelemetry::trace and others +// as necessary OPENTELEMETRY_BEGIN_NAMESPACE namespace ext @@ -137,7 +139,8 @@ class ThreadsafeSpanData final : public opentelemetry::sdk::trace::Recordable attributes_[std::string(key)] = nostd::visit(converter_, value); } - void SetStatus(trace_api::CanonicalCode code, nostd::string_view description) noexcept override + void SetStatus(opentelemetry::trace::CanonicalCode code, + nostd::string_view description) noexcept override { std::lock_guard lock(mutex_); status_code_ = code; diff --git a/sdk/include/opentelemetry/sdk/trace/recordable.h b/sdk/include/opentelemetry/sdk/trace/recordable.h index afba89abfd..95c89c2ce5 100644 --- a/sdk/include/opentelemetry/sdk/trace/recordable.h +++ b/sdk/include/opentelemetry/sdk/trace/recordable.h @@ -13,10 +13,11 @@ #include +// TODO: Create generic short pattern for opentelemetry::common and opentelemetry::trace + OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk { -namespace trace_api = opentelemetry::trace; namespace trace { /** @@ -99,7 +100,7 @@ class Recordable * @param code the status code * @param description a description of the status */ - virtual void SetStatus(trace_api::CanonicalCode code, + virtual void SetStatus(opentelemetry::trace::CanonicalCode code, nostd::string_view description) noexcept = 0; /** diff --git a/sdk/include/opentelemetry/sdk/trace/sampler.h b/sdk/include/opentelemetry/sdk/trace/sampler.h index 49fc0a8e8e..dc084dca22 100644 --- a/sdk/include/opentelemetry/sdk/trace/sampler.h +++ b/sdk/include/opentelemetry/sdk/trace/sampler.h @@ -65,20 +65,12 @@ class Sampler * @since 0.1.0 */ -<<<<<<< HEAD virtual SamplingResult ShouldSample( - const trace_api::SpanContext *parent_context, + const trace_api::SpanContext &parent_context, trace_api::TraceId trace_id, nostd::string_view name, trace_api::SpanKind span_kind, const opentelemetry::common::KeyValueIterable &attributes) noexcept = 0; -======= - virtual SamplingResult ShouldSample(const trace_api::SpanContext &parent_context, - trace_api::TraceId trace_id, - nostd::string_view name, - trace_api::SpanKind span_kind, - const trace_api::KeyValueIterable &attributes) noexcept = 0; ->>>>>>> origin/master /** * Returns the sampler name or short description with the configuration. diff --git a/sdk/include/opentelemetry/sdk/trace/samplers/always_off.h b/sdk/include/opentelemetry/sdk/trace/samplers/always_off.h index 21a0159a36..7fd862be89 100644 --- a/sdk/include/opentelemetry/sdk/trace/samplers/always_off.h +++ b/sdk/include/opentelemetry/sdk/trace/samplers/always_off.h @@ -19,11 +19,12 @@ class AlwaysOffSampler : public Sampler /** * @return Returns DROP always */ - SamplingResult ShouldSample(const trace_api::SpanContext & /*parent_context*/, - trace_api::TraceId /*trace_id*/, - nostd::string_view /*name*/, - trace_api::SpanKind /*span_kind*/, - const opentelemetry::common::KeyValueIterable & /*attributes*/) noexcept override + SamplingResult ShouldSample( + const trace_api::SpanContext & /*parent_context*/, + trace_api::TraceId /*trace_id*/, + nostd::string_view /*name*/, + trace_api::SpanKind /*span_kind*/, + const opentelemetry::common::KeyValueIterable & /*attributes*/) noexcept override { return {Decision::DROP, nullptr}; } diff --git a/sdk/include/opentelemetry/sdk/trace/samplers/parent.h b/sdk/include/opentelemetry/sdk/trace/samplers/parent.h index 551ebea4e0..91337bd497 100644 --- a/sdk/include/opentelemetry/sdk/trace/samplers/parent.h +++ b/sdk/include/opentelemetry/sdk/trace/samplers/parent.h @@ -22,11 +22,12 @@ class ParentBasedSampler : public Sampler * delegateSampler for root spans * @return Returns DROP always */ - SamplingResult ShouldSample(const trace_api::SpanContext &parent_context, - trace_api::TraceId trace_id, - nostd::string_view name, - trace_api::SpanKind span_kind, - const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + SamplingResult ShouldSample( + const trace_api::SpanContext &parent_context, + trace_api::TraceId trace_id, + nostd::string_view name, + trace_api::SpanKind span_kind, + const opentelemetry::common::KeyValueIterable &attributes) noexcept override; /** * @return Description MUST be ParentBased{delegate_sampler_.getDescription()} diff --git a/sdk/include/opentelemetry/sdk/trace/samplers/trace_id_ratio.h b/sdk/include/opentelemetry/sdk/trace/samplers/trace_id_ratio.h index d81491d1f0..80be8ae80d 100644 --- a/sdk/include/opentelemetry/sdk/trace/samplers/trace_id_ratio.h +++ b/sdk/include/opentelemetry/sdk/trace/samplers/trace_id_ratio.h @@ -29,11 +29,12 @@ class TraceIdRatioBasedSampler : public Sampler * is used as a pseudorandom value in conjunction with the predefined * ratio to determine whether this trace should be sampled */ - SamplingResult ShouldSample(const trace_api::SpanContext & /*parent_context*/, - trace_api::TraceId trace_id, - nostd::string_view /*name*/, - trace_api::SpanKind /*span_kind*/, - const opentelemetry::common::KeyValueIterable & /*attributes*/) noexcept override; + SamplingResult ShouldSample( + const trace_api::SpanContext & /*parent_context*/, + trace_api::TraceId trace_id, + nostd::string_view /*name*/, + trace_api::SpanKind /*span_kind*/, + const opentelemetry::common::KeyValueIterable & /*attributes*/) noexcept override; /** * @return Description MUST be TraceIdRatioBasedSampler{0.000100} diff --git a/sdk/include/opentelemetry/sdk/trace/span_data.h b/sdk/include/opentelemetry/sdk/trace/span_data.h index 52df274442..6fde43c342 100644 --- a/sdk/include/opentelemetry/sdk/trace/span_data.h +++ b/sdk/include/opentelemetry/sdk/trace/span_data.h @@ -187,7 +187,8 @@ class SpanData final : public Recordable links_.push_back(link); } - void SetStatus(trace_api::CanonicalCode code, nostd::string_view description) noexcept override + void SetStatus(opentelemetry::trace::CanonicalCode code, + nostd::string_view description) noexcept override { status_code_ = code; status_desc_ = std::string(description); diff --git a/sdk/src/trace/span.cc b/sdk/src/trace/span.cc index 29abddacce..417051e464 100644 --- a/sdk/src/trace/span.cc +++ b/sdk/src/trace/span.cc @@ -148,7 +148,8 @@ void Span::AddEvent(nostd::string_view name, recordable_->AddEvent(name, timestamp, attributes); } -void Span::SetStatus(trace_api::CanonicalCode code, nostd::string_view description) noexcept +void Span::SetStatus(opentelemetry::trace::CanonicalCode code, + nostd::string_view description) noexcept { std::lock_guard lock_guard{mu_}; if (recordable_ == nullptr) diff --git a/sdk/test/trace/always_on_sampler_test.cc b/sdk/test/trace/always_on_sampler_test.cc index 6ca97367a1..31d3a2df01 100644 --- a/sdk/test/trace/always_on_sampler_test.cc +++ b/sdk/test/trace/always_on_sampler_test.cc @@ -30,14 +30,9 @@ TEST(AlwaysOnSampler, ShouldSample) // Test with a valid trace id and empty parent context sampling_result = sampler.ShouldSample( -<<<<<<< HEAD - nullptr, trace_id_valid, "valid trace id test", trace_api::SpanKind::kServer, - opentelemetry::common::KeyValueIterableView>(key_value_container)); -======= SpanContext::GetInvalid(), trace_id_valid, "valid trace id test", trace_api::SpanKind::kServer, - trace_api::KeyValueIterableView>(key_value_container)); ->>>>>>> origin/master + opentelemetry::common::KeyValueIterableView>(key_value_container)); ASSERT_EQ(Decision::RECORD_AND_SAMPLE, sampling_result.decision); ASSERT_EQ(nullptr, sampling_result.attributes); diff --git a/sdk/test/trace/parent_or_else_sampler_test.cc b/sdk/test/trace/parent_or_else_sampler_test.cc deleted file mode 100644 index 65d4678231..0000000000 --- a/sdk/test/trace/parent_or_else_sampler_test.cc +++ /dev/null @@ -1,51 +0,0 @@ -#include -#include -#include "opentelemetry/sdk/trace/samplers/always_off.h" -#include "opentelemetry/sdk/trace/samplers/always_on.h" -#include "opentelemetry/sdk/trace/samplers/parent_or_else.h" - -using opentelemetry::sdk::trace::AlwaysOffSampler; -using opentelemetry::sdk::trace::AlwaysOnSampler; -using opentelemetry::sdk::trace::Decision; -using opentelemetry::sdk::trace::ParentOrElseSampler; -using opentelemetry::trace::SpanContext; - -TEST(ParentOrElseSampler, ShouldSample) -{ - ParentOrElseSampler sampler_off(std::make_shared()); - ParentOrElseSampler sampler_on(std::make_shared()); - - // Set up parameters - opentelemetry::trace::TraceId trace_id; - opentelemetry::trace::SpanKind span_kind = opentelemetry::trace::SpanKind::kInternal; - using M = std::map; - M m1 = {{}}; - opentelemetry::common::KeyValueIterableView view{m1}; - SpanContext parent_context_sampled(true, false); - SpanContext parent_context_nonsampled(false, false); - - // Case 1: Parent doesn't exist. Return result of delegateSampler() - auto sampling_result = sampler_off.ShouldSample(nullptr, trace_id, "", span_kind, view); - auto sampling_result2 = sampler_on.ShouldSample(nullptr, trace_id, "", span_kind, view); - - ASSERT_EQ(Decision::DROP, sampling_result.decision); - ASSERT_EQ(Decision::RECORD_AND_SAMPLE, sampling_result2.decision); - - // Case 2: Parent exists and SampledFlag is true - auto sampling_result3 = - sampler_off.ShouldSample(&parent_context_sampled, trace_id, "", span_kind, view); - ASSERT_EQ(Decision::RECORD_AND_SAMPLE, sampling_result3.decision); - - // Case 3: Parent exists and SampledFlag is false - auto sampling_result4 = - sampler_on.ShouldSample(&parent_context_nonsampled, trace_id, "", span_kind, view); - ASSERT_EQ(Decision::DROP, sampling_result4.decision); -} - -TEST(ParentOrElseSampler, GetDescription) -{ - ParentOrElseSampler sampler(std::make_shared()); - ASSERT_EQ("ParentOrElse{AlwaysOffSampler}", sampler.GetDescription()); - ParentOrElseSampler sampler2(std::make_shared()); - ASSERT_EQ("ParentOrElse{AlwaysOnSampler}", sampler2.GetDescription()); -} diff --git a/sdk/test/trace/parent_sampler_test.cc b/sdk/test/trace/parent_sampler_test.cc index b43eafa938..59eae3b293 100644 --- a/sdk/test/trace/parent_sampler_test.cc +++ b/sdk/test/trace/parent_sampler_test.cc @@ -24,7 +24,7 @@ TEST(ParentBasedSampler, ShouldSample) opentelemetry::trace::SpanKind span_kind = opentelemetry::trace::SpanKind::kInternal; using M = std::map; M m1 = {{}}; - opentelemetry::trace::KeyValueIterableView view{m1}; + opentelemetry::common::KeyValueIterableView view{m1}; trace_api::SpanContext parent_context_sampled(trace_id, span_id, trace_api::TraceFlags{1}, false); trace_api::SpanContext parent_context_nonsampled(trace_id, span_id, trace_api::TraceFlags{0}, false); diff --git a/sdk/test/trace/probability_sampler_test.cc b/sdk/test/trace/probability_sampler_test.cc deleted file mode 100644 index 2b57772239..0000000000 --- a/sdk/test/trace/probability_sampler_test.cc +++ /dev/null @@ -1,226 +0,0 @@ -#include "opentelemetry/sdk/trace/samplers/probability.h" -#include "src/common/random.h" - -#include -#include -#include - -using opentelemetry::sdk::common::Random; -using opentelemetry::sdk::trace::Decision; -using opentelemetry::sdk::trace::ProbabilitySampler; -using opentelemetry::trace::SpanContext; - -namespace -{ -/* - * Helper function for running probability sampler tests. - * Given a span context, sampler, and number of iterations this function - * will return the number of RECORD_AND_SAMPLE decision based on randomly - * generated traces. - * - * @param context a required valid span context - * @param sampler a required valid sampler - * @param iterations a requried number specifying the number of times to - * generate a random trace_id and check if it should sample using the provided - * provider and context - */ -int RunShouldSampleCountDecision(SpanContext &context, ProbabilitySampler &sampler, int iterations) -{ - int actual_count = 0; - - opentelemetry::trace::SpanKind span_kind = opentelemetry::trace::SpanKind::kInternal; - - using M = std::map; - M m1 = {{}}; - opentelemetry::common::KeyValueIterableView view{m1}; - - for (int i = 0; i < iterations; ++i) - { - uint8_t buf[16] = {0}; - Random::GenerateRandomBuffer(buf); - - opentelemetry::trace::TraceId trace_id(buf); - - auto result = sampler.ShouldSample(&context, trace_id, "", span_kind, view); - if (result.decision == Decision::RECORD_AND_SAMPLE) - { - ++actual_count; - } - } - - return actual_count; -} -} // namespace - -TEST(ProbabilitySampler, ShouldSampleWithoutContext) -{ - opentelemetry::trace::TraceId invalid_trace_id; - - opentelemetry::trace::SpanKind span_kind = opentelemetry::trace::SpanKind::kInternal; - - using M = std::map; - M m1 = {{}}; - opentelemetry::common::KeyValueIterableView view{m1}; - - ProbabilitySampler s1(0.01); - - auto sampling_result = s1.ShouldSample(nullptr, invalid_trace_id, "", span_kind, view); - - ASSERT_EQ(Decision::RECORD_AND_SAMPLE, sampling_result.decision); - ASSERT_EQ(nullptr, sampling_result.attributes); - - constexpr uint8_t buf[] = {0, 0, 0, 0, 0, 0, 0, 0x80, 0, 0, 0, 0, 0, 0, 0, 0}; - opentelemetry::trace::TraceId valid_trace_id(buf); - - sampling_result = s1.ShouldSample(nullptr, valid_trace_id, "", span_kind, view); - - ASSERT_EQ(Decision::DROP, sampling_result.decision); - ASSERT_EQ(nullptr, sampling_result.attributes); - - ProbabilitySampler s2(0.50000001); - - sampling_result = s2.ShouldSample(nullptr, valid_trace_id, "", span_kind, view); - - ASSERT_EQ(Decision::RECORD_AND_SAMPLE, sampling_result.decision); - ASSERT_EQ(nullptr, sampling_result.attributes); - - ProbabilitySampler s3(0.49999999); - - sampling_result = s3.ShouldSample(nullptr, valid_trace_id, "", span_kind, view); - - ASSERT_EQ(Decision::DROP, sampling_result.decision); - ASSERT_EQ(nullptr, sampling_result.attributes); - - ProbabilitySampler s4(0.50000000); - - sampling_result = s4.ShouldSample(nullptr, valid_trace_id, "", span_kind, view); - - ASSERT_EQ(Decision::RECORD_AND_SAMPLE, sampling_result.decision); - ASSERT_EQ(nullptr, sampling_result.attributes); -} - -TEST(ProbabilitySampler, ShouldSampleWithContext) -{ - opentelemetry::trace::TraceId trace_id; - opentelemetry::trace::SpanKind span_kind = opentelemetry::trace::SpanKind::kInternal; - SpanContext c1(false, false); - SpanContext c2(true, false); - SpanContext c3(false, true); - SpanContext c4(true, true); - - using M = std::map; - M m1 = {{}}; - opentelemetry::common::KeyValueIterableView view{m1}; - - ProbabilitySampler s1(0.01); - - auto sampling_result = s1.ShouldSample(&c1, trace_id, "", span_kind, view); - - ASSERT_EQ(Decision::DROP, sampling_result.decision); - ASSERT_EQ(nullptr, sampling_result.attributes); - - sampling_result = s1.ShouldSample(&c2, trace_id, "", span_kind, view); - - ASSERT_EQ(Decision::RECORD_AND_SAMPLE, sampling_result.decision); - ASSERT_EQ(nullptr, sampling_result.attributes); - - sampling_result = s1.ShouldSample(&c3, trace_id, "", span_kind, view); - - ASSERT_EQ(Decision::RECORD_AND_SAMPLE, sampling_result.decision); - ASSERT_EQ(nullptr, sampling_result.attributes); - - sampling_result = s1.ShouldSample(&c4, trace_id, "", span_kind, view); - - ASSERT_EQ(Decision::RECORD_AND_SAMPLE, sampling_result.decision); - ASSERT_EQ(nullptr, sampling_result.attributes); -} - -TEST(ProbabilitySampler, ProbabilitySamplerHalf) -{ - double probability = 0.5; - int iterations = 100000; - int expected_count = static_cast(iterations * probability); - int variance = static_cast(iterations * 0.01); - - SpanContext c(true, true); - ProbabilitySampler s(probability); - - int actual_count = RunShouldSampleCountDecision(c, s, iterations); - - ASSERT_TRUE(actual_count < (expected_count + variance)); - ASSERT_TRUE(actual_count > (expected_count - variance)); -} - -TEST(ProbabilitySampler, ProbabilitySamplerOnePercent) -{ - double probability = 0.01; - int iterations = 100000; - int expected_count = static_cast(iterations * probability); - int variance = static_cast(iterations * 0.01); - - SpanContext c(true, true); - ProbabilitySampler s(probability); - - int actual_count = RunShouldSampleCountDecision(c, s, iterations); - - ASSERT_TRUE(actual_count < (expected_count + variance)); - ASSERT_TRUE(actual_count > (expected_count - variance)); -} - -TEST(ProbabilitySampler, ProbabilitySamplerAll) -{ - double probability = 1.0; - int iterations = 100000; - int expected_count = static_cast(iterations * probability); - - SpanContext c(true, true); - ProbabilitySampler s(probability); - - int actual_count = RunShouldSampleCountDecision(c, s, iterations); - - ASSERT_EQ(actual_count, expected_count); -} - -TEST(ProbabilitySampler, ProbabilitySamplerNone) -{ - double probability = 0.0; - int iterations = 100000; - int expected_count = static_cast(iterations * probability); - - SpanContext c(true, true); - ProbabilitySampler s(probability); - - int actual_count = RunShouldSampleCountDecision(c, s, iterations); - - ASSERT_EQ(actual_count, expected_count); -} - -TEST(ProbabilitySampler, GetDescription) -{ - ProbabilitySampler s1(0.01); - ASSERT_EQ("ProbabilitySampler{0.010000}", s1.GetDescription()); - - ProbabilitySampler s2(0.00); - ASSERT_EQ("ProbabilitySampler{0.000000}", s2.GetDescription()); - - ProbabilitySampler s3(1.00); - ASSERT_EQ("ProbabilitySampler{1.000000}", s3.GetDescription()); - - ProbabilitySampler s4(0.102030405); - ASSERT_EQ("ProbabilitySampler{0.102030}", s4.GetDescription()); - - ProbabilitySampler s5(3.00); - ASSERT_EQ("ProbabilitySampler{1.000000}", s5.GetDescription()); - - ProbabilitySampler s6(-3.00); - ASSERT_EQ("ProbabilitySampler{0.000000}", s6.GetDescription()); - - ProbabilitySampler s7(1.00000000001); - ASSERT_EQ("ProbabilitySampler{1.000000}", s7.GetDescription()); - - ProbabilitySampler s8(-1.00000000001); - ASSERT_EQ("ProbabilitySampler{0.000000}", s8.GetDescription()); - - ProbabilitySampler s9(0.50); - ASSERT_EQ("ProbabilitySampler{0.500000}", s9.GetDescription()); -} diff --git a/sdk/test/trace/trace_id_ratio_sampler_test.cc b/sdk/test/trace/trace_id_ratio_sampler_test.cc index db5b2b952c..f60c002804 100644 --- a/sdk/test/trace/trace_id_ratio_sampler_test.cc +++ b/sdk/test/trace/trace_id_ratio_sampler_test.cc @@ -34,7 +34,7 @@ int RunShouldSampleCountDecision(trace_api::SpanContext &context, using M = std::map; M m1 = {{}}; - opentelemetry::trace::KeyValueIterableView view{m1}; + opentelemetry::common::KeyValueIterableView view{m1}; for (int i = 0; i < iterations; ++i) { @@ -62,7 +62,7 @@ TEST(TraceIdRatioBasedSampler, ShouldSampleWithoutContext) using M = std::map; M m1 = {{}}; - opentelemetry::trace::KeyValueIterableView view{m1}; + opentelemetry::common::KeyValueIterableView view{m1}; TraceIdRatioBasedSampler s1(0.01); @@ -122,7 +122,7 @@ TEST(TraceIdRatioBasedSampler, ShouldSampleWithContext) using M = std::map; M m1 = {{}}; - opentelemetry::trace::KeyValueIterableView view{m1}; + opentelemetry::common::KeyValueIterableView view{m1}; TraceIdRatioBasedSampler s1(0.01); diff --git a/sdk/test/trace/tracer_test.cc b/sdk/test/trace/tracer_test.cc index 6ae2a401de..cc21a15b2f 100644 --- a/sdk/test/trace/tracer_test.cc +++ b/sdk/test/trace/tracer_test.cc @@ -23,20 +23,12 @@ using opentelemetry::trace::SpanContext; class MockSampler final : public Sampler { public: -<<<<<<< HEAD SamplingResult ShouldSample( - const SpanContext * /*parent_context*/, + const SpanContext & /*parent_context*/, trace_api::TraceId /*trace_id*/, nostd::string_view /*name*/, trace_api::SpanKind /*span_kind*/, const opentelemetry::common::KeyValueIterable & /*attributes*/) noexcept override -======= - SamplingResult ShouldSample(const SpanContext & /*parent_context*/, - trace_api::TraceId /*trace_id*/, - nostd::string_view /*name*/, - trace_api::SpanKind /*span_kind*/, - const trace_api::KeyValueIterable & /*attributes*/) noexcept override ->>>>>>> origin/master { // Return two pairs of attributes. These attributes should be added to the // span attributes