From c9d5fcb7c79c764de445c20d53a0a343a4efe2dc Mon Sep 17 00:00:00 2001 From: Kebe Date: Mon, 18 Nov 2019 10:58:10 +0800 Subject: [PATCH] > use fmt.Sprintf instead of + --- storage/spl_engine.go | 16 +++++++++------- storage/spl_engine_test.go | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/storage/spl_engine.go b/storage/spl_engine.go index d029e13..57adad8 100644 --- a/storage/spl_engine.go +++ b/storage/spl_engine.go @@ -27,20 +27,22 @@ func MakeSPL(query *prompb.Query, c RemoteClient, index string) (string, error) step = 10 } ls := strings.Join(c.MetricLabels(metricName), " ") - search := "| mstats latest(_value) as " + CommonMetricValue + " where index=" + index + " AND metric_name=" + metricName + " span=" + strconv.FormatInt(step, 10) + "s by metric_name " + ls + search := fmt.Sprintf("| mstats latest(_value) as %s where index=%s AND metric_name=%s span=%ds by metric_name %s", + CommonMetricValue, index, metricName, step, ls) for _, m := range query.Matchers { if m.Name == "__name__" { m.Name = "metric_name" } switch m.Type { case prompb.LabelMatcher_RE: - search += "| regex " + m.Name + "=" + strconv.Quote(m.Value) + //search += fmt.Sprintf("| regex " + m.Name + "=" + strconv.Quote(m.Value)) + search += fmt.Sprintf("| regex %s=%q", m.Name, m.Value) case prompb.LabelMatcher_NRE: - search += "| regex " + m.Name + "!=" + strconv.Quote(m.Value) + search += fmt.Sprintf("| regex %s!=%q", m.Name, m.Value) case prompb.LabelMatcher_EQ: - search += "| where " + m.Name + "=" + strconv.Quote(m.Value) + search += fmt.Sprintf("| where %s=%q", m.Name, m.Value) case prompb.LabelMatcher_NEQ: - search += "| where " + m.Name + "!=" + strconv.Quote(m.Value) + search += fmt.Sprintf("| where %s!=%q", m.Name, m.Value) } } search += "| rename metric_name as " + CommonMetricName @@ -66,12 +68,12 @@ func TimeSeriesToPromMetrics(series prompb.TimeSeries) []SplunkMetricEvent { if metricName == "" { return nil } - mergedKey := metricName + "{" + strings.Join(labels, ",") + "}" + mergedKey := fmt.Sprintf("%s{%s}", metricName, strings.Join(labels, ",")) for _, sample := range series.Samples { valueTime := strconv.FormatFloat(sample.Value, 'f', -1, 64) res = append(res, SplunkMetricEvent{ Time: sample.Timestamp, - MetricStr: mergedKey + " " + valueTime, + MetricStr: fmt.Sprintf("%s %s", mergedKey, valueTime), }) } return res diff --git a/storage/spl_engine_test.go b/storage/spl_engine_test.go index 894796e..4134de5 100644 --- a/storage/spl_engine_test.go +++ b/storage/spl_engine_test.go @@ -222,6 +222,29 @@ func TestTimeSeriesToPromMetrics(t *testing.T) { }, }, }, + { + "float value - 1.23", + prompb.TimeSeries{ + Labels: []prompb.Label{ + { + Name: "__name__", + Value: "test", + }, + }, + Samples: []prompb.Sample{ + { + Value: 1.23, + Timestamp: 1, + }, + }, + }, + []SplunkMetricEvent{ + { + Time: 1, + MetricStr: "test{} 1.23", + }, + }, + }, { "multi values", prompb.TimeSeries{