Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c948864

Browse files
authoredMar 6, 2025··
chore: deprecate sampling delegation (#179)
1 parent cd9793b commit c948864

31 files changed

+2
-1473
lines changed
 

‎doc/API.md

-37
Original file line numberDiff line numberDiff line change
@@ -180,27 +180,6 @@ whose value is the result of evaluating the specified `<value>` in the context
180180
of the current request. `<value>` is a string that may contain
181181
`$`-[variables][2] (including those provided by this module).
182182

183-
### `datadog_delegate_sampling`
184-
- **syntax** `datadog_delegate_sampling [on|off]`
185-
- **default** `off`
186-
- **context** `http`, `server`, `location`
187-
188-
If `on`, and if nginx is making the trace sampling decision (i.e. if nginx is
189-
the first service in the trace), then delegate the sampling decision to the
190-
upstream service. nginx will make a provisional sampling decision, and convey
191-
it along with the intention to delegate to the upstream. The upstream service
192-
might then make its own sampling decision and convey that decision back in the
193-
response. If the upstream does so, then nginx will use the upstream's sampling
194-
decision instead of the provisional decision.
195-
196-
Sampling delegation exists to allow nginx to act as a reverse proxy for multiple
197-
different services, where the trace sampling decision can be better made within
198-
the service than it can within nginx.
199-
200-
Sampling delegation is `off` by default. The directive's argument can be a
201-
variable expression that evaluates to either of `on` or `off`. If the
202-
directive's argument is omitted, then it is as if it were `on`.
203-
204183
### `datadog_tracing`
205184
- **syntax** `datadog_tracing on|off`
206185
- **default** `on`
@@ -328,22 +307,6 @@ request.
328307

329308
This option is `off` by default, so that only one span is produced per request.
330309

331-
### `datadog_allow_sampling_delegation_in_subrequests`
332-
333-
- **syntax** `datadog_allow_sampling_delegation_in_subrequests [on|off]`
334-
- **default**: `off`
335-
- **context**: `http`, `server`, `location`
336-
337-
If `on`, then honor `datadog_delegate_sampling` directives in contexts where
338-
nginx is making a subrequest, e.g. with the `auth_request` directive.
339-
340-
This option is `off` by default, so that sampling delegation is not performed
341-
for subrequests.
342-
343-
Note that in addition to this directive, the nginx configuration must also
344-
contain the `log_subrequest on;` directive in order for tracing to be enabled
345-
for subrequests.
346-
347310
### `datadog_appsec_enabled` (AppSec builds)
348311

349312
- **syntax** `datadog_appsec_enabled [on|off]`

‎src/datadog_conf.h

-20
Original file line numberDiff line numberDiff line change
@@ -208,26 +208,6 @@ struct datadog_loc_conf_t {
208208
// The oldest ancestor (the `http` block) has `depth` zero. Each subsequent
209209
// generation has the `depth` of its parent plus one.
210210
int depth;
211-
// If "on", then sampling decisions will be delegated to the upstream at this
212-
// location. If "off", then not.
213-
ngx_flag_t sampling_delegation_enabled = NGX_CONF_UNSET;
214-
// `sampling_delegation_directive` is the source location of the
215-
// `datadog_delegate_sampling` directive that applies this location, if any.
216-
conf_directive_source_location_t sampling_delegation_directive;
217-
// `allow_sampling_delegation_in_subrequests_script` evaluates to one of "on"
218-
// or "off". If "on", then locations used as subrequests, such as those
219-
// created by the `ngx_http_auth_request_module`, can delegate the trace
220-
// sampling decision to their upstream if so configured (e.g. by a
221-
// `datadog_delegate_sampling` directive at that location or in an enclosing
222-
// configuration context). If "off", then sampling delegation will not be
223-
// performed for subrequests in affected locations, even if those locations
224-
// are configured to delegate sampling.
225-
ngx_flag_t allow_sampling_delegation_in_subrequests = NGX_CONF_UNSET;
226-
// `allow_sampling_delegation_in_subrequests_directive` is the source location
227-
// of the `datadog_allow_sampling_delegation_in_subrequests` directive that
228-
// applies this location, if any.
229-
conf_directive_source_location_t
230-
allow_sampling_delegation_in_subrequests_directive;
231211

232212
#ifdef WITH_WAF
233213
// the thread pool used to run the WAF on

‎src/datadog_directive.cpp

-39
Original file line numberDiff line numberDiff line change
@@ -363,45 +363,6 @@ char *set_datadog_agent_url(ngx_conf_t *cf, ngx_command_t *command,
363363
return NGX_OK;
364364
}
365365

366-
char *hijack_auth_request(ngx_conf_t *cf, ngx_command_t *command,
367-
void *conf) noexcept try {
368-
// Call the underlying directive handler, and then insert the following:
369-
//
370-
// auth_request_set $datadog_dummy_variable $datadog_auth_request_hook;
371-
//
372-
// The handler for $datadog_auth_request_hook will expose the current span to
373-
// any sampling-delegation-relevant response headers from the subrequest. It
374-
// must be evaluated within `auth_request_set` so that the
375-
// `$upstream_http_...` variables refer to the subrequest's response, as
376-
// opposed to the main upstream request's response.
377-
ngx_int_t rcode =
378-
datadog_conf_handler({.conf = cf, .skip_this_module = true});
379-
if (rcode != NGX_OK) {
380-
return static_cast<char *>(NGX_CONF_ERROR);
381-
}
382-
383-
ngx_str_t args[] = {ngx_string("auth_request_set"),
384-
ngx_string("$datadog_dummy_variable"),
385-
ngx_string("$datadog_auth_request_hook")};
386-
ngx_array_t args_array;
387-
args_array.elts = static_cast<void *>(&args);
388-
args_array.nelts = sizeof args / sizeof args[0];
389-
390-
auto old_args = cf->args;
391-
cf->args = &args_array;
392-
const auto guard = defer([&]() { cf->args = old_args; });
393-
394-
rcode = datadog_conf_handler({.conf = cf, .skip_this_module = true});
395-
if (rcode != NGX_OK) {
396-
return static_cast<char *>(NGX_CONF_ERROR);
397-
}
398-
return static_cast<char *>(NGX_CONF_OK);
399-
} catch (const std::exception &e) {
400-
ngx_log_error(NGX_LOG_ERR, cf->log, 0, "hijack_auth_request failed: %s",
401-
e.what());
402-
return static_cast<char *>(NGX_CONF_ERROR);
403-
}
404-
405366
char *warn_deprecated_command_1_2_0(ngx_conf_t *cf, ngx_command_t * /*command*/,
406367
void * /*conf*/) noexcept {
407368
const auto elements = static_cast<ngx_str_t *>(cf->args->elts);

‎src/datadog_directive.h

-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ char *set_datadog_propagation_styles(ngx_conf_t *cf, ngx_command_t *command,
3737

3838
char *set_datadog_agent_url(ngx_conf_t *, ngx_command_t *, void *conf) noexcept;
3939

40-
char *hijack_auth_request(ngx_conf_t *cf, ngx_command_t *command,
41-
void *conf) noexcept;
42-
4340
char *warn_deprecated_command_1_2_0(ngx_conf_t *cf, ngx_command_t * /*command*/,
4441
void * /*conf*/) noexcept;
4542

‎src/ngx_http_datadog_module.cpp

-30
Original file line numberDiff line numberDiff line change
@@ -272,28 +272,6 @@ static ngx_command_t datadog_commands[] = {
272272
0,
273273
nullptr},
274274

275-
{ ngx_string("datadog_delegate_sampling"),
276-
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1 | NGX_CONF_NOARGS,
277-
ngx_conf_set_flag_slot,
278-
NGX_HTTP_LOC_CONF_OFFSET,
279-
offsetof(datadog_loc_conf_t, sampling_delegation_enabled),
280-
nullptr},
281-
282-
{ ngx_string("datadog_allow_sampling_delegation_in_subrequests"),
283-
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1 | NGX_CONF_NOARGS,
284-
ngx_conf_set_flag_slot,
285-
NGX_HTTP_LOC_CONF_OFFSET,
286-
offsetof(datadog_loc_conf_t, allow_sampling_delegation_in_subrequests),
287-
nullptr},
288-
289-
// based on ngx_http_auth_request_module.c
290-
{ ngx_string("auth_request"),
291-
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
292-
hijack_auth_request,
293-
NGX_HTTP_LOC_CONF_OFFSET,
294-
0,
295-
NULL },
296-
297275
#ifdef WITH_WAF
298276
{
299277
ngx_string("datadog_waf_thread_pool_name"),
@@ -825,14 +803,6 @@ static char *merge_datadog_loc_conf(ngx_conf_t *cf, void *parent,
825803
}
826804
}
827805

828-
ngx_conf_merge_value(conf->sampling_delegation_enabled,
829-
prev->sampling_delegation_enabled, 0);
830-
ngx_conf_merge_value(conf->allow_sampling_delegation_in_subrequests,
831-
prev->allow_sampling_delegation_in_subrequests, 0);
832-
conf->sampling_delegation_directive = prev->sampling_delegation_directive;
833-
conf->allow_sampling_delegation_in_subrequests_directive =
834-
prev->allow_sampling_delegation_in_subrequests_directive;
835-
836806
#ifdef WITH_WAF
837807
if (conf->waf_pool == nullptr) {
838808
conf->waf_pool = prev->waf_pool;

‎src/request_tracing.cpp

+2-28
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,6 @@ namespace datadog {
2727
namespace nginx {
2828
namespace {
2929

30-
bool should_delegate(ngx_http_request_t *request,
31-
datadog_loc_conf_t *loc_conf) {
32-
if (request->parent != nullptr) {
33-
return loc_conf->sampling_delegation_enabled;
34-
}
35-
36-
// `request` is a subrequest, sampling delegated must be allowed for
37-
// subrequest.
38-
return loc_conf->sampling_delegation_enabled &&
39-
loc_conf->allow_sampling_delegation_in_subrequests;
40-
}
41-
4230
std::optional<std::string> eval_complex_value(ngx_http_complex_value_t *conf,
4331
ngx_http_request_t *request_) {
4432
if (conf == nullptr) return std::nullopt;
@@ -268,14 +256,10 @@ RequestTracing::RequestTracing(ngx_http_request_t *request,
268256
set_sample_rate_tag(request_, loc_conf_, *request_span_);
269257

270258
// Inject the active span
271-
dd::InjectionOptions injection_opts;
272-
injection_opts.delegate_sampling_decision =
273-
should_delegate(request_, loc_conf_);
274-
275259
NgxHeaderWriter writer(request_);
276260
auto &span = active_span();
277261
span.set_tag("span.kind", "client");
278-
span.inject(writer, injection_opts);
262+
span.inject(writer);
279263
}
280264

281265
void RequestTracing::on_change_block(ngx_http_core_loc_conf_t *core_loc_conf,
@@ -304,14 +288,10 @@ void RequestTracing::on_change_block(ngx_http_core_loc_conf_t *core_loc_conf,
304288
set_sample_rate_tag(request_, loc_conf_, *request_span_);
305289

306290
// Inject the active span
307-
dd::InjectionOptions injection_opts;
308-
injection_opts.delegate_sampling_decision =
309-
should_delegate(request_, loc_conf);
310-
311291
NgxHeaderWriter writer(request_);
312292
auto &span = active_span();
313293
span.set_tag("span.kind", "client");
314-
span.inject(writer, injection_opts);
294+
span.inject(writer);
315295
}
316296

317297
dd::Span &RequestTracing::active_span() {
@@ -366,12 +346,6 @@ void RequestTracing::on_log_request() {
366346

367347
request_span_->set_end_time(finish_timestamp);
368348

369-
if (should_delegate(request_, loc_conf_)) {
370-
NgxHeaderReader reader(&request_->headers_out.headers);
371-
auto delegated = request_span_->read_sampling_delegation_response(reader);
372-
if (!delegated.if_error()) return;
373-
}
374-
375349
// We care about sampling rules for the request span only, because it's the
376350
// only span that could be the root span.
377351
set_sample_rate_tag(request_, loc_conf_, *request_span_);

‎test/cases/configuration/conf/duplicate/environment.conf

-24
This file was deleted.

‎test/cases/configuration/conf/duplicate/service_name.conf

-24
This file was deleted.

‎test/cases/configuration/conf/duplicate/version.conf

-24
This file was deleted.

‎test/cases/sampling_delegation/README.md

-62
This file was deleted.

‎test/cases/sampling_delegation/conf/add_header_in_http.conf

-29
This file was deleted.

‎test/cases/sampling_delegation/conf/add_header_in_http_location.conf

-31
This file was deleted.

‎test/cases/sampling_delegation/conf/add_header_in_http_server.conf

-31
This file was deleted.

‎test/cases/sampling_delegation/conf/add_header_in_http_server_location.conf

-33
This file was deleted.

‎test/cases/sampling_delegation/conf/add_header_in_location.conf

-29
This file was deleted.

‎test/cases/sampling_delegation/conf/add_header_in_server.conf

-29
This file was deleted.

‎test/cases/sampling_delegation/conf/add_header_in_server_location.conf

-31
This file was deleted.

‎test/cases/sampling_delegation/conf/auth.conf

-29
This file was deleted.

‎test/cases/sampling_delegation/conf/nginx.template.conf

-34
This file was deleted.

‎test/cases/sampling_delegation/conf/nginx1.conf

-30
This file was deleted.

‎test/cases/sampling_delegation/conf/nginx2.conf

-34
This file was deleted.

‎test/cases/sampling_delegation/conf/nginx3.conf

-32
This file was deleted.

‎test/cases/sampling_delegation/conf/upstream.conf

-28
This file was deleted.

‎test/cases/sampling_delegation/diagrams/Makefile

-5
This file was deleted.

‎test/cases/sampling_delegation/diagrams/auth.dot

-31
This file was deleted.

‎test/cases/sampling_delegation/diagrams/auth.svg

-105
This file was deleted.

‎test/cases/sampling_delegation/diagrams/delegation.dot

-29
This file was deleted.

‎test/cases/sampling_delegation/diagrams/delegation.svg

-105
This file was deleted.

‎test/cases/sampling_delegation/test_add_header.py

-78
This file was deleted.

‎test/cases/sampling_delegation/test_auth_request.py

-291
This file was deleted.

‎test/cases/sampling_delegation/test_sampling_delegation.py

-138
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.