Skip to content

Commit 7403a6d

Browse files
[CONFIGURATION] Apply general attribute_limits to tracer and logger providers (#4468)
1 parent e2f39c4 commit 7403a6d

17 files changed

Lines changed: 730 additions & 99 deletions

‎CHANGELOG.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ Increment the:
1515

1616
## [Unreleased]
1717

18+
* [CONFIGURATION] Apply general `attribute_limits` per individual limit field.
19+
If a model-specific limit is set it is used, otherwise the matching general
20+
limit, otherwise the model-specific default. Limit fields on
21+
`AttributeLimitsConfiguration`, `SpanLimitsConfiguration`, and
22+
`LogRecordLimitsConfiguration` are now optional so omitted keys and YAML
23+
`null` are distinct from explicit values. This is a breaking change to the
24+
experimental configuration model.
25+
[#4467](https://github.com/open-telemetry/opentelemetry-cpp/issues/4467)
26+
1827
* [CONFIGURATION] Add a configuration builder for the host resource detector
1928
[#4451](https://github.com/open-telemetry/opentelemetry-cpp/issues/4451)
2029
* [CONFIGURATION] Build the configured resource detectors in SdkBuilder, apply

‎install/test/src/test_sdk.cc‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@
189189
#include <opentelemetry/sdk/configuration/metric_reader_configuration.h>
190190
#include <opentelemetry/sdk/configuration/metric_reader_configuration_visitor.h>
191191
#include <opentelemetry/sdk/configuration/open_census_metric_producer_configuration.h>
192+
#include <opentelemetry/sdk/configuration/optional_value.h>
192193
#include <opentelemetry/sdk/configuration/otlp_file_log_record_exporter_configuration.h>
193194
#include <opentelemetry/sdk/configuration/otlp_file_push_metric_exporter_configuration.h>
194195
#include <opentelemetry/sdk/configuration/otlp_file_span_exporter_configuration.h>

‎sdk/include/opentelemetry/sdk/configuration/attribute_limits_configuration.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <cstddef>
77
#include <limits>
88

9+
#include "opentelemetry/sdk/configuration/optional_value.h"
910
#include "opentelemetry/version.h"
1011

1112
OPENTELEMETRY_BEGIN_NAMESPACE
@@ -23,8 +24,8 @@ class AttributeLimitsConfiguration
2324
(std::numeric_limits<std::size_t>::max)();
2425
static constexpr std::size_t kDefaultAttributeCountLimit = 128;
2526

26-
std::size_t attribute_value_length_limit{kDefaultAttributeValueLengthLimit};
27-
std::size_t attribute_count_limit{kDefaultAttributeCountLimit};
27+
OptionalValue<std::size_t> attribute_value_length_limit;
28+
OptionalValue<std::size_t> attribute_count_limit;
2829
};
2930

3031
} // namespace configuration

‎sdk/include/opentelemetry/sdk/configuration/document_node.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <memory>
88
#include <string>
99

10+
#include "opentelemetry/sdk/configuration/optional_value.h"
1011
#include "opentelemetry/version.h"
1112

1213
OPENTELEMETRY_BEGIN_NAMESPACE
@@ -50,6 +51,7 @@ class DocumentNode
5051

5152
virtual std::size_t GetRequiredInteger(const std::string &name) const = 0;
5253
virtual std::size_t GetInteger(const std::string &name, std::size_t default_value) const = 0;
54+
virtual OptionalValue<std::size_t> GetOptionalInteger(const std::string &name) const = 0;
5355

5456
virtual std::int64_t GetSignedInteger(const std::string &name,
5557
std::int64_t default_value) const = 0;

‎sdk/include/opentelemetry/sdk/configuration/log_record_limits_configuration.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <cstddef>
77
#include <limits>
88

9+
#include "opentelemetry/sdk/configuration/optional_value.h"
910
#include "opentelemetry/version.h"
1011

1112
OPENTELEMETRY_BEGIN_NAMESPACE
@@ -23,8 +24,8 @@ class LogRecordLimitsConfiguration
2324
(std::numeric_limits<std::size_t>::max)();
2425
static constexpr std::size_t kDefaultAttributeCountLimit = 128;
2526

26-
std::size_t attribute_value_length_limit{kDefaultAttributeValueLengthLimit};
27-
std::size_t attribute_count_limit{kDefaultAttributeCountLimit};
27+
OptionalValue<std::size_t> attribute_value_length_limit;
28+
OptionalValue<std::size_t> attribute_count_limit;
2829
};
2930

