Skip to content

Commit

Permalink
rename AddAppConfig to RegisterAppConfig and rename AddBulkConfig to …
Browse files Browse the repository at this point in the history
…RegisterAppConfigs

Signed-off-by: Eliott Bouhana <[email protected]>
  • Loading branch information
eliottness committed Jan 30, 2025
1 parent 21d0f01 commit 29fc678
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 38 deletions.
20 changes: 16 additions & 4 deletions internal/newtelemetry/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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()
}
12 changes: 5 additions & 7 deletions internal/newtelemetry/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -324,5 +324,3 @@ func (c *client) Close() error {
c.flushTicker.Stop()
return nil
}

var _ Client = (*client)(nil)
8 changes: 4 additions & 4 deletions internal/newtelemetry/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
10 changes: 5 additions & 5 deletions internal/newtelemetry/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ 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()

if c.config == nil {
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,
}
}

Expand Down
16 changes: 7 additions & 9 deletions internal/newtelemetry/globalclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
})
}

Expand All @@ -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")
})
Expand Down
16 changes: 7 additions & 9 deletions internal/newtelemetry/telemetrytest/telemetrytest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 29fc678

Please sign in to comment.