Skip to content
This repository was archived by the owner on Jan 26, 2024. It is now read-only.

Commit da59012

Browse files
authored
Support key lookup in span context extraction. (#25)
* Support key lookup in span context extraction. * Undo clang-formating change.
1 parent 41c1c97 commit da59012

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

include/opentracing/propagation.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ const std::error_code invalid_carrier_error(2, propagation_error_category());
8080
const std::error_code span_context_corrupted_error(
8181
3, propagation_error_category());
8282

83+
// `key_not_found_error` occurs when TextMapReader::LookupKey fails to find
84+
// an entry for the provided key.
85+
const std::error_code key_not_found_error(4, propagation_error_category());
86+
87+
// `lookup_key_not_supported_error` occurs when TextMapReader::LookupKey is
88+
// not supported for the provided key.
89+
const std::error_code lookup_key_not_supported_error(
90+
5, propagation_error_category());
91+
8392
// TextMapWriter is the Inject() carrier for the TextMap builtin format. With
8493
// it, the caller can encode a SpanContext for propagation as entries in a map
8594
// of unicode strings.
@@ -89,6 +98,18 @@ class TextMapReader {
8998
public:
9099
virtual ~TextMapReader() = default;
91100

101+
// LookupKey returns the value for the specified `key` if available. If no
102+
// such key is present, it returns `key_not_found_error`.
103+
//
104+
// TextMapReaders are not required to implement this method. If not supported,
105+
// the function returns `lookup_key_not_supported_error`.
106+
//
107+
// Tracers may use this as an alternative to `ForeachKey` as a faster way to
108+
// extract span context.
109+
virtual expected<string_view> LookupKey(string_view /*key*/) const {
110+
return make_unexpected(lookup_key_not_supported_error);
111+
}
112+
92113
// ForeachKey returns TextMap contents via repeated calls to the `f`
93114
// function. If any call to `f` returns an error, ForeachKey terminates and
94115
// returns that error.

src/propagation.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ class PropagationErrorCategory : public std::error_category {
88
// Needed to fix bug in macOS build
99
// (https://travis-ci.org/isaachier/hunter/jobs/281868518).
1010
// See https://stackoverflow.com/a/7411708/1930331 for justification.
11-
PropagationErrorCategory()
12-
{
13-
}
11+
PropagationErrorCategory() {}
1412

1513
const char* name() const noexcept override {
1614
return "OpenTracingPropagationError";
@@ -27,6 +25,12 @@ class PropagationErrorCategory : public std::error_category {
2725
if (code == span_context_corrupted_error.value()) {
2826
return std::make_error_condition(std::errc::invalid_argument);
2927
}
28+
if (code == key_not_found_error.value()) {
29+
return std::make_error_condition(std::errc::invalid_argument);
30+
}
31+
if (code == lookup_key_not_supported_error.value()) {
32+
return std::make_error_condition(std::errc::not_supported);
33+
}
3034
return std::error_condition(code, *this);
3135
}
3236

@@ -40,6 +44,12 @@ class PropagationErrorCategory : public std::error_category {
4044
if (code == span_context_corrupted_error.value()) {
4145
return "opentracing: SpanContext data corrupted in Extract carrier";
4246
}
47+
if (code == key_not_found_error.value()) {
48+
return "opentracing: SpanContext key not found";
49+
}
50+
if (code == lookup_key_not_supported_error.value()) {
51+
return "opentracing: Lookup for the given key is not supported";
52+
}
4353
return "opentracing: unknown propagation error";
4454
}
4555
};

0 commit comments

Comments
 (0)