Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always on sampler #122

Merged
merged 50 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from 49 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
89ce9f7
add sampler header file
Jun 17, 2020
e65cf5c
add param and comments
Jun 17, 2020
7b20dc1
change class definition
Jun 17, 2020
81cb054
add Decision enum
Jun 17, 2020
b206ced
add Decision enum
Jun 17, 2020
cdfcd3d
add ShouldSample params and placeholder
Jun 17, 2020
1f972e0
add ShouldSample params and placeholder
Jun 17, 2020
5097b2e
Merge branch 'sampling-api-header' of github.com:ziqizh/opentelemetry…
Jun 17, 2020
279ef0e
minor tweaks
Jun 17, 2020
bc14502
add sampler header file
Jun 17, 2020
f705b35
add param and comments
Jun 17, 2020
8111e93
change class definition
Jun 17, 2020
e4ac250
add Decision enum
Jun 17, 2020
48d4695
add Decision enum
Jun 17, 2020
7c4ac5a
minor tweaks
Jun 17, 2020
6fd2568
resolve conflict
Jun 17, 2020
5fcf4af
resolve conflict
Jun 17, 2020
ef00c7f
Merge branch 'sampling-api-header' of github.com:ziqizh/opentelemetry…
Jun 18, 2020
503edb6
change enum to enum class
Jun 18, 2020
7c204b8
change sampling result to struct
Jun 19, 2020
6d4632b
change param
Jun 19, 2020
71f8d9d
add AttributeKeyValue placeholder
Jun 19, 2020
590be00
add namespace
Jun 19, 2020
11b4420
define functions
Jun 18, 2020
a5fb523
remove sampling result class
Jun 19, 2020
fe817e5
add always on sampler
Jun 19, 2020
78b5be0
add function
Jun 19, 2020
8d43a4a
style fix
Jun 22, 2020
7586d1d
Merge branch 'master' into sampling-api-header
Jun 22, 2020
fab9d74
Merge branch 'sampling-api-header' into always-on-sampler
Jun 22, 2020
fff8bda
add test
Jun 22, 2020
82ccca0
minor syntax change
Jun 22, 2020
249e5be
Revert "minor syntax change"
Jun 22, 2020
d9ddf89
add CMake rule
Jun 22, 2020
620056c
add CMake rule
Jun 22, 2020
1be3e5e
Merge branch 'master' into always-on-sampler
ziqizh Jun 23, 2020
cddfc4e
update struct
Jun 23, 2020
9bd90ca
update imports'
Jun 23, 2020
db4b68b
update namespace
Jun 23, 2020
36a2c85
modify test file
Jun 23, 2020
f392e2f
modify test case
Jun 24, 2020
a555daa
modify test case
Jun 24, 2020
54db68e
remove extra header files
Jun 24, 2020
543ece9
change to inline function
Jun 24, 2020
03002b6
change cmake
Jun 24, 2020
1a621e4
change bazel version
Jun 25, 2020
64a0b7f
resolve conflice
Jun 29, 2020
b08e544
move sampler to separate folder
Jun 29, 2020
a200940
format
Jun 29, 2020
04340b4
add inline keyword
Jun 29, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
*/
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
*/
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());
}