-
Notifications
You must be signed in to change notification settings - Fork 260
[CNS] add config snapshot event metrics at an interval #3140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jackieluc
merged 9 commits into
Azure:master
from
jackieluc:jackieluc/config-snapshot-metric
Nov 21, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c9d4de6
feat: add and expose new ConfigSnapshotIntervalInMins config
jackieluc 37da1b3
feat: add interval event emitting of CNS config snapshot
jackieluc 6318f95
Merge branch 'master' into jackieluc/config-snapshot-metric
jackieluc fd88521
lint: address lint errors
jackieluc b57eaee
chore: send CNS config snapshot immediately
jackieluc 40371f1
test: refactor test to be less coupled
jackieluc ff2131c
lint
jackieluc 022fc2c
fix: remove setting default ConfigSnapshotIntervalInMins
jackieluc 6c72770
chore: remove unnecessary comment
jackieluc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package metric | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/md5" //nolint:gosec // used for checksum | ||
| "encoding/json" | ||
| "time" | ||
|
|
||
| "github.com/Azure/azure-container-networking/aitelemetry" | ||
| "github.com/Azure/azure-container-networking/cns/configuration" | ||
| "github.com/Azure/azure-container-networking/cns/logger" | ||
| "github.com/pkg/errors" | ||
| ) | ||
|
|
||
| // SendCNSConfigSnapshot emits CNS config periodically | ||
| func SendCNSConfigSnapshot(ctx context.Context, config *configuration.CNSConfig) { | ||
| ticker := time.NewTicker(time.Minute * time.Duration(config.TelemetrySettings.ConfigSnapshotIntervalInMins)) | ||
| defer ticker.Stop() | ||
|
|
||
| event, err := createCNSConfigSnapshotEvent(config) | ||
| if err != nil { | ||
| logger.Errorf("[Azure CNS] SendCNSConfigSnapshot: %v", err) | ||
| return | ||
| } | ||
|
|
||
| // Log the first event immediately | ||
| logger.LogEvent(event) | ||
|
|
||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return | ||
| case <-ticker.C: | ||
ramiro-gamarra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| logger.LogEvent(event) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func createCNSConfigSnapshotEvent(config *configuration.CNSConfig) (aitelemetry.Event, error) { | ||
| bb, err := json.Marshal(config) //nolint:musttag // no tag needed for config | ||
| if err != nil { | ||
| return aitelemetry.Event{}, errors.Wrap(err, "failed to marshal config") | ||
| } | ||
|
|
||
| cs := md5.Sum(bb) //nolint:gosec // used for checksum | ||
| csStr := string(cs[:]) | ||
rbtr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| event := aitelemetry.Event{ | ||
| EventName: logger.ConfigSnapshotMetricsStr, | ||
| ResourceID: csStr, // not guaranteed unique, instead use VM ID and Subscription to correlate | ||
ramiro-gamarra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Properties: map[string]string{ | ||
| logger.CNSConfigPropertyStr: string(bb), | ||
| logger.CNSConfigMD5CheckSumPropertyStr: csStr, | ||
| }, | ||
| } | ||
|
|
||
| return event, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package metric | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/Azure/azure-container-networking/cns/configuration" | ||
| "github.com/Azure/azure-container-networking/cns/logger" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestCreateCNSConfigSnapshotEvent(t *testing.T) { | ||
| logger.InitLogger("testlogs", 0, 0, "./") | ||
|
|
||
| config, err := configuration.ReadConfig("../configuration/testdata/good.json") | ||
| require.NoError(t, err) | ||
|
|
||
| event, err := createCNSConfigSnapshotEvent(config) | ||
| require.NoError(t, err) | ||
|
|
||
| assert.Equal(t, logger.ConfigSnapshotMetricsStr, event.EventName) | ||
| assert.NotEmpty(t, event.ResourceID) | ||
| assert.Contains(t, event.Properties[logger.CNSConfigPropertyStr], "\"TLSPort\":\"10091\"") | ||
|
|
||
| eventConfig := &configuration.CNSConfig{} | ||
| err = json.Unmarshal([]byte(event.Properties[logger.CNSConfigPropertyStr]), eventConfig) //nolint:musttag // no tag needed for config | ||
| require.NoError(t, err) | ||
| assert.EqualValues(t, config, eventConfig) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.