Skip to content

Commit

Permalink
feat(sumologicextension): use hostname as default collector name
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikołaj Świątek committed Jan 31, 2023
1 parent e15032e commit 2d69b2b
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 32 deletions.
1 change: 0 additions & 1 deletion pkg/exporter/sumologicexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down
3 changes: 1 addition & 2 deletions pkg/extension/sumologicextension/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"time"

"github.com/cenkalti/backoff/v4"
"github.com/google/uuid"
ps "github.com/mitchellh/go-ps"
"github.com/shirou/gopsutil/v3/host"
"go.opentelemetry.io/collector/component"
Expand Down Expand Up @@ -130,7 +129,7 @@ func newSumologicExtension(conf *Config, logger *zap.Logger, id component.ID, bu
// and that we can reuse collector name save in credentials store.
if creds, err := credentialsStore.Get(hashKey); err != nil {
// If credentials file is not stored on filesystem generate collector name
collectorName = fmt.Sprintf("%s-%s", hostname, uuid.New())
collectorName = hostname
} else {
collectorName = creds.CollectorName
}
Expand Down
34 changes: 8 additions & 26 deletions pkg/extension/sumologicextension/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ import (
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path"
"regexp"
"sync/atomic"
"testing"
"time"
Expand All @@ -41,10 +39,6 @@ import (
"github.com/SumoLogic/sumologic-otel-collector/pkg/extension/sumologicextension/credentials"
)

const (
uuidRegex = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
)

func TestMain(m *testing.M) {
// Enable the feature gates before all tests to avoid flaky tests.
err := featuregate.GlobalRegistry().Apply(map[string]bool{
Expand Down Expand Up @@ -588,10 +582,8 @@ func TestRegisterEmptyCollectorName(t *testing.T) {
se, err := newSumologicExtension(cfg, zap.NewNop(), component.NewID("sumologic"), "1.0.0")
require.NoError(t, err)
require.NoError(t, se.Start(context.Background(), componenttest.NewNopHost()))
regexPattern := fmt.Sprintf("%s-%s", hostname, uuidRegex)
matched, err := regexp.MatchString(regexPattern, se.collectorName)
require.NoError(t, err)
assert.True(t, matched)
require.Equal(t, hostname, se.collectorName)
}

func TestRegisterEmptyCollectorNameForceRegistration(t *testing.T) {
Expand Down Expand Up @@ -680,10 +672,7 @@ func TestRegisterEmptyCollectorNameForceRegistration(t *testing.T) {
require.NoError(t, se.Start(context.Background(), componenttest.NewNopHost()))
require.NoError(t, se.Shutdown(context.Background()))
assert.NotEmpty(t, se.collectorName)
regexPattern := fmt.Sprintf("%s-%s", hostname, uuidRegex)
matched, err := regexp.MatchString(regexPattern, se.collectorName)
require.NoError(t, err)
assert.True(t, matched)
assert.Equal(t, hostname, se.collectorName)
colCreds, err := se.credentialsStore.Get(se.hashKey)
require.NoError(t, err)
colName := colCreds.CollectorName
Expand Down Expand Up @@ -1005,10 +994,7 @@ func TestRegisterEmptyCollectorNameWithBackoff(t *testing.T) {
se, err := newSumologicExtension(cfg, zap.NewNop(), component.NewID("sumologic"), "1.0.0")
require.NoError(t, err)
require.NoError(t, se.Start(context.Background(), componenttest.NewNopHost()))
regexPattern := fmt.Sprintf("%s-%s", hostname, uuidRegex)
matched, err := regexp.MatchString(regexPattern, se.collectorName)
require.NoError(t, err)
assert.True(t, matched)
require.Equal(t, hostname, se.collectorName)
}

func TestRegisterEmptyCollectorNameUnrecoverableError(t *testing.T) {
Expand Down Expand Up @@ -1058,10 +1044,7 @@ func TestRegisterEmptyCollectorNameUnrecoverableError(t *testing.T) {
require.NoError(t, err)
require.EqualError(t, se.Start(context.Background(), componenttest.NewNopHost()),
"collector registration failed: failed to register the collector, got HTTP status code: 404")
regexPattern := fmt.Sprintf("%s-%s", hostname, uuidRegex)
matched, err := regexp.MatchString(regexPattern, se.collectorName)
require.NoError(t, err)
assert.True(t, matched)
require.Equal(t, hostname, se.collectorName)
}

func TestRegistrationRedirect(t *testing.T) {
Expand Down Expand Up @@ -1133,7 +1116,7 @@ func TestRegistrationRedirect(t *testing.T) {
// register
case 1:
require.Equal(t, registerUrl, req.URL.Path)
http.Redirect(w, req, destSrv.URL, 301)
http.Redirect(w, req, destSrv.URL, http.StatusMovedPermanently)

// should not produce any more requests
default:
Expand Down Expand Up @@ -1369,10 +1352,7 @@ func TestRegistrationRequestPayload(t *testing.T) {
se, err := newSumologicExtension(cfg, zap.NewNop(), component.NewID("sumologic"), "1.0.0")
require.NoError(t, err)
require.NoError(t, se.Start(context.Background(), componenttest.NewNopHost()))
regexPattern := fmt.Sprintf("%s-%s", hostname, uuidRegex)
matched, err := regexp.MatchString(regexPattern, se.collectorName)
require.NoError(t, err)
assert.True(t, matched)
require.Equal(t, hostname, se.collectorName)

require.NoError(t, se.Shutdown(context.Background()))
}
Expand All @@ -1394,6 +1374,8 @@ func TestWatchCredentialKey(t *testing.T) {

go func() {
time.Sleep(time.Millisecond * 100)
se.credsNotifyLock.Lock()
defer se.credsNotifyLock.Unlock()
se.registrationInfo.CollectorCredentialKey = "test-credential-key"
close(se.credsNotifyUpdate)
}()
Expand Down
1 change: 0 additions & 1 deletion pkg/extension/sumologicextension/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.18

require (
github.com/cenkalti/backoff/v4 v4.2.0
github.com/google/uuid v1.3.0
github.com/hashicorp/go-multierror v1.1.1
github.com/mitchellh/go-ps v1.0.0
github.com/shirou/gopsutil/v3 v3.22.12
Expand Down
2 changes: 0 additions & 2 deletions pkg/extension/sumologicextension/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/consul/api v1.13.0/go.mod h1:ZlVrynguJKcYr54zGaDbaL3fOvKC9m72FhPvA8T35KQ=
Expand Down

0 comments on commit 2d69b2b

Please sign in to comment.