From 29fc678078304169abae949e76937360eea4eb5b Mon Sep 17 00:00:00 2001
From: Eliott Bouhana <eliott.bouhana@datadoghq.com>
Date: Thu, 30 Jan 2025 16:20:41 +0100
Subject: [PATCH] rename AddAppConfig to RegisterAppConfig and rename
 AddBulkConfig to RegisterAppConfigs

Signed-off-by: Eliott Bouhana <eliott.bouhana@datadoghq.com>
---
 internal/newtelemetry/api.go                  | 20 +++++++++++++++----
 internal/newtelemetry/client.go               | 12 +++++------
 internal/newtelemetry/client_test.go          |  8 ++++----
 internal/newtelemetry/configuration.go        | 10 +++++-----
 internal/newtelemetry/globalclient.go         | 16 +++++++--------
 .../telemetrytest/telemetrytest.go            | 16 +++++++--------
 6 files changed, 44 insertions(+), 38 deletions(-)

diff --git a/internal/newtelemetry/api.go b/internal/newtelemetry/api.go
index 066e4d1f2e..cf7eba5b3c 100644
--- a/internal/newtelemetry/api.go
+++ b/internal/newtelemetry/api.go
@@ -62,6 +62,16 @@ type Integration struct {
 	Error string
 }
 
+// Configuration is a key-value pair that is used to configure the application.
+type Configuration struct {
+	// Key is the key of the configuration.
+	Key string
+	// Value is the value of the configuration. Need to be json serializable.
+	Value any
+	// Origin is the source of the configuration change.
+	Origin Origin
+}
+
 // Client constitutes all the functions available concurrently for the telemetry users. All methods are thread-safe
 // This is an interface for easier testing but all functions will be mirrored at the package level to call
 // the global client.
@@ -93,13 +103,13 @@ type Client interface {
 	// ProductStartError declares that a product could not start because of the following error
 	ProductStartError(product Namespace, err error)
 
-	// AddAppConfig adds a key value pair to the app configuration and send the change to telemetry
+	// RegisterAppConfig adds a key value pair to the app configuration and send the change to telemetry
 	// value has to be json serializable and the origin is the source of the change.
-	AddAppConfig(key string, value any, origin Origin)
+	RegisterAppConfig(key string, value any, origin Origin)
 
-	// AddBulkAppConfig adds a list of key value pairs to the app configuration and sends the change to telemetry.
+	// RegisterAppConfigs adds a list of key value pairs to the app configuration and sends the change to telemetry.
 	// Same as AddAppConfig but for multiple values.
-	AddBulkAppConfig(kvs map[string]any, origin Origin)
+	RegisterAppConfigs(kvs ...Configuration)
 
 	// MarkIntegrationAsLoaded marks an integration as loaded in the telemetry
 	MarkIntegrationAsLoaded(integration Integration)
@@ -108,8 +118,10 @@ type Client interface {
 	Flush()
 
 	// AppStart sends the telemetry necessary to signal that the app is starting.
+	// Preferred use via StartApp package level function
 	AppStart()
 
 	// AppStop sends the telemetry necessary to signal that the app is stopping.
+	// Preferred use via StopApp package level function
 	AppStop()
 }
diff --git a/internal/newtelemetry/client.go b/internal/newtelemetry/client.go
index 5361689199..2452fc94bb 100644
--- a/internal/newtelemetry/client.go
+++ b/internal/newtelemetry/client.go
@@ -178,13 +178,13 @@ func (c *client) ProductStartError(product Namespace, err error) {
 	c.products.Add(product, false, err)
 }
 
-func (c *client) AddAppConfig(key string, value any, origin Origin) {
-	c.configuration.Add(key, value, origin)
+func (c *client) RegisterAppConfig(key string, value any, origin Origin) {
+	c.configuration.Add(Configuration{key, value, origin})
 }
 
-func (c *client) AddBulkAppConfig(kvs map[string]any, origin Origin) {
-	for key, value := range kvs {
-		c.configuration.Add(key, value, origin)
+func (c *client) RegisterAppConfigs(kvs ...Configuration) {
+	for _, value := range kvs {
+		c.configuration.Add(value)
 	}
 }
 
@@ -324,5 +324,3 @@ func (c *client) Close() error {
 	c.flushTicker.Stop()
 	return nil
 }
-
-var _ Client = (*client)(nil)
diff --git a/internal/newtelemetry/client_test.go b/internal/newtelemetry/client_test.go
index 5eb9f45ec0..f4211c544f 100644
--- a/internal/newtelemetry/client_test.go
+++ b/internal/newtelemetry/client_test.go
@@ -111,7 +111,7 @@ func TestClientFlush(t *testing.T) {
 				ExtendedHeartbeatInterval: time.Nanosecond,
 			},
 			when: func(c *client) {
-				c.AddAppConfig("key", "value", OriginDefault)
+				c.RegisterAppConfig("key", "value", OriginDefault)
 			},
 			expect: func(t *testing.T, payloads []transport.Payload) {
 				payload := payloads[0]
@@ -148,7 +148,7 @@ func TestClientFlush(t *testing.T) {
 		{
 			name: "configuration-default",
 			when: func(c *client) {
-				c.AddAppConfig("key", "value", OriginDefault)
+				c.RegisterAppConfig("key", "value", OriginDefault)
 			},
 			expect: func(t *testing.T, payloads []transport.Payload) {
 				payload := payloads[0]
@@ -163,7 +163,7 @@ func TestClientFlush(t *testing.T) {
 		{
 			name: "configuration-default",
 			when: func(c *client) {
-				c.AddAppConfig("key", "value", OriginDefault)
+				c.RegisterAppConfig("key", "value", OriginDefault)
 			},
 			expect: func(t *testing.T, payloads []transport.Payload) {
 				payload := payloads[0]
@@ -340,7 +340,7 @@ func TestClientFlush(t *testing.T) {
 			name: "app-started-with-configuration",
 			when: func(c *client) {
 				c.AppStart()
-				c.AddAppConfig("key", "value", OriginDefault)
+				c.RegisterAppConfig("key", "value", OriginDefault)
 			},
 			expect: func(t *testing.T, payloads []transport.Payload) {
 				payload := payloads[0]
diff --git a/internal/newtelemetry/configuration.go b/internal/newtelemetry/configuration.go
index 1939bf055c..ecbb7e33d1 100644
--- a/internal/newtelemetry/configuration.go
+++ b/internal/newtelemetry/configuration.go
@@ -17,7 +17,7 @@ type configuration struct {
 	seqID  uint64
 }
 
-func (c *configuration) Add(key string, value any, origin Origin) {
+func (c *configuration) Add(kv Configuration) {
 	c.mu.Lock()
 	defer c.mu.Unlock()
 
@@ -25,10 +25,10 @@ func (c *configuration) Add(key string, value any, origin Origin) {
 		c.config = make(map[string]transport.ConfKeyValue)
 	}
 
-	c.config[key] = transport.ConfKeyValue{
-		Name:   key,
-		Value:  value,
-		Origin: origin,
+	c.config[kv.Key] = transport.ConfKeyValue{
+		Name:   kv.Key,
+		Value:  kv.Value,
+		Origin: kv.Origin,
 	}
 }
 
diff --git a/internal/newtelemetry/globalclient.go b/internal/newtelemetry/globalclient.go
index e9275adfa6..d7412ec0b9 100644
--- a/internal/newtelemetry/globalclient.go
+++ b/internal/newtelemetry/globalclient.go
@@ -172,21 +172,21 @@ func ProductStartError(product Namespace, err error) {
 	})
 }
 
-// AddAppConfig adds a key value pair to the app configuration and send the change to telemetry
+// RegisterAppConfig adds a key value pair to the app configuration and send the change to telemetry
 // value has to be json serializable and the origin is the source of the change. If telemetry is disabled, it will do nothing.
 // If the telemetry client has not started yet, it will record the action and replay it once the client is started.
-func AddAppConfig(key string, value any, origin Origin) {
+func RegisterAppConfig(key string, value any, origin Origin) {
 	globalClientCall(func(client Client) {
-		client.AddAppConfig(key, value, origin)
+		client.RegisterAppConfig(key, value, origin)
 	})
 }
 
-// AddBulkAppConfig adds a list of key value pairs to the app configuration and sends the change to telemetry.
+// RegisterAppConfigs adds a list of key value pairs to the app configuration and sends the change to telemetry.
 // Same as AddAppConfig but for multiple values. If telemetry is disabled, it will do nothing.
 // If the telemetry client has not started yet, it will record the action and replay it once the client is started.
-func AddBulkAppConfig(kvs map[string]any, origin Origin) {
+func RegisterAppConfigs(kvs ...Configuration) {
 	globalClientCall(func(client Client) {
-		client.AddBulkAppConfig(kvs, origin)
+		client.RegisterAppConfigs(kvs...)
 	})
 }
 
@@ -201,9 +201,7 @@ func globalClientCall(fun func(client Client)) {
 
 	client := globalClient.Load()
 	if client == nil || *client == nil {
-		if !globalClientRecorder.Record(func(client Client) {
-			fun(client)
-		}) {
+		if !globalClientRecorder.Record(fun) {
 			globalClientLogLossOnce.Do(func() {
 				log.Debug("telemetry: global client recorder queue is full, dropping telemetry data, please start the telemetry client earlier to avoid data loss")
 			})
diff --git a/internal/newtelemetry/telemetrytest/telemetrytest.go b/internal/newtelemetry/telemetrytest/telemetrytest.go
index ac6a592ca9..352262d0bb 100644
--- a/internal/newtelemetry/telemetrytest/telemetrytest.go
+++ b/internal/newtelemetry/telemetrytest/telemetrytest.go
@@ -22,7 +22,7 @@ type MockClient struct {
 	mu            sync.Mutex
 	Started       bool
 	Stopped       bool
-	Configuration map[string]any
+	Configuration []newtelemetry.Configuration
 	Logs          map[newtelemetry.LogLevel]string
 	Integrations  []string
 	Products      map[newtelemetry.Namespace]bool
@@ -157,20 +157,18 @@ func (m *MockClient) ProductStartError(product newtelemetry.Namespace, err error
 	m.Products[product] = false
 }
 
-func (m *MockClient) AddAppConfig(key string, value any, origin newtelemetry.Origin) {
+func (m *MockClient) RegisterAppConfig(key string, value any, origin newtelemetry.Origin) {
 	m.mu.Lock()
 	defer m.mu.Unlock()
-	m.On("AddAppConfig", key, value, origin)
-	m.Configuration[key] = value
+	m.On("RegisterAppConfig", key, value, origin)
+	m.Configuration = append(m.Configuration, newtelemetry.Configuration{Key: key, Value: value, Origin: origin})
 }
 
-func (m *MockClient) AddBulkAppConfig(kvs map[string]any, origin newtelemetry.Origin) {
+func (m *MockClient) RegisterAppConfigs(kvs ...newtelemetry.Configuration) {
 	m.mu.Lock()
 	defer m.mu.Unlock()
-	m.On("AddBulkAppConfig", kvs, origin)
-	for k, v := range kvs {
-		m.Configuration[k] = v
-	}
+	m.On("RegisterAppConfigs", kvs)
+	m.Configuration = append(m.Configuration, kvs...)
 }
 
 func (m *MockClient) MarkIntegrationAsLoaded(integration newtelemetry.Integration) {