Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Increment the:

## [Unreleased]

* [CONFIGURATION] Add support for the composite sampler configuration
(programmatic and from yaml)
([#4366](https://github.com/open-telemetry/opentelemetry-cpp/pull/4366))
* [SDK] `OTELResourceDetector` now percent-decodes values parsed from the
`OTEL_RESOURCE_ATTRIBUTES` environment variable, per the W3C Baggage value
grammar the resource spec defers to. A malformed escape sequence is left
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ namespace configuration
class ComposableProbabilitySamplerConfiguration : public ComposableSamplerConfiguration
{
public:
static constexpr double kDefaultRatio = 1.0;
static constexpr double kDefaultRatio = 1.0; // schema: minimum 0, maximum 1
static constexpr double kMinRatio = 0.0;
static constexpr double kMaxRatio = 1.0;

ComposableProbabilitySamplerConfiguration() = default;
double ratio{kDefaultRatio};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ namespace trace
class ComposableProbabilitySampler : public ComposableSampler
{
public:
/**
* @param ratio the sampling probability: either 0 (never sample) or a
* value in [2^-56, 1.0]. Other values (including NaN) log a warning and
* fall back to the default of 1.0.
*/
explicit ComposableProbabilitySampler(double ratio);

SamplingIntent GetSamplingIntent(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <string>
#include <vector>

#include "opentelemetry/sdk/trace/samplers/predicate.h"
#include "opentelemetry/trace/span_context.h"
#include "opentelemetry/trace/span_metadata.h"
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace trace
{

/**
* Matching criteria for RuleBasedPredicate. A criteria group left at its
* default is inactive and matches every span.
*/
struct RuleBasedPredicateOptions
{
bool match_values = false;
std::string values_key;
std::vector<std::string> values;

bool match_patterns = false;
std::string patterns_key;
std::vector<std::string> included;
std::vector<std::string> excluded;

bool match_parent_none = false;
bool match_parent_remote = false;
bool match_parent_local = false;

bool match_span_kind_internal = false;
bool match_span_kind_server = false;
bool match_span_kind_client = false;
bool match_span_kind_producer = false;
bool match_span_kind_consumer = false;

// Significant digits used to format double attribute values for matching.
int double_precision = 6;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - this type should be std::uint32_t and ranged checked before use. Okay for followup PR

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it.

};

/**
* RuleBasedPredicate matches a span when every active criteria group in its
* options matches: parent kind, span kind, an attribute equal to one of the
* given values, and an attribute matching the included/excluded wildcard
* patterns (excluded wins). Non-string attributes are matched by their string
* representation; doubles are formatted with %g at double_precision significant
* digits, so 404.0 matches "404".
*/
class RuleBasedPredicate : public Predicate
{
public:
explicit RuleBasedPredicate(RuleBasedPredicateOptions options);

bool SpanMatches(
const opentelemetry::trace::SpanContext &parent_context,
nostd::string_view name,
opentelemetry::trace::SpanKind span_kind,
const opentelemetry::common::KeyValueIterable &attributes,
const opentelemetry::trace::SpanContextKeyValueIterable &links) const noexcept override;

nostd::string_view GetDescription() const noexcept override;

private:
bool ParentMatches(const opentelemetry::trace::SpanContext &parent_context) const noexcept;
bool SpanKindMatches(opentelemetry::trace::SpanKind span_kind) const noexcept;

RuleBasedPredicateOptions options_;
};

} // namespace trace
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
8 changes: 4 additions & 4 deletions sdk/src/common/wildcard_match.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#pragma once

#include <cstddef>
#include <string>

#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
Expand All @@ -18,11 +18,11 @@ namespace common
* Matches a text string against a pattern containing '?' (any single character)
* and '*' (any sequence of characters, including empty) wildcards.
*/
inline bool WildcardMatch(const std::string &pattern, const std::string &text)
inline bool WildcardMatch(nostd::string_view pattern, nostd::string_view text)
{
size_t p = 0;
size_t t = 0;
size_t star_p = std::string::npos;
size_t star_p = nostd::string_view::npos;
size_t star_t = 0;

while (t < text.size())
Expand All @@ -38,7 +38,7 @@ inline bool WildcardMatch(const std::string &pattern, const std::string &text)
star_t = t;
++p;
}
else if (star_p != std::string::npos)
else if (star_p != nostd::string_view::npos)
{
p = star_p + 1;
++star_t;
Expand Down
7 changes: 7 additions & 0 deletions sdk/src/configuration/configuration_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1843,6 +1843,13 @@ ConfigurationParser::ParseComposableProbabilitySamplerConfiguration(
using Config = ComposableProbabilitySamplerConfiguration;
auto model = std::make_unique<ComposableProbabilitySamplerConfiguration>();
model->ratio = node->GetDouble("ratio", Config::kDefaultRatio);
if (!(model->ratio >= Config::kMinRatio && model->ratio <= Config::kMaxRatio))
{
std::string message("Illegal ratio: ");
message.append(std::to_string(model->ratio));
throw InvalidSchemaException(node->Location(), message);
}

return model;
}

Expand Down
Loading
Loading