Skip to content

Commit

Permalink
Merge pull request #3 from kebe7jun/fix/use-sprint-intead-plus
Browse files Browse the repository at this point in the history
> use fmt.Sprintf instead of +
  • Loading branch information
kebe7jun authored Nov 18, 2019
2 parents e819c07 + c9d5fcb commit 29bff76
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
16 changes: 9 additions & 7 deletions storage/spl_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
23 changes: 23 additions & 0 deletions storage/spl_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 29bff76

Please sign in to comment.