From 55bcfe7b1f138777568b159a28ea1937512b5484 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20=C3=81lvarez?= Date: Mon, 11 May 2020 08:55:49 +0200 Subject: [PATCH] Disable tracer by default (#18339) (#18354) --- libbeat/cmd/instance/beat.go | 13 +++++++++++-- libbeat/cmd/instance/beat_test.go | 11 +++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) 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()) +}