1
1
#include " injection.h"
2
2
3
+ #include < datadog/telemetry/telemetry.h>
4
+
3
5
extern " C" {
4
6
#include < ngx_core.h>
5
7
}
@@ -9,15 +11,15 @@ extern "C" {
9
11
#include " datadog_conf.h"
10
12
#include " ngx_http_datadog_module.h"
11
13
#include " string_util.h"
14
+ #include " telemetry.h"
12
15
13
16
namespace datadog {
14
17
namespace nginx {
15
18
namespace rum {
16
19
namespace {
17
20
18
- ngx_table_elt_t *search_header (ngx_http_request_t *request,
19
- std::string_view key) {
20
- ngx_list_part_t *part = &request->headers_in .headers .part ;
21
+ ngx_table_elt_t *search_header (ngx_list_t &headers, std::string_view key) {
22
+ ngx_list_part_t *part = &headers.part ;
21
23
auto *h = static_cast <ngx_table_elt_t *>(part->elts );
22
24
23
25
for (std::size_t i = 0 ;; i++) {
@@ -86,32 +88,59 @@ ngx_int_t InjectionHandler::on_header_filter(
86
88
return next_header_filter (r);
87
89
}
88
90
89
- if (auto injected_header = search_header (r, " x-datadog-rum-injected" );
91
+ if (auto injected_header =
92
+ search_header (r->headers_in .headers , " x-datadog-rum-injected" );
90
93
injected_header != nullptr ) {
91
94
if (nginx::to_string_view (injected_header->value ) == " 1" ) {
92
95
ngx_log_error (NGX_LOG_INFO, r->connection ->log , 0 ,
93
96
" RUM SDK injection skipped: resource may already have RUM "
94
97
" SDK injected." );
98
+ datadog::telemetry::counter::increment (
99
+ telemetry::injection_skipped,
100
+ telemetry::build_tags (" reason:already_injected" ,
101
+ cfg->rum_application_id_tag ,
102
+ cfg->rum_remote_config_tag ));
103
+
95
104
return next_header_filter (r);
96
105
}
97
106
}
98
107
99
108
if (r->header_only || r->headers_out .content_length_n == 0 ) {
100
109
ngx_log_error (NGX_LOG_INFO, r->connection ->log , 0 ,
101
110
" RUM SDK injection skipped: empty content" );
111
+
112
+ datadog::telemetry::counter::increment (
113
+ telemetry::injection_skipped,
114
+ telemetry::build_tags (" reason:no_content" , cfg->rum_application_id_tag ,
115
+ cfg->rum_remote_config_tag ));
116
+
102
117
return next_header_filter (r);
103
118
}
104
119
105
120
if (!is_html_content (&r->headers_out .content_type )) {
106
121
ngx_log_error (NGX_LOG_INFO, r->connection ->log , 0 ,
107
122
" RUM SDK injection skipped: not an HTML page" );
123
+
124
+ datadog::telemetry::counter::increment (
125
+ telemetry::injection_skipped,
126
+ telemetry::build_tags (" reason:invalid_content_type" ,
127
+ cfg->rum_application_id_tag ,
128
+ cfg->rum_remote_config_tag ));
129
+
108
130
return next_header_filter (r);
109
131
}
110
132
111
133
if (auto content_encoding = r->headers_out .content_encoding ;
112
134
content_encoding != nullptr && content_encoding->value .len != 0 ) {
113
135
ngx_log_error (NGX_LOG_INFO, r->connection ->log , 0 ,
114
136
" RUM SDK injection skipped: compressed html content" );
137
+
138
+ datadog::telemetry::counter::increment (
139
+ telemetry::injection_skipped,
140
+ telemetry::build_tags (" reason:compressed_html" ,
141
+ cfg->rum_application_id_tag ,
142
+ cfg->rum_remote_config_tag ));
143
+
115
144
return next_header_filter (r);
116
145
}
117
146
@@ -176,6 +205,12 @@ ngx_int_t InjectionHandler::on_body_filter(
176
205
state_ = state::injected;
177
206
ngx_log_error (NGX_LOG_INFO, r->connection ->log , 0 ,
178
207
" RUM SDK injected successfully injected" );
208
+
209
+ datadog::telemetry::counter::increment (
210
+ telemetry::injection_succeed,
211
+ telemetry::build_tags (cfg->rum_application_id_tag ,
212
+ cfg->rum_remote_config_tag ));
213
+
179
214
return output (r, output_chain, next_body_filter);
180
215
}
181
216
}
@@ -192,11 +227,29 @@ ngx_int_t InjectionHandler::on_body_filter(
192
227
193
228
ngx_log_error (NGX_LOG_INFO, r->connection ->log , 0 ,
194
229
" RUM SDK injection failed: no injection point found" );
230
+
231
+ datadog::telemetry::counter::increment (
232
+ telemetry::injection_failed,
233
+ telemetry::build_tags (" reason:missing_header_tag" ,
234
+ cfg->rum_application_id_tag ,
235
+ cfg->rum_remote_config_tag ));
195
236
}
196
237
197
238
return output (r, output_chain, next_body_filter);
198
239
}
199
240
241
+ ngx_int_t InjectionHandler::on_log_request (ngx_http_request_t *r) {
242
+ if (auto csp =
243
+ search_header (r->headers_out .headers , " content-security-policy" );
244
+ csp != nullptr ) {
245
+ datadog::telemetry::counter::increment (
246
+ telemetry::injection_failed,
247
+ telemetry::build_tags (" status:seen" , " kind:header" ));
248
+ }
249
+
250
+ return NGX_OK;
251
+ }
252
+
200
253
// NOTE(@dmehala): this function is not necessary for now, however,
201
254
// it will when we will reuse buffer.
202
255
ngx_int_t InjectionHandler::output (
0 commit comments