Skip to content

Commit

Permalink
+ Removed useless headers.
Browse files Browse the repository at this point in the history
+ Populate Instrumentation Scope attributes

Signed-off-by: owent <[email protected]>
  • Loading branch information
owent committed Feb 28, 2023
1 parent c2063ef commit 1b1bb79
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 31 deletions.
15 changes: 0 additions & 15 deletions api/include/opentelemetry/logs/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,12 @@
#pragma once
#ifdef ENABLE_LOGS_PREVIEW

# include <chrono>
# include <map>
# include <vector>

# include "opentelemetry/common/attribute_value.h"
# include "opentelemetry/common/key_value_iterable.h"
# include "opentelemetry/common/key_value_iterable_view.h"
# include "opentelemetry/common/macros.h"
# include "opentelemetry/common/timestamp.h"
# include "opentelemetry/logs/log_record.h"
# include "opentelemetry/logs/logger_type_traits.h"
# include "opentelemetry/logs/severity.h"
# include "opentelemetry/nostd/shared_ptr.h"
# include "opentelemetry/nostd/span.h"
# include "opentelemetry/nostd/string_view.h"
# include "opentelemetry/nostd/type_traits.h"
# include "opentelemetry/nostd/unique_ptr.h"
# include "opentelemetry/trace/span_context.h"
# include "opentelemetry/trace/span_id.h"
# include "opentelemetry/trace/trace_flags.h"
# include "opentelemetry/trace/trace_id.h"
# include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
Expand Down
4 changes: 0 additions & 4 deletions api/include/opentelemetry/logs/logger_type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
#ifdef ENABLE_LOGS_PREVIEW

# include <chrono>
# include <map>
# include <type_traits>
# include <vector>

