From b1bdd1f60c29960ecea3c457ee13881b714f6125 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nuno=20G=C3=B3is?= Date: Wed, 10 Jul 2024 14:23:41 +0100 Subject: [PATCH] feat: automatically generated instanceId --- README.md | 7 +++++-- client.go | 6 +++--- client_test.go | 31 ------------------------------- config.go | 8 -------- go.mod | 1 + go.sum | 2 ++ metrics_test.go | 11 ----------- repository_test.go | 6 ++---- unleash_mock.go | 1 - utils.go | 19 ------------------- 10 files changed, 13 insertions(+), 79 deletions(-) diff --git a/README.md b/README.md index a15ece6..3972a83 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ go get github.com/Unleash/unleash-client-go The easiest way to get started with Unleash is to initialize it early in your application code: **Asynchronous initialization example:** + ```go import ( "github.com/Unleash/unleash-client-go/v4" @@ -70,6 +71,10 @@ func init() { } ``` +#### instanceId + +Starting from version 5.0.0, `instanceId` is automatically generated and can no longer be set manually. + #### Preloading feature toggles If you'd like to prebake your application with feature toggles (maybe you're working without persistent storage, so Unleash's backup isn't available), you can replace the defaultStorage implementation with a BootstrapStorage. This allows you to pass in a reader to where data in the format of `/api/client/features` can be found. @@ -273,8 +278,6 @@ you can add the following to your apps `go.mod`: replace github.com/Unleash/unleash-client-go/v4 => ../unleash-client-go/ ``` - - ## Steps to release - Update the clientVersion in `client.go` diff --git a/client.go b/client.go index f45c385..f7357b8 100644 --- a/client.go +++ b/client.go @@ -7,6 +7,8 @@ import ( "strings" "time" + "github.com/google/uuid" + "github.com/Unleash/unleash-client-go/v4/api" "github.com/Unleash/unleash-client-go/v4/context" "github.com/Unleash/unleash-client-go/v4/internal/constraints" @@ -159,9 +161,7 @@ func NewClient(options ...ConfigOption) (*Client, error) { return nil, fmt.Errorf("unleash client appName missing") } - if uc.options.instanceId == "" { - uc.options.instanceId = generateInstanceId() - } + uc.options.instanceId = uuid.New().String() uc.repository = newRepository( repositoryOptions{ diff --git a/client_test.go b/client_test.go index 64f50b8..e86c15c 100644 --- a/client_test.go +++ b/client_test.go @@ -30,7 +30,6 @@ func TestClientWithoutListener(t *testing.T) { client, err := NewClient( WithUrl(mockerServer), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), ) assert.Nil(err, "client should not return an error") @@ -46,7 +45,6 @@ func TestClient_WithFallbackFunc(t *testing.T) { gock.New(mockerServer). Post("/client/register"). MatchHeader("UNLEASH-APPNAME", mockAppName). - MatchHeader("UNLEASH-INSTANCEID", mockInstanceId). Reply(200) gock.New(mockerServer). @@ -65,7 +63,6 @@ func TestClient_WithFallbackFunc(t *testing.T) { client, err := NewClient( WithUrl(mockerServer), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), WithListener(mockListener), ) @@ -90,7 +87,6 @@ func TestClient_WithResolver(t *testing.T) { gock.New(mockerServer). Post("/client/register"). MatchHeader("UNLEASH-APPNAME", mockAppName). - MatchHeader("UNLEASH-INSTANCEID", mockInstanceId). Reply(200) gock.New(mockerServer). @@ -109,7 +105,6 @@ func TestClient_WithResolver(t *testing.T) { client, err := NewClient( WithUrl(mockerServer), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), WithListener(mockListener), ) @@ -198,7 +193,6 @@ func TestClient_ListFeatures(t *testing.T) { client, err := NewClient( WithUrl(mockerServer), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), WithListener(mockListener), ) @@ -232,7 +226,6 @@ func TestClientWithProjectName(t *testing.T) { client, err := NewClient( WithUrl(mockerServer), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), WithProjectName(projectName), WithListener(mockListener), ) @@ -265,7 +258,6 @@ func TestClientWithoutProjectName(t *testing.T) { client, err := NewClient( WithUrl(mockerServer), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), WithListener(mockListener), ) @@ -342,7 +334,6 @@ func TestClientWithVariantContext(t *testing.T) { client, err := NewClient( WithUrl(mockerServer), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), WithListener(mockListener), ) @@ -381,7 +372,6 @@ func TestClient_WithSegment(t *testing.T) { gock.New(mockerServer). Post("/client/register"). MatchHeader("UNLEASH-APPNAME", mockAppName). - MatchHeader("UNLEASH-INSTANCEID", mockInstanceId). Reply(200) feature := "feature-segment" @@ -430,7 +420,6 @@ func TestClient_WithSegment(t *testing.T) { client, err := NewClient( WithUrl(mockerServer), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), WithListener(mockListener), ) @@ -466,7 +455,6 @@ func TestClient_WithNonExistingSegment(t *testing.T) { gock.New(mockerServer). Post("/client/register"). MatchHeader("UNLEASH-APPNAME", mockAppName). - MatchHeader("UNLEASH-INSTANCEID", mockInstanceId). Reply(200) feature := "feature-segment-non-existing" @@ -508,7 +496,6 @@ func TestClient_WithNonExistingSegment(t *testing.T) { client, err := NewClient( WithUrl(mockerServer), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), WithListener(mockListener), ) @@ -545,7 +532,6 @@ func TestClient_WithMultipleSegments(t *testing.T) { gock.New(mockerServer). Post("/client/register"). MatchHeader("UNLEASH-APPNAME", mockAppName). - MatchHeader("UNLEASH-INSTANCEID", mockInstanceId). Reply(200) feature := "feature-segment-multiple" @@ -612,7 +598,6 @@ func TestClient_WithMultipleSegments(t *testing.T) { client, err := NewClient( WithUrl(mockerServer), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), WithListener(mockListener), ) @@ -648,7 +633,6 @@ func TestClient_VariantShouldRespectConstraint(t *testing.T) { gock.New(mockerServer). Post("/client/register"). MatchHeader("UNLEASH-APPNAME", mockAppName). - MatchHeader("UNLEASH-INSTANCEID", mockInstanceId). Reply(200) feature := "feature-segment-multiple" @@ -727,7 +711,6 @@ func TestClient_VariantShouldRespectConstraint(t *testing.T) { client, err := NewClient( WithUrl(mockerServer), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), WithListener(mockListener), ) @@ -767,7 +750,6 @@ func TestClient_VariantShouldFailWhenSegmentConstraintsDontMatch(t *testing.T) { gock.New(mockerServer). Post("/client/register"). MatchHeader("UNLEASH-APPNAME", mockAppName). - MatchHeader("UNLEASH-INSTANCEID", mockInstanceId). Reply(200) feature := "feature-segment-multiple" @@ -846,7 +828,6 @@ func TestClient_VariantShouldFailWhenSegmentConstraintsDontMatch(t *testing.T) { client, err := NewClient( WithUrl(mockerServer), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), WithListener(mockListener), ) @@ -949,7 +930,6 @@ func TestClient_ShouldFavorStrategyVariantOverFeatureVariant(t *testing.T) { client, err := NewClient( WithUrl(mockerServer), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), WithListener(mockListener), ) @@ -1049,7 +1029,6 @@ func TestClient_ShouldReturnOldVariantForNonMatchingStrategyVariant(t *testing.T client, err := NewClient( WithUrl(mockerServer), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), WithListener(mockListener), ) @@ -1075,7 +1054,6 @@ func TestClient_VariantFromEnabledFeatureWithNoVariants(t *testing.T) { gock.New(mockerServer). Post("/client/register"). MatchHeader("UNLEASH-APPNAME", mockAppName). - MatchHeader("UNLEASH-INSTANCEID", mockInstanceId). Reply(200) feature := "feature-no-variants" @@ -1112,7 +1090,6 @@ func TestClient_VariantFromEnabledFeatureWithNoVariants(t *testing.T) { client, err := NewClient( WithUrl(mockerServer), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), WithListener(mockListener), ) @@ -1152,7 +1129,6 @@ func TestGetVariantWithFallbackVariantWhenFeatureDisabled(t *testing.T) { gock.New(mockerServer). Post("/client/register"). MatchHeader("UNLEASH-APPNAME", mockAppName). - MatchHeader("UNLEASH-INSTANCEID", mockInstanceId). Reply(200) feature := "feature-disabled" @@ -1183,7 +1159,6 @@ func TestGetVariantWithFallbackVariantWhenFeatureDisabled(t *testing.T) { client, err := NewClient( WithUrl(mockerServer), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), WithListener(&NoopListener{}), ) @@ -1220,7 +1195,6 @@ func TestGetVariantWithFallbackVariantWhenFeatureEnabledButNoVariants(t *testing gock.New(mockerServer). Post("/client/register"). MatchHeader("UNLEASH-APPNAME", mockAppName). - MatchHeader("UNLEASH-INSTANCEID", mockInstanceId). Reply(200) feature := "feature-no-variants" @@ -1251,7 +1225,6 @@ func TestGetVariantWithFallbackVariantWhenFeatureEnabledButNoVariants(t *testing client, err := NewClient( WithUrl(mockerServer), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), WithListener(&NoopListener{}), ) @@ -1292,7 +1265,6 @@ func TestGetVariantWithFallbackVariantWhenFeatureDoesntExist(t *testing.T) { gock.New(mockerServer). Post("/client/register"). MatchHeader("UNLEASH-APPNAME", mockAppName). - MatchHeader("UNLEASH-INSTANCEID", mockInstanceId). Reply(200) feature := "feature-no-variants" @@ -1309,7 +1281,6 @@ func TestGetVariantWithFallbackVariantWhenFeatureDoesntExist(t *testing.T) { client, err := NewClient( WithUrl(mockerServer), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), WithListener(&NoopListener{}), ) @@ -1346,7 +1317,6 @@ func TestGetVariant_FallbackVariantFeatureEnabledSettingIsLeftUnchanged(t *testi gock.New(mockerServer). Post("/client/register"). MatchHeader("UNLEASH-APPNAME", mockAppName). - MatchHeader("UNLEASH-INSTANCEID", mockInstanceId). Reply(200) enabledFeatureNoVariants := "enabled-feature" @@ -1391,7 +1361,6 @@ func TestGetVariant_FallbackVariantFeatureEnabledSettingIsLeftUnchanged(t *testi client, err := NewClient( WithUrl(mockerServer), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), WithListener(&NoopListener{}), ) diff --git a/config.go b/config.go index 9ed7ddc..d03f598 100644 --- a/config.go +++ b/config.go @@ -54,14 +54,6 @@ func WithEnvironment(env string) ConfigOption { } } -// WithInstanceId specifies the instance identifier of the current instance. If not provided, -// one will be generated based on various parameters such as current user and hostname. -func WithInstanceId(instanceId string) ConfigOption { - return func(o *configOption) { - o.instanceId = instanceId - } -} - // WithUrl specifies the url of the unleash server the user is connecting to. func WithUrl(url string) ConfigOption { return func(o *configOption) { diff --git a/go.mod b/go.mod index b4b9962..b3fb230 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/Unleash/unleash-client-go/v4 require ( github.com/Masterminds/semver/v3 v3.1.1 github.com/davecgh/go-spew v1.1.1 // indirect + github.com/google/uuid v1.6.0 github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/stretchr/objx v0.1.1 // indirect diff --git a/go.sum b/go.sum index 61d9218..1624e9f 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030I github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4= github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/metrics_test.go b/metrics_test.go index 82fca5d..6aada23 100644 --- a/metrics_test.go +++ b/metrics_test.go @@ -26,7 +26,6 @@ func TestMetrics_RegisterInstance(t *testing.T) { gock.New(mockerServer). Post("/client/register"). MatchHeader("UNLEASH-APPNAME", mockAppName). - MatchHeader("UNLEASH-INSTANCEID", mockInstanceId). Reply(200) gock.New(mockerServer). @@ -41,7 +40,6 @@ func TestMetrics_RegisterInstance(t *testing.T) { client, err := NewClient( WithUrl(mockerServer), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), WithListener(mockListener), ) @@ -60,7 +58,6 @@ func TestMetrics_VariantsCountToggles(t *testing.T) { gock.New(mockerServer). Post("/client/register"). MatchHeader("UNLEASH-APPNAME", mockAppName). - MatchHeader("UNLEASH-INSTANCEID", mockInstanceId). Reply(200) gock.New(mockerServer). @@ -76,7 +73,6 @@ func TestMetrics_VariantsCountToggles(t *testing.T) { client, err := NewClient( WithUrl(mockerServer), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), WithListener(mockListener), ) @@ -106,7 +102,6 @@ func TestMetrics_DoPost(t *testing.T) { gock.New(mockerServer). Post(""). MatchHeader("UNLEASH-APPNAME", mockAppName). - MatchHeader("UNLEASH-INSTANCEID", mockInstanceId). Reply(200) mockListener := &MockedListener{} @@ -116,7 +111,6 @@ func TestMetrics_DoPost(t *testing.T) { client, err := NewClient( WithUrl(mockerServer), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), WithListener(&DebugListener{}), ) @@ -150,7 +144,6 @@ func TestMetrics_DisabledMetrics(t *testing.T) { WithDisableMetrics(true), WithMetricsInterval(100*time.Millisecond), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), WithListener(mockListener), ) assert.Nil(err, "client should not return an error") @@ -221,7 +214,6 @@ func TestMetrics_SendMetricsFail(t *testing.T) { client, err := NewClient( WithUrl(srv.URL), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), WithListener(mockListener), WithMetricsInterval(time.Millisecond), ) @@ -325,7 +317,6 @@ func TestMetrics_ShouldNotCountMetricsForParentToggles(t *testing.T) { client, err := NewClient( WithUrl(mockerServer), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), WithListener(mockListener), ) assert.Nil(err, "client should not return an error") @@ -370,7 +361,6 @@ func TestMetrics_ShouldBackoffOn500(t *testing.T) { WithUrl(mockerServer), WithMetricsInterval(50*time.Millisecond), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), WithListener(mockListener), ) assert.Nil(err, "client should not return an error") @@ -411,7 +401,6 @@ func TestMetrics_ErrorCountShouldDecreaseIfSuccessful(t *testing.T) { WithUrl(mockerServer), WithMetricsInterval(50*time.Millisecond), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), ) assert.Nil(err, "client should not return an error") diff --git a/repository_test.go b/repository_test.go index 2854730..3205d71 100644 --- a/repository_test.go +++ b/repository_test.go @@ -3,7 +3,6 @@ package unleash import ( "bytes" "encoding/json" - "gopkg.in/h2non/gock.v1" "net/http" "net/http/httptest" "strings" @@ -11,6 +10,8 @@ import ( "testing" "time" + "gopkg.in/h2non/gock.v1" + "github.com/Unleash/unleash-client-go/v4/api" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" @@ -59,7 +60,6 @@ func TestRepository_GetFeaturesFail(t *testing.T) { client, err := NewClient( WithUrl(srv.URL), WithAppName(mockAppName), - WithInstanceId(mockInstanceId), WithListener(mockListener), WithRefreshInterval(time.Millisecond), ) @@ -153,7 +153,6 @@ func TestRepository_backs_off_on_http_statuses(t *testing.T) { WithUrl(mockerServer), WithAppName(mockAppName), WithDisableMetrics(true), - WithInstanceId(mockInstanceId), WithRefreshInterval(time.Millisecond * 15), ) a.Nil(err) @@ -178,7 +177,6 @@ func TestRepository_back_offs_are_gradually_reduced_on_success(t *testing.T) { WithUrl(mockerServer), WithAppName(mockAppName), WithDisableMetrics(true), - WithInstanceId(mockInstanceId), WithRefreshInterval(time.Millisecond * 10), ) a.Nil(err) diff --git a/unleash_mock.go b/unleash_mock.go index 76be4de..7a3477f 100644 --- a/unleash_mock.go +++ b/unleash_mock.go @@ -10,7 +10,6 @@ import ( const ( mockerServer = "http://foo.com" mockAppName = "unleash-client-go-tests" - mockInstanceId = "1234" ) type MockedListener struct { diff --git a/utils.go b/utils.go index 7021925..94c19f5 100644 --- a/utils.go +++ b/utils.go @@ -2,34 +2,15 @@ package unleash import ( "fmt" - "math/rand" "os" - "os/user" "reflect" "sync" - "time" ) func getTmpDirPath() string { return os.TempDir() } -func generateInstanceId() string { - prefix := "" - - if user, err := user.Current(); err == nil && user.Username != "" { - prefix = user.Username - } else { - rand.Seed(time.Now().Unix()) - prefix = fmt.Sprintf("generated-%d-%d", rand.Intn(1000000), os.Getpid()) - } - - if hostname, err := os.Hostname(); err == nil && hostname != "" { - return fmt.Sprintf("%s-%s", prefix, hostname) - } - return prefix -} - func getFetchURLPath(projectName string) string { if projectName != "" { return fmt.Sprintf("./client/features?project=%s", projectName)