Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datadog: sample_rate omitted by default #1

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions internal/ingress/controller/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,16 +706,9 @@ type Configuration struct {
// Default: nginx.handle
DatadogOperationNameOverride string `json:"datadog-operation-name-override"`

// DatadogPrioritySampling specifies to use client-side sampling
// If true disables client-side sampling (thus ignoring sample_rate) and enables distributed
// priority sampling, where traces are sampled based on a combination of user-assigned
// Default: true
DatadogPrioritySampling bool `json:"datadog-priority-sampling"`

// DatadogSampleRate specifies sample rate for any traces created.
// This is effective only when datadog-priority-sampling is false
// Default: 1.0
DatadogSampleRate float32 `json:"datadog-sample-rate"`
// Default: use a dynamic rate instead
DatadogSampleRate *float32 `json:"datadog-sample-rate",omitempty`
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is configuration ever printed/serialized after being parsed into this struct? If so, non-nil DatadogSampleRate would probably render as the pointer value, which is not what we want.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't look like it.


// MainSnippet adds custom configuration to the main section of the nginx configuration
MainSnippet string `json:"main-snippet"`
Expand Down Expand Up @@ -1001,8 +994,7 @@ func NewDefault() Configuration {
DatadogEnvironment: "prod",
DatadogCollectorPort: 8126,
DatadogOperationNameOverride: "nginx.handle",
DatadogSampleRate: 1.0,
DatadogPrioritySampling: true,
DatadogSampleRate: nil,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be needed, except for consistency

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's here for consistency.

LimitReqStatusCode: 503,
LimitConnStatusCode: 503,
SyslogPort: 514,
Expand Down
72 changes: 45 additions & 27 deletions internal/ingress/controller/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -1051,16 +1051,6 @@ const jaegerTmpl = `{
}
}`

const datadogTmpl = `{
"service": "{{ .DatadogServiceName }}",
"agent_host": "{{ .DatadogCollectorHost }}",
"agent_port": {{ .DatadogCollectorPort }},
"environment": "{{ .DatadogEnvironment }}",
"operation_name_override": "{{ .DatadogOperationNameOverride }}",
"sample_rate": {{ .DatadogSampleRate }},
"dd.priority.sampling": {{ .DatadogPrioritySampling }}
}`

const otelTmpl = `
exporter = "otlp"
processor = "batch"
Expand All @@ -1084,37 +1074,65 @@ ratio = {{ .OtelSamplerRatio }}
parent_based = {{ .OtelSamplerParentBased }}
`

func datadogOpentracingCfg(cfg ngx_config.Configuration) (string, error) {
m := map[string]interface{}{
"service": cfg.DatadogServiceName,
"agent_host": cfg.DatadogCollectorHost,
"agent_port": cfg.DatadogCollectorPort,
"environment": cfg.DatadogEnvironment,
"operation_name_override": cfg.DatadogOperationNameOverride,
}

// Omit "sample_rate" if the configuration's sample rate is unset (nil).
// Omitting "sample_rate" from the plugin JSON indicates to the tracer that
// it should use dynamic rates instead of a configured rate.
if cfg.DatadogSampleRate != nil {
m["sample_rate"] = *cfg.DatadogSampleRate
}

buf, err := json.Marshal(m)
if err != nil {
return "", err
}

return string(buf), nil
}

func opentracingCfgFromTemplate(cfg ngx_config.Configuration, tmplName string, tmplText string) (string, error) {
tmpl, err := template.New(tmplName).Parse(tmplText)
if err != nil {
return "", err
}

tmplBuf := bytes.NewBuffer(make([]byte, 0))
err = tmpl.Execute(tmplBuf, cfg)
if err != nil {
return "", err
}

return tmplBuf.String(), nil
}

func createOpentracingCfg(cfg ngx_config.Configuration) error {
var tmpl *template.Template
var configData string
var err error

if cfg.ZipkinCollectorHost != "" {
tmpl, err = template.New("zipkin").Parse(zipkinTmpl)
if err != nil {
return err
}
configData, err = opentracingCfgFromTemplate(cfg, "zipkin", zipkinTmpl)
} else if cfg.JaegerCollectorHost != "" || cfg.JaegerEndpoint != "" {
tmpl, err = template.New("jaeger").Parse(jaegerTmpl)
if err != nil {
return err
}
configData, err = opentracingCfgFromTemplate(cfg, "jaeger", jaegerTmpl)
} else if cfg.DatadogCollectorHost != "" {
tmpl, err = template.New("datadog").Parse(datadogTmpl)
if err != nil {
return err
}
configData, err = datadogOpentracingCfg(cfg)
} else {
tmpl, _ = template.New("empty").Parse("{}")
configData = "{}"
}

tmplBuf := bytes.NewBuffer(make([]byte, 0))
err = tmpl.Execute(tmplBuf, cfg)
if err != nil {
return err
}

// Expand possible environment variables before writing the configuration to file.
expanded := os.ExpandEnv(tmplBuf.String())
expanded := os.ExpandEnv(configData)

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