-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrum.hpp
More file actions
209 lines (170 loc) · 6.57 KB
/
Copy pathrum.hpp
File metadata and controls
209 lines (170 loc) · 6.57 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
// 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 <optional>
#include <shared_mutex>
#include <string>
#include <string_view>
#include "datadog/attribute.hpp"
#include "datadog/rum.hpp"
#include "datadog/impl/core/attribute/typed_attribute.hpp"
#include "datadog/impl/core/feature.hpp"
#include "datadog/impl/core/platform/clock.hpp"
#include "datadog/impl/core/util/diagnostics.hpp"
#include "datadog/impl/rum/command.hpp"
#include "datadog/impl/rum/context.hpp"
#include "datadog/impl/rum/resource_types.hpp"
#include "datadog/impl/rum/scopes/application.hpp"
namespace datadog::impl {
/**
* RUM feature implementation.
*/
class Rum final : public Feature {
public:
explicit Rum(const RumConfig& config, const platform::IClock& clock);
FeatureId GetId() const override { return CreateFeatureId("RUMM"); }
std::string_view GetName() const override { return "rum"; }
std::optional<Report> UploadThread_PrepareReport(
BatchReader& reader, HttpRequestBuilder& builder
) override;
std::optional<std::function<void(const FeatureMessage&)>>
MakeMessageHandler() override;
protected:
/** Responds to SDK start by creating an initial RUM session. */
void Start() override;
/** Responds to SDK stop by clearing any RUM-related global state. */
void Stop() override;
public:
/** Sets an attribute value that will be included in all RUM event payloads. */
void AddAttribute(std::string_view name, const Attribute& value);
/** Clears a global attribute value. */
void RemoveAttribute(std::string_view name);
/** Handles a StopSession API call, clearing the active session. */
void StopSession();
/** Handles a StartView API call, creating a new view with the given key. */
void StartView(
std::string_view key,
std::string_view name = std::string_view{},
const Attribute& attributes = Attribute()
);
/**
* Handles an AddViewAttribute API call, mutating the set of attributes stored for the
* active view scope.
*/
void AddViewAttribute(std::string_view name, const Attribute& value);
/**
* Handles a RemoveViewAttribute API call, mutating the set of attributes stored for
* the active view scope.
*/
void RemoveViewAttribute(std::string_view name);
/** Handles a StopView API call, ending the view that corresponds to the given key. */
void StopView(std::string_view key, const Attribute& attributes = Attribute());
/** Handles an AddAction API call. */
void AddAction(
RumActionType type,
std::string_view name,
const Attribute& attributes = Attribute()
);
/**
* Handles a StartAction API call, recording a continuous user action of the given
* type.
*/
void StartAction(
RumActionType type,
std::string_view name,
const Attribute& attributes = Attribute()
);
/**
* Handles a StopAction API call, recording the end of the currently-active continuous
* user action, if any.
*/
void StopAction(std::string_view new_name, const Attribute& attributes = Attribute());
/**
* Handles a StartResource API call, opening a new resource scope with the given key
* in the currently-active view.
*/
void StartResource(
std::string_view key,
const RumRequestDetails& request,
const Attribute& attributes = Attribute()
);
/**
* Handles a StopResource or StopResourceWithError API call, closing the resource
* scope with the given key, if such a scope exists in the current view.
*/
void StopResource(
std::string_view key,
RumResponseDetails response,
const std::optional<RumErrorDetails>& error = std::nullopt,
const Attribute& attributes = Attribute()
);
/**
* Handles an AddError API call, causing an error to be reported in the context of the
* current view.
*/
void AddError(
RumErrorSource source,
const RumErrorDetails& error,
const Attribute& attributes = Attribute()
);
/**
* Handles a StartOperation API call, recording the start of a user-facing
* operation.
*/
void StartOperation(
std::string_view name,
std::optional<std::string_view> operation_key,
const Attribute& attributes = Attribute()
);
/**
* Handles a StopOperation API call (succeed or fail), recording the conclusion
* of a user-facing operation.
*/
void StopOperation(
std::string_view name,
std::optional<std::string_view> operation_key,
std::optional<RumOperationFailureReason> failure_reason,
const Attribute& attributes = Attribute()
);
private:
RumCommandParams GetBaseCommandParams(
const Attribute& attributes = Attribute()
) const;
void DispatchAsync(const RumCommand& command);
void UpdateApplicationSnapshot();
/**
* Examines the current state of `_application` and `_application_snapshot` and
* publishes any `FeatureMessage` values that reflect state changes since the last
* call. Must be called on the context thread after `UpdateApplicationSnapshot()`.
*/
void BroadcastStateChanges(const MessagePublisher& publisher);
private:
// Global attributes applied to all RUM events
ObjectAttribute _global_attributes;
mutable std::shared_mutex _global_attributes_mutex;
// Input dependencies passed to all child scopes by reference
RumScopeDependencies _deps;
// Root scope in the hierarchy that models current application state
RumApplicationScope _application;
// Reusable struct for storing the latest snapshot of RUM application state
RumContext _application_snapshot;
// Last-broadcast values used to detect changes in BroadcastStateChanges; only
// accessed on the context thread, so no additional locking is needed
std::optional<RumSessionState> _last_broadcast_session_state;
UUID _last_broadcast_view_id{UUID::Zero};
// HTTP request details used on upload; owned by the upload thread
std::string _request_url;
std::string _request_headers;
// Rum is primarily responsible for servicing RUM API calls and maintaining the state
// tree that models the state of the active application. It also handles processing of
// crash reports from previous processes, but this process doesn't depend on or modify
// the active state tree in any way, so this code lives in `rum/crash_processing/`
// rather than polluting `Rum` itself.
friend void ContextThread_HandleCrashReport(
RumScopeDependencies&, const CrashReport&, const EventWriter&
);
};
} // namespace datadog::impl