Skip to content

Commit 6d55e1f

Browse files
authored
revise Datadog trace sampling configuration (#10151)
* datadog: sample_rate omitted by default * config: use *float32 with nil instead of float32 with sentinel value * change some names * gofmt -s -w internal/ingress/controller/nginx.go
1 parent 125b3be commit 6d55e1f

File tree

2 files changed

+48
-38
lines changed

2 files changed

+48
-38
lines changed

internal/ingress/controller/config/config.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -706,16 +706,9 @@ type Configuration struct {
706706
// Default: nginx.handle
707707
DatadogOperationNameOverride string `json:"datadog-operation-name-override"`
708708

709-
// DatadogPrioritySampling specifies to use client-side sampling
710-
// If true disables client-side sampling (thus ignoring sample_rate) and enables distributed
711-
// priority sampling, where traces are sampled based on a combination of user-assigned
712-
// Default: true
713-
DatadogPrioritySampling bool `json:"datadog-priority-sampling"`
714-
715709
// DatadogSampleRate specifies sample rate for any traces created.
716-
// This is effective only when datadog-priority-sampling is false
717-
// Default: 1.0
718-
DatadogSampleRate float32 `json:"datadog-sample-rate"`
710+
// Default: use a dynamic rate instead
711+
DatadogSampleRate *float32 `json:"datadog-sample-rate",omitempty`
719712

720713
// MainSnippet adds custom configuration to the main section of the nginx configuration
721714
MainSnippet string `json:"main-snippet"`
@@ -1001,8 +994,7 @@ func NewDefault() Configuration {
1001994
DatadogEnvironment: "prod",
1002995
DatadogCollectorPort: 8126,
1003996
DatadogOperationNameOverride: "nginx.handle",
1004-
DatadogSampleRate: 1.0,
1005-
DatadogPrioritySampling: true,
997+
DatadogSampleRate: nil,
1006998
LimitReqStatusCode: 503,
1007999
LimitConnStatusCode: 503,
10081000
SyslogPort: 514,

internal/ingress/controller/nginx.go

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,16 +1011,6 @@ const jaegerTmpl = `{
10111011
}
10121012
}`
10131013

1014-
const datadogTmpl = `{
1015-
"service": "{{ .DatadogServiceName }}",
1016-
"agent_host": "{{ .DatadogCollectorHost }}",
1017-
"agent_port": {{ .DatadogCollectorPort }},
1018-
"environment": "{{ .DatadogEnvironment }}",
1019-
"operation_name_override": "{{ .DatadogOperationNameOverride }}",
1020-
"sample_rate": {{ .DatadogSampleRate }},
1021-
"dd.priority.sampling": {{ .DatadogPrioritySampling }}
1022-
}`
1023-
10241014
const otelTmpl = `
10251015
exporter = "otlp"
10261016
processor = "batch"
@@ -1044,37 +1034,65 @@ ratio = {{ .OtelSamplerRatio }}
10441034
parent_based = {{ .OtelSamplerParentBased }}
10451035
`
10461036

1037+
func datadogOpentracingCfg(cfg ngx_config.Configuration) (string, error) {
1038+
m := map[string]interface{}{
1039+
"service": cfg.DatadogServiceName,
1040+
"agent_host": cfg.DatadogCollectorHost,
1041+
"agent_port": cfg.DatadogCollectorPort,
1042+
"environment": cfg.DatadogEnvironment,
1043+
"operation_name_override": cfg.DatadogOperationNameOverride,
1044+
}
1045+
1046+
// Omit "sample_rate" if the configuration's sample rate is unset (nil).
1047+
// Omitting "sample_rate" from the plugin JSON indicates to the tracer that
1048+
// it should use dynamic rates instead of a configured rate.
1049+
if cfg.DatadogSampleRate != nil {
1050+
m["sample_rate"] = *cfg.DatadogSampleRate
1051+
}
1052+
1053+
buf, err := json.Marshal(m)
1054+
if err != nil {
1055+
return "", err
1056+
}
1057+
1058+
return string(buf), nil
1059+
}
1060+
1061+
func opentracingCfgFromTemplate(cfg ngx_config.Configuration, tmplName string, tmplText string) (string, error) {
1062+
tmpl, err := template.New(tmplName).Parse(tmplText)
1063+
if err != nil {
1064+
return "", err
1065+
}
1066+
1067+
tmplBuf := bytes.NewBuffer(make([]byte, 0))
1068+
err = tmpl.Execute(tmplBuf, cfg)
1069+
if err != nil {
1070+
return "", err
1071+
}
1072+
1073+
return tmplBuf.String(), nil
1074+
}
1075+
10471076
func createOpentracingCfg(cfg ngx_config.Configuration) error {
1048-
var tmpl *template.Template
1077+
var configData string
10491078
var err error
10501079

10511080
if cfg.ZipkinCollectorHost != "" {
1052-
tmpl, err = template.New("zipkin").Parse(zipkinTmpl)
1053-
if err != nil {
1054-
return err
1055-
}
1081+
configData, err = opentracingCfgFromTemplate(cfg, "zipkin", zipkinTmpl)
10561082
} else if cfg.JaegerCollectorHost != "" || cfg.JaegerEndpoint != "" {
1057-
tmpl, err = template.New("jaeger").Parse(jaegerTmpl)
1058-
if err != nil {
1059-
return err
1060-
}
1083+
configData, err = opentracingCfgFromTemplate(cfg, "jaeger", jaegerTmpl)
10611084
} else if cfg.DatadogCollectorHost != "" {
1062-
tmpl, err = template.New("datadog").Parse(datadogTmpl)
1063-
if err != nil {
1064-
return err
1065-
}
1085+
configData, err = datadogOpentracingCfg(cfg)
10661086
} else {
1067-
tmpl, _ = template.New("empty").Parse("{}")
1087+
configData = "{}"
10681088
}
10691089

1070-
tmplBuf := bytes.NewBuffer(make([]byte, 0))
1071-
err = tmpl.Execute(tmplBuf, cfg)
10721090
if err != nil {
10731091
return err
10741092
}
10751093

10761094
// Expand possible environment variables before writing the configuration to file.
1077-
expanded := os.ExpandEnv(tmplBuf.String())
1095+
expanded := os.ExpandEnv(configData)
10781096

10791097
return os.WriteFile("/etc/nginx/opentracing.json", []byte(expanded), file.ReadWriteByUser)
10801098
}

0 commit comments

Comments
 (0)