Skip to content

Commit e42e41f

Browse files
paulgmillerrbtr
andauthored
feat: wrap a histogram around syncHostNCVersion (#1273)
* wrap a histogram around syncHostNCVersion * try and fix linter errros though dubious about one * clean up Signed-off-by: Evan Baker <[email protected]> * kick pr? * use success to be consistent with dnc Co-authored-by: Evan Baker <[email protected]>
1 parent 6ec4203 commit e42e41f

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

cns/restserver/internalapi.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/http/httptest"
1313
"reflect"
1414
"strconv"
15+
"time"
1516

1617
"github.com/Azure/azure-container-networking/cns"
1718
"github.com/Azure/azure-container-networking/cns/logger"
@@ -152,6 +153,17 @@ func (service *HTTPRestService) SyncNodeStatus(
152153
func (service *HTTPRestService) SyncHostNCVersion(ctx context.Context, channelMode string) {
153154
service.Lock()
154155
defer service.Unlock()
156+
start := time.Now()
157+
err := service.syncHostNCVersion(ctx, channelMode)
158+
if err != nil {
159+
logger.Errorf("sync host error %v", err)
160+
}
161+
syncHostNcVersion.WithLabelValues(strconv.FormatBool(err == nil)).Observe(time.Since(start).Seconds())
162+
}
163+
164+
var errNonExistentContainerStatus = errors.New("nonExistantContainerstatus")
165+
166+
func (service *HTTPRestService) syncHostNCVersion(ctx context.Context, channelMode string) error {
155167
var hostVersionNeedsUpdateContainers []string
156168
for idx := range service.state.ContainerStatus {
157169
// Will open a separate PR to convert all the NC version related variable to int. Change from string to int is a pain.
@@ -173,12 +185,11 @@ func (service *HTTPRestService) SyncHostNCVersion(ctx context.Context, channelMo
173185
}
174186
}
175187
if len(hostVersionNeedsUpdateContainers) == 0 {
176-
return
188+
return nil
177189
}
178190
ncList, err := service.nmagentClient.GetNCVersionList(ctx)
179191
if err != nil {
180-
logger.Errorf("%v", err)
181-
return
192+
return errors.Wrap(err, "failed to get nc version list from nmagent")
182193
}
183194

184195
newHostNCVersionList := map[string]string{}
@@ -192,15 +203,13 @@ func (service *HTTPRestService) SyncHostNCVersion(ctx context.Context, channelMo
192203
}
193204
version, err := strconv.Atoi(versionStr)
194205
if err != nil {
195-
logger.Errorf("failed to parse %s to int", versionStr)
196-
continue
206+
return errors.Wrapf(err, "failed to parse container version of %s", ncID)
197207
}
198208

199209
// Check whether it exist in service state and get the related nc info
200210
ncInfo, exist := service.state.ContainerStatus[ncID]
201211
if !exist {
202-
logger.Errorf("Can't find NC with ID %s in service state, stop updating this host NC version", ncID)
203-
continue
212+
return errors.Wrapf(errNonExistentContainerStatus, "can't find NC with ID %s in service state, stop updating this host NC version", ncID)
204213
}
205214
if channelMode == cns.CRD {
206215
service.MarkIpsAsAvailableUntransacted(ncInfo.ID, version)
@@ -210,6 +219,7 @@ func (service *HTTPRestService) SyncHostNCVersion(ctx context.Context, channelMo
210219
service.state.ContainerStatus[ncID] = ncInfo
211220
logger.Printf("Updated NC %s host version from %s to %s", ncID, oldHostNCVersion, ncInfo.HostVersion)
212221
}
222+
return nil
213223
}
214224

215225
// This API will be called by CNS RequestController on CRD update.

cns/restserver/metrics.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,22 @@ var ipConfigStatusStateTransitionTime = prometheus.NewHistogramVec(
3939
[]string{"previous_state", "next_state"},
4040
)
4141

42+
var syncHostNcVersion = prometheus.NewHistogramVec(
43+
prometheus.HistogramOpts{
44+
Name: "sync_host_nc_version",
45+
Help: "Sync Host NC Latency",
46+
//nolint:gomnd // default bucket consts
47+
Buckets: prometheus.ExponentialBuckets(0.001, 2, 15), // 1 ms to ~16 seconds
48+
},
49+
[]string{"success"},
50+
)
51+
4252
func init() {
4353
metrics.Registry.MustRegister(
4454
httpRequestLatency,
4555
ipAssignmentLatency,
4656
ipConfigStatusStateTransitionTime,
57+
syncHostNcVersion,
4758
)
4859
}
4960

0 commit comments

Comments
 (0)