# include "opentelemetry/common/attribute_value.h"
# include "opentelemetry/common/key_value_iterable.h"
# include "opentelemetry/common/key_value_iterable_view.h"
# include "opentelemetry/common/macros.h"
# include "opentelemetry/common/timestamp.h"
# include "opentelemetry/logs/log_record.h"
# include "opentelemetry/logs/severity.h"
Expand Down
12 changes: 9 additions & 3 deletions exporters/otlp/src/otlp_metric_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ void OtlpMetricUtils::ConvertHistogramMetric(
// sum
if ((nostd::holds_alternative<int64_t>(histogram_data.sum_)))
{
proto_histogram_point_data->set_sum(nostd::get<int64_t>(histogram_data.sum_));
// Use static_cast to avoid C4244 in MSVC
proto_histogram_point_data->set_sum(
static_cast<double>(nostd::get<int64_t>(histogram_data.sum_)));
}
else
{
Expand All @@ -108,15 +110,19 @@ void OtlpMetricUtils::ConvertHistogramMetric(
{
if (nostd::holds_alternative<int64_t>(histogram_data.min_))
{
proto_histogram_point_data->set_min(nostd::get<int64_t>(histogram_data.min_));
// Use static_cast to avoid C4244 in MSVC
proto_histogram_point_data->set_min(
static_cast<double>(nostd::get<int64_t>(histogram_data.min_)));
}
else
{
proto_histogram_point_data->set_min(nostd::get<double>(histogram_data.min_));
}
if (nostd::holds_alternative<int64_t>(histogram_data.max_))
{
proto_histogram_point_data->set_max(nostd::get<int64_t>(histogram_data.max_));
// Use static_cast to avoid C4244 in MSVC
proto_histogram_point_data->set_max(
static_cast<double>(nostd::get<int64_t>(histogram_data.max_)));
}
else
{
Expand Down
14 changes: 12 additions & 2 deletions exporters/otlp/src/otlp_recordable_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,18 @@ void OtlpRecordableUtils::PopulateRequest(

if (!output_scope_log->has_scope())
{
output_scope_log->mutable_scope()->set_name(input_scope_log.first->GetName());
output_scope_log->mutable_scope()->set_version(input_scope_log.first->GetVersion());
auto proto_scope = output_scope_log->mutable_scope();
if (proto_scope != nullptr)
{
proto_scope->set_name(input_scope_log.first->GetName());
proto_scope->set_version(input_scope_log.first->GetVersion());

for (auto &scope_attribute : input_scope_log.first->GetAttributes())
{
OtlpPopulateAttributeUtils::PopulateAttribute(
proto_scope->add_attributes(), scope_attribute.first, scope_attribute.second);
}
}
output_scope_log->set_schema_url(input_scope_log.first->GetSchemaURL());
}

Expand Down
3 changes: 2 additions & 1 deletion exporters/otlp/test/otlp_grpc_log_record_exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ TEST_F(OtlpGrpcLogRecordExporterTestPeer, ExportIntegrationTest)
opentelemetry::trace::SpanId span_id{span_id_bin};

const std::string schema_url{"https://opentelemetry.io/schemas/1.11.0"};
auto logger = provider->GetLogger("test", "opentelelemtry_library", "", schema_url);
auto logger = provider->GetLogger("test", "opentelelemtry_library", "", schema_url, true,
{{"scope_key1", "scope_value"}, {"scope_key2", 2}});
std::unordered_map<std::string, opentelemetry::v1::common::AttributeValue> attributes;
attributes["service.name"] = "unit_test_service";
attributes["tenant.id"] = "test_user";
Expand Down
75 changes: 69 additions & 6 deletions exporters/otlp/test/otlp_http_log_record_exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ 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", "", schema_url, true,
{{"scope_key1", "scope_value"}, {"scope_key2", 2}});

trace_id.ToLowerBase16(MakeSpan(trace_id_hex));
report_trace_id.assign(trace_id_hex, sizeof(trace_id_hex));
Expand All @@ -147,6 +148,7 @@ 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 scope = scope_logs["scope"];
auto log = *scope_logs["log_records"].begin();
auto received_trace_id = log["trace_id"].get<std::string>();
auto received_span_id = log["span_id"].get<std::string>();
Expand All @@ -161,6 +163,22 @@ class OtlpHttpLogRecordExporterTestPeer : public ::testing::Test
EXPECT_EQ("Custom-Header-Value", custom_header->second);
}

bool check_scope_attribute = false;
auto scope_attributes = scope["attributes"];
for (auto &attribute : scope_attributes)
{
if (!attribute.is_object())
{
continue;
}
if ("scope_key1" == attribute["key"])
{
check_scope_attribute = true;
EXPECT_EQ("scope_value", attribute["value"]["string_value"].get<std::string>());
}
}
ASSERT_TRUE(check_scope_attribute);

http_client::nosend::Response response;
response.Finish(*callback.get());
});
Expand Down Expand Up @@ -228,7 +246,8 @@ 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", "1.2.0", schema_url);
auto logger = provider->GetLogger("test", "opentelelemtry_library", "1.2.0", schema_url, true,
{{"scope_key1", "scope_value"}, {"scope_key2", 2}});

trace_id.ToLowerBase16(MakeSpan(trace_id_hex));
report_trace_id.assign(trace_id_hex, sizeof(trace_id_hex));
Expand Down Expand Up @@ -267,6 +286,22 @@ class OtlpHttpLogRecordExporterTestPeer : public ::testing::Test
EXPECT_EQ("Custom-Header-Value", custom_header->second);
}

bool check_scope_attribute = false;
auto scope_attributes = scope["attributes"];
for (auto &attribute : scope_attributes)
{
if (!attribute.is_object())
{
continue;
}
if ("scope_key1" == attribute["key"])
{
check_scope_attribute = true;
EXPECT_EQ("scope_value", attribute["value"]["string_value"].get<std::string>());
}
}
ASSERT_TRUE(check_scope_attribute);

// let the otlp_http_client to continue
std::thread async_finish{[callback]() {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
Expand Down Expand Up @@ -336,7 +371,8 @@ 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", "1.2.0", schema_url);
auto logger = provider->GetLogger("test", "opentelelemtry_library", "1.2.0", schema_url, true,
{{"scope_key1", "scope_value"}, {"scope_key2", 2}});

report_trace_id.assign(reinterpret_cast<const char *>(trace_id_bin), sizeof(trace_id_bin));
report_span_id.assign(reinterpret_cast<const char *>(span_id_bin), sizeof(span_id_bin));
Expand Down Expand Up @@ -370,6 +406,17 @@ class OtlpHttpLogRecordExporterTestPeer : public ::testing::Test
}
ASSERT_TRUE(check_service_name);

bool check_scope_attribute = false;
for (auto &attribute : scope_log.scope().attributes())
{
if ("scope_key1" == attribute.key())
{
check_scope_attribute = true;
EXPECT_EQ("scope_value", attribute.value().string_value());
}
}
ASSERT_TRUE(check_scope_attribute);

// let the otlp_http_client to continue

http_client::nosend::Response response;
Expand Down Expand Up @@ -437,7 +484,8 @@ 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", "", schema_url, true,
{{"scope_key1", "scope_value"}, {"scope_key2", 2}});

report_trace_id.assign(reinterpret_cast<const char *>(trace_id_bin), sizeof(trace_id_bin));
report_span_id.assign(reinterpret_cast<const char *>(span_id_bin), sizeof(span_id_bin));
Expand All @@ -446,12 +494,13 @@ class OtlpHttpLogRecordExporterTestPeer : public ::testing::Test
auto mock_session =
std::static_pointer_cast<http_client::nosend::Session>(no_send_client->session_);
EXPECT_CALL(*mock_session, SendRequest)
.WillOnce([&mock_session, report_trace_id, report_span_id](
.WillOnce([&mock_session, report_trace_id, report_span_id, schema_url](
std::shared_ptr<opentelemetry::ext::http::client::EventHandler> callback) {
opentelemetry::proto::collector::logs::v1::ExportLogsServiceRequest request_body;
request_body.ParseFromArray(&mock_session->GetRequest()->body_[0],
static_cast<int>(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);
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());
Expand All @@ -467,6 +516,20 @@ class OtlpHttpLogRecordExporterTestPeer : public ::testing::Test
}
ASSERT_TRUE(check_service_name);

auto &scope = scope_log.scope();
EXPECT_EQ(scope.name(), "opentelelemtry_library");
EXPECT_EQ(scope_log.schema_url(), schema_url);
bool check_scope_attribute = false;
for (auto &attribute : scope.attributes())
{
if ("scope_key1" == attribute.key())
{
check_scope_attribute = true;
EXPECT_EQ("scope_value", attribute.value().string_value());
}
}
ASSERT_TRUE(check_scope_attribute);

// let the otlp_http_client to continue

std::thread async_finish{[callback]() {
Expand Down
2 changes: 2 additions & 0 deletions sdk/include/opentelemetry/sdk/logs/logger_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class LoggerProvider final : public opentelemetry::logs::LoggerProvider

~LoggerProvider() override;

using opentelemetry::logs::LoggerProvider::GetLogger;

/**
* Creates a logger with the given name, and returns a shared pointer to it.
* If a logger with that name already exists, return a shared pointer to it
Expand Down

0 comments on commit 1b1bb79

Please sign in to comment.