-
Notifications
You must be signed in to change notification settings - Fork 410
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
98 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |