From 20446ac5f4860478043f61fd20d2d4736fcdc65e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20=C5=9Awi=C4=85tek?= Date: Tue, 31 Jan 2023 15:12:57 +0100 Subject: [PATCH] feat(sumologicextension): use hostname as default collector name --- CHANGELOG.md | 9 +++++ pkg/exporter/sumologicexporter/go.mod | 1 - pkg/extension/sumologicextension/README.md | 6 ++-- pkg/extension/sumologicextension/extension.go | 7 ++-- .../sumologicextension/extension_test.go | 34 +++++-------------- pkg/extension/sumologicextension/go.mod | 1 - pkg/extension/sumologicextension/go.sum | 2 -- 7 files changed, 25 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 593a37d15d..92c721792e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +This release introduces the following breaking changes: + +- feat(sumologicextension): use hostname as default collector name [#918] + +[#918]: https://github.com/SumoLogic/sumologic-otel-collector/pull/918 +[Unreleased]: https://github.com/SumoLogic/sumologic-otel-collector/compare/v0.70.0-sumo-0...main + ## [v0.70.0-sumo-0] ### Added diff --git a/pkg/exporter/sumologicexporter/go.mod b/pkg/exporter/sumologicexporter/go.mod index 0801e2266e..404d9f6624 100644 --- a/pkg/exporter/sumologicexporter/go.mod +++ b/pkg/exporter/sumologicexporter/go.mod @@ -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 diff --git a/pkg/extension/sumologicextension/README.md b/pkg/extension/sumologicextension/README.md index 3f88cf8daf..0dd9f54b15 100644 --- a/pkg/extension/sumologicextension/README.md +++ b/pkg/extension/sumologicextension/README.md @@ -30,8 +30,7 @@ and can be used as an authenticator for the - `install_token`: (required) collector install token for the Sumo Logic service, see [help][credentials_help] for more details -- `collector_name`: name that will be used for registration; by default it is a - hostname followed by UUID +- `collector_name`: name that will be used for registration; by default the hostname is used. In the event of a conflict, a timestamp will be appended to the name. See [here][clobber] for more information. - `collector_description`: collector description that will be used for registration - `collector_category`: collector category that will be used for registration - `collector_fields`: a map of key value pairs that will be used as collector @@ -49,7 +48,7 @@ and can be used as an authenticator for the - `collector_credentials_directory`: directory where state files with registration info will be stored after successful collector registration (default: `$HOME/.sumologic-otel-collector`) -- `clobber`: defines whether to delete any existing collector with the same name +- `clobber`: defines whether to delete any existing collector with the same name. See [here][clobber] for more information. - `force_registration`: defines whether to force registration every time the collector starts. This will cause the collector to not look at the locally stored credentials @@ -71,6 +70,7 @@ and can be used as an authenticator for the [credentials_help]: https://help.sumologic.com/docs/manage/security/installation-tokens [fields_help]: https://help.sumologic.com/docs/manage/fields +[clobber]: https://help.sumologic.com/docs/send-data/installed-collectors/collector-installation-reference/force-collectors-name-clobber/ ## Example Config diff --git a/pkg/extension/sumologicextension/extension.go b/pkg/extension/sumologicextension/extension.go index b112f697ca..e71e33a99d 100644 --- a/pkg/extension/sumologicextension/extension.go +++ b/pkg/extension/sumologicextension/extension.go @@ -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" @@ -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 } @@ -435,6 +434,10 @@ func (se *SumologicExtension) registerCollector(ctx context.Context, collectorNa return credentials.CollectorCredentials{}, err } + if collectorName != resp.CollectorName { + se.logger.Warn("Collector name already in use, registered modified name", zap.String("registered_name", resp.CollectorName)) + } + return credentials.CollectorCredentials{ CollectorName: collectorName, Credentials: resp, diff --git a/pkg/extension/sumologicextension/extension_test.go b/pkg/extension/sumologicextension/extension_test.go index c3d15b3e62..ca64f521e5 100644 --- a/pkg/extension/sumologicextension/extension_test.go +++ b/pkg/extension/sumologicextension/extension_test.go @@ -20,12 +20,10 @@ import ( "crypto/sha256" "encoding/base64" "encoding/json" - "fmt" "net/http" "net/http/httptest" "os" "path" - "regexp" "sync/atomic" "testing" "time" @@ -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{ @@ -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) { @@ -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 @@ -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) { @@ -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) { @@ -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: @@ -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())) } @@ -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) }() diff --git a/pkg/extension/sumologicextension/go.mod b/pkg/extension/sumologicextension/go.mod index 511caba534..88f49d6638 100644 --- a/pkg/extension/sumologicextension/go.mod +++ b/pkg/extension/sumologicextension/go.mod @@ -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 diff --git a/pkg/extension/sumologicextension/go.sum b/pkg/extension/sumologicextension/go.sum index 444d9df1d4..382c1131fa 100644 --- a/pkg/extension/sumologicextension/go.sum +++ b/pkg/extension/sumologicextension/go.sum @@ -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=