Skip to content

Commit

Permalink
fix: dd.p parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
dmehala committed Apr 29, 2024
1 parent 674b5bf commit 38ee414
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 21 deletions.
7 changes: 6 additions & 1 deletion examples/http-server/common/tracingutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ class HeaderWriter final : public datadog::tracing::DictWriter {
explicit HeaderWriter(httplib::Headers& headers) : headers_(headers) {}

void set(std::string_view key, std::string_view value) override {
headers_.emplace(key, value);
auto found = headers_.find(std::string(key));
if (found == headers_.cend()) {
headers_.emplace(key, value);
} else {
found->second = value;
}
}
};

Expand Down
27 changes: 14 additions & 13 deletions examples/http-server/proxy/proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,43 +43,44 @@ int main() {

dd::Tracer tracer{*finalized_config};

httplib::Client upstream_client("server", 80);
httplib::Client upstream_client("server", 8080);

// Configure the HTTP server.
auto forward_handler = [&tracer, &upstream_client](
const httplib::Request& req,
httplib::Response& res) {
auto span = tracer.create_span();
span.set_name("forward.request");
span.set_resource_name(req.method + " " + req.path);
span.set_tag("network.origin.ip", req.remote_addr);
span.set_tag("network.origin.port", std::to_string(req.remote_port));
span.set_tag("http.url_details.path", req.target);
span.set_tag("http.route", req.path);
span.set_tag("http.method", req.method);
tracingutil::HeaderReader reader(req.headers);
auto span = tracer.extract_or_create_span(reader);
span->set_name("forward.request");
span->set_resource_name(req.method + " " + req.path);
span->set_tag("network.origin.ip", req.remote_addr);
span->set_tag("network.origin.port", std::to_string(req.remote_port));
span->set_tag("http.url_details.path", req.target);
span->set_tag("http.route", req.path);
span->set_tag("http.method", req.method);

httplib::Error er;
httplib::Request forward_request(req);
forward_request.path = req.target;

tracingutil::HeaderWriter writer(forward_request.headers);
span.inject(writer);
span->inject(writer);

upstream_client.send(forward_request, res, er);
if (er != httplib::Error::Success) {
res.status = 500;
span.set_error_message(httplib::to_string(er));
span->set_error_message(httplib::to_string(er));
std::cerr << "Error occurred while proxying request: " << req.target
<< "\n";
} else {
tracingutil::HeaderReader reader(res.headers);
auto status = span.read_sampling_delegation_response(reader);
auto status = span->read_sampling_delegation_response(reader);
if (auto error = status.if_error()) {
std::cerr << error << "\n";
}
}

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

httplib::Server server;
Expand Down
5 changes: 1 addition & 4 deletions src/datadog/tags.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "tags.h"

#include "string_util.h"

namespace datadog {
namespace tracing {
namespace tags {
Expand Down Expand Up @@ -32,11 +30,10 @@ const std::string process_id = "process_id";
const std::string language = "language";
const std::string runtime_id = "runtime-id";
const std::string sampling_decider = "_dd.is_sampling_decider";
const std::string w3c_parent_id = "_dd.parent_id";

} // namespace internal

bool is_internal(StringView tag_name) { return starts_with(tag_name, "_dd."); }

} // namespace tags
} // namespace tracing
} // namespace datadog
6 changes: 5 additions & 1 deletion src/datadog/tags.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <string>

#include "string_util.h"
#include "string_view.h"

namespace datadog {
Expand Down Expand Up @@ -36,11 +37,14 @@ extern const std::string process_id;
extern const std::string language;
extern const std::string runtime_id;
extern const std::string sampling_decider;
extern const std::string w3c_parent_id;
} // namespace internal

// Return whether the specified `tag_name` is reserved for use internal to this
// library.
bool is_internal(StringView tag_name);
inline bool is_internal(StringView tag_name) {
return starts_with(tag_name, "_dd.");
}

} // namespace tags
} // namespace tracing
Expand Down
2 changes: 1 addition & 1 deletion src/datadog/tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ Expected<Span> Tracer::extract_span(const DictReader& reader,
}

if (datadog_w3c_parent_id) {
span_data->tags["_dd.parent_id"] = *datadog_w3c_parent_id;
span_data->tags[tags::internal::w3c_parent_id] = *datadog_w3c_parent_id;
}

Optional<SamplingDecision> sampling_decision;
Expand Down
2 changes: 1 addition & 1 deletion src/datadog/w3c_propagation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ void parse_datadog_tracestate(ExtractedData& result, StringView datadog_value) {
continue;
}

const auto maybe_id = parse_int(value, 16);
const auto maybe_id = parse_uint64(value, 16);
if (!maybe_id || *maybe_id == 0) {
// chaff!
pair_begin = pair_end == end ? end : pair_end + 1;
Expand Down

0 comments on commit 38ee414

Please sign in to comment.