Skip to content

Commit 32f0338

Browse files
authored
update the api (#131)
1 parent f3958f3 commit 32f0338

File tree

8 files changed

+36
-41
lines changed

8 files changed

+36
-41
lines changed

examples/http-server/proxy/proxy.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,36 +51,36 @@ int main() {
5151
httplib::Response& res) {
5252
tracingutil::HeaderReader reader(req.headers);
5353
auto span = tracer.extract_or_create_span(reader);
54-
span->set_name("forward.request");
55-
span->set_resource_name(req.method + " " + req.path);
56-
span->set_tag("network.origin.ip", req.remote_addr);
57-
span->set_tag("network.origin.port", std::to_string(req.remote_port));
58-
span->set_tag("http.url_details.path", req.target);
59-
span->set_tag("http.route", req.path);
60-
span->set_tag("http.method", req.method);
54+
span.set_name("forward.request");
55+
span.set_resource_name(req.method + " " + req.path);
56+
span.set_tag("network.origin.ip", req.remote_addr);
57+
span.set_tag("network.origin.port", std::to_string(req.remote_port));
58+
span.set_tag("http.url_details.path", req.target);
59+
span.set_tag("http.route", req.path);
60+
span.set_tag("http.method", req.method);
6161

6262
httplib::Error er;
6363
httplib::Request forward_request(req);
6464
forward_request.path = req.target;
6565

6666
tracingutil::HeaderWriter writer(forward_request.headers);
67-
span->inject(writer);
67+
span.inject(writer);
6868

6969
upstream_client.send(forward_request, res, er);
7070
if (er != httplib::Error::Success) {
7171
res.status = 500;
72-
span->set_error_message(httplib::to_string(er));
72+
span.set_error_message(httplib::to_string(er));
7373
std::cerr << "Error occurred while proxying request: " << req.target
7474
<< "\n";
7575
} else {
7676
tracingutil::HeaderReader reader(res.headers);
77-
auto status = span->read_sampling_delegation_response(reader);
77+
auto status = span.read_sampling_delegation_response(reader);
7878
if (auto error = status.if_error()) {
7979
std::cerr << error << "\n";
8080
}
8181
}
8282

83-
span->set_tag("http.status_code", std::to_string(res.status));
83+
span.set_tag("http.status_code", std::to_string(res.status));
8484
};
8585

8686
httplib::Server server;

examples/http-server/server/server.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,9 @@ void on_request_headers_consumed(const httplib::Request& request, dd::Tracer& tr
207207
config.start = context->request_start;
208208

209209
tracingutil::HeaderReader reader{request.headers};
210-
auto maybe_span = tracer.extract_or_create_span(reader, config);
211-
if (dd::Error* error = maybe_span.if_error()) {
212-
std::cerr << "While extracting trace context from request: " << *error << '\n';
213-
// Create a trace from scratch.
214-
context->spans.push(tracer.create_span(config));
215-
} else {
216-
context->spans.push(std::move(*maybe_span));
210+
{
211+
auto span = tracer.extract_or_create_span(reader, config);
212+
context->spans.push(std::move(span));
217213
}
218214

219215
dd::Span& span = context->spans.top();
@@ -237,7 +233,7 @@ void on_healthcheck(const httplib::Request& request, httplib::Response& response
237233
// We'd prefer not to send healthcheck traces to Datadog. They're
238234
// noisy. So, override the sampling decision to "definitely
239235
// drop," and don't even bother creating a span here.
240-
context->spans.top().trace_segment().override_sampling_priority(int(dd::SamplingPriority::USER_DROP));
236+
context->spans.top().trace_segment().override_sampling_priority(dd::SamplingPriority::USER_DROP);
241237

242238
response.set_content("I'm still here!\n", "text/plain");
243239
}

src/datadog/trace_segment.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,10 @@ void TraceSegment::span_finished() {
281281
tracer_telemetry_->metrics().tracer.trace_segments_closed.inc();
282282
}
283283

284+
void TraceSegment::override_sampling_priority(SamplingPriority priority) {
285+
override_sampling_priority(static_cast<int>(priority));
286+
}
287+
284288
void TraceSegment::override_sampling_priority(int priority) {
285289
SamplingDecision decision;
286290
decision.priority = priority;

src/datadog/trace_segment.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "optional.h"
3939
#include "propagation_style.h"
4040
#include "sampling_decision.h"
41+
#include "sampling_priority.h"
4142
#include "tracer_telemetry.h"
4243

4344
namespace datadog {
@@ -147,6 +148,7 @@ class TraceSegment {
147148
// Set the sampling decision to be a local, manual decision with the specified
148149
// sampling `priority`. Overwrite any previous sampling decision.
149150
void override_sampling_priority(int priority);
151+
void override_sampling_priority(SamplingPriority priority);
150152

151153
private:
152154
// If `sampling_decision_` is null, use `trace_sampler_` to make a

src/datadog/tracer.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,17 +322,17 @@ Expected<Span> Tracer::extract_span(const DictReader& reader,
322322
return span;
323323
}
324324

325-
Expected<Span> Tracer::extract_or_create_span(const DictReader& reader) {
325+
Span Tracer::extract_or_create_span(const DictReader& reader) {
326326
return extract_or_create_span(reader, SpanConfig{});
327327
}
328328

329-
Expected<Span> Tracer::extract_or_create_span(const DictReader& reader,
330-
const SpanConfig& config) {
329+
Span Tracer::extract_or_create_span(const DictReader& reader,
330+
const SpanConfig& config) {
331331
auto maybe_span = extract_span(reader, config);
332-
if (!maybe_span && maybe_span.error().code == Error::NO_SPAN_TO_EXTRACT) {
333-
return create_span(config);
332+
if (maybe_span) {
333+
return std::move(*maybe_span);
334334
}
335-
return maybe_span;
335+
return create_span(config);
336336
}
337337

338338
} // namespace tracing

src/datadog/tracer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ class Tracer {
7676
// new trace (see `create_span`). Optionally specify a `config` indicating
7777
// the attributes of the span. If a failure occurs, then return an error.
7878
// Note that the absence of a span to extract is not considered an error.
79-
Expected<Span> extract_or_create_span(const DictReader& reader);
80-
Expected<Span> extract_or_create_span(const DictReader& reader,
81-
const SpanConfig& config);
79+
Span extract_or_create_span(const DictReader& reader);
80+
Span extract_or_create_span(const DictReader& reader,
81+
const SpanConfig& config);
8282

8383
// Return a JSON object describing this Tracer's configuration. It is the same
8484
// JSON object that was logged when this Tracer was created.

test/system-tests/request_handler.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,11 @@ void RequestHandler::on_span_start(const httplib::Request& req,
102102
if (auto http_headers = utils::get_if_exists<nlohmann::json::array_t>(
103103
request_json, "http_headers")) {
104104
if (!http_headers->empty()) {
105-
auto maybe_span = tracer_.extract_or_create_span(
105+
auto span = tracer_.extract_or_create_span(
106106
utils::HeaderReader(*http_headers), span_cfg);
107-
if (auto error = maybe_span.if_error()) {
108-
logger_->log_error(
109-
error->with_prefix("could not extract span from http_headers: "));
110-
} else {
111-
success(*maybe_span, res);
112-
active_spans_.emplace(maybe_span->id(), std::move(*maybe_span));
113-
return;
114-
}
107+
success(span, res);
108+
active_spans_.emplace(span.id(), std::move(span));
109+
return;
115110
}
116111
}
117112

test/test_tracer.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,7 @@ TEST_CASE("span extraction") {
263263
const std::unordered_map<std::string, std::string> no_headers;
264264
MockDictReader reader{no_headers};
265265
auto span = tracer.extract_or_create_span(reader);
266-
REQUIRE(span);
267-
REQUIRE(!span->parent_id());
266+
REQUIRE(!span.parent_id());
268267
}
269268

270269
SECTION("extraction failures") {
@@ -561,8 +560,7 @@ TEST_CASE("span extraction") {
561560
auto span = tracer.extract_or_create_span(reader);
562561
auto method = "extract_or_create_span";
563562
CAPTURE(method);
564-
REQUIRE(span);
565-
checks(test_case, *span);
563+
checks(test_case, span);
566564
}
567565
}
568566

0 commit comments

Comments
 (0)