Skip to content

Commit a0d6f80

Browse files
authored
Cleanup metrics statuses (#475)
1 parent 37a002e commit a0d6f80

File tree

2 files changed

+66
-48
lines changed

2 files changed

+66
-48
lines changed

cli/cmd/get.go

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -244,18 +244,22 @@ func describeAPI(name string, resourcesRes *schema.GetResourcesResponse, flagVer
244244
Rows: [][]interface{}{row},
245245
}
246246

247-
var predictionMetrics string
247+
var out string
248248
apiMetrics, err := getAPIMetrics(ctx.App.Name, api.Name)
249-
if err != nil || apiMetrics == nil {
250-
predictionMetrics = "\nmetrics are not available yet\n"
251-
} else {
252-
statusTable = appendNetworkMetrics(statusTable, apiMetrics)
253-
if api.Tracker != nil {
254-
predictionMetrics = "\n" + predictionMetricsTable(apiMetrics, api) + "\n"
249+
statusTable = appendNetworkMetrics(statusTable, apiMetrics) // adds blank stats when there is an error
250+
out = "\n" + table.MustFormat(statusTable) + "\n"
251+
252+
var predictionMetrics string
253+
if err != nil {
254+
if !strings.Contains(err.Error(), "api is still initializing") {
255+
predictionMetrics = fmt.Sprintf("\nerror fetching metrics: %s\n", err.Error())
255256
}
256257
}
257258

258-
out := "\n" + table.MustFormat(statusTable) + "\n"
259+
if api.Tracker != nil && len(predictionMetrics) == 0 {
260+
predictionMetrics = "\n" + predictionMetricsTable(apiMetrics, api) + "\n"
261+
}
262+
259263
out += predictionMetrics
260264

261265
out += "\n" + console.Bold("url: ") + apiEndpoint
@@ -275,44 +279,55 @@ func describeAPI(name string, resourcesRes *schema.GetResourcesResponse, flagVer
275279
return out, nil
276280
}
277281

278-
func getAPIMetrics(appName, apiName string) (*schema.APIMetrics, error) {
282+
func getAPIMetrics(appName, apiName string) (schema.APIMetrics, error) {
279283
params := map[string]string{"appName": appName, "apiName": apiName}
280284
httpResponse, err := HTTPGet("/metrics", params)
281285
if err != nil {
282-
return nil, err
286+
return schema.APIMetrics{}, err
283287
}
284288

285289
var apiMetrics schema.APIMetrics
286290
err = json.Unmarshal(httpResponse, &apiMetrics)
287291
if err != nil {
288-
return nil, err
292+
return schema.APIMetrics{}, err
289293
}
290294

291-
return &apiMetrics, nil
295+
return apiMetrics, nil
292296
}
293297

294-
func appendNetworkMetrics(apiTable table.Table, apiMetrics *schema.APIMetrics) table.Table {
295-
headers := []table.Header{
296-
{Title: "avg latency", Hidden: apiMetrics.NetworkStats.Latency == nil},
297-
{Title: "2XX", Hidden: apiMetrics.NetworkStats.Code2XX == 0},
298-
{Title: "4XX", Hidden: apiMetrics.NetworkStats.Code4XX == 0},
299-
{Title: "5XX", Hidden: apiMetrics.NetworkStats.Code5XX == 0},
300-
}
301-
302-
latency := ""
303-
if apiMetrics.NetworkStats.Latency != nil {
304-
if *apiMetrics.NetworkStats.Latency < 1000 {
305-
latency = fmt.Sprintf("%.6g ms", *apiMetrics.NetworkStats.Latency)
306-
} else {
307-
latency = fmt.Sprintf("%.6g s", (*apiMetrics.NetworkStats.Latency)/1000)
298+
func appendNetworkMetrics(apiTable table.Table, apiMetrics schema.APIMetrics) table.Table {
299+
latency := "-"
300+
code2XX := "-"
301+
code4XX := 0
302+
code5XX := 0
303+
304+
if apiMetrics.NetworkStats != nil {
305+
code4XX = apiMetrics.NetworkStats.Code4XX
306+
code5XX = apiMetrics.NetworkStats.Code5XX
307+
if apiMetrics.NetworkStats.Latency != nil {
308+
if *apiMetrics.NetworkStats.Latency < 1000 {
309+
latency = fmt.Sprintf("%.6g ms", *apiMetrics.NetworkStats.Latency)
310+
} else {
311+
latency = fmt.Sprintf("%.6g s", (*apiMetrics.NetworkStats.Latency)/1000)
312+
}
313+
}
314+
if apiMetrics.NetworkStats.Code2XX != 0 {
315+
code2XX = s.Int(apiMetrics.NetworkStats.Code2XX)
308316
}
309317
}
310318

319+
headers := []table.Header{
320+
{Title: "avg latency"},
321+
{Title: "2XX"},
322+
{Title: "4XX", Hidden: code4XX == 0},
323+
{Title: "5XX", Hidden: code5XX == 0},
324+
}
325+
311326
row := []interface{}{
312327
latency,
313-
apiMetrics.NetworkStats.Code2XX,
314-
apiMetrics.NetworkStats.Code4XX,
315-
apiMetrics.NetworkStats.Code5XX,
328+
code2XX,
329+
code4XX,
330+
code5XX,
316331
}
317332

318333
apiTable.Headers = append(apiTable.Headers, headers...)
@@ -321,7 +336,7 @@ func appendNetworkMetrics(apiTable table.Table, apiMetrics *schema.APIMetrics) t
321336
return apiTable
322337
}
323338

324-
func predictionMetricsTable(apiMetrics *schema.APIMetrics, api *context.API) string {
339+
func predictionMetricsTable(apiMetrics schema.APIMetrics, api *context.API) string {
325340
if api.Tracker == nil {
326341
return ""
327342
}
@@ -332,20 +347,23 @@ func predictionMetricsTable(apiMetrics *schema.APIMetrics, api *context.API) str
332347
return regressionMetricsTable(apiMetrics)
333348
}
334349

335-
func regressionMetricsTable(apiMetrics *schema.APIMetrics) string {
350+
func regressionMetricsTable(apiMetrics schema.APIMetrics) string {
336351
minStr := "-"
337-
if apiMetrics.RegressionStats.Min != nil {
338-
minStr = fmt.Sprintf("%.9g", *apiMetrics.RegressionStats.Min)
339-
}
340-
341352
maxStr := "-"
342-
if apiMetrics.RegressionStats.Max != nil {
343-
maxStr = fmt.Sprintf("%.9g", *apiMetrics.RegressionStats.Max)
344-
}
345-
346353
avgStr := "-"
347-
if apiMetrics.RegressionStats.Avg != nil {
348-
avgStr = fmt.Sprintf("%.9g", *apiMetrics.RegressionStats.Avg)
354+
355+
if apiMetrics.RegressionStats != nil {
356+
if apiMetrics.RegressionStats.Min != nil {
357+
minStr = fmt.Sprintf("%.9g", *apiMetrics.RegressionStats.Min)
358+
}
359+
360+
if apiMetrics.RegressionStats.Max != nil {
361+
maxStr = fmt.Sprintf("%.9g", *apiMetrics.RegressionStats.Max)
362+
}
363+
364+
if apiMetrics.RegressionStats.Avg != nil {
365+
avgStr = fmt.Sprintf("%.9g", *apiMetrics.RegressionStats.Avg)
366+
}
349367
}
350368

351369
t := table.Table{
@@ -360,7 +378,7 @@ func regressionMetricsTable(apiMetrics *schema.APIMetrics) string {
360378
return table.MustFormat(t)
361379
}
362380

363-
func classificationMetricsTable(apiMetrics *schema.APIMetrics) string {
381+
func classificationMetricsTable(apiMetrics schema.APIMetrics) string {
364382
classList := make([]string, len(apiMetrics.ClassDistribution))
365383

366384
i := 0

pkg/operator/workloads/metrics.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,21 @@ import (
3636
"github.com/cortexlabs/cortex/pkg/operator/config"
3737
)
3838

39-
func GetMetrics(appName, apiName string) (*schema.APIMetrics, error) {
39+
func GetMetrics(appName, apiName string) (schema.APIMetrics, error) {
4040
ctx := CurrentContext(appName)
4141
api := ctx.APIs[apiName]
4242

4343
apiSavedStatus, err := getAPISavedStatus(api.ID, api.WorkloadID, appName)
4444
if err != nil {
45-
return nil, err
45+
return schema.APIMetrics{}, err
4646
}
4747

4848
if apiSavedStatus == nil {
49-
return nil, errors.Wrap(ErrorAPIInitializing(), api.Name)
49+
return schema.APIMetrics{}, errors.Wrap(ErrorAPIInitializing(), api.Name)
5050
}
5151

5252
if apiSavedStatus.Start == nil {
53-
return nil, errors.Wrap(ErrorAPIInitializing(), api.Name)
53+
return schema.APIMetrics{}, errors.Wrap(ErrorAPIInitializing(), api.Name)
5454
}
5555

5656
apiStartTime := apiSavedStatus.Start.Truncate(time.Second)
@@ -83,12 +83,12 @@ func GetMetrics(appName, apiName string) (*schema.APIMetrics, error) {
8383
if len(requestList) != 0 {
8484
err = parallel.RunFirstErr(requestList...)
8585
if err != nil {
86-
return nil, err
86+
return schema.APIMetrics{}, err
8787
}
8888
}
8989

9090
mergedMetrics := realTimeMetrics.Merge(batchMetrics)
91-
return &mergedMetrics, nil
91+
return mergedMetrics, nil
9292
}
9393

9494
func getAPIMetricsFunc(ctx *context.Context, api *context.API, period int64, startTime *time.Time, endTime *time.Time, apiMetrics *schema.APIMetrics) func() error {

0 commit comments

Comments
 (0)