Skip to content

Commit

Permalink
Add support for static labels in queries.yaml (#4)
Browse files Browse the repository at this point in the history
* add support for static labels in queries.yaml. Fixes #3

* update README.md with the example query and short description

* small renaming fixes proposed by crevil, along with a small correction in the README

* add a description in the design section about static label values
  • Loading branch information
kaspernissen authored Feb 4, 2019
1 parent 9cefec7 commit 137400d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 17 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ queries:
repo: humio
interval: 30m
metric_name: humio_status_sum
metric_labels:
- key: squad
value: nasa
```
As seen in the last example query, you can also specify a set of static labels to be outputtet along with the metric.

Currently the export supports the above aggregate query functions

```
Expand Down Expand Up @@ -111,11 +116,24 @@ The specified queries in the configuration file will be exporter with two labels
- `repo` - the repository that the query was executed against
- `interval` - the interval the query result represent.

Here is an example.
There is an option to add static labels as well. This can be done as follows:

```
- query: sum(status)
repo: humio
interval: 30m
metric_name: humio_status_sum
metric_labels:
- key: squad
value: nasa
```

Example.

```
humio_total{interval="5m", repo="humio"} 3458
humio_audit_total{interval="5m", repo="humio-audit"} 2976
humio_status_sum{interval="30m", repo="humio", squad="nasa"} 235
```

# Build
Expand Down
3 changes: 3 additions & 0 deletions examples/queries.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ queries:
repo: humio
interval: 30m
metric_name: humio_status_sum
metric_labels:
- key: squad
value: nasa



Expand Down
3 changes: 2 additions & 1 deletion humio.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type startQueryPayload struct {
IsLive bool `json:"isLive"`
}

func (c *client) startQueryJob(query, repo, metricName, start, end string) (queryJob, error) {
func (c *client) startQueryJob(query, repo, metricName, start, end string, labels []MetricLabel) (queryJob, error) {
postData := startQueryPayload{
QueryString: query,
Start: start,
Expand Down Expand Up @@ -57,6 +57,7 @@ func (c *client) startQueryJob(query, repo, metricName, start, end string) (quer
queryResponse.Timespan = start
queryResponse.Repo = repo
queryResponse.MetricName = metricName
queryResponse.MetricLabels = labels

return queryResponse, nil
}
Expand Down
52 changes: 37 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import (
)

type queryJob struct {
Id string
Timespan string
Repo string
MetricName string
Id string
Timespan string
Repo string
MetricName string
MetricLabels []MetricLabel
}

type queryJobData struct {
Expand All @@ -39,13 +40,19 @@ type MetricMap struct {

type YamlConfig struct {
Queries []struct {
Query string `yaml:"query"`
Repo string `yaml:"repo"`
Interval string `yaml:"interval"`
MetricName string `yaml:"metric_name"`
Query string `yaml:"query"`
Repo string `yaml:"repo"`
Interval string `yaml:"interval"`
MetricName string `yaml:"metric_name"`
MetricLabels []MetricLabel `yaml:"metric_labels"`
} `yaml:"queries"`
}

type MetricLabel struct {
Key string `yaml:"key"`
Value string `yaml:"value"`
}

var (
version = ""
supportedFunctions = []string{"_count", "_min", "_max", "_avg", "_rate", "_range", "_stddev", "_sum"}
Expand Down Expand Up @@ -91,7 +98,7 @@ func main() {
}

for _, q := range yamlConfig.Queries {
metricMap.AddGauge(q.MetricName, q.Repo, q.Interval)
metricMap.AddGauge(q.MetricName, q.MetricLabels)
}

err = metricMap.Register()
Expand Down Expand Up @@ -142,7 +149,7 @@ func runAPIPolling(done chan error, url, token string, yamlConfig YamlConfig, re
var jobs []queryJob

for _, q := range yamlConfig.Queries {
job, err := client.startQueryJob(q.Query, q.Repo, q.MetricName, q.Interval, "now")
job, err := client.startQueryJob(q.Query, q.Repo, q.MetricName, q.Interval, "now", q.MetricLabels)
if err != nil {
done <- err
return
Expand Down Expand Up @@ -185,7 +192,7 @@ func runAPIPolling(done chan error, url, token string, yamlConfig YamlConfig, re
}

if poll.Done {
metricMap.UpdateMetricValue(job.MetricName, job.Timespan, job.Repo, floatValue)
metricMap.UpdateMetricValue(job.MetricName, job.Timespan, job.Repo, floatValue, job.MetricLabels)
if err != nil {
done <- err
return
Expand All @@ -212,17 +219,32 @@ func (m *MetricMap) Register() error {
return nil
}

func (m *MetricMap) UpdateMetricValue(metricName, timespan, repo string, value float64) error {
func (m *MetricMap) UpdateMetricValue(metricName, timespan, repo string, value float64, staticLabels []MetricLabel) error {

labels := make(map[string]string)
labels[intervalLabel] = timespan
labels[repoLabel] = repo
for _, l := range staticLabels {
labels[l.Key] = l.Value
}

gauge := m.Gauges[metricName]
gauge.WithLabelValues(timespan, repo).Set(value)
gauge.With(labels).Set(value)
return nil
}

func (m *MetricMap) AddGauge(metricName, repo, interval string) error {
func (m *MetricMap) AddGauge(metricName string, staticLabels []MetricLabel) error {
var labelKeys []string
labelKeys = append(labelKeys, intervalLabel)
labelKeys = append(labelKeys, repoLabel)
for _, l := range staticLabels {
labelKeys = append(labelKeys, l.Key)
}

m.Gauges[metricName] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: metricName,
Help: "Gauge for humio query",
}, []string{intervalLabel, repoLabel})
}, labelKeys)
return nil
}

0 comments on commit 137400d

Please sign in to comment.