Skip to content

Commit

Permalink
Add Graphite metric gatherer creation and usage support to the dataso…
Browse files Browse the repository at this point in the history
…urce gatherer

Signed-off-by: Xabier Larrakoetxea <[email protected]>
  • Loading branch information
slok committed May 20, 2019
1 parent 33695fe commit 08429ad
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
28 changes: 28 additions & 0 deletions internal/service/metric/datasource/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"net/http"
"time"

prometheusapi "github.com/prometheus/client_golang/api"
Expand All @@ -12,9 +13,14 @@ import (
"github.com/slok/grafterm/internal/model"
"github.com/slok/grafterm/internal/service/metric"
"github.com/slok/grafterm/internal/service/metric/fake"
"github.com/slok/grafterm/internal/service/metric/graphite"
"github.com/slok/grafterm/internal/service/metric/prometheus"
)

const (
defGraphiteTimeout = 7 * time.Second
)

// ConfigGatherer is the configuration of the multi Gatherer.
type ConfigGatherer struct {
// DashboardDatasources are the datasources that are on the dashboards and
Expand All @@ -32,6 +38,8 @@ type ConfigGatherer struct {
CreateFakeFunc func(ds model.FakeDatasource) (metric.Gatherer, error)
// CreatePrometheusFunc is the function that will be called to create Prometheus gatherers.
CreatePrometheusFunc func(ds model.PrometheusDatasource) (metric.Gatherer, error)
// CreateGraphiteFunc is the function that will be called to create Graphite gatherers.
CreateGraphiteFunc func(ds model.GraphiteDatasource) (metric.Gatherer, error)
}

func (c *ConfigGatherer) defaults() {
Expand All @@ -58,6 +66,24 @@ func (c *ConfigGatherer) defaults() {
return g, nil
}
}

// Set default creator function for Graphite.
if c.CreateGraphiteFunc == nil {
c.CreateGraphiteFunc = func(ds model.GraphiteDatasource) (metric.Gatherer, error) {
g, err := graphite.NewGatherer(graphite.ConfigGatherer{
GraphiteAPIURL: ds.Address,
HTTPCli: &http.Client{
Timeout: defGraphiteTimeout,
},
})
if err != nil {
return nil, err
}

return g, nil
}
}

if c.Aliases == nil {
c.Aliases = map[string]string{}
}
Expand Down Expand Up @@ -148,6 +174,8 @@ func createGatherer(cfg ConfigGatherer, ds model.Datasource) (metric.Gatherer, e
switch {
case ds.Prometheus != nil:
return cfg.CreatePrometheusFunc(*ds.Prometheus)
case ds.Graphite != nil:
return cfg.CreateGraphiteFunc(*ds.Graphite)
case ds.Fake != nil:
return cfg.CreateFakeFunc(*ds.Fake)
}
Expand Down
7 changes: 6 additions & 1 deletion internal/service/metric/datasource/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestGathererGatherSingle(t *testing.T) {
datasources2 := []model.Datasource{
model.Datasource{
ID: "ds2",
DatasourceSource: model.DatasourceSource{Fake: &model.FakeDatasource{}},
DatasourceSource: model.DatasourceSource{Graphite: &model.GraphiteDatasource{}},
},
model.Datasource{
ID: "ds3",
Expand Down Expand Up @@ -150,6 +150,11 @@ func TestGathererGatherSingle(t *testing.T) {
gCount++
return g, nil
},
CreateGraphiteFunc: func(_ model.GraphiteDatasource) (metric.Gatherer, error) {
g := mgs[gCount]
gCount++
return g, nil
},
})
require.NoError(err)

Expand Down

0 comments on commit 08429ad

Please sign in to comment.