-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
468 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
api/include/opentelemetry/trace/propagation/detail/context.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#pragma once | ||
#include "opentelemetry/context/context.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace trace | ||
{ | ||
namespace propagation | ||
{ | ||
namespace detail | ||
{ | ||
|
||
trace::SpanContext GetCurrentSpan(const context::Context &context) | ||
{ | ||
context::ContextValue span = context.GetValue(trace::kSpanKey); | ||
if (nostd::holds_alternative<nostd::shared_ptr<trace::Span>>(span)) | ||
{ | ||
return nostd::get<nostd::shared_ptr<trace::Span>>(span).get()->GetContext(); | ||
} | ||
|
||
return trace::SpanContext::GetInvalid(); | ||
} | ||
|
||
} // namespace detail | ||
} // namespace propagation | ||
} // namespace trace | ||
OPENTELEMETRY_END_NAMESPACE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#pragma once | ||
|
||
#include "opentelemetry/nostd/string_view.h" | ||
|
||
#include <algorithm> | ||
#include <cstring> | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace trace | ||
{ | ||
namespace propagation | ||
{ | ||
namespace detail | ||
{ | ||
|
||
constexpr int8_t kHexDigits[256] = { | ||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, | ||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
}; | ||
|
||
inline int8_t HexToInt(char c) | ||
{ | ||
return kHexDigits[uint8_t(c)]; | ||
} | ||
|
||
bool IsValidHex(nostd::string_view s) | ||
{ | ||
return std::all_of(s.begin(), s.end(), [](char c) { return HexToInt(c) != -1; }); | ||
} | ||
|
||
/** | ||
* Converts a hexadecimal to binary format if the hex string will fit the buffer. | ||
* Smaller hex strings are left padded with zeroes. | ||
*/ | ||
bool HexToBinary(nostd::string_view hex, uint8_t *buffer, size_t buffer_size) | ||
{ | ||
if (hex.size() > buffer_size * 2) | ||
{ | ||
return false; | ||
} | ||
|
||
std::memset(buffer, 0, buffer_size); | ||
|
||
int64_t hex_size = int64_t(hex.size()); | ||
int64_t buffer_pos = int64_t(buffer_size) - (hex_size + 1) / 2; | ||
int64_t last_hex_pos = hex_size - 1; | ||
|
||
int64_t i = 0; | ||
for (; i < last_hex_pos; i += 2) | ||
{ | ||
buffer[buffer_pos++] = (HexToInt(hex[i]) << 4) | HexToInt(hex[i + 1]); | ||
} | ||
|
||
if (i == last_hex_pos) | ||
{ | ||
buffer[buffer_pos] = HexToInt(hex[i]); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
} // namespace detail | ||
} // namespace propagation | ||
} // namespace trace | ||
OPENTELEMETRY_END_NAMESPACE |
54 changes: 54 additions & 0 deletions
54
api/include/opentelemetry/trace/propagation/detail/string.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#pragma once | ||
|
||
#include "opentelemetry/nostd/string_view.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace trace | ||
{ | ||
namespace propagation | ||
{ | ||
namespace detail | ||
{ | ||
|
||
/** | ||
* Splits a string by separator, up to given buffer count words. | ||
* Returns the amount of words the input was split into. | ||
*/ | ||
size_t SplitString(nostd::string_view s, char separator, nostd::string_view *results, size_t count) | ||
{ | ||
if (count == 0) | ||
{ | ||
return count; | ||
} | ||
|
||
size_t filled = 0; | ||
size_t token_start = 0; | ||
for (size_t i = 0; i < s.size(); i++) | ||
{ | ||
if (s[i] != separator) | ||
{ | ||
continue; | ||
} | ||
|
||
results[filled++] = s.substr(token_start, i - token_start); | ||
|
||
if (filled == count) | ||
{ | ||
return count; | ||
} | ||
|
||
token_start = i + 1; | ||
} | ||
|
||
if (filled < count) | ||
{ | ||
results[filled++] = s.substr(token_start); | ||
} | ||
|
||
return filled; | ||
} | ||
|
||
} // namespace detail | ||
} // namespace propagation | ||
} // namespace trace | ||
OPENTELEMETRY_END_NAMESPACE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
#pragma once | ||
|
||
// Copyright 2021, OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "detail/context.h" | ||
#include "detail/hex.h" | ||
#include "detail/string.h" | ||
#include "opentelemetry/trace/default_span.h" | ||
#include "opentelemetry/trace/propagation/text_map_propagator.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace trace | ||
{ | ||
namespace propagation | ||
{ | ||
|
||
static const nostd::string_view kTraceHeader = "uber-trace-id"; | ||
|
||
template <typename T> | ||
class JaegerPropagator : public TextMapPropagator<T> | ||
{ | ||
public: | ||
using Getter = nostd::string_view (*)(const T &carrier, nostd::string_view trace_type); | ||
|
||
using Setter = void (*)(T &carrier, | ||
nostd::string_view trace_type, | ||
nostd::string_view trace_description); | ||
|
||
void Inject(Setter setter, T &carrier, const context::Context &context) noexcept override | ||
{ | ||
SpanContext span_context = detail::GetCurrentSpan(context); | ||
if (!span_context.IsValid()) | ||
{ | ||
return; | ||
} | ||
|
||
const size_t trace_id_length = 32; | ||
const size_t span_id_length = 16; | ||
|
||
// trace-id(32):span-id(16):0:debug(2) | ||
char trace_identity[trace_id_length + span_id_length + 6]; | ||
span_context.trace_id().ToLowerBase16({&trace_identity[0], trace_id_length}); | ||
trace_identity[trace_id_length] = ':'; | ||
span_context.span_id().ToLowerBase16({&trace_identity[trace_id_length + 1], span_id_length}); | ||
trace_identity[trace_id_length + span_id_length + 1] = ':'; | ||
trace_identity[trace_id_length + span_id_length + 2] = '0'; | ||
trace_identity[trace_id_length + span_id_length + 3] = ':'; | ||
trace_identity[trace_id_length + span_id_length + 4] = '0'; | ||
trace_identity[trace_id_length + span_id_length + 5] = span_context.IsSampled() ? '1' : '0'; | ||
|
||
setter(carrier, kTraceHeader, nostd::string_view(trace_identity, sizeof(trace_identity))); | ||
} | ||
|
||
context::Context Extract(Getter getter, | ||
const T &carrier, | ||
context::Context &context) noexcept override | ||
{ | ||
SpanContext span_context = ExtractImpl(getter, carrier); | ||
nostd::shared_ptr<Span> sp{new DefaultSpan(span_context)}; | ||
return context.SetValue(kSpanKey, sp); | ||
} | ||
|
||
private: | ||
static constexpr uint8_t kIsSampled = 0x01; | ||
|
||
static TraceFlags GetTraceFlags(uint8_t jaeger_flags) | ||
{ | ||
uint8_t sampled = jaeger_flags & kIsSampled; | ||
return TraceFlags(sampled); | ||
} | ||
|
||
static SpanContext ExtractImpl(Getter getter, const T &carrier) | ||
{ | ||
nostd::string_view trace_identity = getter(carrier, kTraceHeader); | ||
|
||
const size_t trace_field_count = 4; | ||
nostd::string_view trace_fields[trace_field_count]; | ||
|
||
if (detail::SplitString(trace_identity, ':', trace_fields, trace_field_count) != | ||
trace_field_count) | ||
{ | ||
return SpanContext::GetInvalid(); | ||
} | ||
|
||
nostd::string_view trace_id_hex = trace_fields[0]; | ||
nostd::string_view span_id_hex = trace_fields[1]; | ||
nostd::string_view flags_hex = trace_fields[3]; | ||
|
||
if (!detail::IsValidHex(trace_id_hex) || !detail::IsValidHex(span_id_hex) || | ||
!detail::IsValidHex(flags_hex)) | ||
{ | ||
return SpanContext::GetInvalid(); | ||
} | ||
|
||
uint8_t trace_id[16]; | ||
if (!detail::HexToBinary(trace_id_hex, trace_id, sizeof(trace_id))) | ||
{ | ||
return SpanContext::GetInvalid(); | ||
} | ||
|
||
uint8_t span_id[8]; | ||
if (!detail::HexToBinary(span_id_hex, span_id, sizeof(span_id))) | ||
{ | ||
return SpanContext::GetInvalid(); | ||
} | ||
|
||
uint8_t flags; | ||
if (!detail::HexToBinary(flags_hex, &flags, sizeof(flags))) | ||
{ | ||
return SpanContext::GetInvalid(); | ||
} | ||
|
||
return SpanContext(TraceId(trace_id), SpanId(span_id), GetTraceFlags(flags), true); | ||
} | ||
}; | ||
|
||
} // namespace propagation | ||
} // namespace trace | ||
OPENTELEMETRY_END_NAMESPACE |
Oops, something went wrong.