Skip to content

Commit 487f604

Browse files
authored
feat: add interval to header and connection Id to body (#189)
* feat: add interval to header and connection Id to body
1 parent be713e2 commit 487f604

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

client.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,15 @@ func NewClient(options ...ConfigOption) (*Client, error) {
165165
uc.options.instanceId = generateInstanceId()
166166
}
167167

168+
connectionId := getConnectionId()
169+
168170
headers := make(http.Header)
169171
if uc.options.customHeaders != nil {
170172
headers = uc.options.customHeaders
171173
}
172174
headers.Set("unleash-appname", uc.options.appName)
173175
headers.Set("unleash-sdk", fmt.Sprintf("%s:%s", clientName, clientVersion))
174-
headers.Set("unleash-connection-id", getConnectionId())
176+
headers.Set("unleash-connection-id", connectionId)
175177

176178
uc.repository = newRepository(
177179
repositoryOptions{
@@ -202,6 +204,7 @@ func NewClient(options ...ConfigOption) (*Client, error) {
202204
metricsOptions{
203205
appName: uc.options.appName,
204206
instanceId: uc.options.instanceId,
207+
connectionId: connectionId,
205208
strategies: strategyNames,
206209
metricsInterval: uc.options.metricsInterval,
207210
url: *parsedUrl,

client_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,3 +1450,49 @@ func TestSendIdentificationHeaders(t *testing.T) {
14501450

14511451
assert.True(gock.IsDone(), "there should be no more mocks")
14521452
}
1453+
1454+
func TestConnectionAndIntervalHeadersAndBody(t *testing.T) {
1455+
assert := assert.New(t)
1456+
defer gock.OffAll()
1457+
1458+
gock.Observe(gock.DumpRequest)
1459+
1460+
gock.New(mockerServer).
1461+
Post("/client/register").
1462+
MatchHeader("UNLEASH-CONNECTION-ID", `[0-9a-f\-]{36}`).
1463+
BodyString(`.*connectionId.*`).
1464+
Reply(200)
1465+
1466+
gock.New(mockerServer).
1467+
Post("/client/metrics").
1468+
MatchHeader("UNLEASH-CONNECTION-ID", `[0-9a-f\-]{36}`).
1469+
MatchHeader("Unleash-Interval", "50").
1470+
BodyString(`.*connectionId.*`).
1471+
Reply(200)
1472+
1473+
gock.New(mockerServer).
1474+
Get("/client/features").
1475+
MatchHeader("UNLEASH-CONNECTION-ID", `[0-9a-f\-]{36}`).
1476+
MatchHeader("Unleash-Interval", "100").
1477+
Reply(200).
1478+
JSON(api.FeatureResponse{})
1479+
1480+
client, err := NewClient(
1481+
WithUrl(mockerServer),
1482+
WithAppName(mockAppName),
1483+
WithMetricsInterval(50*time.Millisecond),
1484+
WithRefreshInterval(100*time.Millisecond),
1485+
WithInstanceId(mockInstanceId),
1486+
WithListener(&NoopListener{}),
1487+
WithDisableMetrics(false),
1488+
)
1489+
1490+
assert.NoError(err)
1491+
1492+
client.IsEnabled("foo")
1493+
1494+
time.Sleep(100 * time.Millisecond)
1495+
err = client.Close()
1496+
1497+
assert.True(gock.IsDone(), "there should be no more mocks")
1498+
}

config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ type repositoryOptions struct {
259259
type metricsOptions struct {
260260
appName string
261261
instanceId string
262+
connectionId string
262263
url url.URL
263264
strategies []string
264265
metricsInterval time.Duration

metrics.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ type MetricsData struct {
2323
// InstanceID is the instance identifier.
2424
InstanceID string `json:"instanceId"`
2525

26+
// ConnectionId is the connection id for instance.
27+
ConnectionId string `json:"connectionId"`
28+
2629
// Bucket is the payload data sent to the server.
2730
Bucket api.Bucket `json:"bucket"`
2831

@@ -50,6 +53,9 @@ type ClientData struct {
5053
// InstanceID is the instance identifier.
5154
InstanceID string `json:"instanceId"`
5255

56+
// ConnectionId is the connection id for instance.
57+
ConnectionId string `json:"connectionId"`
58+
5359
// Optional field that describes the sdk version (name:version)
5460
SDKVersion string `json:"sdkVersion"`
5561

@@ -201,6 +207,7 @@ func (m *metrics) sendMetrics() {
201207
payload := MetricsData{
202208
AppName: m.options.appName,
203209
InstanceID: m.options.instanceId,
210+
ConnectionId: m.options.connectionId,
204211
Bucket: bucket,
205212
SDKVersion: fmt.Sprintf("%s:%s", clientName, clientVersion),
206213
PlatformName: "go",
@@ -258,6 +265,7 @@ func (m *metrics) doPost(url *url.URL, payload interface{}) (*http.Response, err
258265
req.Header.Add("UNLEASH-APPNAME", m.options.appName)
259266
req.Header.Add("UNLEASH-INSTANCEID", m.options.instanceId)
260267
req.Header.Add("User-Agent", m.options.appName)
268+
req.Header.Add("Unleash-Interval", fmt.Sprintf("%d", m.options.metricsInterval.Milliseconds()))
261269

262270
for k, v := range m.options.headers {
263271
req.Header[k] = v
@@ -331,6 +339,7 @@ func (m *metrics) getClientData() ClientData {
331339
return ClientData{
332340
m.options.appName,
333341
m.options.instanceId,
342+
m.options.connectionId,
334343
fmt.Sprintf("%s:%s", clientName, clientVersion),
335344
m.options.strategies,
336345
m.started,

repository.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ func (r *repository) fetch() error {
125125

126126
req.Header.Add("UNLEASH-APPNAME", r.options.appName)
127127
req.Header.Add("UNLEASH-INSTANCEID", r.options.instanceId)
128+
req.Header.Add("Unleash-Interval", fmt.Sprintf("%d", r.options.refreshInterval.Milliseconds()))
128129
req.Header.Add("User-Agent", r.options.appName)
129130
// Needs to reference a version of the client specifications that include
130131
// global segments

0 commit comments

Comments
 (0)