-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmain.cpp
More file actions
95 lines (83 loc) · 2.89 KB
/
main.cpp
File metadata and controls
95 lines (83 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <datadog/dict_reader.h>
#include <datadog/tracer.h>
#include <iostream>
namespace dd = datadog::tracing;
struct CinReader : public dd::DictReader {
std::string input;
dd::Optional<dd::StringView> lookup(dd::StringView key) const override {
return input;
}
void visit(
const std::function<void(dd::StringView key, dd::StringView value)>&
visitor) const override{};
};
std::istream& operator>>(std::istream& is, CinReader& reader) {
is >> reader.input;
return is;
}
std::ostream& operator<<(std::ostream& os, dd::Baggage::Error error) {
using dd::Baggage;
switch (error.code) {
case Baggage::Error::MISSING_HEADER:
os << "missing `baggage` header";
break;
case Baggage::Error::MALFORMED_BAGGAGE_HEADER: {
os << "malformed `baggage` header";
if (error.pos) {
os << " at position " << *error.pos;
}
} break;
case Baggage::Error::MAXIMUM_CAPACITY_REACHED:
os << "maximum number of bagge items reached";
break;
case Baggage::Error::MAXIMUM_BYTES_REACHED:
os << "maximum amount of bytes written";
break;
default:
os << "unknown error code";
break;
}
return os;
}
int main() {
dd::TracerConfig cfg;
cfg.log_on_startup = false;
cfg.telemetry.enabled = false;
cfg.agent.remote_configuration_enabled = false;
const auto finalized_cfg = datadog::tracing::finalize_config(cfg);
if (auto error = finalized_cfg.if_error()) {
std::cerr << "Failed to initialize the tracer: " << error->message
<< std::endl;
return error->code;
}
dd::Tracer tracer(*finalized_cfg);
std::cout
<< "This program demonstrates how to use baggage, a feature that allows "
"metadata (key-value pairs) to be attached to a request and "
"propagated across services.\n"
"Baggage can be useful for passing contextual information, such as "
"user IDs, session tokens, or request attributes, between different "
"components of a distributed system.\n\n"
"This example lets you input baggage values, validate them and "
"displays the baggage content parsed.\n"
"You can enter baggage manually or provide it through a file, try:\n"
"- k1=v1,k2=v2\n"
"- ,invalid=input\n"
"or ./baggage-example < list-of-baggages.txt\n\n";
CinReader reader;
std::cout << "Enter baggage (or 'CTRL+C' to quit): ";
while (std::getline(std::cin, reader.input)) {
auto baggage = tracer.extract_baggage(reader);
if (!baggage) {
std::cout << "Error parsing \"" << reader.input
<< "\": " << baggage.error() << ".\n";
} else {
std::cout << "Baggage key-value parsed: \n";
baggage->visit([](dd::StringView key, dd::StringView value) {
std::cout << key << ": " << value << std::endl;
});
}
std::cout << "\nEnter baggage (or 'CTRL+C' to quit): ";
}
return 0;
}