Skip to content

Commit

Permalink
Always on sampler (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
ziqizh authored Jun 29, 2020
1 parent ed0a36e commit 79b6141
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 6 deletions.
4 changes: 2 additions & 2 deletions sdk/include/opentelemetry/sdk/trace/sampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace trace
namespace trace_api = opentelemetry::trace;

/**
* A sampling Decision for a Span to be created.
* A sampling Decision for a Span to be created.
*/
enum class Decision
{
Expand All @@ -31,7 +31,7 @@ enum class Decision
};

/**
* The output of ShouldSample.
* The output of ShouldSample.
* It contains a sampling Decision and a set of Span Attributes.
*/
struct SamplingResult
Expand Down
36 changes: 36 additions & 0 deletions sdk/include/opentelemetry/sdk/trace/samplers/always_on.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

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

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace trace
{
namespace trace_api = opentelemetry::trace;
/**
* The always on sampler is a default sampler which always return Decision::RECORD_AND_SAMPLE
*/
class AlwaysOnSampler : public Sampler
{
public:
/**
* @return Always return Decision RECORD_AND_SAMPLE
*/
inline 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::RECORD_AND_SAMPLE, nullptr};
}

/**
* @return Description MUST be AlwaysOnSampler
*/
inline std::string GetDescription() const noexcept override { return "AlwaysOnSampler"; }
};
} // namespace trace
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
19 changes: 16 additions & 3 deletions sdk/test/trace/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,25 @@ cc_test(
)

cc_test(
name = "always_off_sampler_test",
name = "always_on_sampler_test",
srcs = [
"always_off_sampler_test.cc"
"always_on_sampler_test.cc",
],
deps = [
"//sdk/src/trace",
"@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 always_off_sampler_test)
tracer_test always_off_sampler_test always_on_sampler_test)
add_executable(${testname} "${testname}.cc")
target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace)
Expand Down
43 changes: 43 additions & 0 deletions sdk/test/trace/always_on_sampler_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "opentelemetry/nostd/span.h"
#include "opentelemetry/sdk/trace/samplers/always_on.h"

#include <gtest/gtest.h>
#include <map>

using namespace opentelemetry::sdk::trace;
using namespace opentelemetry::nostd;

TEST(AlwaysOnSampler, ShouldSample)
{
AlwaysOnSampler sampler;

// A buffer of trace_id with no specific meaning
constexpr uint8_t buf[] = {0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7};

trace_api::TraceId trace_id_invalid;
trace_api::TraceId trace_id_valid(buf);
std::map<std::string, int> key_value_container = {{"key", 0}};

// Test with invalid (empty) trace id and empty parent context
auto sampling_result = sampler.ShouldSample(
nullptr, trace_id_invalid, "invalid trace id test", trace_api::SpanKind::kServer,
trace_api::KeyValueIterableView<std::map<std::string, int>>(key_value_container));

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

// Test with a valid trace id and empty parent context
sampling_result = sampler.ShouldSample(
nullptr, trace_id_valid, "valid trace id test", trace_api::SpanKind::kServer,
trace_api::KeyValueIterableView<std::map<std::string, int>>(key_value_container));

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

TEST(AlwaysOnSampler, GetDescription)
{
AlwaysOnSampler sampler;

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

0 comments on commit 79b6141

Please sign in to comment.