From 82f5e8c04576c83ed8700d4314b961c18d4a6d9e Mon Sep 17 00:00:00 2001 From: Oliver Zhang <30583106+ziqizh@users.noreply.github.com> Date: Thu, 23 Jul 2020 13:57:19 -0400 Subject: [PATCH] change Sampler description from string to string_view (#193) --- sdk/include/opentelemetry/sdk/trace/sampler.h | 2 +- .../sdk/trace/samplers/always_off.h | 20 ++++++++----------- .../sdk/trace/samplers/always_on.h | 2 +- .../sdk/trace/samplers/parent_or_else.h | 2 +- .../sdk/trace/samplers/probability.h | 4 ++-- sdk/src/trace/samplers/parent_or_else.cc | 4 ++-- sdk/src/trace/samplers/probability.cc | 6 +++--- sdk/test/trace/tracer_test.cc | 2 +- 8 files changed, 19 insertions(+), 23 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/trace/sampler.h b/sdk/include/opentelemetry/sdk/trace/sampler.h index 35a0927404a..f0ab8856263 100644 --- a/sdk/include/opentelemetry/sdk/trace/sampler.h +++ b/sdk/include/opentelemetry/sdk/trace/sampler.h @@ -77,7 +77,7 @@ class Sampler * * @return the description of this Sampler. */ - virtual std::string GetDescription() const noexcept = 0; + virtual nostd::string_view GetDescription() const noexcept = 0; }; } // namespace trace } // namespace sdk diff --git a/sdk/include/opentelemetry/sdk/trace/samplers/always_off.h b/sdk/include/opentelemetry/sdk/trace/samplers/always_off.h index 252cd9e4fd4..894b8ed598f 100644 --- a/sdk/include/opentelemetry/sdk/trace/samplers/always_off.h +++ b/sdk/include/opentelemetry/sdk/trace/samplers/always_off.h @@ -19,23 +19,19 @@ class AlwaysOffSampler : public Sampler /** * @return Returns NOT_RECORD always */ - 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 override + 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 override { - return { Decision::NOT_RECORD, nullptr }; + return {Decision::NOT_RECORD, nullptr}; } - + /** * @return Description MUST be AlwaysOffSampler */ - std::string GetDescription() const noexcept override - { - return "AlwaysOffSampler"; - } + nostd::string_view GetDescription() const noexcept override { return "AlwaysOffSampler"; } }; } // namespace trace } // namespace sdk diff --git a/sdk/include/opentelemetry/sdk/trace/samplers/always_on.h b/sdk/include/opentelemetry/sdk/trace/samplers/always_on.h index 5dd5f864579..5a11f1c10cd 100644 --- a/sdk/include/opentelemetry/sdk/trace/samplers/always_on.h +++ b/sdk/include/opentelemetry/sdk/trace/samplers/always_on.h @@ -30,7 +30,7 @@ class AlwaysOnSampler : public Sampler /** * @return Description MUST be AlwaysOnSampler */ - inline std::string GetDescription() const noexcept override { return "AlwaysOnSampler"; } + inline nostd::string_view GetDescription() const noexcept override { return "AlwaysOnSampler"; } }; } // namespace trace } // namespace sdk diff --git a/sdk/include/opentelemetry/sdk/trace/samplers/parent_or_else.h b/sdk/include/opentelemetry/sdk/trace/samplers/parent_or_else.h index aec99346046..95cf9afa37e 100644 --- a/sdk/include/opentelemetry/sdk/trace/samplers/parent_or_else.h +++ b/sdk/include/opentelemetry/sdk/trace/samplers/parent_or_else.h @@ -31,7 +31,7 @@ class ParentOrElseSampler : public Sampler /** * @return Description MUST be ParentOrElse{delegate_sampler_.getDescription()} */ - std::string GetDescription() const noexcept override; + nostd::string_view GetDescription() const noexcept override; private: const std::shared_ptr delegate_sampler_; diff --git a/sdk/include/opentelemetry/sdk/trace/samplers/probability.h b/sdk/include/opentelemetry/sdk/trace/samplers/probability.h index 18bca7042a3..aa585409a4a 100644 --- a/sdk/include/opentelemetry/sdk/trace/samplers/probability.h +++ b/sdk/include/opentelemetry/sdk/trace/samplers/probability.h @@ -39,10 +39,10 @@ class ProbabilitySampler : public Sampler /** * @return Description MUST be ProbabilitySampler{0.000100} */ - std::string GetDescription() const noexcept override; + nostd::string_view GetDescription() const noexcept override; private: - std::string sampler_description_; + std::string description_; const uint64_t threshold_; }; } // namespace trace diff --git a/sdk/src/trace/samplers/parent_or_else.cc b/sdk/src/trace/samplers/parent_or_else.cc index 2b56ab9d4b3..f3b926c2b69 100644 --- a/sdk/src/trace/samplers/parent_or_else.cc +++ b/sdk/src/trace/samplers/parent_or_else.cc @@ -7,7 +7,7 @@ namespace trace { ParentOrElseSampler::ParentOrElseSampler(std::shared_ptr delegate_sampler) noexcept : delegate_sampler_(delegate_sampler), - description_("ParentOrElse{" + delegate_sampler->GetDescription() + "}") + description_("ParentOrElse{" + std::string{delegate_sampler->GetDescription()} + "}") {} SamplingResult ParentOrElseSampler::ShouldSample( @@ -32,7 +32,7 @@ SamplingResult ParentOrElseSampler::ShouldSample( return {Decision::NOT_RECORD, nullptr}; } -std::string ParentOrElseSampler::GetDescription() const noexcept +nostd::string_view ParentOrElseSampler::GetDescription() const noexcept { return description_; } diff --git a/sdk/src/trace/samplers/probability.cc b/sdk/src/trace/samplers/probability.cc index 2f8c9453cd4..0038ed11fbd 100644 --- a/sdk/src/trace/samplers/probability.cc +++ b/sdk/src/trace/samplers/probability.cc @@ -77,7 +77,7 @@ ProbabilitySampler::ProbabilitySampler(double probability) { if (probability > 1.0) probability = 1.0; if (probability < 0.0) probability = 0.0; - sampler_description_ = "ProbabilitySampler{" + std::to_string(probability) + "}"; + description_ = "ProbabilitySampler{" + std::to_string(probability) + "}"; } SamplingResult ProbabilitySampler::ShouldSample( @@ -105,9 +105,9 @@ SamplingResult ProbabilitySampler::ShouldSample( return { Decision::NOT_RECORD, nullptr }; } -std::string ProbabilitySampler::GetDescription() const noexcept +nostd::string_view ProbabilitySampler::GetDescription() const noexcept { - return sampler_description_; + return description_; } } // namespace trace } // namespace sdk diff --git a/sdk/test/trace/tracer_test.cc b/sdk/test/trace/tracer_test.cc index 29812bf0594..3d10670e166 100644 --- a/sdk/test/trace/tracer_test.cc +++ b/sdk/test/trace/tracer_test.cc @@ -33,7 +33,7 @@ class MockSampler final : public Sampler {{"sampling_attr1", 123}, {"sampling_attr2", "string"}}))}; } - std::string GetDescription() const noexcept override { return "MockSampler"; } + nostd::string_view GetDescription() const noexcept override { return "MockSampler"; } }; /**