-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.hpp
More file actions
472 lines (400 loc) · 15.1 KB
/
Copy pathcommand.hpp
File metadata and controls
472 lines (400 loc) · 15.1 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// 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.
#pragma once
#include <cinttypes>
#include <optional>
#include <string>
#include <string_view>
#include <variant>
#include "datadog/rum.hpp"
#include "datadog/impl/core/attribute/typed_attribute.hpp"
#include "datadog/impl/core/platform/clock.hpp"
#include "datadog/impl/rum/resource_types.hpp"
namespace datadog::impl {
/**
* Flags describing the intrinsic traits of each type of RumCommandPayload that can be
* processed by RUM scopes.
*/
enum class RumCommandFlags : uint8_t {
/**
* This command is subject to the ordinary command-processing rules. It has no special
* impact on session or view lifecycle.
*/
None = 0,
/**
* In order for this command to be processed, the application must have an active
* session. If set, the application scope may trigger session refresh when processing
* this command.
*/
RequiresActiveSession = 0x01,
/**
* In order for this command to be processed, the application must have an active
* session with an active view. If set, the session scope may attempt to automatically
* recreate the last active view when processing this command.
*/
RequiresActiveView = RequiresActiveSession | 0x02,
/**
* This command represents any change in application state that results from
* user-initiated input, such as navigation between views or tracked actions.
*/
UserInteraction = RequiresActiveSession | 0x04,
};
/**
* Enables bitwise OR on command flags.
*/
constexpr RumCommandFlags operator|(RumCommandFlags lhs, RumCommandFlags rhs) {
return static_cast<RumCommandFlags>(
static_cast<std::underlying_type_t<RumCommandFlags>>(lhs) |
static_cast<std::underlying_type_t<RumCommandFlags>>(rhs)
);
}
/**
* Enables bitwise AND on command flags.
*/
constexpr RumCommandFlags operator&(RumCommandFlags lhs, RumCommandFlags rhs) {
return static_cast<RumCommandFlags>(
static_cast<std::underlying_type_t<RumCommandFlags>>(lhs) &
static_cast<std::underlying_type_t<RumCommandFlags>>(rhs)
);
}
/**
* Parameters that are defined for all RUM commands.
*/
struct RumCommandParams {
Timestamp issued_at;
Attribute global_attributes;
Attribute attributes;
explicit RumCommandParams(
Timestamp in_issued_at, Attribute in_global_attributes, Attribute in_attributes
)
: issued_at(in_issued_at),
global_attributes(in_global_attributes),
attributes(in_attributes) {}
};
/**
* On SDKInit, the SDK has been initialized with RUM as a registered feature.
*/
struct RumSDKInitPayload {
static constexpr const char* COMMAND_NAME = "SDKInit";
static constexpr RumCommandFlags FLAGS = RumCommandFlags::None;
};
/**
* On ApplicationStart, either a.) the SDK has been initialized in response to a
* user-initiated foreground launch of the application, or b.) another RUM command has
* been processed by the first session prior to the explicit creation of the first view.
* This results in the creation of an initial 'ApplicationLaunch' view.
*/
// TODO(RUM-12242): Add 'ApplicationStart'
/**
* On StopSession, the application has explicitly signalled the end of the current
* session via a StopSession API call.
*/
struct RumStopSessionPayload {
static constexpr const char* COMMAND_NAME = "StopSession";
static constexpr RumCommandFlags FLAGS = RumCommandFlags::None;
};
/**
* On StartView, the application has called the StartView API function, explicitly
* recording that the user has just been presented with a new view.
*/
struct RumStartViewPayload {
static constexpr const char* COMMAND_NAME = "StartView";
static constexpr RumCommandFlags FLAGS = RumCommandFlags::UserInteraction;
std::string key; // Unique identifier; e.g. '/accounts', 'user-info'
std::string name; // Human-readable name to identify this view in the RUM UI
explicit RumStartViewPayload(std::string_view in_key, std::string_view in_name)
: key(in_key), name(in_name.empty() ? in_key : in_name) {}
};
/**
* On AddViewAttribute, the application has called the AddViewAttribute API function,
* inserting or updating a custom view-level attribute on the current view.
*/
struct RumAddViewAttributePayload {
static constexpr const char* COMMAND_NAME = "AddViewAttribute";
static constexpr RumCommandFlags FLAGS = RumCommandFlags::None;
std::string name;
Attribute value;
explicit RumAddViewAttributePayload(
std::string_view in_name, const Attribute& in_value
)
: name(in_name), value(in_value) {}
};
/**
* On RemoveViewAttribute, the application has called the RemoveViewAttribute API
* function, removing a custom view-level attribute from the current view.
*/
struct RumRemoveViewAttributePayload {
static constexpr const char* COMMAND_NAME = "RemoveViewAttribute";
static constexpr RumCommandFlags FLAGS = RumCommandFlags::None;
std::string name;
explicit RumRemoveViewAttributePayload(std::string_view in_name) : name(in_name) {}
};
/**
* On StopView, the application has called the StopView API function, explicitly
* recording that the view with the given key is no longer active.
*/
struct RumStopViewPayload {
static constexpr const char* COMMAND_NAME = "StopView";
static constexpr RumCommandFlags FLAGS = RumCommandFlags::None;
std::string key;
explicit RumStopViewPayload(std::string_view in_key) : key(in_key) {}
};
/**
* On StartResource, the application has called the StartResource API function,
* recording the start of an HTTP request.
*/
struct RumStartResourcePayload {
static constexpr const char* COMMAND_NAME = "StartResource";
static constexpr RumCommandFlags FLAGS = RumCommandFlags::RequiresActiveView;
std::string key;
RumRequestDetails request;
explicit RumStartResourcePayload(
std::string_view in_key, const RumRequestDetails& in_request
)
: key(in_key), request(in_request) {}
};
/**
* On StopResource, the application has called the StopResource or StopResourceWithError
* API functions, concluding an HTTP request that was previously recorded via
* StartResource.
*/
struct RumStopResourcePayload {
static constexpr const char* COMMAND_NAME = "StopResource";
static constexpr RumCommandFlags FLAGS = RumCommandFlags::None;
std::string key;
RumResponseDetails response;
std::optional<RumErrorDetails> error;
explicit RumStopResourcePayload(
std::string_view in_key,
const RumResponseDetails& in_response,
const std::optional<RumErrorDetails>& in_error
)
: key(in_key), response(in_response), error(in_error) {}
};
/**
* On AddAction, the application has called the AddAction API function, recording an
* instantaneous or short-lived user interaction of a specific type.
*/
struct RumAddActionPayload {
static constexpr const char* COMMAND_NAME = "AddAction";
static constexpr RumCommandFlags FLAGS =
RumCommandFlags::UserInteraction | RumCommandFlags::RequiresActiveView;
RumActionType type;
std::string name;
explicit RumAddActionPayload(RumActionType in_type, std::string_view in_name)
: type(in_type), name(in_name) {}
};
/**
* On StartAction, the application has called the StartAction API function, recording
* the start of a continuous user interaction of a specific type.
*/
struct RumStartActionPayload {
static constexpr const char* COMMAND_NAME = "StartAction";
static constexpr RumCommandFlags FLAGS =
RumCommandFlags::UserInteraction | RumCommandFlags::RequiresActiveView;
RumActionType type;
std::string name;
explicit RumStartActionPayload(RumActionType in_type, std::string_view in_name)
: type(in_type), name(in_name) {}
};
/**
* On StopAction, the application has called the StopAction API function, recording that
* the currently-active action has stopped.
*/
struct RumStopActionPayload {
static constexpr const char* COMMAND_NAME = "StopAction";
static constexpr RumCommandFlags FLAGS = RumCommandFlags::UserInteraction;
std::string name;
explicit RumStopActionPayload(std::string_view in_name) : name(in_name) {}
};
/**
* On AddError, the application has called the AddError API function, recording that the
* application has encountered a runtime error that should be reported in the context of
* the current view.
*/
struct RumAddErrorPayload {
static constexpr const char* COMMAND_NAME = "AddError";
static constexpr RumCommandFlags FLAGS = RumCommandFlags::RequiresActiveView;
RumErrorSource source;
RumErrorDetails error;
explicit RumAddErrorPayload(RumErrorSource in_source, const RumErrorDetails& in_error)
: source(in_source), error(in_error) {}
};
/**
* On StartOperation, the application has called the StartOperation API
* function, recording the start of a user-facing operation (e.g. login, checkout).
*/
struct RumStartOperationPayload {
static constexpr const char* COMMAND_NAME = "StartOperation";
static constexpr RumCommandFlags FLAGS = RumCommandFlags::None;
std::string name;
std::optional<std::string> operation_key;
explicit RumStartOperationPayload(
std::string_view in_name, std::optional<std::string_view> in_operation_key
)
: name(in_name), operation_key(in_operation_key) {}
};
/**
* On StopOperation, the application has called the SucceedOperation or
* FailOperation API function, recording the conclusion of a user-facing
* operation.
*/
struct RumStopOperationPayload {
static constexpr const char* COMMAND_NAME = "StopOperation";
static constexpr RumCommandFlags FLAGS = RumCommandFlags::None;
std::string name;
std::optional<std::string> operation_key;
std::optional<RumOperationFailureReason> failure_reason;
explicit RumStopOperationPayload(
std::string_view in_name,
std::optional<std::string_view> in_operation_key,
std::optional<RumOperationFailureReason> in_failure_reason
)
: name(in_name),
operation_key(in_operation_key),
failure_reason(in_failure_reason) {}
};
struct RumCommand {
using Payload = std::variant<
RumSDKInitPayload,
// TODO(RUM-12242): Add 'ApplicationStart'
RumStopSessionPayload,
RumStartViewPayload,
RumAddViewAttributePayload,
RumRemoveViewAttributePayload,
RumStopViewPayload,
RumStartResourcePayload,
RumStopResourcePayload,
RumAddActionPayload,
RumStartActionPayload,
RumStopActionPayload,
RumAddErrorPayload,
RumStartOperationPayload,
RumStopOperationPayload>;
RumCommandParams base;
Payload payload;
explicit RumCommand(RumCommandParams&& in_base, Payload&& in_payload)
: base(std::move(in_base)), payload(std::move(in_payload)) {}
/** Creates a new 'SDKInit' command. */
static RumCommand SDKInit(RumCommandParams&& base) {
return RumCommand(std::move(base), RumSDKInitPayload());
}
// TODO(RUM-12242): Add 'ApplicationStart'
/** Creates a new 'StopSession' command. */
static RumCommand StopSession(RumCommandParams&& base) {
return RumCommand(std::move(base), RumStopSessionPayload());
}
/** Creates a new 'StartView' command. */
static RumCommand StartView(
RumCommandParams&& base, std::string_view key, std::string_view name
) {
return RumCommand(std::move(base), RumStartViewPayload(key, name));
}
/** Creates a new 'StopView' command. */
static RumCommand StopView(RumCommandParams&& base, std::string_view key) {
return RumCommand(std::move(base), RumStopViewPayload(key));
}
/** Creates a new 'AddViewAttribute' command. */
static RumCommand AddViewAttribute(
RumCommandParams&& base, std::string_view name, const Attribute& value
) {
return RumCommand(std::move(base), RumAddViewAttributePayload(name, value));
}
/** Creates a new 'RemoveViewAttribute' command. */
static RumCommand RemoveViewAttribute(
RumCommandParams&& base, std::string_view name
) {
return RumCommand(std::move(base), RumRemoveViewAttributePayload(name));
}
/** Creates a new 'StartResource' command. */
static RumCommand StartResource(
RumCommandParams&& base, std::string_view key, const RumRequestDetails& request
) {
return RumCommand(std::move(base), RumStartResourcePayload(key, request));
}
/** Creates a new 'StopResource' command. */
static RumCommand StopResource(
RumCommandParams&& base,
std::string_view key,
const RumResponseDetails& response = RumResponseDetails(),
const std::optional<RumErrorDetails>& error = std::nullopt
) {
return RumCommand(std::move(base), RumStopResourcePayload(key, response, error));
}
/** Creates a new 'AddAction' command. */
static RumCommand AddAction(
RumCommandParams&& base, RumActionType type, std::string_view name
) {
return RumCommand(std::move(base), RumAddActionPayload(type, name));
}
/** Creates a new 'StartAction' command. */
static RumCommand StartAction(
RumCommandParams&& base, RumActionType type, std::string_view name
) {
return RumCommand(std::move(base), RumStartActionPayload(type, name));
}
/** Creates a new 'StopAction' command. */
static RumCommand StopAction(RumCommandParams&& base, std::string_view name) {
return RumCommand(std::move(base), RumStopActionPayload(name));
}
/** Creates a new 'AddError' command. */
static RumCommand AddError(
RumCommandParams&& base, RumErrorSource source, const RumErrorDetails& error
) {
return RumCommand(std::move(base), RumAddErrorPayload(source, error));
}
/** Creates a new 'StartOperation' command. */
static RumCommand StartOperation(
RumCommandParams&& base,
std::string_view name,
std::optional<std::string_view> operation_key
) {
return RumCommand(std::move(base), RumStartOperationPayload(name, operation_key));
}
/** Creates a new 'StopOperation' command. */
static RumCommand StopOperation(
RumCommandParams&& base,
std::string_view name,
std::optional<std::string_view> operation_key,
std::optional<RumOperationFailureReason> failure_reason
) {
return RumCommand(
std::move(base), RumStopOperationPayload(name, operation_key, failure_reason)
);
}
/**
* Returns true if the command's payload type matches the given type T. If true, it is
* safe to access the payload by calling As<T>().
*/
template <typename T>
bool Is() const {
return std::holds_alternative<T>(payload);
}
/**
* Returns a reference to the payload of a command given its known type. Calling
* As<T>() when the command's type is not known to be `T` is undefined behavior.
*/
template <typename T>
const T& As() const {
return std::get<T>(payload);
}
/**
* Returns whether the given flag is set for commands of this type.
*/
bool HasFlag(RumCommandFlags flag) const {
// Resolve the static FLAGS value declared on the payload type for this command
const RumCommandFlags type_flags = std::visit(
[](const auto& payload) {
using T = std::decay_t<decltype(payload)>;
return T::FLAGS;
},
payload
);
// Test whether that value has the desired (potentially multi-bit) flag set
return (type_flags & flag) == flag;
}
};
} // namespace datadog::impl