Skip to content

Commit

Permalink
Always Off Sampler (open-telemetry#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
nholbrook authored and nadiaciobanu committed Jul 2, 2020
1 parent 268d59c commit 6b428a3
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.2.0
3.3.0
41 changes: 41 additions & 0 deletions sdk/include/opentelemetry/sdk/trace/samplers/always_off.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include "opentelemetry/sdk/trace/sampler.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace trace
{
namespace trace_api = opentelemetry::trace;
/**
* The always off sampler always returns NOT_RECORD, effectively disabling
* tracing functionality.
*/
class AlwaysOffSampler : public Sampler
{
public:
/**
* @return Returns NOT_RECORD always
*/
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
{
return { Decision::NOT_RECORD, nullptr };
}

/**
* @return Description MUST be AlwaysOffSampler
*/
std::string GetDescription() const noexcept override
{
return "AlwaysOffSampler";
}
};
} // namespace trace
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
11 changes: 11 additions & 0 deletions sdk/test/trace/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,14 @@ cc_test(
"@com_google_googletest//:gtest_main",
],
)

cc_test(
name = "always_off_sampler_test",
srcs = [
"always_off_sampler_test.cc"
],
deps = [
"//sdk/src/trace",
"@com_google_googletest//:gtest_main",
]
)
2 changes: 1 addition & 1 deletion sdk/test/trace/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
foreach(testname tracer_provider_test span_data_test simple_processor_test
tracer_test)
tracer_test always_off_sampler_test)
add_executable(${testname} "${testname}.cc")
target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace)
Expand Down
30 changes: 30 additions & 0 deletions sdk/test/trace/always_off_sampler_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "opentelemetry/sdk/trace/samplers/always_off.h"

#include <gtest/gtest.h>

using opentelemetry::sdk::trace::AlwaysOffSampler;
using opentelemetry::sdk::trace::Decision;

TEST(AlwaysOffSampler, ShouldSample)
{
AlwaysOffSampler sampler;

opentelemetry::trace::TraceId trace_id;
opentelemetry::trace::SpanKind span_kind = opentelemetry::trace::SpanKind::kInternal;

using M = std::map<std::string, int>;
M m1 = {{}};
opentelemetry::trace::KeyValueIterableView<M> view{m1};

auto sampling_result = sampler.ShouldSample(nullptr, trace_id, "", span_kind, view);

ASSERT_EQ(Decision::NOT_RECORD, sampling_result.decision);
ASSERT_EQ(nullptr, sampling_result.attributes);
}

TEST(AlwaysOffSampler, GetDescription)
{
AlwaysOffSampler sampler;

ASSERT_EQ("AlwaysOffSampler", sampler.GetDescription());
}

0 comments on commit 6b428a3

Please sign in to comment.