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

[8.x](backport #5712) Fix typo in float format for APM_SAMPLING_RATE #5714

Merged
merged 1 commit into from
Oct 7, 2024
Merged
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
2 changes: 1 addition & 1 deletion internal/pkg/agent/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ func initTracer(agentName, version string, mcfg *monitoringCfg.MonitoringConfig)
defer os.Unsetenv(envCACert)
}
if cfg.SamplingRate != nil {
os.Setenv(envSampleRate, strconv.FormatFloat(float64(*cfg.SamplingRate), 'b', -1, 32))
os.Setenv(envSampleRate, strconv.FormatFloat(float64(*cfg.SamplingRate), 'f', -1, 32))
defer os.Unsetenv(envSampleRate)
}

Expand Down
95 changes: 95 additions & 0 deletions internal/pkg/agent/cmd/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License 2.0;
// you may not use this file except in compliance with the Elastic License 2.0.

package cmd

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"

monitoringCfg "github.com/elastic/elastic-agent/internal/pkg/core/monitoring/config"
)

func Test_initTracer(t *testing.T) {
tenPercentSamplingRate := float32(0.1)

type args struct {
agentName string
version string
mcfg *monitoringCfg.MonitoringConfig
}
tests := []struct {
name string
args args
want assert.ValueAssertionFunc // value assertion for *apm.Tracer returned by initTracer
wantErr assert.ErrorAssertionFunc
}{
{
name: "monitoring config disabled",
args: args{
agentName: "testagent",
version: "1.2.3",
mcfg: &monitoringCfg.MonitoringConfig{
Enabled: false,
},
},
want: assert.Nil,
wantErr: assert.NoError,
},
{
name: "monitoring config enabled but traces disabled",
args: args{
agentName: "testagent",
version: "1.2.3",
mcfg: &monitoringCfg.MonitoringConfig{
Enabled: true,
MonitorTraces: false,
},
},
want: assert.Nil,
wantErr: assert.NoError,
},
{
name: "traces enabled, no TLS",
args: args{
agentName: "testagent",
version: "1.2.3",
mcfg: &monitoringCfg.MonitoringConfig{
Enabled: true,
MonitorTraces: true,
APM: monitoringCfg.APMConfig{
Environment: "unit-test",
APIKey: "api-key",
SecretToken: "secret-token",
Hosts: []string{"localhost:8888"},
GlobalLabels: map[string]string{
"k1": "v1",
"k2": "v2",
},
TLS: monitoringCfg.APMTLS{},
SamplingRate: &tenPercentSamplingRate,
},
},
},
want: assert.NotNil,
wantErr: assert.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := initTracer(tt.args.agentName, tt.args.version, tt.args.mcfg)
if got != nil {
t.Cleanup(func() {
got.Close()
})
}
if !tt.wantErr(t, err, fmt.Sprintf("initTracer(%v, %v, %v)", tt.args.agentName, tt.args.version, tt.args.mcfg)) {
return
}
tt.want(t, got, "initTracer(%v, %v, %v)", tt.args.agentName, tt.args.version, tt.args.mcfg)
})
}
}