Skip to content

Commit

Permalink
influx output format support
Browse files Browse the repository at this point in the history
  • Loading branch information
portertech committed Oct 2, 2017
1 parent 3373f23 commit 8d67f82
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions sensu-prometheus-collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"flag"
"fmt"
"os"
"time"

"github.com/prometheus/client_golang/api/prometheus"
Expand Down Expand Up @@ -66,11 +67,37 @@ func CreateGraphiteMetrics(samples model.Vector) string {
return metrics
}

func CreateInfluxMetrics(samples model.Vector) string {
metrics := ""

for _, sample := range samples {
metric := string(sample.Metric["__name__"])

for name, value := range sample.Metric {
if name != "__name__" {
metric += fmt.Sprintf(",%s=%s", name, value)
}
}

value := sample.Value

now := time.Now()
timestamp := now.Unix()

metric += fmt.Sprintf(" value=%f %v\n", value, timestamp)

metrics += metric
}

return metrics
}

func OutputMetrics(samples model.Vector, outputFormat string) error {
output := ""

switch outputFormat {
case "influx":
output = CreateInfluxMetrics(samples)
case "graphite":
output = CreateGraphiteMetrics(samples)
case "json":
Expand Down Expand Up @@ -120,13 +147,13 @@ func main() {

if err != nil {
fmt.Errorf("%v", err)
return
os.Exit(2)
}

err = OutputMetrics(samples, *outputFormat)

if err != nil {
fmt.Errorf("%v", err)
return
os.Exit(2)
}
}

0 comments on commit 8d67f82

Please sign in to comment.