Skip to content

Commit 93240f3

Browse files
authored
Merge pull request #108 from open-telemetry/main
merged from upstream
2 parents d4c5800 + ac4bba7 commit 93240f3

File tree

7 files changed

+71
-176
lines changed

7 files changed

+71
-176
lines changed

exporters/elasticsearch/src/es_log_record_exporter.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class ResponseHandler : public http_client::EventHandler
6363
{
6464
log_message = BuildResponseLogMessage(response, body_);
6565

66-
OTEL_INTERNAL_LOG_ERROR("ES Log Exporter] Export failed, " << log_message);
66+
OTEL_INTERNAL_LOG_ERROR("[ES Log Exporter] Export failed, " << log_message);
6767
}
6868

6969
if (console_debug_)

exporters/elasticsearch/src/es_log_recordable.cc

Lines changed: 39 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
#include <chrono>
5+
#include <ctime>
6+
#include <nlohmann/json.hpp>
7+
#include <string>
8+
49
#include "opentelemetry/exporters/elasticsearch/es_log_recordable.h"
510
#include "opentelemetry/logs/severity.h"
11+
#include "opentelemetry/nostd/variant.h"
612
#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h"
713
#include "opentelemetry/sdk/resource/resource.h"
814
#include "opentelemetry/trace/span_id.h"
915
#include "opentelemetry/trace/trace_flags.h"
1016
#include "opentelemetry/trace/trace_id.h"
1117

