Skip to content

Commit 823fbfb

Browse files
committed
Add CHANGELOG
1 parent d800b20 commit 823fbfb

File tree

5 files changed

+103
-4
lines changed

5 files changed

+103
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ Increment the:
2727
* [SDK] Implements options for the ParentBasedSampler with default values
2828
[#3553](https://github.com/open-telemetry/opentelemetry-cpp/pull/3553)
2929

30+
* [SDK] Add bundle version of utf8_range to validate attributes
31+
[#3512](https://github.com/open-telemetry/opentelemetry-cpp/pull/3512)
32+
3033
## [1.22 2025-07-11]
3134

3235
* [DOC] Udpate link to membership document

sdk/include/opentelemetry/sdk/instrumentationscope/instrumentation_scope.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,16 @@ class InstrumentationScope
102102
result->attributes_.reserve(opentelemetry::nostd::size(arg));
103103
for (auto &argv : arg)
104104
{
105+
if (!common::AttributeValidator::IsValid(argv.first))
106+
{
107+
OTEL_INTERNAL_LOG_WARN("[InstrumentationScope] Invalid attribute key "
108+
<< std::string{argv.first} << ". This attribute will be ignored.");
109+
continue;
110+
}
111+
105112
if (!common::AttributeValidator::IsValid(argv.second))
106113
{
107-
OTEL_INTERNAL_LOG_WARN("[InstrumentationScope] Invalid attribute value for: "
114+
OTEL_INTERNAL_LOG_WARN("[InstrumentationScope] Invalid attribute value for "
108115
<< std::string{argv.first} << ". This attribute will be ignored.");
109116
continue;
110117
}
@@ -168,9 +175,16 @@ class InstrumentationScope
168175
void SetAttribute(nostd::string_view key,
169176
const opentelemetry::common::AttributeValue &value) noexcept
170177
{
178+
if (!common::AttributeValidator::IsValid(key))
179+
{
180+
OTEL_INTERNAL_LOG_WARN("[InstrumentationScope] Invalid attribute key "
181+
<< std::string{key} << ". This attribute will be ignored.");
182+
return;
183+
}
184+
171185
if (!common::AttributeValidator::IsValid(value))
172186
{
173-
OTEL_INTERNAL_LOG_WARN("[InstrumentationScope] Invalid attribute value for: "
187+
OTEL_INTERNAL_LOG_WARN("[InstrumentationScope] Invalid attribute value for "
174188
<< std::string{key} << ". This attribute will be ignored.");
175189
return;
176190
}

sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "opentelemetry/common/attribute_value.h"
1111
#include "opentelemetry/common/key_value_iterable.h"
1212
#include "opentelemetry/nostd/string_view.h"
13+
#include "opentelemetry/sdk/common/attribute_validity.h"
1314
#include "opentelemetry/sdk/metrics/state/filtered_ordered_attribute_map.h"
1415
#include "opentelemetry/version.h"
1516

@@ -50,7 +51,8 @@ class DefaultAttributesProcessor : public AttributesProcessor
5051
MetricAttributes process(
5152
const opentelemetry::common::KeyValueIterable &attributes) const noexcept override
5253
{
53-
MetricAttributes result(attributes);
54+
MetricAttributes result(
55+
opentelemetry::sdk::common::KeyValueFilterIterable(attributes, "[Metrics] "));
5456
return result;
5557
}
5658

@@ -78,7 +80,9 @@ class FilteringAttributesProcessor : public AttributesProcessor
7880
const opentelemetry::common::KeyValueIterable &attributes) const noexcept override
7981
{
8082
MetricAttributes result;
81-
attributes.ForEachKeyValue(
83+
opentelemetry::sdk::common::KeyValueFilterIterable validate_attributes{attributes,
84+
"[Metrics] "};
85+
validate_attributes.ForEachKeyValue(
8286
[&](nostd::string_view key, opentelemetry::common::AttributeValue value) noexcept {
8387
if (allowed_attribute_keys_.find(key.data()) != allowed_attribute_keys_.end())
8488
{

sdk/test/instrumentationscope/instrumentationscope_test.cc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ TEST(InstrumentationScope, CreateInstrumentationScope)
6161
}
6262
}
6363

64+
TEST(InstrumentationScope, CreateInstrumentationScopeWithInvalidAttributes)
65+
{
66+
std::string library_name = "opentelemetry-cpp";
67+
std::string library_version = "0.1.0";
68+
std::string schema_url = "https://opentelemetry.io/schemas/1.2.0";
69+
uint32_t attrubite_value3[] = {7, 8, 9};
70+
auto instrumentation_scope =
71+
InstrumentationScope::Create(library_name, library_version, schema_url,
72+
{{"attribute-key1", "attribute-value"},
73+
{"invalid-key\xff", "valid-value"},
74+
{"valid-key", "invalid-value\xff"}});
75+
76+
EXPECT_EQ(instrumentation_scope->GetAttributes().size(), 1);
77+
}
78+
6479
TEST(InstrumentationScope, CreateInstrumentationScopeWithLoopForAttributes)
6580
{
6681
std::string library_name = "opentelemetry-cpp";
@@ -195,6 +210,25 @@ TEST(InstrumentationScope, SetAttribute)
195210
}
196211
}
197212

213+
TEST(InstrumentationScope, SetInvalidAttribute)
214+
{
215+
std::string library_name = "opentelemetry-cpp";
216+
std::string library_version = "0.1.0";
217+
std::string schema_url = "https://opentelemetry.io/schemas/1.2.0";
218+
auto instrumentation_scope =
219+
InstrumentationScope::Create(library_name, library_version, schema_url);
220+
221+
EXPECT_EQ(instrumentation_scope->GetName(), library_name);
222+
EXPECT_EQ(instrumentation_scope->GetVersion(), library_version);
223+
EXPECT_EQ(instrumentation_scope->GetSchemaURL(), schema_url);
224+
EXPECT_EQ(instrumentation_scope->GetAttributes().size(), 0);
225+
226+
instrumentation_scope->SetAttribute("attribute-key1", "attribute-value");
227+
instrumentation_scope->SetAttribute("invalid-key\xff", "valid-value");
228+
instrumentation_scope->SetAttribute("valid-key", "invalid-value\xff");
229+
EXPECT_EQ(instrumentation_scope->GetAttributes().size(), 1);
230+
}
231+
198232
TEST(InstrumentationScope, LegacyInstrumentationLibrary)
199233
{
200234

sdk/test/metrics/meter_test.cc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,50 @@ TEST_F(MeterCreateInstrumentTest, ViewCorrectedDuplicateSyncInstrumentsByDescrip
656656
});
657657
}
658658

659+
TEST_F(MeterCreateInstrumentTest, SyncInstrumentWithInvalidAttributes)
660+
{
661+
InstrumentDescriptor descriptor{"my_counter", "desc", "unit", InstrumentType::kCounter,
662+
InstrumentValueType::kDouble};
663+
AddDescriptionCorrectionView(descriptor.name_, descriptor.unit_, descriptor.type_,
664+
descriptor.description_);
665+
666+
auto counter1 = meter_->CreateDoubleCounter("my_counter", "desc", "unit");
667+
counter1->Add(
668+
1,
669+
{{"key", "value1"}, {"invalid-key\xff", "valid-value"}, {"valid-key", "invalid-value\xff"}});
670+
671+
metric_reader_ptr_->Collect([this](ResourceMetrics &metric_data) {
672+
EXPECT_EQ(metric_data.scope_metric_data_.size(), 1);
673+
// only one metric_data object expected after correction with the view
674+
EXPECT_EQ(metric_data.scope_metric_data_[0].metric_data_.size(), 1);
675+
EXPECT_EQ(metric_data.scope_metric_data_[0].metric_data_[0].point_data_attr_.size(), 1);
676+
return true;
677+
});
678+
}
679+
680+
TEST_F(MeterCreateInstrumentTest, AsyncInstrumentWithInvalidAttributes)
681+
{
682+
auto observable_counter1 =
683+
meter_->CreateInt64ObservableCounter("observable_counter", "desc", "unit");
684+
auto callback1 = [](opentelemetry::metrics::ObserverResult observer, void * /* state */) {
685+
auto observer_long =
686+
nostd::get<nostd::shared_ptr<opentelemetry::metrics::ObserverResultT<int64_t>>>(observer);
687+
observer_long->Observe(12, {{"key", "value1"},
688+
{"invalid-key\xff", "valid-value"},
689+
{"valid-key", "invalid-value\xff"}});
690+
};
691+
692+
observable_counter1->AddCallback(callback1, nullptr);
693+
694+
metric_reader_ptr_->Collect([this](ResourceMetrics &metric_data) {
695+
EXPECT_EQ(metric_data.scope_metric_data_.size(), 1);
696+
EXPECT_EQ(metric_data.scope_metric_data_[0].metric_data_.size(), 1);
697+
auto &point_data_attr = metric_data.scope_metric_data_[0].metric_data_[0].point_data_attr_;
698+
EXPECT_EQ(point_data_attr.size(), 1);
699+
return true;
700+
});
701+
}
702+
659703
TEST_F(MeterCreateInstrumentTest, IdenticalAsyncInstruments)
660704
{
661705
auto observable_counter1 =

0 commit comments

Comments
 (0)