3031
} // namespace configuration
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#pragma once
5+
6+
#include <stdexcept>
7+
8+
#include "opentelemetry/version.h"
9+
10+
OPENTELEMETRY_BEGIN_NAMESPACE
11+
namespace sdk
12+
{
13+
namespace configuration
14+
{
15+
16+
/**
17+
* C++14-friendly optional for configuration fields that may be set, omitted, or
18+
* explicitly null in YAML.
19+
*/
20+
template <typename T>
21+
class OptionalValue
22+
{
23+
public:
24+
OptionalValue() = default;
25+
26+
explicit OptionalValue(T value) : has_value_(true), value_(value) {}
27+
28+
bool HasValue() const { return has_value_; }
29+
30+
const T &Value() const
31+
{
32+
if (!has_value_)
33+
{
34+
throw std::runtime_error("OptionalValue has no value");
35+
}
36+
return value_;
37+
}
38+
39+
T ValueOr(T fallback) const { return has_value_ ? value_ : fallback; }
40+
41+
OptionalValue &operator=(T value)
42+
{
43+
has_value_ = true;
44+
value_ = value;
45+
return *this;
46+
}
47+
48+
private:
49+
bool has_value_{false};
50+
T value_{};
51+
};
52+
53+
} // namespace configuration
54+
} // namespace sdk
55+
OPENTELEMETRY_END_NAMESPACE

‎sdk/include/opentelemetry/sdk/configuration/ryml_document_node.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <string>
1111

1212
#include "opentelemetry/sdk/configuration/document_node.h"
13+
#include "opentelemetry/sdk/configuration/optional_value.h"
1314
#include "opentelemetry/version.h"
1415

1516
OPENTELEMETRY_BEGIN_NAMESPACE
@@ -50,6 +51,7 @@ class RymlDocumentNode : public DocumentNode
5051

5152
std::size_t GetRequiredInteger(const std::string &name) const override;
5253
std::size_t GetInteger(const std::string &name, std::size_t default_value) const override;
54+
OptionalValue<std::size_t> GetOptionalInteger(const std::string &name) const override;
5355

5456
std::int64_t GetSignedInteger(const std::string &name, std::int64_t default_value) const override;
5557

‎sdk/include/opentelemetry/sdk/configuration/sdk_builder.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <memory>
88

99
#include "opentelemetry/sdk/configuration/always_off_sampler_configuration.h"
10+
#include "opentelemetry/sdk/configuration/attribute_limits_configuration.h"
1011
#include "opentelemetry/sdk/configuration/base2_exponential_bucket_histogram_aggregation_configuration.h"
1112
#include "opentelemetry/sdk/configuration/batch_log_record_processor_configuration.h"
1213
#include "opentelemetry/sdk/configuration/batch_span_processor_configuration.h"
@@ -161,7 +162,8 @@ class SdkBuilder
161162

162163
std::unique_ptr<opentelemetry::sdk::trace::TracerProvider> CreateTracerProvider(
163164
const std::unique_ptr<opentelemetry::sdk::configuration::TracerProviderConfiguration> &model,
164-
const opentelemetry::sdk::resource::Resource &resource) const;
165+
const opentelemetry::sdk::resource::Resource &resource,
166+
const AttributeLimitsConfiguration *attribute_limits = nullptr) const;
165167

166168
std::unique_ptr<opentelemetry::context::propagation::TextMapPropagator> CreateTextMapPropagator(
167169
const std::string &name) const;
@@ -286,7 +288,8 @@ class SdkBuilder
286288

