Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing(parametric): update start span parametric endpoint #170

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions test/system-tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ int main(int argc, char* argv[]) {
[&handler](const httplib::Request& req, httplib::Response& res) {
handler.on_inject_headers(req, res);
});
svr.Post("/trace/span/extract_headers",
[&handler](const httplib::Request& req, httplib::Response& res) {
handler.on_extract_headers(req, res);
});
svr.Post("/trace/span/flush",
[&handler](const httplib::Request& req, httplib::Response& res) {
handler.on_span_flush(req, res);
Expand Down
94 changes: 64 additions & 30 deletions test/system-tests/request_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,6 @@ void RequestHandler::on_span_start(const httplib::Request& req,
span_cfg.resource = *resource;
}

if (auto origin =
utils::get_if_exists<std::string_view>(request_json, "origin")) {
logger_->log_info(
"[start_span] origin, but this can only be set via the "
"'x-datadog-origin' header");
}

auto success = [](const datadog::tracing::Span& span,
httplib::Response& res) {
// clang-format off
Expand All @@ -115,35 +108,39 @@ void RequestHandler::on_span_start(const httplib::Request& req,

if (auto parent_id =
utils::get_if_exists<uint64_t>(request_json, "parent_id")) {
if (*parent_id != 0) {
auto parent_span_it = active_spans_.find(*parent_id);
if (parent_span_it == active_spans_.cend()) {
const auto msg = "on_span_start: span not found for id " +
std::to_string(*parent_id);
VALIDATION_ERROR(res, msg);
}

auto parent_span_it = active_spans_.find(*parent_id);
auto parent_header_it = http_headers_.find(*parent_id);
if (parent_span_it != active_spans_.cend()) {
auto span = parent_span_it->second.create_child(span_cfg);
success(span, res);
active_spans_.emplace(span.id(), std::move(span));
return;
}
}

if (auto http_headers = utils::get_if_exists<nlohmann::json::array_t>(
request_json, "http_headers")) {
if (!http_headers->empty()) {
auto span = tracer_.extract_or_create_span(
utils::HeaderReader(*http_headers), span_cfg);
} else if (parent_header_it != http_headers_.cend()) {
auto span = tracer_.extract_span(
utils::HeaderReader(parent_header_it->second), span_cfg);
if (span) {
success(*span, res);
active_spans_.emplace(span->id(), std::move(*span));
} else {
const auto msg =
"on_span_start: unable to create span from http_headers identified "
"by parent_id " +
std::to_string(*parent_id);
VALIDATION_ERROR(res, msg);
}
} else if (*parent_id != 0) {
const auto msg = "on_span_start: span or http_headers not found for id " +
std::to_string(*parent_id);
VALIDATION_ERROR(res, msg);
} else {
auto span = tracer_.create_span(span_cfg);
success(span, res);
active_spans_.emplace(span.id(), std::move(span));
return;
}
} else {
auto span = tracer_.create_span(span_cfg);
success(span, res);
active_spans_.emplace(span.id(), std::move(span));
}

auto span = tracer_.create_span(span_cfg);
success(span, res);
active_spans_.emplace(span.id(), std::move(span));
}

void RequestHandler::on_span_end(const httplib::Request& req,
Expand All @@ -162,7 +159,6 @@ void RequestHandler::on_span_end(const httplib::Request& req,
VALIDATION_ERROR(res, msg);
}

active_spans_.erase(span_it);
res.status = 200;
}

Expand Down Expand Up @@ -240,9 +236,47 @@ void RequestHandler::on_inject_headers(const httplib::Request& req,
res.set_content(response_json.dump(), "application/json");
}

void RequestHandler::on_extract_headers(const httplib::Request& req,
httplib::Response& res) {
const auto request_json = nlohmann::json::parse(req.body);
auto http_headers = utils::get_if_exists<nlohmann::json::array_t>(
request_json, "http_headers");
if (!http_headers) {
VALIDATION_ERROR(res, "on_extract_headers: missing `http_headers` field.");
}

datadog::tracing::SpanConfig span_cfg;
// The span below will not be finished and flushed.
auto span =
tracer_.extract_span(utils::HeaderReader(*http_headers), span_cfg);
mabdinur marked this conversation as resolved.
Show resolved Hide resolved

if (span.if_error()) {
// clang-format off
const auto response_body_fail = nlohmann::json{
{"span_id", nullptr},
};
// clang-format on
res.set_content(response_body_fail.dump(), "application/json");
return;
}

// clang-format off
const auto response_body = nlohmann::json{
{"span_id", span->parent_id().value() },
};
// clang-format on

res.set_content(response_body.dump(), "application/json");
http_headers_[span->parent_id().value()] = std::move(*http_headers);
// The span below will not be finished and flushed.
extract_headers_spans_.push_back(std::move(*span));
}

void RequestHandler::on_span_flush(const httplib::Request& /* req */,
httplib::Response& res) {
scheduler_->flush_telemetry();
active_spans_.clear();
http_headers_.clear();
res.status = 200;
}

Expand Down
5 changes: 5 additions & 0 deletions test/system-tests/request_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <datadog/tracer.h>
#include <datadog/tracer_config.h>

#include <datadog/json.hpp>

#include "developer_noise.h"
#include "httplib.h"
#include "manual_scheduler.h"
Expand All @@ -25,6 +27,7 @@ class RequestHandler final {
void on_set_meta(const httplib::Request& req, httplib::Response& res);
void on_set_metric(const httplib::Request& /* req */, httplib::Response& res);
void on_inject_headers(const httplib::Request& req, httplib::Response& res);
void on_extract_headers(const httplib::Request& req, httplib::Response& res);
void on_span_flush(const httplib::Request& /* req */, httplib::Response& res);
void on_stats_flush(const httplib::Request& /* req */,
httplib::Response& res);
Expand All @@ -35,6 +38,8 @@ class RequestHandler final {
std::shared_ptr<ManualScheduler> scheduler_;
std::shared_ptr<DeveloperNoiseLogger> logger_;
std::unordered_map<uint64_t, datadog::tracing::Span> active_spans_;
std::vector<datadog::tracing::Span> extract_headers_spans_;
std::unordered_map<uint64_t, nlohmann::json::array_t> http_headers_;

#undef VALIDATION_ERROR
};
Loading