Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions exporters/otlp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,20 @@ cc_library(
],
)

cc_test(
name = "otlp_populate_attribute_utils_test",
srcs = ["test/otlp_populate_attribute_utils_test.cc"],
tags = [
"otlp",
"test",
],
deps = [
":otlp_recordable",
"//sdk/src/metrics",
"@com_google_googletest//:gtest_main",
],
)

cc_test(
name = "otlp_recordable_test",
srcs = ["test/otlp_recordable_test.cc"],
Expand Down
15 changes: 15 additions & 0 deletions exporters/otlp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,21 @@ if(WITH_OTLP_FILE)
endif()

if(BUILD_TESTING)
add_executable(otlp_populate_attribute_utils_test
test/otlp_populate_attribute_utils_test.cc)
target_link_libraries(
otlp_populate_attribute_utils_test ${GTEST_BOTH_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT} opentelemetry_otlp_recordable
protobuf::libprotobuf)
gtest_add_tests(
TARGET otlp_populate_attribute_utils_test
TEST_PREFIX exporter.otlp.
TEST_LIST otlp_populate_attribute_utils_test)
if(WITH_OTLP_UTF8_VALIDITY AND TARGET utf8_range::utf8_validity)
target_compile_definitions(otlp_populate_attribute_utils_test
PRIVATE ENABLE_OTLP_UTF8_VALIDITY)
endif()

add_executable(otlp_recordable_test test/otlp_recordable_test.cc)
target_link_libraries(
otlp_recordable_test ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#pragma once

#include <stdint.h>
#include <unordered_map>

#include "opentelemetry/common/attribute_value.h"
#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h"
Expand All @@ -13,6 +14,7 @@

// clang-format off
#include "opentelemetry/exporters/otlp/protobuf_include_prefix.h" // IWYU pragma: keep
#include "opentelemetry/proto/common/v1/common.pb.h"
#include "opentelemetry/proto/logs/v1/logs.pb.h"
#include "opentelemetry/exporters/otlp/protobuf_include_suffix.h" // IWYU pragma: keep
// clang-format on
Expand Down Expand Up @@ -111,6 +113,7 @@ class OtlpLogRecordable final : public opentelemetry::sdk::logs::Recordable

private:
proto::logs::v1::LogRecord proto_record_;
std::unordered_map<std::string, proto::common::v1::AnyValue *> attributes_map_;
const opentelemetry::sdk::resource::Resource *resource_ = nullptr;
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *instrumentation_scope_ =
nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,20 @@ class OtlpPopulateAttributeUtils
&instrumentation_scope) noexcept;

static void PopulateAnyValue(opentelemetry::proto::common::v1::AnyValue *proto_value,
const opentelemetry::common::AttributeValue &value,
bool allow_bytes) noexcept;
const opentelemetry::common::AttributeValue &value) noexcept;

static void PopulateAnyValue(opentelemetry::proto::common::v1::AnyValue *proto_value,
const opentelemetry::sdk::common::OwnedAttributeValue &value,
bool allow_bytes) noexcept;
static void PopulateAnyValue(
opentelemetry::proto::common::v1::AnyValue *proto_value,
const opentelemetry::sdk::common::OwnedAttributeValue &value) noexcept;

static void PopulateAttribute(opentelemetry::proto::common::v1::KeyValue *attribute,
nostd::string_view key,
const opentelemetry::common::AttributeValue &value,
bool allow_bytes) noexcept;
const opentelemetry::common::AttributeValue &value) noexcept;

static void PopulateAttribute(opentelemetry::proto::common::v1::KeyValue *attribute,
nostd::string_view key,
const opentelemetry::sdk::common::OwnedAttributeValue &value,
bool allow_bytes) noexcept;
static void PopulateAttribute(
opentelemetry::proto::common::v1::KeyValue *attribute,
nostd::string_view key,
const opentelemetry::sdk::common::OwnedAttributeValue &value) noexcept;
};

} // namespace otlp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <cstdint> // For std::uint32_t
#include <limits> // For std::numeric_limits
#include <string>
#include <unordered_map>

// clang-format off
#include "opentelemetry/exporters/otlp/protobuf_include_prefix.h" // IWYU pragma: keep
Expand Down Expand Up @@ -97,6 +98,7 @@ class OtlpRecordable final : public opentelemetry::sdk::trace::Recordable

private:
proto::trace::v1::Span span_;
std::unordered_map<std::string, proto::common::v1::AnyValue *> attributes_map_;
const opentelemetry::sdk::resource::Resource *resource_ = nullptr;
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *instrumentation_scope_ =
nullptr;
Expand Down
27 changes: 24 additions & 3 deletions exporters/otlp/src/otlp_log_recordable.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include "opentelemetry/exporters/otlp/otlp_log_recordable.h"
#include <cstdint>
#include <string>

