Skip to content

Commit e2e3e76

Browse files
committed
best effort to extract headers in one endpoint and create a local root span in another [probably won't work tho]
1 parent c13f728 commit e2e3e76

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

test/system-tests/request_handler.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include <datadog/tracer.h>
66
#include <datadog/tracer_config.h>
77

8-
#include <datadog/json.hpp>
9-
108
#include "httplib.h"
119
#include "utils.h"
1210

@@ -108,24 +106,26 @@ void RequestHandler::on_span_start(const httplib::Request& req,
108106

109107
if (auto parent_id =
110108
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()) {
119112
auto span = parent_span_it->second.create_child(span_cfg);
120113
success(span, res);
121114
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);
123123
}
124+
} else {
125+
auto span = tracer_.create_span(span_cfg);
126+
success(span, res);
127+
spans_.emplace(span.id(), std::move(span));
124128
}
125-
126-
auto span = tracer_.create_span(span_cfg);
127-
success(span, res);
128-
spans_.emplace(span.id(), std::move(span));
129129
}
130130

131131
void RequestHandler::on_span_end(const httplib::Request& req,
@@ -238,17 +238,18 @@ void RequestHandler::on_extract_headers(const httplib::Request& req,
238238
}
239239

240240
const auto response_body = nlohmann::json{
241-
{"span_id", span->id()},
241+
{"span_id", span->parent_id()},
242242
};
243243

244244
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));
246246
}
247247

248248
void RequestHandler::on_span_flush(const httplib::Request& /* req */,
249249
httplib::Response& res) {
250250
scheduler_->flush_telemetry();
251251
spans_.clear();
252+
http_headers_.clear();
252253
res.status = 200;
253254
}
254255

test/system-tests/request_handler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <datadog/span_config.h>
55
#include <datadog/tracer.h>
66
#include <datadog/tracer_config.h>
7+
#include <datadog/json.hpp>
78

89
#include "developer_noise.h"
910
#include "httplib.h"
@@ -36,6 +37,7 @@ class RequestHandler final {
3637
std::shared_ptr<ManualScheduler> scheduler_;
3738
std::shared_ptr<DeveloperNoiseLogger> logger_;
3839
std::unordered_map<uint64_t, datadog::tracing::Span> spans_;
40+
std::unordered_map<uint64_t, nlohmann::json::array_t> http_headers_;
3941

4042
#undef VALIDATION_ERROR
4143
};

0 commit comments

Comments
 (0)