diff --git a/libbeat/cmd/instance/beat.go b/libbeat/cmd/instance/beat.go index 1863c3aa0ab..ca346085866 100644 --- a/libbeat/cmd/instance/beat.go +++ b/libbeat/cmd/instance/beat.go @@ -124,8 +124,7 @@ var debugf = logp.MakeDebug("beat") func init() { initRand() - // we need to close the default tracer to prevent the beat sending events to localhost:8200 - apm.DefaultTracer.Close() + preventDefaultTracing() } // initRand initializes the runtime random number generator seed using @@ -145,6 +144,16 @@ func initRand() { rand.Seed(seed) } +func preventDefaultTracing() { + // By default, the APM tracer is active. We switch behaviour to not require users to have + // an APM Server running, making it opt-in + if os.Getenv("ELASTIC_APM_ACTIVE") == "" { + os.Setenv("ELASTIC_APM_ACTIVE", "false") + } + // we need to close the default tracer to prevent the beat sending events to localhost:8200 + apm.DefaultTracer.Close() +} + // Run initializes and runs a Beater implementation. name is the name of the // Beat (e.g. packetbeat or metricbeat). version is version number of the Beater // implementation. bt is the `Creator` callback for creating a new beater diff --git a/libbeat/cmd/instance/beat_test.go b/libbeat/cmd/instance/beat_test.go index 8c04e87390f..81e4575f64e 100644 --- a/libbeat/cmd/instance/beat_test.go +++ b/libbeat/cmd/instance/beat_test.go @@ -24,6 +24,10 @@ import ( "os" "testing" + "github.com/stretchr/testify/require" + + "go.elastic.co/apm" + "github.com/elastic/beats/v7/libbeat/cfgfile" "github.com/gofrs/uuid" @@ -115,3 +119,10 @@ func TestEmptyMetaJson(t *testing.T) { assert.Equal(t, nil, err, "Unable to load meta file properly") assert.NotEqual(t, uuid.Nil, b.Info.ID, "Beats UUID is not set") } + +func TestAPMTracerDisabledByDefault(t *testing.T) { + tracer, err := apm.NewTracer("", "") + require.NoError(t, err) + defer tracer.Close() + assert.False(t, tracer.Active()) +}