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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ Increment the:
* [RELEASE] Bump main branch to 1.29.0-dev
[#4259](https://github.com/open-telemetry/opentelemetry-cpp/pull/4259)

* [ETW] Ensure spans own their names until they are ended
[#4247](https://github.com/open-telemetry/opentelemetry-cpp/pull/4247)

* [Metrics SDK] Implement configurable cardinality limit
[#4188](https://github.com/open-telemetry/opentelemetry-cpp/pull/4188)
* [CONFIGURATION] Programmatic configuration use case tests and fixes
[#4243](https://github.com/open-telemetry/opentelemetry-cpp/pull/4243)

Expand Down
46 changes: 46 additions & 0 deletions sdk/include/opentelemetry/sdk/metrics/cardinality_limits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <cstddef>

#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{

/**
* Cardinality limits per instrument type for a MetricReader.
*
* All fields default to kDefaultCardinalityLimit (2000), which is the
* OpenTelemetry Metrics SDK specification default.
*
* To configure a reader-level limit, set the relevant fields and pass the
* struct to MetricReader::SetCardinalityLimits(). The sdk_builder converts
* the declarative CardinalityLimitsConfiguration (where 0 means "unset, use
* default_limit") into a fully-resolved CardinalityLimits before calling
* SetCardinalityLimits(), so SetCardinalityLimits() itself is a plain setter
* with no sentinel logic.
*/
struct CardinalityLimits
{
std::size_t default_limit = 2000;
std::size_t counter = 2000;
std::size_t gauge = 2000;
std::size_t histogram = 2000;
std::size_t observable_counter = 2000;
std::size_t observable_gauge = 2000;
std::size_t observable_up_down_counter = 2000;
std::size_t up_down_counter = 2000;
};

/// The OpenTelemetry Metrics SDK spec default cardinality limit.
constexpr std::size_t kDefaultCardinalityLimit = 2000;

} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
25 changes: 25 additions & 0 deletions sdk/include/opentelemetry/sdk/metrics/metric_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

#include <atomic>
#include <chrono>
#include <cstddef>

#include "opentelemetry/nostd/function_ref.h"
#include "opentelemetry/sdk/metrics/cardinality_limits.h"
#include "opentelemetry/sdk/metrics/export/metric_producer.h"
#include "opentelemetry/sdk/metrics/instruments.h"
#include "opentelemetry/version.h"
Expand All @@ -16,6 +18,7 @@ namespace sdk
{
namespace metrics
{

/**
* MetricReader defines the interface to collect metrics from SDK
*/
Expand Down Expand Up @@ -45,6 +48,27 @@ class MetricReader
virtual AggregationTemporality GetAggregationTemporality(
InstrumentType instrument_type) const noexcept = 0;

/**
* Get the cardinality limit for a given instrument type.
* Defaults to kAggregationCardinalityLimit (2000) unless overridden via
* SetCardinalityLimits().
*
* @param instrument_type The instrument type to get the limit for
* @return The cardinality limit for the requested instrument type
*/
std::size_t GetCardinalityLimit(InstrumentType instrument_type) const noexcept;

/**
* Set per-instrument-type cardinality limits for this reader.
*
* TODO: Reader-level limits are stored but not yet enforced as a per-collector

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.

It looks like reader-level limits are only stored, but never actually applied during collection. So if someone configures a reader limit of 10, the reader can still export more than 10 series. Should we really expose this configuration before it is enforced?

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.

Yes, I do agree on this one: reader-level limits are currently just plumbing with no enforcement, so a configured limit of 10 doesn't actually cap anything yet. I called this out as a known follow-up in the original PR description; I'd like to tackle enforcement in a separate PR since it touches the collection path per CollectorHandle, which is a bigger change than the histogram fix. Left the TODO in place for now to track it; happy to prioritize it if you'd rather not ship the config surface unenforced in the meantime.

* fallback during the collection path. Enforcement will be added in a follow-up.
* View-level limits (via AggregationConfig) are enforced today.
*
* @param limits The cardinality limits to apply
Comment thread
dbarker marked this conversation as resolved.
Resolved
*/
void SetCardinalityLimits(const CardinalityLimits &limits) noexcept;

/**
* Shutdown the metric reader.
*/
Expand Down Expand Up @@ -73,6 +97,7 @@ class MetricReader
private:
MetricProducer *metric_producer_{nullptr};
std::atomic<bool> shutdown_{false};
CardinalityLimits cardinality_limits_;
};
} // namespace metrics
} // namespace sdk
Expand Down
12 changes: 12 additions & 0 deletions sdk/include/opentelemetry/sdk/metrics/state/metric_collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
#pragma once

#include <chrono>
#include <cstddef>
#include <memory>

#include "opentelemetry/nostd/function_ref.h"
#include "opentelemetry/sdk/metrics/cardinality_limits.h"
#include "opentelemetry/sdk/metrics/export/metric_filter.h"
#include "opentelemetry/sdk/metrics/export/metric_producer.h"
#include "opentelemetry/sdk/metrics/instruments.h"
Expand Down Expand Up @@ -36,6 +38,14 @@ class CollectorHandle

virtual AggregationTemporality GetAggregationTemporality(
InstrumentType instrument_type) noexcept = 0;

/**
* Get the cardinality limit for a given instrument type.
*
* @param instrument_type The instrument type to get the limit for
* @return The cardinality limit for the requested instrument type
*/
virtual std::size_t GetCardinalityLimit(InstrumentType instrument_type) const noexcept = 0;
};

/**
Expand All @@ -61,6 +71,8 @@ class MetricCollector : public MetricProducer, public CollectorHandle
AggregationTemporality GetAggregationTemporality(
InstrumentType instrument_type) noexcept override;

std::size_t GetCardinalityLimit(InstrumentType instrument_type) const noexcept override;

/**
* The callback to be called for each metric exporter. This will only be those
* metrics that have been produced since the last time this method was called.
Expand Down
76 changes: 63 additions & 13 deletions sdk/src/configuration/sdk_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "opentelemetry/sdk/configuration/batch_span_processor_configuration.h"
#include "opentelemetry/sdk/configuration/boolean_array_attribute_value_configuration.h"
#include "opentelemetry/sdk/configuration/boolean_attribute_value_configuration.h"
#include "opentelemetry/sdk/configuration/cardinality_limits_configuration.h"
#include "opentelemetry/sdk/configuration/composable_probability_sampler_configuration.h"
#include "opentelemetry/sdk/configuration/configuration.h"
#include "opentelemetry/sdk/configuration/configured_sdk.h"
Expand Down Expand Up @@ -145,6 +146,8 @@
#include "opentelemetry/sdk/logs/processor.h"
#include "opentelemetry/sdk/logs/simple_log_record_processor_factory.h"
#include "opentelemetry/sdk/metrics/aggregation/aggregation_config.h"
#include "opentelemetry/sdk/metrics/cardinality_limits.h"
#include "opentelemetry/sdk/metrics/exemplar/filter_type.h"
#include "opentelemetry/sdk/metrics/export/metric_producer.h"
#include "opentelemetry/sdk/metrics/instruments.h"
#include "opentelemetry/sdk/metrics/meter_config.h"
Expand Down Expand Up @@ -180,10 +183,6 @@
#include "opentelemetry/version.h"
#include "src/common/wildcard_match.h"

#ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
# include "opentelemetry/sdk/metrics/exemplar/filter_type.h"
#endif

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
Expand Down Expand Up @@ -257,6 +256,40 @@ static opentelemetry::logs::Severity ToLogSeverity(
return opentelemetry::logs::Severity::kInvalid;
}

/// Convert a CardinalityLimitsConfiguration (where 0 means "unset, use
/// default_limit") into a fully-resolved CardinalityLimits struct ready for
/// MetricReader::SetCardinalityLimits().
static opentelemetry::sdk::metrics::CardinalityLimits ToCardinalityLimits(
const opentelemetry::sdk::configuration::CardinalityLimitsConfiguration &cfg)
{
opentelemetry::sdk::metrics::CardinalityLimits limits;
// If the caller supplied a non-zero default, use it; otherwise keep the
// SDK default (kDefaultCardinalityLimit = 2000) already in the struct.
const std::size_t d = cfg.default_limit != CardinalityLimitsConfiguration::kInheritDefault
? cfg.default_limit
: limits.default_limit;
limits.default_limit = d;
limits.counter = cfg.counter != CardinalityLimitsConfiguration::kInheritDefault ? cfg.counter : d;
limits.gauge = cfg.gauge != CardinalityLimitsConfiguration::kInheritDefault ? cfg.gauge : d;
limits.histogram =
cfg.histogram != CardinalityLimitsConfiguration::kInheritDefault ? cfg.histogram : d;
limits.observable_counter =
cfg.observable_counter != CardinalityLimitsConfiguration::kInheritDefault
? cfg.observable_counter
: d;
limits.observable_gauge = cfg.observable_gauge != CardinalityLimitsConfiguration::kInheritDefault
? cfg.observable_gauge
: d;
limits.observable_up_down_counter =
cfg.observable_up_down_counter != CardinalityLimitsConfiguration::kInheritDefault
? cfg.observable_up_down_counter
: d;
limits.up_down_counter = cfg.up_down_counter != CardinalityLimitsConfiguration::kInheritDefault
? cfg.up_down_counter
: d;
return limits;
}

class ResourceAttributeValueSetter
: public opentelemetry::sdk::configuration::AttributeValueConfigurationVisitor
{
Expand Down Expand Up @@ -1561,22 +1594,25 @@ std::unique_ptr<opentelemetry::sdk::metrics::MetricReader> SdkBuilder::CreatePer
OTEL_INTERNAL_LOG_WARN("metric producer not supported, ignoring");
}

if (model->cardinality_limits != nullptr)
{
OTEL_INTERNAL_LOG_WARN("cardinality limits not supported, ignoring");
}

const PeriodicMetricReaderBuilder *builder = registry_->GetPeriodicMetricReaderBuilder();

if (builder != nullptr)
{
OTEL_INTERNAL_LOG_DEBUG("CreatePeriodicMetricReader() using registered builder");
sdk = builder->Build(model, std::move(exporter_sdk));
return sdk;
}
else
{
static const std::string die("No builder for PeriodicMetricReader");
throw UnsupportedException(die);
}

static const std::string die("No builder for PeriodicMetricReader");
throw UnsupportedException(die);
if (model->cardinality_limits != nullptr)
{
sdk->SetCardinalityLimits(ToCardinalityLimits(*model->cardinality_limits));
}

return sdk;
}

std::unique_ptr<opentelemetry::sdk::metrics::MetricReader> SdkBuilder::CreatePullMetricReader(
Expand All @@ -1593,7 +1629,7 @@ std::unique_ptr<opentelemetry::sdk::metrics::MetricReader> SdkBuilder::CreatePul

if (model->cardinality_limits != nullptr)
{
OTEL_INTERNAL_LOG_WARN("cardinality limits not supported, ignoring");
sdk->SetCardinalityLimits(ToCardinalityLimits(*model->cardinality_limits));
}

return sdk;
Expand Down Expand Up @@ -1700,6 +1736,20 @@ void SdkBuilder::AddView(
sdk_aggregation_config = CreateAggregationConfig(stream->aggregation, sdk_aggregation_type);
}

// Apply aggregation_cardinality_limit from the view stream configuration
if (stream->aggregation_cardinality_limit != 0)
{
if (sdk_aggregation_config)
{
sdk_aggregation_config->cardinality_limit_ = stream->aggregation_cardinality_limit;
}
else
{
sdk_aggregation_config = std::make_shared<opentelemetry::sdk::metrics::AggregationConfig>(

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.

I think this breaks for histograms. When only aggregation_cardinality_limit is set, we create a regular AggregationConfig here. But the view registry expects a histogram config for histogram instruments, so it rejects the whole view. Can we create the config based on the instrument’s default aggregation type?

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.

Apologies for missing histograms. I just confirmed that this rejects the view for any histogram/base2-exponential-histogram instrument when only aggregation_cardinality_limit is set. Fixed in #4314: AddView() now resolves the instrument's effective aggregation type (falling back to the instrument-derived default, same as ViewRegistry does) and builds the matching AggregationConfig subtype before applying the limit. Added a test that reproduces the rejection against the old code and verifies the fix.

stream->aggregation_cardinality_limit);
}
}

std::unique_ptr<opentelemetry::sdk::metrics::AttributesProcessor> sdk_attribute_processor;

if (stream->attribute_keys != nullptr)
Expand Down
35 changes: 35 additions & 0 deletions sdk/src/metrics/metric_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "opentelemetry/sdk/metrics/metric_reader.h"
#include "opentelemetry/sdk/common/global_log_handler.h"
#include "opentelemetry/sdk/metrics/cardinality_limits.h"
#include "opentelemetry/sdk/metrics/export/metric_producer.h"
#include "opentelemetry/version.h"

Expand Down Expand Up @@ -84,6 +85,40 @@ bool MetricReader::IsShutdown() const noexcept
return shutdown_.load(std::memory_order_acquire);
}

std::size_t MetricReader::GetCardinalityLimit(InstrumentType instrument_type) const noexcept
{
switch (instrument_type)
{
case InstrumentType::kCounter:
return cardinality_limits_.counter;
case InstrumentType::kHistogram:
return cardinality_limits_.histogram;
case InstrumentType::kUpDownCounter:
return cardinality_limits_.up_down_counter;
case InstrumentType::kObservableCounter:
return cardinality_limits_.observable_counter;
case InstrumentType::kObservableGauge:
return cardinality_limits_.observable_gauge;
case InstrumentType::kObservableUpDownCounter:
return cardinality_limits_.observable_up_down_counter;
case InstrumentType::kGauge:
return cardinality_limits_.gauge;
default:
return cardinality_limits_.default_limit;
}
}

void MetricReader::SetCardinalityLimits(const CardinalityLimits &limits) noexcept
{
cardinality_limits_ = limits;
Comment thread
dbarker marked this conversation as resolved.
Resolved
// TODO: Reader-level limits are stored but not yet enforced as a per-collector
// fallback during the collection path. Enforcement will be added in a follow-up.
OTEL_INTERNAL_LOG_WARN(
"MetricReader::SetCardinalityLimits - reader-level cardinality limits are stored "
"but not yet enforced during collection. Use view-level AggregationConfig to "
"enforce limits today.");
}

} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
6 changes: 6 additions & 0 deletions sdk/src/metrics/state/metric_collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

#include <chrono>
#include <cstddef>
#include <memory>
#include <ostream>
#include <utility>
Expand Down Expand Up @@ -45,6 +46,11 @@ AggregationTemporality MetricCollector::GetAggregationTemporality(
return aggregation_temporality;
}

std::size_t MetricCollector::GetCardinalityLimit(InstrumentType instrument_type) const noexcept
{
return metric_reader_->GetCardinalityLimit(instrument_type);
}

MetricProducer::Result MetricCollector::Produce() noexcept
{
if (!meter_context_)
Expand Down
1 change: 1 addition & 0 deletions sdk/test/metrics/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ cc_test(
],
deps = [
"metrics_common_test_utils",
"//sdk/src/metrics",
"//test_common:headers",
"@com_google_googletest//:gtest_main",
],
Expand Down
9 changes: 9 additions & 0 deletions sdk/test/metrics/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
#pragma once

#include <chrono>
#include <cstddef>
#include <memory>

#include "opentelemetry/sdk/metrics/cardinality_limits.h"

#include "opentelemetry/sdk/metrics/instruments.h"
#include "opentelemetry/sdk/metrics/metric_reader.h"
#include "opentelemetry/sdk/metrics/push_metric_exporter.h"
Expand Down Expand Up @@ -67,6 +70,12 @@ class MockCollectorHandle : public opentelemetry::sdk::metrics::CollectorHandle
opentelemetry::sdk::metrics::AggregationTemporality GetAggregationTemporality(
opentelemetry::sdk::metrics::InstrumentType) noexcept override;

std::size_t GetCardinalityLimit(
opentelemetry::sdk::metrics::InstrumentType) const noexcept override
{
return opentelemetry::sdk::metrics::kDefaultCardinalityLimit;
}

private:
opentelemetry::sdk::metrics::AggregationTemporality temporality_;
};
Loading
Loading