Skip to content

Commit 768bd99

Browse files
authored
Merge branch 'main' into upgrade_weaver_0.11.0
2 parents 38531e5 + 762b73d commit 768bd99

23 files changed

+172
-40
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ Increment the:
149149
* [bazel] Update opentelemetry-proto in MODULE.bazel
150150
[#3163](https://github.com/open-telemetry/opentelemetry-cpp/pull/3163)
151151

152+
* [EXPORTER] Fix scope attributes missing from otlp traces metrics
153+
[#3185](https://github.com/open-telemetry/opentelemetry-cpp/pull/3185)
154+
152155
Important changes:
153156

154157
* [API] Jaeger Propagator should not be deprecated

exporters/memory/include/opentelemetry/exporters/memory/in_memory_span_exporter.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ class InMemorySpanExporter final : public opentelemetry::sdk::trace::SpanExporte
7575
return sdk::common::ExportResult::kSuccess;
7676
}
7777

78+
virtual bool ForceFlush(std::chrono::microseconds /* timeout */) noexcept override
79+
{
80+
return true;
81+
}
82+
7883
/**
7984
* @param timeout an optional value containing the timeout of the exporter
8085
* note: passing custom timeout values is not currently supported for this exporter

exporters/otlp/include/opentelemetry/exporters/otlp/otlp_populate_attribute_utils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "opentelemetry/common/attribute_value.h"
77
#include "opentelemetry/nostd/string_view.h"
88
#include "opentelemetry/sdk/common/attribute_utils.h"
9+
#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h"
910
#include "opentelemetry/sdk/resource/resource.h"
1011
#include "opentelemetry/version.h"
1112

@@ -20,6 +21,7 @@ namespace v1
2021
{
2122
class AnyValue;
2223
class KeyValue;
24+
class InstrumentationScope;
2325
} // namespace v1
2426
} // namespace common
2527

@@ -49,6 +51,10 @@ class OtlpPopulateAttributeUtils
4951
static void PopulateAttribute(opentelemetry::proto::resource::v1::Resource *proto,
5052
const opentelemetry::sdk::resource::Resource &resource) noexcept;
5153

54+
static void PopulateAttribute(opentelemetry::proto::common::v1::InstrumentationScope *proto,
55+
const opentelemetry::sdk::instrumentationscope::InstrumentationScope
56+
&instrumentation_scope) noexcept;
57+
5258
static void PopulateAnyValue(opentelemetry::proto::common::v1::AnyValue *proto_value,
5359
const opentelemetry::common::AttributeValue &value) noexcept;
5460

exporters/otlp/src/otlp_metric_utils.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ void OtlpMetricUtils::PopulateResourceMetrics(
242242
OtlpPopulateAttributeUtils::PopulateAttribute(resource_metrics->mutable_resource(),
243243
*(data.resource_));
244244

245+
resource_metrics->set_schema_url(data.resource_->GetSchemaURL());
246+
245247
for (auto &scope_metrics : data.scope_metric_data_)
246248
{
247249
if (scope_metrics.scope_ == nullptr)
@@ -252,7 +254,9 @@ void OtlpMetricUtils::PopulateResourceMetrics(
252254
proto::common::v1::InstrumentationScope *scope = scope_lib_metrics->mutable_scope();
253255
scope->set_name(scope_metrics.scope_->GetName());
254256
scope->set_version(scope_metrics.scope_->GetVersion());
255-
resource_metrics->set_schema_url(scope_metrics.scope_->GetSchemaURL());
257+
scope_lib_metrics->set_schema_url(scope_metrics.scope_->GetSchemaURL());
258+
259+
OtlpPopulateAttributeUtils::PopulateAttribute(scope, *scope_metrics.scope_);
256260

257261
for (auto &metric_data : scope_metrics.metric_data_)
258262
{

exporters/otlp/src/otlp_populate_attribute_utils.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "opentelemetry/nostd/utility.h"
1616
#include "opentelemetry/nostd/variant.h"
1717
#include "opentelemetry/sdk/common/attribute_utils.h"
18+
#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h"
1819
#include "opentelemetry/sdk/resource/resource.h"
1920
#include "opentelemetry/version.h"
2021

@@ -315,6 +316,17 @@ void OtlpPopulateAttributeUtils::PopulateAttribute(
315316
}
316317
}
317318

319+
void OtlpPopulateAttributeUtils::PopulateAttribute(
320+
opentelemetry::proto::common::v1::InstrumentationScope *proto,
321+
const opentelemetry::sdk::instrumentationscope::InstrumentationScope
322+
&instrumentation_scope) noexcept
323+
{
324+
for (const auto &kv : instrumentation_scope.GetAttributes())
325+
{
326+
OtlpPopulateAttributeUtils::PopulateAttribute(proto->add_attributes(), kv.first, kv.second);
327+
}
328+
}
329+
318330
} // namespace otlp
319331
} // namespace exporter
320332
OPENTELEMETRY_END_NAMESPACE

exporters/otlp/src/otlp_recordable.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ proto::common::v1::InstrumentationScope OtlpRecordable::GetProtoInstrumentationS
107107
{
108108
instrumentation_scope.set_name(instrumentation_scope_->GetName());
109109
instrumentation_scope.set_version(instrumentation_scope_->GetVersion());
110+
OtlpPopulateAttributeUtils::PopulateAttribute(&instrumentation_scope, *instrumentation_scope_);
110111
}
111112
return instrumentation_scope;
112113
}

exporters/otlp/src/otlp_recordable_utils.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ void OtlpRecordableUtils::PopulateRequest(
107107
proto::common::v1::InstrumentationScope instrumentation_scope_proto;
108108
instrumentation_scope_proto.set_name(input_scope_spans.first->GetName());
109109
instrumentation_scope_proto.set_version(input_scope_spans.first->GetVersion());
110+
OtlpPopulateAttributeUtils::PopulateAttribute(&instrumentation_scope_proto,
111+
*input_scope_spans.first);
112+
110113
*scope_spans->mutable_scope() = instrumentation_scope_proto;
111114
scope_spans->set_schema_url(input_scope_spans.first->GetSchemaURL());
112115
}
@@ -170,11 +173,7 @@ void OtlpRecordableUtils::PopulateRequest(
170173
proto_scope->set_name(input_scope_log.first->GetName());
171174
proto_scope->set_version(input_scope_log.first->GetVersion());
172175

173-
for (auto &scope_attribute : input_scope_log.first->GetAttributes())
174-
{
175-
OtlpPopulateAttributeUtils::PopulateAttribute(
176-
proto_scope->add_attributes(), scope_attribute.first, scope_attribute.second);
177-
}
176+
OtlpPopulateAttributeUtils::PopulateAttribute(proto_scope, *input_scope_log.first);
178177
}
179178
output_scope_log->set_schema_url(input_scope_log.first->GetSchemaURL());
180179
}

exporters/otlp/test/otlp_file_exporter_test.cc

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class OtlpFileExporterTestPeer : public ::testing::Test
9595
resource_attributes["vec_uint64_value"] = std::vector<uint64_t>{7, 8};
9696
resource_attributes["vec_double_value"] = std::vector<double>{3.2, 3.3};
9797
resource_attributes["vec_string_value"] = std::vector<std::string>{"vector", "string"};
98-
auto resource = resource::Resource::Create(resource_attributes);
98+
auto resource = resource::Resource::Create(resource_attributes, "resource_url");
9999

100100
auto processor_opts = sdk::trace::BatchSpanProcessorOptions();
101101
processor_opts.max_export_batch_size = 5;
@@ -109,9 +109,17 @@ class OtlpFileExporterTestPeer : public ::testing::Test
109109

110110
std::string report_trace_id;
111111

112+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
113+
auto tracer = provider->GetTracer("scope_name", "scope_version", "scope_url",
114+
{{ "scope_key",
115+
"scope_value" }});
116+
#else
117+
auto tracer = provider->GetTracer("scope_name", "scope_version", "scope_url");
118+
#endif
119+
120+
auto parent_span = tracer->StartSpan("Test parent span");
121+
112122
char trace_id_hex[2 * trace_api::TraceId::kSize] = {0};
113-
auto tracer = provider->GetTracer("test");
114-
auto parent_span = tracer->StartSpan("Test parent span");
115123

116124
trace_api::StartSpanOptions child_span_opts = {};
117125
child_span_opts.parent = parent_span->GetContext();
@@ -136,11 +144,22 @@ class OtlpFileExporterTestPeer : public ::testing::Test
136144
auto check_json = nlohmann::json::parse(check_json_text, nullptr, false);
137145
if (!check_json.is_discarded())
138146
{
139-
auto resource_span = *check_json["resourceSpans"].begin();
140-
auto scope_span = *resource_span["scopeSpans"].begin();
141-
auto span = *scope_span["spans"].begin();
142-
auto received_trace_id = span["traceId"].get<std::string>();
143-
EXPECT_EQ(received_trace_id, report_trace_id);
147+
auto resource_span = *check_json["resourceSpans"].begin();
148+
auto scope_span = *resource_span["scopeSpans"].begin();
149+
auto scope = scope_span["scope"];
150+
auto span = *scope_span["spans"].begin();
151+
152+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
153+
ASSERT_EQ(1, scope["attributes"].size());
154+
const auto scope_attribute = scope["attributes"].front();
155+
EXPECT_EQ("scope_key", scope_attribute["key"].get<std::string>());
156+
EXPECT_EQ("scope_value", scope_attribute["value"]["stringValue"].get<std::string>());
157+
#endif
158+
EXPECT_EQ("resource_url", resource_span["schemaUrl"].get<std::string>());
159+
EXPECT_EQ("scope_url", scope_span["schemaUrl"].get<std::string>());
160+
EXPECT_EQ("scope_name", scope["name"].get<std::string>());
161+
EXPECT_EQ("scope_version", scope["version"].get<std::string>());
162+
EXPECT_EQ(report_trace_id, span["traceId"].get<std::string>());
144163
}
145164
else
146165
{

exporters/otlp/test/otlp_file_metric_exporter_test.cc

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,14 @@ class OtlpFileMetricExporterTestPeer : public ::testing::Test
8585
opentelemetry::sdk::metrics::SumPointData sum_point_data2{};
8686
sum_point_data2.value_ = 20.0;
8787
opentelemetry::sdk::metrics::ResourceMetrics data;
88+
8889
auto resource = opentelemetry::sdk::resource::Resource::Create(
89-
opentelemetry::sdk::resource::ResourceAttributes{});
90+
opentelemetry::sdk::resource::ResourceAttributes{}, "resource_url");
9091
data.resource_ = &resource;
91-
auto scope = opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create(
92-
"library_name", "1.5.0");
92+
93+
auto scope = opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create(
94+
"library_name", "1.5.0", "scope_url", {{"scope_key", "scope_value"}});
95+
9396
opentelemetry::sdk::metrics::MetricData metric_data{
9497
opentelemetry::sdk::metrics::InstrumentDescriptor{
9598
"metrics_library_name", "metrics_description", "metrics_unit",
@@ -100,6 +103,7 @@ class OtlpFileMetricExporterTestPeer : public ::testing::Test
100103
std::vector<opentelemetry::sdk::metrics::PointDataAttributes>{
101104
{opentelemetry::sdk::metrics::PointAttributes{{"a1", "b1"}}, sum_point_data},
102105
{opentelemetry::sdk::metrics::PointAttributes{{"a2", "b2"}}, sum_point_data2}}};
106+
103107
data.scope_metric_data_ = std::vector<opentelemetry::sdk::metrics::ScopeMetrics>{
104108
{scope.get(), std::vector<opentelemetry::sdk::metrics::MetricData>{metric_data}}};
105109

@@ -111,15 +115,23 @@ class OtlpFileMetricExporterTestPeer : public ::testing::Test
111115
output.flush();
112116
output.sync();
113117
auto check_json_text = output.str();
118+
114119
if (!check_json_text.empty())
115120
{
116121
auto check_json = nlohmann::json::parse(check_json_text, nullptr, false);
117122

118123
auto resource_metrics = *check_json["resourceMetrics"].begin();
119124
auto scope_metrics = *resource_metrics["scopeMetrics"].begin();
120125
auto scope = scope_metrics["scope"];
126+
127+
EXPECT_EQ("resource_url", resource_metrics["schemaUrl"].get<std::string>());
121128
EXPECT_EQ("library_name", scope["name"].get<std::string>());
122129
EXPECT_EQ("1.5.0", scope["version"].get<std::string>());
130+
EXPECT_EQ("scope_url", scope_metrics["schemaUrl"].get<std::string>());
131+
ASSERT_EQ(1, scope["attributes"].size());
132+
const auto scope_attribute = scope["attributes"].front();
133+
EXPECT_EQ("scope_key", scope_attribute["key"].get<std::string>());
134+
EXPECT_EQ("scope_value", scope_attribute["value"]["stringValue"].get<std::string>());
123135

124136
auto metric = *scope_metrics["metrics"].begin();
125137
EXPECT_EQ("metrics_library_name", metric["name"].get<std::string>());

exporters/otlp/test/otlp_metrics_serialization_test.cc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@
1313
#include "opentelemetry/common/timestamp.h"
1414
#include "opentelemetry/exporters/otlp/otlp_metric_utils.h"
1515
#include "opentelemetry/exporters/otlp/otlp_preferred_temporality.h"
16+
#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h"
1617
#include "opentelemetry/sdk/metrics/data/metric_data.h"
1718
#include "opentelemetry/sdk/metrics/data/point_data.h"
1819
#include "opentelemetry/sdk/metrics/export/metric_producer.h"
1920
#include "opentelemetry/sdk/metrics/instruments.h"
21+
#include "opentelemetry/sdk/resource/resource.h"
2022
#include "opentelemetry/version.h"
2123

2224
// clang-format off
2325
#include "opentelemetry/exporters/otlp/protobuf_include_prefix.h" // IWYU pragma: keep
26+
#include "opentelemetry/proto/collector/metrics/v1/metrics_service.pb.h"
2427
#include "opentelemetry/proto/common/v1/common.pb.h"
2528
#include "opentelemetry/proto/metrics/v1/metrics.pb.h"
2629
#include "opentelemetry/exporters/otlp/protobuf_include_suffix.h" // IWYU pragma: keep
@@ -302,6 +305,41 @@ TEST(OtlpMetricSerializationTest, ObservableUpDownCounter)
302305
EXPECT_EQ(1, 1);
303306
}
304307

308+
TEST(OtlpMetricSerializationTest, PopulateExportMetricsServiceRequest)
309+
{
310+
const auto resource =
311+
resource::Resource::Create({{"service.name", "test_service_name"}}, "resource_schema_url");
312+
const auto scope = opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create(
313+
"scope_name", "scope_version", "scope_schema_url", {{"scope_key", "scope_value"}});
314+
315+
metrics_sdk::ScopeMetrics scope_metrics{scope.get(), CreateSumAggregationData()};
316+
metrics_sdk::ResourceMetrics resource_metrics{&resource, scope_metrics};
317+
318+
proto::collector::metrics::v1::ExportMetricsServiceRequest request_proto;
319+
otlp_exporter::OtlpMetricUtils::PopulateRequest(resource_metrics, &request_proto);
320+
321+
ASSERT_EQ(1, request_proto.resource_metrics_size());
322+
const auto &resource_metrics_proto = request_proto.resource_metrics(0);
323+
EXPECT_EQ("resource_schema_url", resource_metrics_proto.schema_url());
324+
325+
ASSERT_EQ(1, resource_metrics_proto.scope_metrics_size());
326+
const auto &scope_metrics_proto = resource_metrics_proto.scope_metrics(0);
327+
EXPECT_EQ("scope_schema_url", scope_metrics_proto.schema_url());
328+
329+
ASSERT_EQ(1, scope_metrics_proto.metrics_size());
330+
const auto &metric_proto = scope_metrics_proto.metrics(0);
331+
EXPECT_EQ("Counter", metric_proto.name());
332+
333+
const auto &scope_proto = scope_metrics_proto.scope();
334+
EXPECT_EQ("scope_name", scope_proto.name());
335+
EXPECT_EQ("scope_version", scope_proto.version());
336+
337+
ASSERT_EQ(1, scope_proto.attributes_size());
338+
const auto &scope_attributes_proto = scope_proto.attributes(0);
339+
EXPECT_EQ("scope_key", scope_attributes_proto.key());
340+
EXPECT_EQ("scope_value", scope_attributes_proto.value().string_value());
341+
}
342+
305343
} // namespace otlp
306344
} // namespace exporter
307345
OPENTELEMETRY_END_NAMESPACE

exporters/otlp/test/otlp_recordable_test.cc

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,28 @@ TEST(OtlpRecordable, SetInstrumentationLibraryWithSchemaURL)
119119
EXPECT_EQ(expected_schema_url, rec.GetInstrumentationLibrarySchemaURL());
120120
}
121121

122+
TEST(OtlpRecordable, SetInstrumentationScopeWithAttributes)
123+
{
124+
exporter::otlp::OtlpRecordable rec;
125+
126+
auto inst_lib = trace_sdk::InstrumentationScope::Create(
127+
"test_scope_name", "test_version", "test_schema_url", {{"test_key", "test_value"}});
128+
129+
ASSERT_EQ(inst_lib->GetAttributes().size(), 1);
130+
131+
rec.SetInstrumentationScope(*inst_lib);
132+
133+
const auto proto_instr_libr = rec.GetProtoInstrumentationScope();
134+
EXPECT_EQ("test_scope_name", proto_instr_libr.name());
135+
EXPECT_EQ("test_version", proto_instr_libr.version());
136+
137+
ASSERT_EQ(proto_instr_libr.attributes_size(), 1);
138+
const auto &proto_attributes = proto_instr_libr.attributes(0);
139+
ASSERT_TRUE(proto_attributes.value().has_string_value());
140+
EXPECT_EQ("test_key", proto_attributes.key());
141+
EXPECT_EQ("test_value", proto_attributes.value().string_value());
142+
}
143+
122144
TEST(OtlpRecordable, SetStartTime)
123145
{
124146
OtlpRecordable rec;
@@ -324,7 +346,8 @@ TEST(OtlpRecordable, PopulateRequest)
324346
auto rec1 = std::unique_ptr<sdk::trace::Recordable>(new OtlpRecordable);
325347
auto resource1 = resource::Resource::Create({{"service.name", "one"}});
326348
rec1->SetResource(resource1);
327-
auto inst_lib1 = trace_sdk::InstrumentationScope::Create("one", "1");
349+
auto inst_lib1 = trace_sdk::InstrumentationScope::Create("one", "1", "scope_schema",
350+
{{"scope_key", "scope_value"}});
328351
rec1->SetInstrumentationScope(*inst_lib1);
329352

330353
auto rec2 = std::unique_ptr<sdk::trace::Recordable>(new OtlpRecordable);
@@ -350,12 +373,23 @@ TEST(OtlpRecordable, PopulateRequest)
350373
EXPECT_EQ(req.resource_spans().size(), 2);
351374
for (const auto &resource_spans : req.resource_spans())
352375
{
353-
auto service_name = resource_spans.resource().attributes(0).value().string_value();
354-
auto scope_spans_size = resource_spans.scope_spans().size();
376+
ASSERT_GT(resource_spans.resource().attributes_size(), 0);
377+
const auto service_name = resource_spans.resource().attributes(0).value().string_value();
378+
const auto scope_spans_size = resource_spans.scope_spans().size();
355379
if (service_name == "one")
356380
{
381+
ASSERT_GT(resource_spans.scope_spans_size(), 0);
382+
const auto &scope_one = resource_spans.scope_spans(0).scope();
383+
357384
EXPECT_EQ(scope_spans_size, 1);
358-
EXPECT_EQ(resource_spans.scope_spans(0).scope().name(), "one");
385+
EXPECT_EQ(scope_one.name(), "one");
386+
EXPECT_EQ(scope_one.version(), "1");
387+
388+
ASSERT_EQ(scope_one.attributes_size(), 1);
389+
const auto &scope_attribute = scope_one.attributes(0);
390+
391+
EXPECT_EQ(scope_attribute.key(), "scope_key");
392+
EXPECT_EQ(scope_attribute.value().string_value(), "scope_value");
359393
}
360394
if (service_name == "two")
361395
{

sdk/include/opentelemetry/sdk/logs/exporter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class OPENTELEMETRY_EXPORT LogRecordExporter
5151
* Force flush the log records pushed into this log exporter.
5252
*/
5353
virtual bool ForceFlush(
54-
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept;
54+
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept = 0;
5555

5656
/**
5757
* Marks the exporter as ShutDown and cleans up any resources as required.

sdk/include/opentelemetry/sdk/logs/logger_provider.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class OPENTELEMETRY_EXPORT LoggerProvider final : public opentelemetry::logs::Lo
9393
/**
9494
* Shutdown the log processor associated with this log provider.
9595
*/
96-
bool Shutdown() noexcept;
96+
bool Shutdown(std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept;
9797

9898
/**
9999
* Force flush the log processor associated with this log provider.

sdk/include/opentelemetry/sdk/metrics/meter_context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class MeterContext : public std::enable_shared_from_this<MeterContext>
147147
/**
148148
* Shutdown the Collectors associated with this meter provider.
149149
*/
150-
bool Shutdown() noexcept;
150+
bool Shutdown(std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept;
151151

152152
private:
153153
opentelemetry::sdk::resource::Resource resource_;

sdk/include/opentelemetry/sdk/metrics/meter_provider.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class OPENTELEMETRY_EXPORT MeterProvider final : public opentelemetry::metrics::
110110
/**
111111
* Shutdown the meter provider.
112112
*/
113-
bool Shutdown() noexcept;
113+
bool Shutdown(std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept;
114114

115115
/**
116116
* Force flush the meter provider.

0 commit comments

Comments
 (0)