-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.cpp
More file actions
252 lines (211 loc) · 6.53 KB
/
Copy pathcore.cpp
File metadata and controls
252 lines (211 loc) · 6.53 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// 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/core.hpp"
#include <iostream>
#include <limits>
#include "datadog/impl/core/core.hpp"
#include "datadog/impl/core/util/diagnostics.hpp"
namespace datadog {
void StderrDiagnosticHandler(const DiagnosticMessage& message) {
static const char* level_names[] = {"DEBUG", "STATUS", "WARNING", "ERROR"};
const size_t i = static_cast<size_t>(message.level);
const char* level_name = i < std::size(level_names) ? level_names[i] : ""; // NOLINT
std::cerr << "[DATADOG " << level_name << "] " << message.text << "\n";
}
CoreConfig::CoreConfig(
std::string_view in_client_token,
std::string_view in_service,
std::string_view in_env
)
: client_token(in_client_token), service(in_service), env(in_env) {}
CoreConfig::~CoreConfig() = default;
CoreConfig::CoreConfig(const CoreConfig&) = default;
CoreConfig& CoreConfig::operator=(const CoreConfig&) = default;
CoreConfig::CoreConfig(CoreConfig&&) = default;
CoreConfig& CoreConfig::operator=(CoreConfig&&) = default;
CoreConfig& CoreConfig::SetDiagnosticHandler(DiagnosticHandler value) {
diagnostic_handler = std::move(value);
return *this;
}
CoreConfig& CoreConfig::SetDiagnosticThreshold(DiagnosticLevel value) {
diagnostic_threshold = value;
return *this;
}
CoreConfig& CoreConfig::SetApplicationStoragePath(std::string_view value) {
application_storage_path = value;
return *this;
}
CoreConfig& CoreConfig::SetSite(Site value) {
site = value;
return *this;
}
CoreConfig& CoreConfig::SetClientToken(std::string_view value) {
client_token = value;
return *this;
}
CoreConfig& CoreConfig::SetService(std::string_view value) {
service = value;
return *this;
}
CoreConfig& CoreConfig::SetEnv(std::string_view value) {
env = value;
return *this;
}
CoreConfig& CoreConfig::SetVersion(std::string_view value) {
application_version = value;
return *this;
}
CoreConfig& CoreConfig::SetVariant(std::string_view value) {
variant = value;
return *this;
}
CoreConfig& CoreConfig::SetBatchSize(BatchSize value) {
batch_size = value;
return *this;
}
CoreConfig& CoreConfig::SetUploadFrequency(UploadFrequency value) {
upload_frequency = value;
return *this;
}
CoreConfig& CoreConfig::SetBatchProcessingLevel(BatchProcessingLevel value) {
batch_processing_level = value;
return *this;
}
CoreConfig& CoreConfig::Internal_FlushHttpRequestsOnStop() {
const size_t value = std::numeric_limits<size_t>::max();
internal_options.num_http_requests_per_feature_to_flush_on_stop = value;
return *this;
}
CoreConfig& CoreConfig::Internal_UseCustomEndpoint(std::string_view value) {
internal_options.custom_endpoint_url = value;
return *this;
}
CoreConfig& CoreConfig::Internal_SetSource(std::string_view value) {
internal_options.source = value;
return *this;
}
CoreConfig& CoreConfig::Internal_SetSdkVersion(std::string_view value) {
internal_options.sdk_version = value;
return *this;
}
Core::Core(Core::PrivateCtorTag)
: _impl(nullptr),
_diagnostic_handler(nullptr),
_diagnostic_threshold(DiagnosticLevel::Error) {}
Core::Core(
std::unique_ptr<impl::Core>&& impl,
DiagnosticHandler diagnostic_handler,
DiagnosticLevel diagnostic_threshold,
PrivateCtorTag
)
: _impl(std::move(impl)),
_diagnostic_handler(std::move(diagnostic_handler)),
_diagnostic_threshold(diagnostic_threshold) {}
Core::~Core() = default;
std::shared_ptr<Core> Core::Create(
const CoreConfig& config, TrackingConsent tracking_consent
) {
// Prepare a diagnostic logger that will allow us to emit errors for invalid API usage
const impl::DiagnosticLogger diagnostic_logger{
config.diagnostic_handler, config.diagnostic_threshold
};
// Validate the config: if we don't have all required parameters, return a no-op Core
if (config.client_token.empty()) {
diagnostic_logger.Error(
"SDK initialization failed: application must supply a non-empty 'client_token' "
"value in CoreConfig"
);
return std::make_shared<Core>(Core::PrivateCtorTag{});
}
if (config.service.empty()) {
diagnostic_logger.Error(
"SDK initialization failed: application must supply a non-empty 'service' "
"value in CoreConfig"
);
return std::make_shared<Core>(Core::PrivateCtorTag{});
}
if (config.env.empty()) {
diagnostic_logger.Error(
"SDK initialization failed: application must supply a non-empty 'env' value in "
"CoreConfig"
);
return std::make_shared<Core>(Core::PrivateCtorTag{});
}
// Create core subsystems using default implementations
auto subsystems = impl::CoreSubsystems::Init(diagnostic_logger);
if (!subsystems.has_value()) {
return std::make_shared<Core>(Core::PrivateCtorTag{});
}
// Create the core implementation
auto impl =
std::make_unique<impl::Core>(config, tracking_consent, std::move(*subsystems));
if (!impl->Init()) {
// If subsystem initialization fails, return a no-op core
return std::make_shared<Core>(Core::PrivateCtorTag{});
}
// Wrap the implementation in a C++ struct that exposes the public API for the core,
// using the pimpl idiom, with automatic cleanup
return std::make_shared<Core>(
std::move(impl),
config.diagnostic_handler,
config.diagnostic_threshold,
PrivateCtorTag{}
);
}
void Core::SetTrackingConsent(TrackingConsent value) {
if (_impl) {
_impl->SetTrackingConsent(value);
}
}
void Core::SetUserInfo(
std::string_view id,
std::string_view name,
std::string_view email,
const Attribute& extra_info
) {
if (_impl) {
_impl->SetUserInfo(id, name, email, extra_info);
}
}
void Core::AddUserExtraInfo(const Attribute& extra_info) {
if (_impl) {
_impl->AddUserExtraInfo(extra_info);
}
}
void Core::ClearUserInfo() {
if (_impl) {
_impl->ClearUserInfo();
}
}
void Core::SetAccountInfo(
std::string_view id, std::string_view name, const Attribute& extra_info
) {
if (_impl) {
_impl->SetAccountInfo(id, name, extra_info);
}
}
void Core::AddAccountExtraInfo(const Attribute& extra_info) {
if (_impl) {
_impl->AddAccountExtraInfo(extra_info);
}
}
void Core::ClearAccountInfo() {
if (_impl) {
_impl->ClearAccountInfo();
}
}
bool Core::Start() {
if (_impl) {
return _impl->Start();
}
return false;
}
void Core::Stop() {
if (_impl) {
_impl->Stop();
}
}
} // namespace datadog