287289
std::unique_ptr<opentelemetry::sdk::logs::LoggerProvider> CreateLoggerProvider(
288290
const std::unique_ptr<opentelemetry::sdk::configuration::LoggerProviderConfiguration> &model,
289-
const opentelemetry::sdk::resource::Resource &resource) const;
291+
const opentelemetry::sdk::resource::Resource &resource,
292+
const AttributeLimitsConfiguration *attribute_limits = nullptr) const;
290293

291294
std::unique_ptr<opentelemetry::sdk::resource::ResourceDetector> CreateContainerResourceDetector(
292295
const opentelemetry::sdk::configuration::ContainerResourceDetectorConfiguration *model) const;

‎sdk/include/opentelemetry/sdk/configuration/span_limits_configuration.h‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
#pragma once
55

66
#include <cstddef>
7+
#include <cstdint>
78
#include <limits>
89

10+
#include "opentelemetry/sdk/configuration/optional_value.h"
911
#include "opentelemetry/version.h"
1012

1113
OPENTELEMETRY_BEGIN_NAMESPACE
@@ -27,12 +29,12 @@ class SpanLimitsConfiguration
2729
static constexpr std::uint32_t kDefaultEventAttributeCountLimit = 128;
2830
static constexpr std::uint32_t kDefaultLinkAttributeCountLimit = 128;
2931

30-
std::size_t attribute_value_length_limit{kDefaultAttributeValueLengthLimit};
31-
std::uint32_t attribute_count_limit{kDefaultAttributeCountLimit};
32-
std::uint32_t event_count_limit{kDefaultEventCountLimit};
33-
std::uint32_t link_count_limit{kDefaultLinkCountLimit};
34-
std::uint32_t event_attribute_count_limit{kDefaultEventAttributeCountLimit};
35-
std::uint32_t link_attribute_count_limit{kDefaultLinkAttributeCountLimit};
32+
OptionalValue<std::size_t> attribute_value_length_limit;
33+
OptionalValue<std::uint32_t> attribute_count_limit;
34+
OptionalValue<std::uint32_t> event_count_limit;
35+
OptionalValue<std::uint32_t> link_count_limit;
36+
OptionalValue<std::uint32_t> event_attribute_count_limit;
37+
OptionalValue<std::uint32_t> link_attribute_count_limit;
3638
};
3739

3840
} // namespace configuration

