-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdatadog_context.cpp
More file actions
329 lines (291 loc) · 10.1 KB
/
Copy pathdatadog_context.cpp
File metadata and controls
329 lines (291 loc) · 10.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
#include "datadog_context.h"
#include <algorithm>
#include <sstream>
#include <stdexcept>
#include <string_view>
#include "datadog/span.h"
#include "datadog_handler.h"
#include "dd.h"
#include "ngx_http_datadog_module.h"
#ifdef WITH_WAF
#include "security/context.h"
#endif
#include "string_util.h"
namespace datadog {
namespace nginx {
DatadogContext::DatadogContext(ngx_http_request_t *request,
ngx_http_core_loc_conf_t *core_loc_conf,
datadog_loc_conf_t *loc_conf)
#ifdef WITH_WAF
: sec_ctx_{security::Context::maybe_create(
security::Library::max_saved_output_data(),
security::Library::apm_tracing_enabled())}
#endif
{
if (loc_conf->enable_tracing) {
traces_.emplace_back(request, core_loc_conf, loc_conf);
}
#ifdef WITH_RUM
if (loc_conf->rum_enable) {
auto *trace = find_trace(request);
if (trace != nullptr) {
auto rum_span = trace->active_span().create_child();
rum_span.set_name("rum_sdk_injection.on_rewrite_handler");
auto status = rum_ctx_.on_rewrite_handler(request);
if (status == NGX_ERROR) {
rum_span.set_error(true);
}
} else {
rum_ctx_.on_rewrite_handler(request);
}
}
#endif
}
void DatadogContext::on_change_block(ngx_http_request_t *request,
ngx_http_core_loc_conf_t *core_loc_conf,
datadog_loc_conf_t *loc_conf) {
if (loc_conf->enable_tracing) {
auto trace = find_trace(request);
if (trace != nullptr) {
trace->on_change_block(core_loc_conf, loc_conf);
} else {
// This is a new subrequest, so add a RequestTracing for it.
// TODO: Should `active_span` be `request_span` instead?
traces_.emplace_back(request, core_loc_conf, loc_conf,
&traces_[0].active_span());
}
}
}
#ifdef WITH_WAF
bool DatadogContext::on_main_req_access(ngx_http_request_t *request) {
if (!sec_ctx_) {
return false;
}
// there should only one trace at this point
dd::Span &span = single_trace().active_span();
return sec_ctx_->on_request_start(*request, span);
}
#endif
ngx_int_t DatadogContext::on_header_filter(ngx_http_request_t *request) {
auto *loc_conf = static_cast<datadog_loc_conf_t *>(
ngx_http_get_module_loc_conf(request, ngx_http_datadog_module));
if (loc_conf == nullptr) {
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"on_header_filter failed: could not get loc conf");
return ngx_http_next_header_filter(request);
}
#if defined(WITH_RUM) || defined(WITH_WAF)
RequestTracing *trace{};
#endif
#ifdef WITH_RUM
if (loc_conf->rum_enable) {
trace = find_trace(request);
if (trace != nullptr) {
auto rum_span = trace->active_span().create_child();
rum_span.set_name("rum_sdk_injection.on_header");
auto status = rum_ctx_.on_header_filter(request, loc_conf,
ngx_http_next_header_filter);
if (status == NGX_ERROR) {
rum_span.set_error(true);
}
} else {
rum_ctx_.on_header_filter(request, loc_conf, ngx_http_next_header_filter);
}
}
#elif WITH_WAF
if (sec_ctx_) {
trace = find_trace(request);
}
#endif
#ifdef WITH_WAF
if (sec_ctx_ && trace) {
dd::Span &span = trace->active_span();
return sec_ctx_->header_filter(*request, span);
}
#endif
return ngx_http_next_header_filter(request);
}
#ifdef WITH_WAF
ngx_int_t DatadogContext::request_body_filter(ngx_http_request_t *request,
ngx_chain_t *chain) {
if (!sec_ctx_) {
return ngx_http_next_request_body_filter(request, chain);
}
auto *trace = find_trace(request);
if (trace == nullptr) {
throw std::runtime_error{
"request_body_filter: could not find request trace"};
}
dd::Span &span = trace->active_span();
return sec_ctx_->request_body_filter(*request, chain, span);
}
#endif
ngx_int_t DatadogContext::on_output_body_filter(ngx_http_request_t *request,
ngx_chain_t *chain) {
auto *loc_conf = static_cast<datadog_loc_conf_t *>(
ngx_http_get_module_loc_conf(request, ngx_http_datadog_module));
if (loc_conf == nullptr) {
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"on_output_body_filter failed: could not get loc conf");
return ngx_http_next_output_body_filter(request, chain);
}
#ifdef WITH_WAF
if (!sec_ctx_) {
return ngx_http_next_output_body_filter(request, chain);
}
auto *trace = find_trace(request);
if (trace == nullptr) {
throw std::runtime_error{
"main_output_body_filter: could not find request trace"};
}
dd::Span &span = trace->active_span();
return sec_ctx_->output_body_filter(*request, chain, span);
#endif
#ifdef WITH_RUM
// TODO: If WAF is blocking, no need to inject the RUM SDK.
if (loc_conf->rum_enable) {
auto *trace = find_trace(request);
if (trace != nullptr) {
auto rum_span = trace->active_span().create_child();
rum_span.set_name("rum_sdk_injection.on_body_filter");
rum_span.set_tag("configuration.length",
std::to_string(loc_conf->rum_snippet->length));
auto status = rum_ctx_.on_body_filter(request, loc_conf, chain,
ngx_http_next_output_body_filter);
if (status == NGX_ERROR) {
rum_span.set_error(true);
}
return status;
} else {
return rum_ctx_.on_body_filter(request, loc_conf, chain,
ngx_http_next_output_body_filter);
}
}
#endif
return ngx_http_next_output_body_filter(request, chain);
}
void DatadogContext::on_log_request(ngx_http_request_t *request) {
auto *loc_conf = static_cast<datadog_loc_conf_t *>(
ngx_http_get_module_loc_conf(request, ngx_http_datadog_module));
if (loc_conf == nullptr) {
throw std::runtime_error{"on_log_request failed: could not get loc conf"};
}
#ifdef WITH_RUM
if (loc_conf->rum_enable) {
rum_ctx_.on_log_request(request);
}
#endif
if (!loc_conf->enable_tracing) {
return;
}
auto trace = find_trace(request);
if (trace == nullptr) {
throw std::runtime_error{
"on_log_request failed: could not find request trace"};
}
trace->on_log_request();
#ifdef WITH_WAF
if (sec_ctx_ && request == request->main) {
sec_ctx_->on_main_log_request(*request, trace->active_span());
}
#endif
}
ngx_str_t DatadogContext::lookup_span_variable_value(
ngx_http_request_t *request, std::string_view key) {
auto trace = find_trace(request);
if (trace == nullptr) {
throw std::runtime_error{
"lookup_span_variable_value failed: could not find request trace"};
}
return trace->lookup_span_variable_value(key);
}
RequestTracing *DatadogContext::find_trace(ngx_http_request_t *request) {
const auto found = std::find_if(
traces_.begin(), traces_.end(),
[=](const auto &trace) { return trace.request() == request; });
if (found != traces_.end()) {
return &*found;
}
return nullptr;
}
RequestTracing &DatadogContext::single_trace() {
if (traces_.size() != 1) {
throw std::runtime_error{"Expected there to be exactly one trace"};
}
return traces_[0];
}
const RequestTracing *DatadogContext::find_trace(
ngx_http_request_t *request) const {
return const_cast<DatadogContext *>(this)->find_trace(request);
}
static void cleanup_datadog_context(void *data) noexcept {
delete static_cast<DatadogContext *>(data);
}
static ngx_pool_cleanup_t *find_datadog_cleanup(ngx_http_request_t *request) {
for (auto cleanup = request->pool->cleanup; cleanup;
cleanup = cleanup->next) {
if (cleanup->handler == cleanup_datadog_context) {
return cleanup;
}
}
return nullptr;
}
DatadogContext *get_datadog_context(ngx_http_request_t *request) noexcept {
auto context = static_cast<DatadogContext *>(
ngx_http_get_module_ctx(request, ngx_http_datadog_module));
if (context != nullptr || !request->internal) {
return context;
}
// If this is an internal redirect, the DatadogContext will have been
// reset, but we can still recover it from the cleanup handler.
//
// See set_datadog_context below.
auto cleanup = find_datadog_cleanup(request);
if (cleanup != nullptr) {
context = static_cast<DatadogContext *>(cleanup->data);
}
// If we found a context, attach with ngx_http_set_ctx so that we don't have
// to loop through the cleanup handlers again.
if (context != nullptr) {
ngx_http_set_ctx(request, static_cast<void *>(context),
ngx_http_datadog_module);
}
return context;
}
// Attaches an DatadogContext to a request.
//
// Note that internal redirects for nginx will clear any data attached via
// ngx_http_set_ctx. Since DatadogContext needs to persist across
// redirection, as a workaround the context is stored in a cleanup handler where
// it can be later recovered.
//
// See the discussion in
// https://forum.nginx.org/read.php?29,272403,272403#msg-272403
// or the approach taken by the standard nginx realip module.
void set_datadog_context(ngx_http_request_t *request, DatadogContext *context) {
auto cleanup = ngx_pool_cleanup_add(request->pool, 0);
if (cleanup == nullptr) {
delete context;
throw std::runtime_error{"failed to allocate cleanup handler"};
}
cleanup->data = static_cast<void *>(context);
cleanup->handler = cleanup_datadog_context;
ngx_http_set_ctx(request, static_cast<void *>(context),
ngx_http_datadog_module);
}
// Supports early destruction of the DatadogContext (in case of an
// unrecoverable error).
void destroy_datadog_context(ngx_http_request_t *request) noexcept {
auto cleanup = find_datadog_cleanup(request);
if (cleanup == nullptr) {
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Unable to find Datadog cleanup handler for request %p",
request);
return;
}
delete static_cast<DatadogContext *>(cleanup->data);
cleanup->data = nullptr;
ngx_http_set_ctx(request, nullptr, ngx_http_datadog_module);
}
} // namespace nginx
} // namespace datadog