@@ -116,34 +116,23 @@ void RequestHandler::on_span_start(const httplib::Request& req,
116116 if (auto parent_id =
117117 utils::get_if_exists<uint64_t >(request_json, " parent_id" )) {
118118 if (*parent_id != 0 ) {
119- auto parent_span_it = active_spans_ .find (*parent_id);
120- if (parent_span_it == active_spans_ .cend ()) {
119+ auto parent_span_it = spans_ .find (*parent_id);
120+ if (parent_span_it == spans_ .cend ()) {
121121 const auto msg = " on_span_start: span not found for id " +
122122 std::to_string (*parent_id);
123123 VALIDATION_ERROR (res, msg);
124124 }
125125
126126 auto span = parent_span_it->second .create_child (span_cfg);
127127 success (span, res);
128- active_spans_.emplace (span.id (), std::move (span));
129- return ;
130- }
131- }
132-
133- if (auto http_headers = utils::get_if_exists<nlohmann::json::array_t >(
134- request_json, " http_headers" )) {
135- if (!http_headers->empty ()) {
136- auto span = tracer_.extract_or_create_span (
137- utils::HeaderReader (*http_headers), span_cfg);
138- success (span, res);
139- active_spans_.emplace (span.id (), std::move (span));
128+ spans_.emplace (span.id (), std::move (span));
140129 return ;
141130 }
142131 }
143132
144133 auto span = tracer_.create_span (span_cfg);
145134 success (span, res);
146- active_spans_ .emplace (span.id (), std::move (span));
135+ spans_ .emplace (span.id (), std::move (span));
147136}
148137
149138void RequestHandler::on_span_end (const httplib::Request& req,
@@ -155,14 +144,13 @@ void RequestHandler::on_span_end(const httplib::Request& req,
155144 VALIDATION_ERROR (res, " on_span_end: missing `span_id` field." );
156145 }
157146
158- auto span_it = active_spans_ .find (*span_id);
159- if (span_it == active_spans_ .cend ()) {
147+ auto span_it = spans_ .find (*span_id);
148+ if (span_it == spans_ .cend ()) {
160149 const auto msg =
161150 " on_span_end: span not found for id " + std::to_string (*span_id);
162151 VALIDATION_ERROR (res, msg);
163152 }
164153
165- active_spans_.erase (span_it);
166154 res.status = 200 ;
167155}
168156
@@ -175,8 +163,8 @@ void RequestHandler::on_set_meta(const httplib::Request& req,
175163 VALIDATION_ERROR (res, " on_set_meta: missing `span_id` field." );
176164 }
177165
178- auto span_it = active_spans_ .find (*span_id);
179- if (span_it == active_spans_ .cend ()) {
166+ auto span_it = spans_ .find (*span_id);
167+ if (span_it == spans_ .cend ()) {
180168 const auto msg =
181169 " on_set_meta: span not found for id " + std::to_string (*span_id);
182170 VALIDATION_ERROR (res, msg);
@@ -198,8 +186,8 @@ void RequestHandler::on_set_metric(const httplib::Request& /* req */,
198186 VALIDATION_ERROR (res, " on_set_meta: missing `span_id` field." );
199187 }
200188
201- auto span_it = active_spans_ .find (*span_id);
202- if (span_it == active_spans_ .cend ()) {
189+ auto span_it = spans_ .find (*span_id);
190+ if (span_it == spans_ .cend ()) {
203191 const auto msg =
204192 " on_set_meta: span not found for id " + std::to_string (*span_id);
205193 VALIDATION_ERROR (res, msg);
@@ -221,8 +209,8 @@ void RequestHandler::on_inject_headers(const httplib::Request& req,
221209 VALIDATION_ERROR (res, " on_inject_headers: missing `span_id` field." );
222210 }
223211
224- auto span_it = active_spans_ .find (*span_id);
225- if (span_it == active_spans_ .cend ()) {
212+ auto span_it = spans_ .find (*span_id);
213+ if (span_it == spans_ .cend ()) {
226214 const auto msg =
227215 " on_inject_headers: span not found for id " + std::to_string (*span_id);
228216 VALIDATION_ERROR (res, msg);
@@ -240,9 +228,38 @@ void RequestHandler::on_inject_headers(const httplib::Request& req,
240228 res.set_content (response_json.dump (), " application/json" );
241229}
242230
231+
232+ void RequestHandler::on_extract_headers (const httplib::Request& req,
233+ httplib::Response& res) {
234+ const auto request_json = nlohmann::json::parse (req.body );
235+ auto http_headers = utils::get_if_exists<nlohmann::json::array_t >(request_json, " http_headers" );
236+ if (!http_headers) {
237+ VALIDATION_ERROR (res, " on_extract_headers: missing `http_headers` field." );
238+ }
239+
240+ auto span = tracer_.extract_span (
241+ utils::HeaderReader (*http_headers), span_cfg);
242+
243+ auto success = [](const datadog::tracing::Span& span,
244+ httplib::Response& res) {
245+ // clang-format off
246+ const auto response_body = nlohmann::json{
247+ { " span_id" , span.id () }
248+ };
249+ // clang-format on
250+
251+ res.set_content (response_body.dump (), " application/json" );
252+ };
253+
254+ success (span, res);
255+ spans_.emplace (span.id (), std::move (span));
256+ }
257+
258+
243259void RequestHandler::on_span_flush (const httplib::Request& /* req */ ,
244260 httplib::Response& res) {
245261 scheduler_->flush_telemetry ();
262+ spans_.clear ();
246263 res.status = 200 ;
247264}
248265
@@ -261,8 +278,8 @@ void RequestHandler::on_span_error(const httplib::Request& req,
261278 VALIDATION_ERROR (res, " on_span_error: missing `span_id` field." );
262279 }
263280
264- auto span_it = active_spans_ .find (*span_id);
265- if (span_it == active_spans_ .cend ()) {
281+ auto span_it = spans_ .find (*span_id);
282+ if (span_it == spans_ .cend ()) {
266283 const auto msg =
267284 " on_span_error: span not found for id " + std::to_string (*span_id);
268285 VALIDATION_ERROR (res, msg);
0 commit comments