#include "opentelemetry/common/attribute_value.h"
#include "opentelemetry/common/timestamp.h"
#include "opentelemetry/exporters/otlp/otlp_log_recordable.h"
#include "opentelemetry/exporters/otlp/otlp_populate_attribute_utils.h"
#include "opentelemetry/logs/severity.h"
#include "opentelemetry/nostd/span.h"
Expand All @@ -18,6 +21,8 @@

// clang-format off
#include "opentelemetry/exporters/otlp/protobuf_include_prefix.h" // IWYU pragma: keep
#include <google/protobuf/repeated_ptr_field.h>
#include "opentelemetry/proto/common/v1/common.pb.h"
#include "opentelemetry/proto/logs/v1/logs.pb.h"
#include "opentelemetry/exporters/otlp/protobuf_include_suffix.h" // IWYU pragma: keep
// clang-format on
Expand Down Expand Up @@ -194,7 +199,7 @@ void OtlpLogRecordable::SetSeverity(opentelemetry::logs::Severity severity) noex

void OtlpLogRecordable::SetBody(const opentelemetry::common::AttributeValue &message) noexcept
{
OtlpPopulateAttributeUtils::PopulateAnyValue(proto_record_.mutable_body(), message, true);
OtlpPopulateAttributeUtils::PopulateAnyValue(proto_record_.mutable_body(), message);
}

void OtlpLogRecordable::SetEventId(int64_t /* id */, nostd::string_view event_name) noexcept
Expand Down Expand Up @@ -236,7 +241,23 @@ void OtlpLogRecordable::SetTraceFlags(const opentelemetry::trace::TraceFlags &tr
void OtlpLogRecordable::SetAttribute(opentelemetry::nostd::string_view key,
const opentelemetry::common::AttributeValue &value) noexcept
{
OtlpPopulateAttributeUtils::PopulateAttribute(proto_record_.add_attributes(), key, value, true);
if (key.empty())
{
return;
}
const std::string key_str(key);
auto it = attributes_map_.find(key_str);
if (it != attributes_map_.end())
{
// Update an existing attribute value
OtlpPopulateAttributeUtils::PopulateAnyValue(it->second, value);
return;
}
auto *kv = proto_record_.add_attributes();
kv->set_key(key_str);
auto *any = kv->mutable_value();
OtlpPopulateAttributeUtils::PopulateAnyValue(any, value);
attributes_map_.emplace(std::move(key_str), any);
}

void OtlpLogRecordable::SetResource(const opentelemetry::sdk::resource::Resource &resource) noexcept
Expand Down
8 changes: 4 additions & 4 deletions exporters/otlp/src/otlp_metric_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void OtlpMetricUtils::ConvertSumMetric(const metric_sdk::MetricData &metric_data
for (auto &kv_attr : point_data_with_attributes.attributes)
{
OtlpPopulateAttributeUtils::PopulateAttribute(proto_sum_point_data->add_attributes(),
kv_attr.first, kv_attr.second, false);
kv_attr.first, kv_attr.second);
}
}
}
Expand Down Expand Up @@ -178,7 +178,7 @@ void OtlpMetricUtils::ConvertHistogramMetric(
for (auto &kv_attr : point_data_with_attributes.attributes)
{
OtlpPopulateAttributeUtils::PopulateAttribute(proto_histogram_point_data->add_attributes(),
kv_attr.first, kv_attr.second, false);
kv_attr.first, kv_attr.second);
}
}
}
Expand Down Expand Up @@ -242,7 +242,7 @@ void OtlpMetricUtils::ConvertExponentialHistogramMetric(
for (auto &kv_attr : point_data_with_attributes.attributes)
{
OtlpPopulateAttributeUtils::PopulateAttribute(proto_histogram_point_data->add_attributes(),
kv_attr.first, kv_attr.second, false);
kv_attr.first, kv_attr.second);
}
}
}
Expand Down Expand Up @@ -272,7 +272,7 @@ void OtlpMetricUtils::ConvertGaugeMetric(const opentelemetry::sdk::metrics::Metr
for (auto &kv_attr : point_data_with_attributes.attributes)
{
OtlpPopulateAttributeUtils::PopulateAttribute(proto_gauge_point_data->add_attributes(),
kv_attr.first, kv_attr.second, false);
kv_attr.first, kv_attr.second);
}
}
}
Expand Down
Loading
Loading