-
Notifications
You must be signed in to change notification settings - Fork 546
Probability Sampler #136
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
Merged
reyang
merged 34 commits into
open-telemetry:master
from
ziqizh:nholbrook/probability-sampler
Jul 14, 2020
Merged
Probability Sampler #136
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
29e127b
Add ProbabilitySampler
nholbrook f38322f
Add ProbabilitySampler unit tests
nholbrook 7061588
Add sampler config and copyright attribution
nholbrook e763732
Fix syntax issue
nholbrook e994b31
Add newlines to EOF
nholbrook d371d7a
Update trace buffer type
nholbrook 85c9dbe
Update static_assert
nholbrook 5dea575
Fix inconsistent spacing
nholbrook 1049c3f
Move variable definitions
nholbrook a52cc95
Fix function to remove unnecessary copying of values
nholbrook 4436867
Move probability sampler to correct directory
nholbrook b40793a
Update sampler import paths
nholbrook 7cdb783
Update comments
nholbrook d5f0f05
Update CMakeList
nholbrook 12991df
Update comments
nholbrook eb38fb6
Fixed inconsistent spacing
nholbrook 2db9c11
Move functions to private
nholbrook 88fdf96
Change GetDescription to cache string
nholbrook 9c0f807
Modify function to use memcpy
nholbrook 482cf35
Add proper error handling for invalid arguments
nholbrook 3586063
Remove optional config
nholbrook 42da2e7
Remove constructor exception
nholbrook 1e52ec8
Update comments
nholbrook 81c00e9
Update description string construction
nholbrook 38350ab
Move functions to anonymous namespace
nholbrook ca91bf9
Update function to calculate threshold from buffer correctly
nholbrook 13f9639
Add unit tests to check probability
nholbrook 363f3b8
Update probability unit tests
nholbrook 0211c7d
Update function header
nholbrook 93c3e98
Update trace/test CMakeList
nholbrook 9348b32
Update function description
nholbrook 289fab9
Format probability sampler test
nholbrook 920404d
Update sampler usage
nholbrook a788576
Remove pseudorandom config from test
nholbrook File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
sdk/include/opentelemetry/sdk/trace/samplers/probability.h
This file contains hidden or 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,50 @@ | ||
| #pragma once | ||
|
|
||
| #include "opentelemetry/sdk/trace/sampler.h" | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace sdk | ||
| { | ||
| namespace trace | ||
| { | ||
| namespace trace_api = opentelemetry::trace; | ||
| /** | ||
| * The probability sampler, based on it's configuration, should either defer the | ||
| * decision to sample to it's parent, or compute and return a decision based on | ||
| * the provided trace_id and probability. | ||
| */ | ||
| class ProbabilitySampler : public Sampler | ||
| { | ||
| public: | ||
| /** | ||
| * @param probability a required value, 1.0 >= probability >= 0.0, that given any | ||
| * random trace_id, ShouldSample will return RECORD_AND_SAMPLE | ||
| * @throws invalid_argument if probability is out of bounds [0.0, 1.0] | ||
| */ | ||
| explicit ProbabilitySampler(double probability); | ||
|
|
||
| /** | ||
| * @return Returns either RECORD_AND_SAMPLE or NOT_RECORD based on current | ||
| * sampler configuration and provided parent_context / tracer_id. tracer_id | ||
| * is used as a pseudorandom value in conjunction with the predefined | ||
| * threshold to determine whether this trace should be sampled | ||
| */ | ||
| 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 Description MUST be ProbabilitySampler{0.000100} | ||
| */ | ||
| std::string GetDescription() const noexcept override; | ||
|
|
||
| private: | ||
| std::string sampler_description_; | ||
| const uint64_t threshold_; | ||
| }; | ||
| } // namespace trace | ||
| } // namespace sdk | ||
| OPENTELEMETRY_END_NAMESPACE |
This file contains hidden or 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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| add_library(opentelemetry_trace tracer_provider.cc tracer.cc span.cc samplers/parent_or_else.cc) | ||
| add_library(opentelemetry_trace tracer_provider.cc tracer.cc span.cc | ||
| samplers/parent_or_else.cc samplers/probability.cc) |
This file contains hidden or 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,114 @@ | ||
| // Copyright 2020, Open Telemetry Authors | ||
| // Copyright 2017, OpenCensus Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "opentelemetry/sdk/trace/samplers/probability.h" | ||
|
|
||
| #include <cmath> | ||
| #include <cstdint> | ||
| #include <stdexcept> | ||
|
|
||
| namespace trace_api = opentelemetry::trace; | ||
|
|
||
| namespace | ||
| { | ||
| /** | ||
| * Converts a probability in [0, 1] to a threshold in [0, UINT64_MAX] | ||
| * | ||
| * @param probability a required value top be converted to uint64_t. is | ||
| * bounded by 1 >= probability >= 0. | ||
| * @return Returns threshold value computed after converting probability to | ||
| * uint64_t datatype | ||
| */ | ||
| uint64_t CalculateThreshold(double probability) noexcept | ||
| { | ||
| if (probability <= 0.0) return 0; | ||
| if (probability >= 1.0) return UINT64_MAX; | ||
|
|
||
| // We can't directly return probability * UINT64_MAX. | ||
| // | ||
| // UINT64_MAX is (2^64)-1, but as a double rounds up to 2^64. | ||
| // For probabilities >= 1-(2^-54), the product wraps to zero! | ||
| // Instead, calculate the high and low 32 bits separately. | ||
| const double product = UINT32_MAX * probability; | ||
| double hi_bits, lo_bits = ldexp(modf(product, &hi_bits), 32) + product; | ||
| return (static_cast<uint64_t>(hi_bits) << 32) + | ||
| static_cast<uint64_t>(lo_bits); | ||
| } | ||
|
|
||
| /** | ||
| * @param trace_id a required value to be converted to uint64_t. trace_id must | ||
| * at least 8 bytes long | ||
| * @return Returns threshold value computed after converting trace_id to | ||
| * uint64_t datatype | ||
| */ | ||
| uint64_t CalculateThresholdFromBuffer(const trace_api::TraceId& trace_id) noexcept | ||
| { | ||
| // We only use the first 8 bytes of TraceId. | ||
| static_assert(trace_api::TraceId::kSize >= 8, "TraceID must be at least 8 bytes long."); | ||
|
|
||
| uint64_t res = 0; | ||
| std::memcpy(&res, &trace_id, 8); | ||
|
|
||
| double probability = (double) res / UINT64_MAX; | ||
|
|
||
| return CalculateThreshold(probability); | ||
| } | ||
| } // namespace | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace sdk | ||
| { | ||
| namespace trace | ||
| { | ||
| ProbabilitySampler::ProbabilitySampler(double probability) | ||
| : threshold_(CalculateThreshold(probability)) | ||
| { | ||
| if (probability > 1.0) probability = 1.0; | ||
| if (probability < 0.0) probability = 0.0; | ||
| sampler_description_ = "ProbabilitySampler{" + std::to_string(probability) + "}"; | ||
| } | ||
|
|
||
| SamplingResult ProbabilitySampler::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 | ||
| { | ||
| if (parent_context && !parent_context->HasRemoteParent()) { | ||
| if (parent_context->IsSampled()) { | ||
| return { Decision::RECORD_AND_SAMPLE, nullptr }; | ||
| } else { | ||
| return { Decision::NOT_RECORD, nullptr }; | ||
| } | ||
| } | ||
|
|
||
| if (threshold_ == 0) return { Decision::NOT_RECORD, nullptr }; | ||
|
|
||
| if (CalculateThresholdFromBuffer(trace_id) <= threshold_) | ||
| { | ||
| return { Decision::RECORD_AND_SAMPLE, nullptr }; | ||
| } | ||
|
|
||
| return { Decision::NOT_RECORD, nullptr }; | ||
| } | ||
|
|
||
| std::string ProbabilitySampler::GetDescription() const noexcept | ||
| { | ||
| return sampler_description_; | ||
| } | ||
| } // namespace trace | ||
| } // namespace sdk | ||
| OPENTELEMETRY_END_NAMESPACE | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| foreach(testname tracer_provider_test span_data_test simple_processor_test | ||
| tracer_test always_off_sampler_test always_on_sampler_test | ||
| parent_or_else_sampler_test) | ||
| parent_or_else_sampler_test probability_sampler_test) | ||
| add_executable(${testname} "${testname}.cc") | ||
| target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES} | ||
| ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace) | ||
| ${CMAKE_THREAD_LIBS_INIT} opentelemetry_common opentelemetry_trace) | ||
| gtest_add_tests(TARGET ${testname} TEST_PREFIX trace. TEST_LIST ${testname}) | ||
| endforeach() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.