‎sdk/src/configuration/configuration_parser.cc‎

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
#include "opentelemetry/sdk/configuration/metric_producer_configuration.h"
8888
#include "opentelemetry/sdk/configuration/metric_reader_configuration.h"
8989
#include "opentelemetry/sdk/configuration/open_census_metric_producer_configuration.h"
90+
#include "opentelemetry/sdk/configuration/optional_value.h"
9091
#include "opentelemetry/sdk/configuration/otlp_file_log_record_exporter_configuration.h"
9192
#include "opentelemetry/sdk/configuration/otlp_file_push_metric_exporter_configuration.h"
9293
#include "opentelemetry/sdk/configuration/otlp_file_span_exporter_configuration.h"
@@ -360,13 +361,10 @@ std::unique_ptr<AttributeLimitsConfiguration>
360361
ConfigurationParser::ParseAttributeLimitsConfiguration(
361362
const std::unique_ptr<DocumentNode> &node) const
362363
{
363-
using Config = AttributeLimitsConfiguration;
364-
auto model = std::make_unique<AttributeLimitsConfiguration>();
364+
auto model = std::make_unique<AttributeLimitsConfiguration>();
365365

366-
model->attribute_value_length_limit =
367-
node->GetInteger("attribute_value_length_limit", Config::kDefaultAttributeValueLengthLimit);
368-
model->attribute_count_limit =
369-
node->GetInteger("attribute_count_limit", Config::kDefaultAttributeCountLimit);
366+
model->attribute_value_length_limit = node->GetOptionalInteger("attribute_value_length_limit");
367+
model->attribute_count_limit = node->GetOptionalInteger("attribute_count_limit");
370368

371369
return model;
372370
}
@@ -640,13 +638,10 @@ std::unique_ptr<LogRecordLimitsConfiguration>
640638
ConfigurationParser::ParseLogRecordLimitsConfiguration(
641639
const std::unique_ptr<DocumentNode> &node) const
642640
{
643-
using Config = LogRecordLimitsConfiguration;
644-
auto model = std::make_unique<LogRecordLimitsConfiguration>();
641+
auto model = std::make_unique<LogRecordLimitsConfiguration>();
645642

646-
model->attribute_value_length_limit =
647-
node->GetInteger("attribute_value_length_limit", Config::kDefaultAttributeValueLengthLimit);
648-
model->attribute_count_limit =
649-
node->GetInteger("attribute_count_limit", Config::kDefaultAttributeCountLimit);
643+
model->attribute_value_length_limit = node->GetOptionalInteger("attribute_value_length_limit");
644+
model->attribute_count_limit = node->GetOptionalInteger("attribute_count_limit");
650645

651646
return model;
652647
}
@@ -1673,29 +1668,28 @@ std::unique_ptr<PropagatorConfiguration> ConfigurationParser::ParsePropagatorCon
16731668
std::unique_ptr<SpanLimitsConfiguration> ConfigurationParser::ParseSpanLimitsConfiguration(
16741669
const std::unique_ptr<DocumentNode> &node) const
16751670
{
1676-
using Config = SpanLimitsConfiguration;
1677-
auto model = std::make_unique<SpanLimitsConfiguration>();
1671+
auto model = std::make_unique<SpanLimitsConfiguration>();
16781672

1679-
const auto get_valid_uint32 = [&node](const std::string &name, std::size_t default_value) {
1680-
std::size_t value = node->GetInteger(name, default_value);
1681-
if (value > std::numeric_limits<std::uint32_t>::max())
1673+
const auto get_optional_uint32 = [&node](const std::string &name) {
1674+
OptionalValue<std::size_t> value = node->GetOptionalInteger(name);
1675+
if (!value.HasValue())
16821676
{
1683-
std::string message = "Invalid value for " + name + ": " + std::to_string(value);
1677+
return OptionalValue<std::uint32_t>{};
1678+
}
1679+
if (value.Value() > std::numeric_limits<std::uint32_t>::max())
1680+
{
1681+
std::string message = "Invalid value for " + name + ": " + std::to_string(value.Value());
16841682
throw InvalidSchemaException(node->Location(), message);
16851683
}
1686-
return static_cast<uint32_t>(value);
1684+
return OptionalValue<std::uint32_t>{static_cast<std::uint32_t>(value.Value())};
16871685
};
16881686

1689-
model->attribute_value_length_limit =
1690-
node->GetInteger("attribute_value_length_limit", Config::kDefaultAttributeValueLengthLimit);
1691-
model->attribute_count_limit =
1692-
get_valid_uint32("attribute_count_limit", Config::kDefaultAttributeCountLimit);
1693-
model->event_count_limit = get_valid_uint32("event_count_limit", Config::kDefaultEventCountLimit);
1694-
model->link_count_limit = get_valid_uint32("link_count_limit", Config::kDefaultLinkCountLimit);
1695-
model->event_attribute_count_limit =
1696-
get_valid_uint32("event_attribute_count_limit", Config::kDefaultEventAttributeCountLimit);
1697-
model->link_attribute_count_limit =
1698-
get_valid_uint32("link_attribute_count_limit", Config::kDefaultLinkAttributeCountLimit);
1687+
model->attribute_value_length_limit = node->GetOptionalInteger("attribute_value_length_limit");
1688+
model->attribute_count_limit = get_optional_uint32("attribute_count_limit");
1689+
model->event_count_limit = get_optional_uint32("event_count_limit");
1690+
model->link_count_limit = get_optional_uint32("link_count_limit");
1691+
model->event_attribute_count_limit = get_optional_uint32("event_attribute_count_limit");
1692+
model->link_attribute_count_limit = get_optional_uint32("link_attribute_count_limit");
16991693

17001694
return model;
17011695
}

0 commit comments

Comments
 (0)