|
5 | 5 | #include <datadog/tracer.h> |
6 | 6 | #include <datadog/tracer_config.h> |
7 | 7 |
|
8 | | -#include <datadog/json.hpp> |
9 | | - |
10 | 8 | #include "httplib.h" |
11 | 9 | #include "utils.h" |
12 | 10 |
|
@@ -108,24 +106,26 @@ void RequestHandler::on_span_start(const httplib::Request& req, |
108 | 106 |
|
109 | 107 | if (auto parent_id = |
110 | 108 | utils::get_if_exists<uint64_t>(request_json, "parent_id")) { |
111 | | - if (*parent_id != 0) { |
112 | | - auto parent_span_it = spans_.find(*parent_id); |
113 | | - if (parent_span_it == spans_.cend()) { |
114 | | - const auto msg = "on_span_start: span not found for id " + |
115 | | - std::to_string(*parent_id); |
116 | | - VALIDATION_ERROR(res, msg); |
117 | | - } |
118 | | - |
| 109 | + auto parent_span_it = spans_.find(*parent_id); |
| 110 | + auto parent_header_it = http_headers_.find(*parent_id); |
| 111 | + if (parent_span_it != spans_.cend()) { |
119 | 112 | auto span = parent_span_it->second.create_child(span_cfg); |
120 | 113 | success(span, res); |
121 | 114 | spans_.emplace(span.id(), std::move(span)); |
122 | | - return; |
| 115 | + } else if (parent_header_it == http_headers_.cend()) { |
| 116 | + auto span = tracer_.extract_span(utils::HeaderReader(*parent_header_it), span_cfg); |
| 117 | + success(span, res); |
| 118 | + spans_.emplace(span.id(), std::move(span)); |
| 119 | + } else { |
| 120 | + const auto msg = "on_span_start: span or http_headers not found for id " + |
| 121 | + std::to_string(*parent_id); |
| 122 | + VALIDATION_ERROR(res, msg); |
123 | 123 | } |
| 124 | + } else { |
| 125 | + auto span = tracer_.create_span(span_cfg); |
| 126 | + success(span, res); |
| 127 | + spans_.emplace(span.id(), std::move(span)); |
124 | 128 | } |
125 | | - |
126 | | - auto span = tracer_.create_span(span_cfg); |
127 | | - success(span, res); |
128 | | - spans_.emplace(span.id(), std::move(span)); |
129 | 129 | } |
130 | 130 |
|
131 | 131 | void RequestHandler::on_span_end(const httplib::Request& req, |
@@ -238,17 +238,18 @@ void RequestHandler::on_extract_headers(const httplib::Request& req, |
238 | 238 | } |
239 | 239 |
|
240 | 240 | const auto response_body = nlohmann::json{ |
241 | | - {"span_id", span->id()}, |
| 241 | + {"span_id", span->parent_id()}, |
242 | 242 | }; |
243 | 243 |
|
244 | 244 | res.set_content(response_body.dump(), "application/json"); |
245 | | - spans_.emplace(span->id(), std::move(*span)); |
| 245 | + http_headers_.emplace(span->parent_id(), std::move(*http_headers)); |
246 | 246 | } |
247 | 247 |
|
248 | 248 | void RequestHandler::on_span_flush(const httplib::Request& /* req */, |
249 | 249 | httplib::Response& res) { |
250 | 250 | scheduler_->flush_telemetry(); |
251 | 251 | spans_.clear(); |
| 252 | + http_headers_.clear(); |
252 | 253 | res.status = 200; |
253 | 254 | } |
254 | 255 |
|
|
0 commit comments