Skip to content

Commit b445e2e

Browse files
committed
Add size to all string view mappings between OT and OTel
1 parent fb6fde8 commit b445e2e

File tree

5 files changed

+31
-21
lines changed

5 files changed

+31
-21
lines changed

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
{

opentracing-shim/src/tracer_shim.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ std::unique_ptr<opentracing::Span> TracerShim::StartSpanWithOptions(
2424
if (is_closed_)
2525
return nullptr;
2626

27-
const auto &opts = utils::makeOptionsShim(options);
28-
const auto &links = utils::makeIterableLinks(options);
29-
const auto &attributes = utils::makeIterableTags(options);
30-
const auto &baggage = utils::makeBaggage(options);
31-
auto span = tracer_->StartSpan(operation_name.data(), attributes, links, opts);
32-
auto span_shim = new (std::nothrow) SpanShim(*this, span, baggage);
27+
const auto &opts = utils::makeOptionsShim(options);
28+
const auto &links = utils::makeIterableLinks(options);
29+
const auto &attributes = utils::makeIterableTags(options);
30+
const auto &baggage = utils::makeBaggage(options);
31+
auto operation_name_view = nostd::string_view{operation_name.data(), operation_name.size()};
32+
auto span = tracer_->StartSpan(operation_name_view, attributes, links, opts);
33+
auto span_shim = new (std::nothrow) SpanShim(*this, span, baggage);
3334

3435
// If an initial set of tags is specified and the OpenTracing error tag
3536
// is included after the OpenTelemetry Span was created.

0 commit comments

Comments
 (0)