18+
namespace nlohmann
19+
{
20+
template <>
21+
struct adl_serializer<opentelemetry::sdk::common::OwnedAttributeValue>
22+
{
23+
static void to_json(json &j, const opentelemetry::sdk::common::OwnedAttributeValue &v)
24+
{
25+
opentelemetry::nostd::visit([&j](const auto &value) { j = value; }, v);
26+
}
27+
};
28+
29+
template <>
30+
struct adl_serializer<opentelemetry::common::AttributeValue>
31+
{
32+
static void to_json(json &j, const opentelemetry::common::AttributeValue &v)
33+
{
34+
opentelemetry::nostd::visit([&j](const auto &value) { j = value; }, v);
35+
}
36+
};
37+
} // namespace nlohmann
38+
1239
OPENTELEMETRY_BEGIN_NAMESPACE
1340
namespace exporter
1441
{
@@ -18,146 +45,13 @@ void ElasticSearchRecordable::WriteValue(
1845
const opentelemetry::sdk::common::OwnedAttributeValue &value,
1946
const std::string &name)
2047
{
21-
namespace common = opentelemetry::sdk::common;
22-
switch (value.index())
23-
{
24-
case common::kTypeBool:
25-
json_[name] = opentelemetry::nostd::get<bool>(value) ? true : false;
26-
return;
27-
case common::kTypeInt:
28-
json_[name] = opentelemetry::nostd::get<int>(value);
29-
return;
30-
case common::kTypeInt64:
31-
json_[name] = opentelemetry::nostd::get<int64_t>(value);
32-
return;
33-
case common::kTypeUInt:
34-
json_[name] = opentelemetry::nostd::get<unsigned int>(value);
35-
return;
36-
case common::kTypeUInt64:
37-
json_[name] = opentelemetry::nostd::get<uint64_t>(value);
38-
return;
39-
case common::kTypeDouble:
40-
json_[name] = opentelemetry::nostd::get<double>(value);
41-
return;
42-
case common::kTypeString:
43-
json_[name] = opentelemetry::nostd::get<std::string>(value).data();
44-
return;
45-
default:
46-
return;
47-
}
48+
json_[name] = value;
4849
}
4950

5051
void ElasticSearchRecordable::WriteValue(const opentelemetry::common::AttributeValue &value,
5152
const std::string &name)
5253
{
53-
54-
// Assert size of variant to ensure that this method gets updated if the variant
55-
// definition changes
56-
57-
if (nostd::holds_alternative<bool>(value))
58-
{
59-
json_[name] = opentelemetry::nostd::get<bool>(value) ? true : false;
60-
}
61-
else if (nostd::holds_alternative<int>(value))
62-
{
63-
json_[name] = opentelemetry::nostd::get<int>(value);
64-
}
65-
else if (nostd::holds_alternative<int64_t>(value))
66-
{
67-
json_[name] = opentelemetry::nostd::get<int64_t>(value);
68-
}
69-
else if (nostd::holds_alternative<unsigned int>(value))
70-
{
71-
json_[name] = opentelemetry::nostd::get<unsigned int>(value);
72-
}
73-
else if (nostd::holds_alternative<uint64_t>(value))
74-
{
75-
json_[name] = opentelemetry::nostd::get<uint64_t>(value);
76-
}
77-
else if (nostd::holds_alternative<double>(value))
78-
{
79-
json_[name] = opentelemetry::nostd::get<double>(value);
80-
}
81-
else if (nostd::holds_alternative<const char *>(value))
82-
{
83-
json_[name] = std::string(nostd::get<const char *>(value));
84-
}
85-
else if (nostd::holds_alternative<nostd::string_view>(value))
86-
{
87-
json_[name] = static_cast<std::string>(opentelemetry::nostd::get<nostd::string_view>(value));
88-
}
89-
else if (nostd::holds_alternative<nostd::span<const uint8_t>>(value))
90-
{
91-
nlohmann::json array_value = nlohmann::json::array();
92-
for (const auto &val : nostd::get<nostd::span<const uint8_t>>(value))
93-
{
94-
array_value.push_back(val);
95-
}
96-
json_[name] = array_value;
97-
}
98-
else if (nostd::holds_alternative<nostd::span<const bool>>(value))
99-
{
100-
nlohmann::json array_value = nlohmann::json::array();
101-
for (const auto &val : nostd::get<nostd::span<const bool>>(value))
102-
{
103-
array_value.push_back(val);
104-
}
105-
json_[name] = array_value;
106-
}
107-
else if (nostd::holds_alternative<nostd::span<const int>>(value))
108-
{
109-
nlohmann::json array_value = nlohmann::json::array();
110-
for (const auto &val : nostd::get<nostd::span<const int>>(value))
111-
{
112-
array_value.push_back(val);
113-
}
114-
json_[name] = array_value;
115-
}
116-
else if (nostd::holds_alternative<nostd::span<const int64_t>>(value))
117-
{
118-
nlohmann::json array_value = nlohmann::json::array();
119-
for (const auto &val : nostd::get<nostd::span<const int64_t>>(value))
120-
{
121-
array_value.push_back(val);
122-
}
123-
json_[name] = array_value;
124-
}
125-
else if (nostd::holds_alternative<nostd::span<const unsigned int>>(value))
126-
{
127-
nlohmann::json array_value = nlohmann::json::array();
128-
for (const auto &val : nostd::get<nostd::span<const unsigned int>>(value))
129-
{
130-
array_value.push_back(val);
131-
}
132-
json_[name] = array_value;
133-
}
134-
else if (nostd::holds_alternative<nostd::span<const uint64_t>>(value))
135-
{
136-
nlohmann::json array_value = nlohmann::json::array();
137-
for (const auto &val : nostd::get<nostd::span<const uint64_t>>(value))
138-
{
139-
array_value.push_back(val);
140-
}
141-
json_[name] = array_value;
142-
}
143-
else if (nostd::holds_alternative<nostd::span<const double>>(value))
144-
{
145-
nlohmann::json array_value = nlohmann::json::array();
146-
for (const auto &val : nostd::get<nostd::span<const double>>(value))
147-
{
148-
array_value.push_back(val);
149-
}
150-
json_[name] = array_value;
151-
}
152-
else if (nostd::holds_alternative<nostd::span<const nostd::string_view>>(value))
153-
{
154-
nlohmann::json array_value = nlohmann::json::array();
155-
for (const auto &val : nostd::get<nostd::span<const nostd::string_view>>(value))
156-
{
157-
array_value.push_back(static_cast<std::string>(val));
158-
}
159-
json_[name] = array_value;
160-
}
54+
json_[name] = value;
16155
}
16256

16357
ElasticSearchRecordable::ElasticSearchRecordable() noexcept : sdk::logs::Recordable()
@@ -180,27 +74,19 @@ void ElasticSearchRecordable::SetTimestamp(
18074
#if __cplusplus >= 202002L
18175
const std::string dateStr = std::format("{:%FT%T%Ez}", timePoint);
18276
#else
183-
const static int dateToSecondsSize = 19;
184-
const static int millisecondsSize = 8;
185-
const static int timeZoneSize = 1;
186-
const static int dateSize = dateToSecondsSize + millisecondsSize + timeZoneSize;
187-
18877
std::time_t time = std::chrono::system_clock::to_time_t(timePoint);
18978
std::tm tm = *std::gmtime(&time);
190-
191-
char bufferDate[dateSize]; // example: 2024-10-18T07:26:00.123456Z
192-
std::strftime(bufferDate, sizeof(bufferDate), "%Y-%m-%dT%H:%M:%S", &tm);
19379
auto microseconds =
19480
std::chrono::duration_cast<std::chrono::microseconds>(timePoint.time_since_epoch()) %
19581
std::chrono::seconds(1);
19682

197-
char bufferMilliseconds[millisecondsSize];
198-
std::snprintf(bufferMilliseconds, sizeof(bufferMilliseconds), ".%06ld",
83+
// `sizeof()` includes the null terminator
84+
constexpr auto dateSize = sizeof("YYYY-MM-DDTHH:MM:SS.uuuuuuZ");
85+
char bufferDate[dateSize];
86+
auto offset = std::strftime(bufferDate, sizeof(bufferDate), "%Y-%m-%dT%H:%M:%S", &tm);
87+
std::snprintf(bufferDate + offset, sizeof(bufferDate) - offset, ".%06ldZ",
19988
static_cast<long>(microseconds.count()));
20089

201-
std::strcat(bufferDate, bufferMilliseconds);
202-
std::strcat(bufferDate, "Z");
203-
20490
const std::string dateStr(bufferDate);
20591
#endif
20692

@@ -221,9 +107,8 @@ void ElasticSearchRecordable::SetSeverity(opentelemetry::logs::Severity severity
221107
std::uint32_t severity_index = static_cast<std::uint32_t>(severity);
222108
if (severity_index >= std::extent<decltype(opentelemetry::logs::SeverityNumToText)>::value)
223109
{
224-
std::stringstream sout;
225-
sout << "Invalid severity(" << severity_index << ")";
226-
severityField = sout.str();
110+
severityField =
111+
std::string("Invalid severity(").append(std::to_string(severity_index)).append(")");
227112
}
228113
else
229114
{
@@ -240,7 +125,7 @@ void ElasticSearchRecordable::SetTraceId(const opentelemetry::trace::TraceId &tr
240125
{
241126
if (trace_id.IsValid())
242127
{
243-
char trace_buf[32];
128+
char trace_buf[opentelemetry::trace::TraceId::kSize * 2];
244129
trace_id.ToLowerBase16(trace_buf);
245130
json_["traceid"] = std::string(trace_buf, sizeof(trace_buf));
246131
}
@@ -254,7 +139,7 @@ void ElasticSearchRecordable::SetSpanId(const opentelemetry::trace::SpanId &span
254139
{
255140
if (span_id.IsValid())
256141
{
257-
char span_buf[16];
142+
char span_buf[opentelemetry::trace::SpanId::kSize * 2];
258143
span_id.ToLowerBase16(span_buf);
259144
json_["spanid"] = std::string(span_buf, sizeof(span_buf));
260145
}
@@ -282,7 +167,7 @@ void ElasticSearchRecordable::SetAttribute(
282167
void ElasticSearchRecordable::SetResource(
283168
const opentelemetry::sdk::resource::Resource &resource) noexcept
284169
{
285-
for (auto &attribute : resource.GetAttributes())
170+
for (const auto &attribute : resource.GetAttributes())
286171
{
287172
WriteValue(attribute.second, attribute.first);
288173
}

opentracing-shim/include/opentelemetry/opentracingshim/propagation.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class CarrierWriterShim : public opentelemetry::context::propagation::TextMapCar
2929

3030
virtual void Set(nostd::string_view key, nostd::string_view value) noexcept override
3131
{
32-
writer_.Set(key.data(), value.data());
32+
writer_.Set(opentracing::string_view{key.data(), key.size()},
33+
opentracing::string_view{value.data(), value.size()});
3334
}
3435

3536
private:
@@ -46,17 +47,17 @@ class CarrierReaderShim : public opentelemetry::context::propagation::TextMapCar
4647
nostd::string_view value;
4748

4849
// First try carrier.LookupKey since that can potentially be the fastest approach.
49-
if (auto result = reader_.LookupKey(key.data()))
50+
if (auto result = reader_.LookupKey(opentracing::string_view{key.data(), key.size()}))
5051
{
51-
value = result.value().data();
52+
value = nostd::string_view{result.value().data(), result.value().size()};
5253
}
5354
else // Fall back to iterating through all of the keys.
5455
{
5556
reader_.ForeachKey([key, &value](opentracing::string_view k,
5657
opentracing::string_view v) -> opentracing::expected<void> {
57-
if (k == key.data())
58+
if (key == nostd::string_view{k.data(), k.size()})
5859
{
59-
value = v.data();
60+
value = nostd::string_view{v.data(), v.size()};
6061
// Found key, so bail out of the loop with a success error code.
6162
return opentracing::make_unexpected(std::error_code{});
6263
}
@@ -77,8 +78,9 @@ class CarrierReaderShim : public opentelemetry::context::propagation::TextMapCar
7778
return reader_
7879
.ForeachKey([&callback](opentracing::string_view key,
7980
opentracing::string_view) -> opentracing::expected<void> {
80-
return callback(key.data()) ? opentracing::make_expected()
81-
: opentracing::make_unexpected(std::error_code{});
81+
return callback(nostd::string_view{key.data(), key.size()})
82+
? opentracing::make_expected()
83+
: opentracing::make_unexpected(std::error_code{});
8284
})
8385
.has_value();
8486
}

opentracing-shim/include/opentelemetry/opentracingshim/shim_utils.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ static inline opentelemetry::common::AttributeValue attributeFromValue(
2929
AttributeValue operator()(int64_t v) { return v; }
3030
AttributeValue operator()(uint64_t v) { return v; }
3131
AttributeValue operator()(const std::string &v) { return nostd::string_view{v}; }
32-
AttributeValue operator()(opentracing::string_view v) { return nostd::string_view{v.data()}; }
32+
AttributeValue operator()(opentracing::string_view v)
33+
{
34+
return nostd::string_view{v.data(), v.size()};
35+
}
3336
AttributeValue operator()(std::nullptr_t) { return nostd::string_view{}; }
3437
AttributeValue operator()(const char *v) { return v; }
3538
AttributeValue operator()(opentracing::util::recursive_wrapper<opentracing::Values>)
@@ -54,7 +57,7 @@ static inline std::string stringFromValue(const opentracing::Value &value)
5457
std::string operator()(int64_t v) { return std::to_string(v); }
5558
std::string operator()(uint64_t v) { return std::to_string(v); }
5659
std::string operator()(const std::string &v) { return v; }
57-
std::string operator()(opentracing::string_view v) { return std::string{v.data()}; }
60+
std::string operator()(opentracing::string_view v) { return std::string{v.data(), v.size()}; }
5861
std::string operator()(std::nullptr_t) { return std::string{}; }
5962
std::string operator()(const char *v) { return std::string{v}; }
6063
std::string operator()(opentracing::util::recursive_wrapper<opentracing::Values>)

opentracing-shim/src/span_context_shim.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ bool SpanContextShim::BaggageItem(nostd::string_view key, std::string &value) co
2323
void SpanContextShim::ForeachBaggageItem(VisitBaggageItem f) const
2424
{
2525
baggage_->GetAllEntries([&f](nostd::string_view key, nostd::string_view value) {
26-
return f(key.data(), value.data());
26+
return f(std::string{key.data(), key.size()}, std::string{value.data(), value.size()});
2727
});
2828
}
2929

opentracing-shim/src/span_shim.cc

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void SpanShim::FinishWithOptions(const opentracing::FinishSpanOptions &finish_sp
4545

4646
void SpanShim::SetOperationName(opentracing::string_view name) noexcept
4747
{
48-
span_->UpdateName(name.data());
48+
span_->UpdateName(nostd::string_view{name.data(), name.size()});
4949
}
5050

5151
void SpanShim::SetTag(opentracing::string_view key, const opentracing::Value &value) noexcept
@@ -57,7 +57,8 @@ void SpanShim::SetTag(opentracing::string_view key, const opentracing::Value &va
5757
}
5858
else
5959
{
60-
span_->SetAttribute(key.data(), utils::attributeFromValue(value));
60+
auto key_view = nostd::string_view{key.data(), key.size()};
61+
span_->SetAttribute(key_view, utils::attributeFromValue(value));
6162
}
6263
}
6364

@@ -68,9 +69,11 @@ void SpanShim::SetBaggageItem(opentracing::string_view restricted_key,
6869
// Baggage key/value pair, and sets it as the current instance for this Span Shim.
6970
if (restricted_key.empty() || value.empty())
7071
return;
72+
auto restricted_key_view = nostd::string_view{restricted_key.data(), restricted_key.size()};
73+
auto value_view = nostd::string_view{value.data(), value.size()};
7174
// This operation MUST be safe to be called concurrently.
7275
const std::lock_guard<decltype(context_lock_)> guard(context_lock_);
73-
context_ = context_.newWithKeyValue(restricted_key.data(), value.data());
76+
context_ = context_.newWithKeyValue(restricted_key_view, value_view);
7477
}
7578

7679
std::string SpanShim::BaggageItem(opentracing::string_view restricted_key) const noexcept
@@ -82,7 +85,8 @@ std::string SpanShim::BaggageItem(opentracing::string_view restricted_key) const
8285
// This operation MUST be safe to be called concurrently.
8386
const std::lock_guard<decltype(context_lock_)> guard(context_lock_);
8487
std::string value;
85-
return context_.BaggageItem(restricted_key.data(), value) ? value : "";
88+
auto restricted_key_view = nostd::string_view{restricted_key.data(), restricted_key.size()};
89+
return context_.BaggageItem(restricted_key_view, value) ? value : "";
8690
}
8791

8892
void SpanShim::Log(std::initializer_list<EventEntry> fields) noexcept
@@ -128,7 +132,7 @@ void SpanShim::logImpl(nostd::span<const EventEntry> fields,
128132

129133
for (const auto &entry : fields)
130134
{
131-
nostd::string_view key = entry.first.data();
135+
nostd::string_view key{entry.first.data(), entry.first.size()};
132136
// ... including mapping of the following key/value pairs:
133137
if (is_error)
134138
{

0 commit comments

Comments
 (0)