Skip to content

Commit 505ca62

Browse files
Damien MEHALAdmehala
authored andcommitted
wip
1 parent d9e5d49 commit 505ca62

File tree

10 files changed

+120
-93
lines changed

10 files changed

+120
-93
lines changed

examples/baggage/main.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct CinReader : public dd::DictReader {
1414

1515
void visit(
1616
const std::function<void(dd::StringView key, dd::StringView value)>&
17-
visitor) const override{};
17+
visitor) const override {};
1818
};
1919

2020
std::istream& operator>>(std::istream& is, CinReader& reader) {
@@ -24,7 +24,7 @@ std::istream& operator>>(std::istream& is, CinReader& reader) {
2424

2525
std::ostream& operator<<(std::ostream& os, dd::Baggage::Error error) {
2626
using dd::Baggage;
27-
switch (error) {
27+
switch (error.code) {
2828
case Baggage::Error::MISSING_HEADER:
2929
os << "missing `baggage` header";
3030
break;
@@ -47,7 +47,14 @@ std::ostream& operator<<(std::ostream& os, dd::Baggage::Error error) {
4747
int main() {
4848
dd::TracerConfig cfg;
4949
cfg.log_on_startup = false;
50+
cfg.telemetry.enabled = false;
51+
cfg.agent.remote_configuration_enabled = false;
5052
const auto finalized_cfg = datadog::tracing::finalize_config(cfg);
53+
if (auto error = finalized_cfg.if_error()) {
54+
std::cerr << "Failed to initialize the tracer: " << error->message
55+
<< std::endl;
56+
return error->code;
57+
}
5158

5259
dd::Tracer tracer(*finalized_cfg);
5360

include/datadog/baggage.h

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
#pragma once
2+
13
#include <datadog/dict_reader.h>
24
#include <datadog/dict_writer.h>
35
#include <datadog/expected.h>
46
#include <datadog/optional.h>
57
#include <datadog/string_view.h>
68

7-
#include <initializer_list>
89
#include <string>
910
#include <unordered_map>
1011

@@ -22,31 +23,42 @@ namespace tracing {
2223
/// interface using the `inject` method.
2324
class Baggage {
2425
public:
25-
enum class Error : char {
26-
/// Baggage propagation is disabled. This may be due to one of the following
27-
/// reasons:
28-
/// - `baggage` is not set as an extraction or injection propagation style.
29-
/// - The maximum number of items is less than 0.
30-
/// - The number of bytes is less than 3.
31-
DISABLED,
32-
MISSING_HEADER,
33-
MALFORMED_BAGGAGE_HEADER,
34-
MAXIMUM_CAPACITY_REACHED,
35-
MAXIMUM_BYTES_REACHED,
26+
struct Error final {
27+
enum Code : char {
28+
/// Baggage propagation is disabled. This may be due to one of the
29+
/// following
30+
/// reasons:
31+
/// - `baggage` is not set as an extraction or injection propagation
32+
/// style.
33+
/// - The maximum number of items is less than 0.
34+
/// - The number of bytes is less than 3.
35+
DISABLED,
36+
MISSING_HEADER,
37+
MALFORMED_BAGGAGE_HEADER,
38+
MAXIMUM_CAPACITY_REACHED,
39+
MAXIMUM_BYTES_REACHED,
40+
};
41+
Code code;
42+
Optional<size_t> pos;
43+
44+
Error(Code in_code) : code(in_code), pos(nullopt) {}
45+
Error(Code in_code, size_t position) : code(in_code), pos(position) {}
46+
};
47+
48+
struct Options final {
49+
size_t max_bytes;
50+
size_t max_items;
3651
};
3752

3853
static constexpr size_t default_max_capacity = 64;
54+
static constexpr Options default_options{2048, default_max_capacity};
3955

4056
/// Extracts a Baggage instance from a `DictReader` and creates a Baggage
4157
/// instance if no errors are encounters .
4258
///
4359
/// @param `reader` The input `DictReader` from which to extract the data.
44-
/// @param `max_capacity` The maximum number of Baggage items allowd to
45-
/// parse. An error is returned when the number of element parsed exceed this
46-
/// value. If not specified, a default capacity is used.
4760
/// @return A `Baggage` instance or an `Error`.
48-
static Expected<Baggage, Error> extract(
49-
const DictReader& reader, size_t max_capacity = default_max_capacity);
61+
static Expected<Baggage, Error> extract(const DictReader& reader);
5062

5163
/// Initializes an empty Baggage with the default maximum capacity.
5264
Baggage() = default;
@@ -65,14 +77,6 @@ class Baggage {
6577
Baggage(std::unordered_map<std::string, std::string>,
6678
size_t max_capacity = default_max_capacity);
6779

68-
/// Initializes a Baggage instance using the provided initializer list of
69-
/// key-value pairs. The maximum capacity can also be specified.
70-
///
71-
/// @param `init_list` An initializer list containing key-value pairs.
72-
/// @param `max_capacity` The maximum capacity for this Baggage instance.
73-
Baggage(std::initializer_list<std::pair<const std::string, std::string>>,
74-
size_t max_capacity = default_max_capacity);
75-
7680
/// Checks if the Baggage contains a specified key.
7781
///
7882
/// @param `key` The key to check.
@@ -124,10 +128,11 @@ class Baggage {
124128
/// limit.
125129
///
126130
/// @param `writer` The DictWriter to inject the data into.
127-
/// @param `max_bytes` The maximum number of bytes to write.
131+
/// @param `opts` Injection options.
128132
/// @return An `Expected<void>`, which may either succeed or contain an
129133
/// error.
130-
Expected<void> inject(DictWriter& writer, size_t max_bytes) const;
134+
Expected<void> inject(DictWriter& writer,
135+
const Options& opts = default_options) const;
131136

132137
/// Equality operator for comparing two Baggage instances.
133138
inline bool operator==(const Baggage& rhs) const {

include/datadog/propagation_style.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ enum class PropagationStyle {
2222
// propagation is disabled in the relevant direction (extraction or
2323
// injection).
2424
NONE,
25-
// TBD
25+
// Baggage header
2626
BAGGAGE,
2727
};
2828

include/datadog/tracer.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ class Tracer {
5454
// read to determine if the process is instrumented with a tracer and to
5555
// retrieve relevant tracing information.
5656
std::shared_ptr<InMemoryFile> metadata_file_;
57-
std::size_t baggage_max_items_;
58-
std::size_t baggage_max_bytes_;
57+
Baggage::Options baggage_opts_;
5958
bool baggage_injection_enabled_;
6059
bool baggage_extraction_enabled_;
6160

include/datadog/tracer_config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <variant>
1212
#include <vector>
1313

14+
#include "baggage.h"
1415
#include "clock.h"
1516
#include "datadog_agent_config.h"
1617
#include "expected.h"
@@ -201,8 +202,7 @@ class FinalizedTracerConfig final {
201202
bool delegate_trace_sampling;
202203
bool report_traces;
203204
std::unordered_map<ConfigName, ConfigMetadata> metadata;
204-
std::size_t baggage_max_items;
205-
std::size_t baggage_max_bytes;
205+
Baggage::Options baggage_opts;
206206
};
207207

208208
// Return a `FinalizedTracerConfig` from the specified `config` and from any

src/datadog/baggage.cpp

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace tracing {
66
namespace {
77

88
Expected<std::unordered_map<std::string, std::string>, Baggage::Error>
9-
parse_baggage(StringView input, size_t max_capacity) {
9+
parse_baggage(StringView input) {
1010
std::unordered_map<std::string, std::string> result;
1111
if (input.empty()) return result;
1212

@@ -29,9 +29,6 @@ parse_baggage(StringView input, size_t max_capacity) {
2929
switch (internal_state) {
3030
case state::leading_spaces_key: {
3131
if (input[i] != ' ') {
32-
if (result.size() == max_capacity)
33-
return Baggage::Error::MAXIMUM_CAPACITY_REACHED;
34-
3532
beg = i;
3633
tmp_end = i;
3734
internal_state = state::key;
@@ -40,7 +37,7 @@ parse_baggage(StringView input, size_t max_capacity) {
4037

4138
case state::key: {
4239
if (input[i] == ',') {
43-
return Baggage::Error::MALFORMED_BAGGAGE_HEADER;
40+
return Baggage::Error{Baggage::Error::MALFORMED_BAGGAGE_HEADER, i};
4441
} else if (input[i] == '=') {
4542
key = StringView{input.data() + beg, tmp_end - beg + 1};
4643
internal_state = state::leading_spaces_value;
@@ -72,7 +69,7 @@ parse_baggage(StringView input, size_t max_capacity) {
7269
}
7370

7471
if (internal_state != state::value) {
75-
return Baggage::Error::MALFORMED_BAGGAGE_HEADER;
72+
return {Baggage::Error::MALFORMED_BAGGAGE_HEADER};
7673
}
7774

7875
value = StringView{input.data() + beg, tmp_end - beg + 1};
@@ -85,11 +82,6 @@ parse_baggage(StringView input, size_t max_capacity) {
8582

8683
Baggage::Baggage(size_t max_capacity) : max_capacity_(max_capacity) {}
8784

88-
Baggage::Baggage(
89-
std::initializer_list<std::pair<const std::string, std::string>> baggage,
90-
size_t max_capacity)
91-
: max_capacity_(max_capacity), baggage_(baggage) {}
92-
9385
Baggage::Baggage(std::unordered_map<std::string, std::string> baggage,
9486
size_t max_capacity)
9587
: max_capacity_(max_capacity), baggage_(std::move(baggage)) {}
@@ -127,12 +119,15 @@ void Baggage::visit(std::function<void(StringView, StringView)>&& visitor) {
127119
}
128120
}
129121

130-
Expected<void> Baggage::inject(DictWriter& writer, size_t max_bytes) const {
122+
Expected<void> Baggage::inject(DictWriter& writer, const Options& opts) const {
131123
if (baggage_.empty()) return {};
124+
if (baggage_.size() > opts.max_items)
125+
return datadog::tracing::Error{
126+
datadog::tracing::Error::Code::BAGGAGE_MAXIMUM_BYTES_REACHED, ""};
132127

133128
// TODO(@dmehala): Memory alloc optimization, (re)use fixed size buffer.
134129
std::string res;
135-
res.reserve(max_bytes);
130+
res.reserve(opts.max_bytes);
136131

137132
auto it = baggage_.cbegin();
138133
res += it->first;
@@ -146,7 +141,7 @@ Expected<void> Baggage::inject(DictWriter& writer, size_t max_bytes) const {
146141
res += it->second;
147142
}
148143

149-
if (res.size() >= max_bytes)
144+
if (res.size() >= opts.max_bytes)
150145
return datadog::tracing::Error{
151146
datadog::tracing::Error::Code::BAGGAGE_MAXIMUM_BYTES_REACHED, ""};
152147

@@ -156,20 +151,19 @@ Expected<void> Baggage::inject(DictWriter& writer, size_t max_bytes) const {
156151
return {};
157152
}
158153

159-
Expected<Baggage, Baggage::Error> Baggage::extract(const DictReader& headers,
160-
size_t max_capacity) {
154+
Expected<Baggage, Baggage::Error> Baggage::extract(const DictReader& headers) {
161155
auto found = headers.lookup("baggage");
162156
if (!found) {
163-
return Error::MISSING_HEADER;
157+
return Baggage::Error{Error::MISSING_HEADER};
164158
}
165159

166160
// TODO(@dmehala): Avoid allocation
167-
auto bv = parse_baggage(*found, max_capacity);
161+
auto bv = parse_baggage(*found);
168162
if (auto error = bv.if_error()) {
169163
return *error;
170164
}
171165

172-
Baggage result(*bv, max_capacity);
166+
Baggage result(*bv);
173167
return result;
174168
}
175169

src/datadog/tracer.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ Tracer::Tracer(const FinalizedTracerConfig& config,
6060
extraction_styles_(config.extraction_styles),
6161
tags_header_max_size_(config.tags_header_size),
6262
sampling_delegation_enabled_(config.delegate_trace_sampling),
63-
baggage_max_items_(config.baggage_max_items),
64-
baggage_max_bytes_(config.baggage_max_bytes),
63+
baggage_opts_(config.baggage_opts),
6564
baggage_injection_enabled_(false),
6665
baggage_extraction_enabled_(false) {
6766
if (config.report_hostname) {
@@ -120,6 +119,10 @@ std::string Tracer::config() const {
120119
{"extraction_styles", extraction_styles_},
121120
{"tags_header_size", tags_header_max_size_},
122121
{"environment_variables", nlohmann::json::parse(environment::to_json())},
122+
{"baggage", nlohmann::json{
123+
{"max_bytes", baggage_opts_.max_bytes},
124+
{"max_items", baggage_opts_.max_items},
125+
}},
123126
});
124127
// clang-format on
125128

@@ -420,15 +423,15 @@ Span Tracer::extract_or_create_span(const DictReader& reader,
420423
return create_span(config);
421424
}
422425

423-
Baggage Tracer::create_baggage() { return Baggage(baggage_max_items_); }
426+
Baggage Tracer::create_baggage() { return Baggage(baggage_opts_.max_items); }
424427

425428
Expected<Baggage, Baggage::Error> Tracer::extract_baggage(
426429
const DictReader& reader) {
427430
if (!baggage_extraction_enabled_) {
428-
return Baggage::Error::DISABLED;
431+
return Baggage::Error{Baggage::Error::DISABLED};
429432
}
430433

431-
return Baggage::extract(reader, baggage_max_items_);
434+
return Baggage::extract(reader);
432435
}
433436

434437
Baggage Tracer::extract_or_create_baggage(const DictReader& reader) {
@@ -446,7 +449,7 @@ Expected<void> Tracer::inject(const Baggage& baggage, DictWriter& writer) {
446449
return Error{Error::Code::OTHER, "Baggage propagation is disabled"};
447450
}
448451

449-
return baggage.inject(writer, baggage_max_bytes_);
452+
return baggage.inject(writer, baggage_opts_);
450453
}
451454

452455
} // namespace tracing

src/datadog/tracer_config.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -380,20 +380,20 @@ Expected<FinalizedTracerConfig> finalize_config(const TracerConfig &user_config,
380380
tracer_version);
381381

382382
// Baggage
383-
std::tie(origin, final_config.baggage_max_items) =
383+
std::tie(origin, final_config.baggage_opts.max_items) =
384384
pick(env_config->baggage_max_items, user_config.baggage_max_items, 64);
385385
final_config.metadata[ConfigName::TRACE_BAGGAGE_MAX_ITEMS] =
386386
ConfigMetadata(ConfigName::TRACE_BAGGAGE_MAX_ITEMS,
387-
to_string(final_config.baggage_max_items), origin);
387+
to_string(final_config.baggage_opts.max_items), origin);
388388

389-
std::tie(origin, final_config.baggage_max_bytes) =
389+
std::tie(origin, final_config.baggage_opts.max_bytes) =
390390
pick(env_config->baggage_max_bytes, user_config.baggage_max_bytes, 8192);
391391
final_config.metadata[ConfigName::TRACE_BAGGAGE_MAX_BYTES] =
392392
ConfigMetadata(ConfigName::TRACE_BAGGAGE_MAX_BYTES,
393-
to_string(final_config.baggage_max_bytes), origin);
393+
to_string(final_config.baggage_opts.max_bytes), origin);
394394

395-
if (final_config.baggage_max_items <= 0 ||
396-
final_config.baggage_max_bytes < 3) {
395+
if (final_config.baggage_opts.max_items <= 0 ||
396+
final_config.baggage_opts.max_bytes < 3) {
397397
auto it = std::remove(final_config.extraction_styles.begin(),
398398
final_config.extraction_styles.end(),
399399
PropagationStyle::BAGGAGE);

0 commit comments

Comments
 (0)