Skip to content

Commit 37da1b3

Browse files
committed
feat: add interval event emitting of CNS config snapshot
1 parent c9d4de6 commit 37da1b3

File tree

4 files changed

+107
-6
lines changed

4 files changed

+107
-6
lines changed

cns/logger/constants.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ const (
77
ConfigSnapshotMetricsStr = "ConfigSnapshot"
88

99
// Dimensions
10-
OrchestratorTypeStr = "OrchestratorType"
11-
NodeIDStr = "NodeID"
12-
HomeAZStr = "HomeAZ"
13-
IsAZRSupportedStr = "IsAZRSupported"
14-
HomeAZErrorCodeStr = "HomeAZErrorCode"
15-
HomeAZErrorMsgStr = "HomeAZErrorMsg"
10+
OrchestratorTypeStr = "OrchestratorType"
11+
NodeIDStr = "NodeID"
12+
HomeAZStr = "HomeAZ"
13+
IsAZRSupportedStr = "IsAZRSupported"
14+
HomeAZErrorCodeStr = "HomeAZErrorCode"
15+
HomeAZErrorMsgStr = "HomeAZErrorMsg"
16+
CNSConfigPropertyStr = "CNSConfiguration"
17+
CNSConfigMD5CheckSumPropertyStr = "CNSConfigurationMD5Checksum"
1618

1719
// CNS NC Snspshot properties
1820
CnsNCSnapshotEventStr = "CNSNCSnapshot"

cns/metric/configsnapshot.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2018 Microsoft. All rights reserved.
2+
// MIT License
3+
4+
package metric
5+
6+
import (
7+
"context"
8+
"crypto/md5" //nolint:gosec // used for checksum
9+
"encoding/json"
10+
"time"
11+
12+
"github.com/Azure/azure-container-networking/aitelemetry"
13+
"github.com/Azure/azure-container-networking/cns/configuration"
14+
"github.com/Azure/azure-container-networking/cns/logger"
15+
)
16+
17+
// SendCNSConfigSnapshot emits CNS config periodically
18+
func SendCNSConfigSnapshot(ctx context.Context, config *configuration.CNSConfig) {
19+
ticker := time.NewTicker(time.Minute * time.Duration(config.TelemetrySettings.ConfigSnapshotIntervalInMins))
20+
defer ticker.Stop()
21+
22+
event, err := createCNSConfigSnapshotEvent(config)
23+
if err != nil {
24+
logger.Errorf("[Azure CNS] SendCNSConfigSnapshot failed to create event at an interval: %v", err)
25+
return
26+
}
27+
28+
for {
29+
select {
30+
case <-ctx.Done():
31+
return
32+
case <-ticker.C:
33+
logger.LogEvent(event)
34+
}
35+
}
36+
}
37+
38+
func createCNSConfigSnapshotEvent(config *configuration.CNSConfig) (aitelemetry.Event, error) {
39+
bb, err := json.Marshal(config) //nolint:musttag // no tag needed for config
40+
if err != nil {
41+
return aitelemetry.Event{}, err
42+
}
43+
44+
cs := md5.Sum(bb) //nolint:gosec // used for checksum
45+
csStr := string(cs[:])
46+
47+
event := aitelemetry.Event{
48+
EventName: logger.ConfigSnapshotMetricsStr,
49+
ResourceID: csStr, // not guaranteed unique, instead use VM ID and Subscription to correlate
50+
Properties: map[string]string{
51+
logger.CNSConfigPropertyStr: string(bb),
52+
logger.CNSConfigMD5CheckSumPropertyStr: csStr,
53+
},
54+
}
55+
56+
return event, nil
57+
}

cns/metric/configsnapshot_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package metric
2+
3+
import (
4+
"crypto/md5"
5+
"encoding/json"
6+
"testing"
7+
8+
"github.com/Azure/azure-container-networking/aitelemetry"
9+
"github.com/Azure/azure-container-networking/cns/configuration"
10+
"github.com/Azure/azure-container-networking/cns/logger"
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func TestCreateCNSConfigSnapshotEvent(t *testing.T) {
16+
logger.InitLogger("testlogs", 0, 0, "./")
17+
18+
config, err := configuration.ReadConfig("../configuration/testdata/good.json")
19+
require.NoError(t, err)
20+
21+
event, err := createCNSConfigSnapshotEvent(config)
22+
require.NoError(t, err)
23+
24+
bb, err := json.Marshal(config)
25+
require.NoError(t, err)
26+
27+
cs := md5.Sum(bb)
28+
csStr := string(cs[:])
29+
30+
expected := aitelemetry.Event{
31+
EventName: logger.ConfigSnapshotMetricsStr,
32+
ResourceID: csStr,
33+
Properties: map[string]string{
34+
logger.CNSConfigPropertyStr: string(bb),
35+
logger.CNSConfigMD5CheckSumPropertyStr: csStr,
36+
},
37+
}
38+
39+
assert.Equal(t, expected, event)
40+
}

cns/service/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,8 @@ func main() {
578578
} else {
579579
logger.InitAI(aiConfig, ts.DisableTrace, ts.DisableMetric, ts.DisableEvent)
580580
}
581+
582+
go metric.SendCNSConfigSnapshot(rootCtx, cnsconfig)
581583
}
582584
logger.Printf("[Azure CNS] Using config: %+v", cnsconfig)
583585

0 commit comments

Comments
 (0)