-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload_util.cpp
More file actions
203 lines (174 loc) · 6.16 KB
/
Copy pathupload_util.cpp
File metadata and controls
203 lines (174 loc) · 6.16 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2025-Present Datadog, Inc.
#include "datadog/impl/core/upload_util.hpp"
#include <sstream>
#include "datadog/impl/core/util/assert.hpp"
namespace datadog::impl {
static bool _is_valid_custom_endpoint_url(std::string_view s) {
if (s.find("http:") == 0 || s.find("https:") == 0) {
return s.back() != '/';
}
return false;
}
static std::string _replace_whitespace_with_hyphen(std::string_view s) {
std::string result;
result.reserve(s.size());
bool in_whitespace = false;
for (char c : s) {
if (std::isspace(static_cast<unsigned char>(c))) {
if (!in_whitespace) {
result += '-';
in_whitespace = true;
}
} else {
result += c;
in_whitespace = false;
}
}
return result;
}
std::string GetIntakeHost(Site site) {
// Use hardcoded values for sites with special URLs
switch (site) {
case Site::us1:
return "browser-intake-datadoghq.com";
case Site::eu1:
return "browser-intake-datadoghq.eu";
case Site::us1_fed:
return "browser-intake-ddog-gov.com";
case Site::us2_fed:
return "browser-intake-us2-ddog-gov.com";
// Fall out to default implementation
default:
break;
}
// For all other sites, fall back to 'browser-intake-%s-datadoghq.com'
std::ostringstream oss;
oss << "browser-intake-" << Site_ToString(site) << "-datadoghq.com";
return oss.str();
}
std::string GetIntakeOrigin(Site site, std::string_view custom_endpoint_url) {
// Allow a custom endpoint URL (used for internal testing) to override site
if (_is_valid_custom_endpoint_url(custom_endpoint_url)) {
return std::string(custom_endpoint_url);
}
// Use the canonical host for the configured site, with HTTPS
std::ostringstream oss;
oss << "https://" << GetIntakeHost(site);
return oss.str();
}
std::string GetUserAgent(
std::string_view service,
std::string_view application_version,
std::string_view reporter_name,
std::string_view reporter_version,
std::string_view device_name,
std::string_view os_name,
std::string_view os_version
) {
std::ostringstream oss;
// First whitespace-delimited token: user application, e.g. 'my-service/1.0.0'
oss << _replace_whitespace_with_hyphen(service);
if (!application_version.empty()) {
oss << '/' << _replace_whitespace_with_hyphen(application_version);
}
oss << ' ';
// Second token: HTTP client implementation, e.g. 'libcurl/8.11.0'
oss << _replace_whitespace_with_hyphen(reporter_name);
if (!reporter_version.empty()) {
oss << '/' << _replace_whitespace_with_hyphen(reporter_version);
}
oss << ' ';
// In parentheses: device and OS, e.g. '(ThinkPad-T14-Gen-2; Ubuntu/22.04)'
oss << '(' << _replace_whitespace_with_hyphen(device_name);
oss << "; " << _replace_whitespace_with_hyphen(os_name);
if (!os_version.empty()) {
oss << '/' << _replace_whitespace_with_hyphen(os_version);
}
oss << ')';
return oss.str();
}
std::string BuildDdTags(
std::string_view service,
std::string_view application_version,
std::string_view env,
std::string_view sdk_version,
std::string_view variant,
std::string_view tail
) {
// Prepare prefixes for all supported 'key:value' pairs
constexpr std::string_view service_prefix = "service:";
constexpr std::string_view version_prefix = "version:";
constexpr std::string_view env_prefix = "env:";
constexpr std::string_view sdk_version_prefix = "sdk_version:";
constexpr std::string_view variant_prefix = "variant:";
// service and env are set via config and are required in order to initialize the SDK;
// sdk_version is resolved internally and will never be empty
DATADOG_ASSERT(!service.empty(), "attempting to build ddtags w/o service");
DATADOG_ASSERT(!env.empty(), "attempting to build ddtags w/o env");
DATADOG_ASSERT(!sdk_version.empty(), "attempting to build ddtags w/o sdk_version");
size_t max_size = (service_prefix.size() + service.size()) +
(1 + env_prefix.size() + env.size()) +
(1 + sdk_version_prefix.size() + sdk_version.size());
// version, variant, and tail are not required; add them to our precomputed max size
// if present
if (!application_version.empty()) {
max_size += 1 + version_prefix.size() + application_version.size();
}
if (!variant.empty()) {
max_size += 1 + variant_prefix.size() + variant.size();
}
if (!tail.empty()) {
max_size += 1 + tail.size();
}
// Allocate a string large enough to hold the worst-case ddtags value, assuming no
// reserved characters need to be filtered out
std::string result;
result.reserve(max_size);
// We control the tag names, but application-provided values used in ddtags must be
// sanitized to strip any commas or colons
auto append_sanitized_tag_value = [&result](std::string_view s) {
for (const char c : s) {
if (c == ',' || c == ':') {
// In the edge case where every value in a non-empty string is a colon or comma,
// we end up with an empty tag value and that's OK
continue;
}
result += c;
}
};
// Append 'service:<service>' (required)
result += service_prefix;
append_sanitized_tag_value(service);
// Append ',version:<version>' if specified
if (!application_version.empty()) {
result += ',';
result += version_prefix;
append_sanitized_tag_value(application_version);
}
// Append ',env:<env>' (required)
result += ',';
result += env_prefix;
append_sanitized_tag_value(env);
// Append ',sdk_version:<sdk_version>' (required)
result += ',';
result += sdk_version_prefix;
append_sanitized_tag_value(sdk_version);
// Append ',variant:<variant>' if specified
if (!variant.empty()) {
result += ',';
result += variant_prefix;
append_sanitized_tag_value(variant);
}
// Append ',<tail>' if provided, assuming that the value is already sanitized (see
// also LoggerTags)
if (!tail.empty()) {
result += ',';
result += tail;
}
return result;
}
} // namespace datadog::impl