Skip to content

Commit

Permalink
Move everything to the beat package
Browse files Browse the repository at this point in the history
  • Loading branch information
jalvz committed Jun 3, 2020
1 parent bd791f7 commit 9bda5dc
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 38 deletions.
5 changes: 5 additions & 0 deletions libbeat/beat/instrumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ import (
"github.com/elastic/beats/v7/libbeat/pipe"
)

func init() {
// we need to close the default tracer to prevent the beat sending events to localhost:8200
apm.DefaultTracer.Close()
}

// Instrumentation holds an APM tracer a net.Listener
type Instrumentation struct {
tracer *apm.Tracer
Expand Down
77 changes: 77 additions & 0 deletions libbeat/beat/instrumentation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package beat

import (
"net/url"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/version"
)

func TestAPMTracerDisabledByDefault(t *testing.T) {
b := Beat{
Info: Info{
Beat: "my-beat",
IndexPrefix: "my-beat-*",
Version: version.GetDefaultVersion(),
Name: "my-beat",
},
}
tracer := b.Instrumentation.GetTracer()
require.NotNil(t, tracer)
defer tracer.Close()
assert.False(t, tracer.Active())
}

func TestAPMInstrumentationConfig(t *testing.T) {
cfg := common.MustNewConfigFrom(map[string]interface{}{
"instrumentation": map[string]interface{}{
"enabled": "true",
},
})
instrumentation, err := CreateInstrumentation(cfg, Info{Name: "my-beat", Version: version.GetDefaultVersion()})
require.NoError(t, err)

assert.NotNil(t, instrumentation.Listener)

tracer := instrumentation.GetTracer()
defer tracer.Close()
assert.True(t, tracer.Active())
}

func TestAPMInstrumentationExplicitHosts(t *testing.T) {
cfg := common.MustNewConfigFrom(map[string]interface{}{
"instrumentation": map[string]interface{}{
"enabled": "true",
"hosts": []url.URL{{
Scheme: "http",
Host: "localhost:8200",
}},
},
})
instrumentation, err := CreateInstrumentation(cfg, Info{Name: "my-beat", Version: version.GetDefaultVersion()})
require.NoError(t, err)
defer instrumentation.GetTracer().Close()

assert.Nil(t, instrumentation.Listener)
}
8 changes: 0 additions & 8 deletions libbeat/cmd/instance/beat.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ import (
"strings"
"time"

"go.elastic.co/apm"

"github.com/gofrs/uuid"
errw "github.com/pkg/errors"
"go.uber.org/zap"
Expand Down Expand Up @@ -124,7 +122,6 @@ var debugf = logp.MakeDebug("beat")

func init() {
initRand()
preventDefaultTracing()
}

// initRand initializes the runtime random number generator seed using
Expand All @@ -144,11 +141,6 @@ func initRand() {
rand.Seed(seed)
}

func preventDefaultTracing() {
// 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
Expand Down
30 changes: 0 additions & 30 deletions libbeat/cmd/instance/beat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ import (
"os"
"testing"

"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/common"

"github.com/stretchr/testify/require"

"github.com/elastic/beats/v7/libbeat/cfgfile"

"github.com/gofrs/uuid"
Expand Down Expand Up @@ -120,28 +115,3 @@ 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) {
b, err := NewBeat("filebeat", "testidx", "0.9")
require.NoError(t, err)

tracer := b.Instrumentation.GetTracer()
defer tracer.Close()
assert.False(t, tracer.Active())
}

func TestAPMInstrumentationConfig(t *testing.T) {
cfg := common.MustNewConfigFrom(map[string]interface{}{
"instrumentation": map[string]interface{}{
"enabled": "true",
},
})
instrumentation, err := beat.CreateInstrumentation(cfg, beat.Info{Name: "filebeat", Version: "8.0"})
require.NoError(t, err)

assert.NotNil(t, instrumentation.Listener)

tracer := instrumentation.GetTracer()
defer tracer.Close()
assert.True(t, tracer.Active())
}

0 comments on commit 9bda5dc

Please sign in to comment.