From 94a6783025bfc0723dc1dfb3eb5839727e815abe Mon Sep 17 00:00:00 2001 From: WenTao Ou Date: Fri, 23 Dec 2022 16:31:44 +0800 Subject: [PATCH] Fix resource and scope of new Logs SDK implementation. (#1881) --- .../test/otlp_http_log_record_exporter_test.cc | 17 ++++++++++++++--- sdk/src/logs/logger.cc | 7 ++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/exporters/otlp/test/otlp_http_log_record_exporter_test.cc b/exporters/otlp/test/otlp_http_log_record_exporter_test.cc index 08bc4b1fca..c3fc69231a 100644 --- a/exporters/otlp/test/otlp_http_log_record_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_log_record_exporter_test.cc @@ -226,7 +226,7 @@ class OtlpHttpLogRecordExporterTestPeer : public ::testing::Test opentelemetry::trace::SpanId span_id{span_id_bin}; const std::string schema_url{"https://opentelemetry.io/schemas/1.2.0"}; - auto logger = provider->GetLogger("test", "", "opentelelemtry_library", "", schema_url); + auto logger = provider->GetLogger("test", "", "opentelelemtry_library", "1.2.0", schema_url); trace_id.ToLowerBase16(MakeSpan(trace_id_hex)); report_trace_id.assign(trace_id_hex, sizeof(trace_id_hex)); @@ -244,9 +244,16 @@ class OtlpHttpLogRecordExporterTestPeer : public ::testing::Test nlohmann::json::parse(mock_session->GetRequest()->body_, nullptr, false); auto resource_logs = *check_json["resource_logs"].begin(); auto scope_logs = *resource_logs["scope_logs"].begin(); + auto schema_url = scope_logs["schema_url"].get(); + auto scope = scope_logs["scope"]; + auto scope_name = scope["name"]; + auto scope_version = scope["version"]; auto log = *scope_logs["log_records"].begin(); auto received_trace_id = log["trace_id"].get(); auto received_span_id = log["span_id"].get(); + EXPECT_EQ(schema_url, "https://opentelemetry.io/schemas/1.2.0"); + EXPECT_EQ(scope_name, "opentelelemtry_library"); + EXPECT_EQ(scope_version, "1.2.0"); EXPECT_EQ(received_trace_id, report_trace_id); EXPECT_EQ(received_span_id, report_span_id); EXPECT_EQ("Log message", log["body"]["string_value"].get()); @@ -325,7 +332,7 @@ class OtlpHttpLogRecordExporterTestPeer : public ::testing::Test opentelemetry::trace::SpanId span_id{span_id_bin}; const std::string schema_url{"https://opentelemetry.io/schemas/1.2.0"}; - auto logger = provider->GetLogger("test", "", "opentelelemtry_library", "", schema_url); + auto logger = provider->GetLogger("test", "", "opentelelemtry_library", "1.2.0", schema_url); report_trace_id.assign(reinterpret_cast(trace_id_bin), sizeof(trace_id_bin)); report_span_id.assign(reinterpret_cast(span_id_bin), sizeof(span_id_bin)); @@ -339,7 +346,11 @@ class OtlpHttpLogRecordExporterTestPeer : public ::testing::Test opentelemetry::proto::collector::logs::v1::ExportLogsServiceRequest request_body; request_body.ParseFromArray(&mock_session->GetRequest()->body_[0], static_cast(mock_session->GetRequest()->body_.size())); - auto received_log = request_body.resource_logs(0).scope_logs(0).log_records(0); + auto scope_log = request_body.resource_logs(0).scope_logs(0); + EXPECT_EQ(scope_log.schema_url(), "https://opentelemetry.io/schemas/1.2.0"); + EXPECT_EQ(scope_log.scope().name(), "opentelelemtry_library"); + EXPECT_EQ(scope_log.scope().version(), "1.2.0"); + auto received_log = scope_log.log_records(0); EXPECT_EQ(received_log.trace_id(), report_trace_id); EXPECT_EQ(received_log.span_id(), report_span_id); EXPECT_EQ("Log message", received_log.body().string_value()); diff --git a/sdk/src/logs/logger.cc b/sdk/src/logs/logger.cc index a87e4af8a2..a955d7a1ae 100644 --- a/sdk/src/logs/logger.cc +++ b/sdk/src/logs/logger.cc @@ -48,12 +48,17 @@ void Logger::EmitLogRecord(nostd::unique_ptr &&l return; } + std::unique_ptr recordable = + std::unique_ptr(static_cast(log_record.release())); + recordable->SetResource(context_->GetResource()); + recordable->SetInstrumentationScope(GetInstrumentationScope()); + auto &processor = context_->GetProcessor(); // TODO: Sampler (should include check for minSeverity) // Send the log recordable to the processor - processor.OnEmit(std::unique_ptr(static_cast(log_record.release()))); + processor.OnEmit(std::move(recordable)); } const opentelemetry::sdk::instrumentationscope::InstrumentationScope &