-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathextract_span_context.cpp
64 lines (59 loc) · 2.25 KB
/
extract_span_context.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <opentracing/propagation.h>
#include <opentracing/tracer.h>
#include "utility.h"
using opentracing::expected;
using opentracing::make_unexpected;
using opentracing::string_view;
extern "C" {
#include <nginx.h>
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
}
namespace ngx_opentracing {
//------------------------------------------------------------------------------
// NgxHeaderCarrierReader
//------------------------------------------------------------------------------
namespace {
class NgxHeaderCarrierReader : public opentracing::HTTPHeadersReader {
public:
explicit NgxHeaderCarrierReader(const ngx_http_request_t *request)
: request_{request} {}
expected<void> ForeachKey(
std::function<expected<void>(string_view, string_view)> f)
const override {
expected<void> result;
for_each<ngx_table_elt_t>(
request_->headers_in.headers, [&](const ngx_table_elt_t &header) {
if (!result) return;
auto key = string_view{reinterpret_cast<char *>(header.lowcase_key),
header.key.len};
auto value = string_view{reinterpret_cast<char *>(header.value.data),
header.value.len};
result = f(key, value);
});
return result;
}
private:
const ngx_http_request_t *request_;
};
} // namespace
//------------------------------------------------------------------------------
// extract_span_context
//------------------------------------------------------------------------------
std::unique_ptr<opentracing::SpanContext> extract_span_context(
const opentracing::Tracer &tracer, const ngx_http_request_t *request) {
auto carrier_reader = NgxHeaderCarrierReader{request};
auto span_context_maybe = tracer.Extract(carrier_reader);
if (!span_context_maybe) {
ngx_log_error(
NGX_LOG_ERR, request->connection->log, 0,
"failed to extract an opentracing span context from request %p: %s",
request, span_context_maybe.error().message().c_str());
return nullptr;
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, request->connection->log, 0,
"extraced opentracing span context from request %p", request);
return std::move(*span_context_maybe);
}
} // namespace ngx_opentracing