-
Notifications
You must be signed in to change notification settings - Fork 438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Jaeger propagator #599
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
5f82a06
Add jaeger propagator
seemk 127e726
Merge branch 'main' into jaeger-propagator
seemk 37370b8
Update CHANGELOG
seemk 3f22fe5
CMake format
seemk 1d02372
Attempt at bazel build fix
seemk 32c8559
Bazel include
seemk 37aab69
Bazel script format ...
seemk 822b823
Explicit string_view construct
seemk c1db832
jaeger propagator: rename trace_state to trace_identity
seemk d1cbbd8
Move GetCurrentSpan to propagation detail
seemk beb32c2
Merge branch 'main' into jaeger-propagator
seemk 96795ea
Formatting
seemk 35493ee
Formatting
seemk 1dd50c5
More test cases
seemk 907bf23
Merge branch 'main' into jaeger-propagator
lalitb 6ed17d4
jaeger propagator: add kIsSampled flag
seemk 5e46c5f
Merge branch 'jaeger-propagator' of github.com:seemk/opentelemetry-cp…
seemk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
trace_fields[2]
not used?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, it's the deprecated parent span ID (https://www.jaegertracing.io/docs/1.22/client-libraries/#value)