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