Skip to content

Commit 7a55843

Browse files
authored
feat: add manual_keep and manual_drop endpoints for parametric test (#271)
* feat: add manual_keep and manual_drop endpoints for parametric test * chore: fmt * chore: fmt 2 * fix: missing includes
1 parent ccc2a54 commit 7a55843

3 files changed

Lines changed: 62 additions & 4 deletions

File tree

test/system-tests/main.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ int main(int argc, char* argv[]) {
120120
[&handler](const httplib::Request& req, httplib::Response& res) {
121121
handler.on_stats_flush(req, res);
122122
});
123+
svr.Post("/trace/span/manual_drop",
124+
[&handler](const httplib::Request& req, httplib::Response& res) {
125+
handler.on_manual_drop(req, res);
126+
});
127+
svr.Post("/trace/span/manual_keep",
128+
[&handler](const httplib::Request& req, httplib::Response& res) {
129+
handler.on_manual_keep(req, res);
130+
});
123131

124132
// Not implemented
125133
svr.Post("/trace/span/set_metric",

test/system-tests/request_handler.cpp

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#include "request_handler.h"
22

33
#include <datadog/optional.h>
4+
#include <datadog/sampling_priority.h>
45
#include <datadog/span_config.h>
6+
#include <datadog/trace_segment.h>
57
#include <datadog/tracer.h>
68
#include <datadog/tracer_config.h>
79

@@ -216,19 +218,19 @@ void RequestHandler::on_set_meta(const httplib::Request& req,
216218
res.status = 200;
217219
}
218220

219-
void RequestHandler::on_set_metric(const httplib::Request& /* req */,
221+
void RequestHandler::on_set_metric(const httplib::Request& req,
220222
httplib::Response& res) {
221-
const auto request_json = nlohmann::json::parse(res.body);
223+
const auto request_json = nlohmann::json::parse(req.body);
222224

223225
auto span_id = utils::get_if_exists<uint64_t>(request_json, "span_id");
224226
if (!span_id) {
225-
VALIDATION_ERROR(res, "on_set_meta: missing `span_id` field.");
227+
VALIDATION_ERROR(res, "on_set_metric: missing `span_id` field.");
226228
}
227229

228230
auto span_it = active_spans_.find(*span_id);
229231
if (span_it == active_spans_.cend()) {
230232
const auto msg =
231-
"on_set_meta: span not found for id " + std::to_string(*span_id);
233+
"on_set_metric: span not found for id " + std::to_string(*span_id);
232234
VALIDATION_ERROR(res, msg);
233235
}
234236

@@ -239,6 +241,52 @@ void RequestHandler::on_set_metric(const httplib::Request& /* req */,
239241
res.status = 200;
240242
}
241243

244+
void RequestHandler::on_manual_keep(const httplib::Request& req,
245+
httplib::Response& res) {
246+
const auto request_json = nlohmann::json::parse(req.body);
247+
248+
auto span_id = utils::get_if_exists<uint64_t>(request_json, "span_id");
249+
if (!span_id) {
250+
VALIDATION_ERROR(res, "on_manual_keep: missing `span_id` field.");
251+
}
252+
253+
auto span_it = active_spans_.find(*span_id);
254+
if (span_it == active_spans_.cend()) {
255+
const auto msg =
256+
"on_manual_keep: span not found for id " + std::to_string(*span_id);
257+
VALIDATION_ERROR(res, msg);
258+
}
259+
260+
auto& span = span_it->second;
261+
span.trace_segment().override_sampling_priority(
262+
datadog::tracing::SamplingPriority::USER_KEEP);
263+
264+
res.status = 200;
265+
}
266+
267+
void RequestHandler::on_manual_drop(const httplib::Request& req,
268+
httplib::Response& res) {
269+
const auto request_json = nlohmann::json::parse(req.body);
270+
271+
auto span_id = utils::get_if_exists<uint64_t>(request_json, "span_id");
272+
if (!span_id) {
273+
VALIDATION_ERROR(res, "on_manual_drop: missing `span_id` field.");
274+
}
275+
276+
auto span_it = active_spans_.find(*span_id);
277+
if (span_it == active_spans_.cend()) {
278+
const auto msg =
279+
"on_manual_drop: span not found for id " + std::to_string(*span_id);
280+
VALIDATION_ERROR(res, msg);
281+
}
282+
283+
auto& span = span_it->second;
284+
span.trace_segment().override_sampling_priority(
285+
datadog::tracing::SamplingPriority::USER_DROP);
286+
287+
res.status = 200;
288+
}
289+
242290
void RequestHandler::on_inject_headers(const httplib::Request& req,
243291
httplib::Response& res) {
244292
const auto request_json = nlohmann::json::parse(req.body);

test/system-tests/request_handler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class RequestHandler final {
2525
void on_span_end(const httplib::Request& req, httplib::Response& res);
2626
void on_set_meta(const httplib::Request& req, httplib::Response& res);
2727
void on_set_metric(const httplib::Request& /* req */, httplib::Response& res);
28+
void on_manual_keep(const httplib::Request& req, httplib::Response& res);
29+
void on_manual_drop(const httplib::Request& req, httplib::Response& res);
2830
void on_inject_headers(const httplib::Request& req, httplib::Response& res);
2931
void on_extract_headers(const httplib::Request& req, httplib::Response& res);
3032
void on_span_flush(const httplib::Request& /* req */, httplib::Response& res);

0 commit comments

Comments
 (0)