From bc5d3b06c9cfd9ae962a142e71e37cbf6e3ae1e5 Mon Sep 17 00:00:00 2001 From: Han Kang Date: Wed, 12 Oct 2022 13:42:48 -0700 Subject: [PATCH] account for timing ratio histogram function calls Change-Id: Ib27d6018657e4221c36645860bdb9cb9fcf7ebf5 --- test/instrumentation/decode_metric.go | 57 ++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/test/instrumentation/decode_metric.go b/test/instrumentation/decode_metric.go index 2100e3cd6b3..ef0ee99e3ae 100644 --- a/test/instrumentation/decode_metric.go +++ b/test/instrumentation/decode_metric.go @@ -60,6 +60,14 @@ func (c *metricDecoder) decodeNewMetricCall(fc *ast.CallExpr) (*metric, error) { var err error se, ok := fc.Fun.(*ast.SelectorExpr) if !ok { + // account for timing ratio histogram functions + switch v := fc.Fun.(type) { + case *ast.Ident: + if v.Name == "NewTimingRatioHistogramVec" { + m, err = c.decodeMetricVecForTimingRatioHistogram(fc) + return &m, err + } + } return nil, newDecodeErrorf(fc, errNotDirectCall) } functionName := se.Sel.String() @@ -97,7 +105,7 @@ func getMetricType(functionName string) string { return histogramMetricType case "NewSummary", "NewSummaryVec": return summaryMetricType - case "NewTimingHistogram", "NewTimingHistogramVec": + case "NewTimingHistogram", "NewTimingHistogramVec", "NewTimingRatioHistogramVec": return timingRatioHistogram default: panic("getMetricType expects correct function name") @@ -128,6 +136,53 @@ func (c *metricDecoder) decodeMetricVec(call *ast.CallExpr) (metric, error) { return m, nil } +func (c *metricDecoder) decodeMetricVecForTimingRatioHistogram(call *ast.CallExpr) (metric, error) { + m, err := c.decodeOpts(call.Args[0]) + if err != nil { + return m, err + } + labels, err := c.decodeLabelsFromArray(call.Args[1:]) + if err != nil { + return m, err + } + sort.Strings(labels) + m.Labels = labels + return m, nil +} + +func (c *metricDecoder) decodeLabelsFromArray(exprs []ast.Expr) ([]string, error) { + retval := []string{} + for _, e := range exprs { + id, ok := e.(*ast.Ident) + if !ok { + if bl, ok := e.(*ast.BasicLit); ok { + v, err := stringValue(bl) + if err != nil { + return nil, err + } + retval = append(retval, v) + continue + } + return nil, newDecodeErrorf(e, errInvalidNewMetricCall) + } + variableExpr, found := c.variables[id.Name] + if !found { + return nil, newDecodeErrorf(e, "couldn't find variable for labels") + } + bl, ok := variableExpr.(*ast.BasicLit) + if !ok { + return nil, newDecodeErrorf(e, "couldn't interpret variable for labels") + } + v, err := stringValue(bl) + if err != nil { + return nil, err + } + retval = append(retval, v) + } + + return retval, nil +} + func (c *metricDecoder) decodeLabels(expr ast.Expr) ([]string, error) { cl, ok := expr.(*ast.CompositeLit) if !ok {