|
| 1 | +#include <datadog/optional.h> |
| 2 | +#include <datadog/span_config.h> |
| 3 | +#include <datadog/tracer.h> |
| 4 | +#include <datadog/tracer_config.h> |
| 5 | + |
| 6 | +#include <chrono> |
| 7 | +#include <datadog/json.hpp> |
| 8 | +#include <iostream> |
| 9 | +#include <optional> |
| 10 | +#include <string_view> |
| 11 | +#include <unordered_map> |
| 12 | + |
| 13 | +#include "developer_noise.h" |
| 14 | +#include "httplib.h" |
| 15 | +#include "request_handler.h" |
| 16 | + |
| 17 | +// `hard_stop` is installed as a signal handler for `SIGTERM`. |
| 18 | +// For some reason, the default handler was not being called. |
| 19 | +void hard_stop(int /*signal*/) { std::exit(0); } |
| 20 | + |
| 21 | +std::optional<uint16_t> get_port() { |
| 22 | + try { |
| 23 | + auto port_env = std::getenv("APM_TEST_CLIENT_SERVER_PORT"); |
| 24 | + if (port_env == nullptr) { |
| 25 | + return std::nullopt; |
| 26 | + } |
| 27 | + |
| 28 | + uint16_t port = std::atoi(port_env); |
| 29 | + return port; |
| 30 | + } catch (...) { |
| 31 | + return std::nullopt; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +void print_usage(std::string_view app) { |
| 36 | + // clang-format off |
| 37 | + std::cout << app << "\n\n" |
| 38 | + << "Usage: HTTP server for parametric system tests\n\n" |
| 39 | + << "-h, --help\t\tPrint this help message.\n" |
| 40 | + << "-v, --version\t\tPrint the version of dd-trace-cpp.\n\n" |
| 41 | + << "Environment variables:\n\n" |
| 42 | + << "APM_TEST_CLIENT_SERVER_PORT\tDefines port to use." |
| 43 | + << "\n"; |
| 44 | + // clang-format on |
| 45 | +} |
| 46 | + |
| 47 | +int main(int argc, char* argv[]) { |
| 48 | + if (argc > 1) { |
| 49 | + for (int i = 0; i < argc; ++i) { |
| 50 | + const std::string_view arg{argv[i]}; |
| 51 | + if (arg == "-h" || arg == "--help") { |
| 52 | + print_usage(argv[0]); |
| 53 | + return 0; |
| 54 | + } else if (arg == "-v" || arg == "--version") { |
| 55 | + std::cout << datadog::tracing::tracer_version << "\n"; |
| 56 | + return 0; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + auto logger = make_logger(); |
| 62 | + |
| 63 | + auto port = get_port(); |
| 64 | + if (!port) { |
| 65 | + logger->log_error( |
| 66 | + "environment variable APM_TEST_CLIENT_SERVER_PORT is not set or the " |
| 67 | + "port is not valid"); |
| 68 | + return 1; |
| 69 | + } |
| 70 | + |
| 71 | + // An event scheduler needs to be shared between the TracingService and the |
| 72 | + // tracer. |
| 73 | + auto event_scheduler = std::make_shared<ManualScheduler>(); |
| 74 | + |
| 75 | + datadog::tracing::TracerConfig config; |
| 76 | + config.logger = logger; |
| 77 | + config.agent.event_scheduler = event_scheduler; |
| 78 | + config.service = "cpp-parametric-test"; |
| 79 | + config.environment = "staging"; |
| 80 | + config.name = "http.request"; |
| 81 | + |
| 82 | + auto finalized_config = datadog::tracing::finalize_config(config); |
| 83 | + if (auto error = finalized_config.if_error()) { |
| 84 | + logger->log_error(error->with_prefix("unable to initialize tracer:")); |
| 85 | + return 1; |
| 86 | + } |
| 87 | + |
| 88 | + RequestHandler handler(*finalized_config, event_scheduler, logger); |
| 89 | + |
| 90 | + httplib::Server svr; |
| 91 | + svr.Post("/trace/span/start", |
| 92 | + [&handler](const httplib::Request& req, httplib::Response& res) { |
| 93 | + handler.on_span_start(req, res); |
| 94 | + }); |
| 95 | + svr.Post("/trace/span/finish", |
| 96 | + [&handler](const httplib::Request& req, httplib::Response& res) { |
| 97 | + handler.on_span_end(req, res); |
| 98 | + }); |
| 99 | + svr.Post("/trace/span/set_meta", |
| 100 | + [&handler](const httplib::Request& req, httplib::Response& res) { |
| 101 | + handler.on_set_meta(req, res); |
| 102 | + }); |
| 103 | + svr.Post("/trace/span/inject_headers", |
| 104 | + [&handler](const httplib::Request& req, httplib::Response& res) { |
| 105 | + handler.on_inject_headers(req, res); |
| 106 | + }); |
| 107 | + svr.Post("/trace/span/flush", |
| 108 | + [&handler](const httplib::Request& req, httplib::Response& res) { |
| 109 | + handler.on_span_flush(req, res); |
| 110 | + }); |
| 111 | + svr.Post("/trace/stats/flush", |
| 112 | + [&handler](const httplib::Request& req, httplib::Response& res) { |
| 113 | + handler.on_stats_flush(req, res); |
| 114 | + }); |
| 115 | + |
| 116 | + // Not implemented |
| 117 | + svr.Post("/trace/span/set_metric", |
| 118 | + [&handler](const httplib::Request& req, httplib::Response& res) { |
| 119 | + handler.on_set_metric(req, res); |
| 120 | + }); |
| 121 | + |
| 122 | + svr.set_logger([&logger](const auto& req, const auto&) { |
| 123 | + std::string msg{req.method}; |
| 124 | + msg += " "; |
| 125 | + msg += req.path; |
| 126 | + msg += " "; |
| 127 | + msg += req.version; |
| 128 | + logger->log_info(msg); |
| 129 | + |
| 130 | + if (!req.body.empty()) { |
| 131 | + msg = " body: "; |
| 132 | + msg += req.body; |
| 133 | + logger->log_info(msg); |
| 134 | + } |
| 135 | + }); |
| 136 | + |
| 137 | + svr.set_exception_handler([](const auto&, auto& res, std::exception_ptr ep) { |
| 138 | + try { |
| 139 | + std::rethrow_exception(ep); |
| 140 | + } catch (const nlohmann::json::exception& e) { |
| 141 | + // clang-format off |
| 142 | + nlohmann::json j{ |
| 143 | + {"detail", { |
| 144 | + {"loc", nlohmann::json::array({__FILE__, __LINE__})}, |
| 145 | + {"type", "JSON Parsing error"}, |
| 146 | + {"msg", e.what()} |
| 147 | + }} |
| 148 | + }; |
| 149 | + // clang-format on |
| 150 | + |
| 151 | + res.set_content(j.dump(), "application/json"); |
| 152 | + res.status = 422; |
| 153 | + return; |
| 154 | + } catch (std::exception& e) { |
| 155 | + res.set_content(e.what(), "text/plain"); |
| 156 | + } catch (...) { |
| 157 | + res.set_content("Unknown Exception", "text/plain"); |
| 158 | + } |
| 159 | + |
| 160 | + res.status = 500; |
| 161 | + }); |
| 162 | + |
| 163 | + std::signal(SIGTERM, hard_stop); |
| 164 | + svr.listen("0.0.0.0", *port); |
| 165 | + return 0; |
| 166 | +} |
0 commit comments