Skip to content

Commit

Permalink
metric ignore list
Browse files Browse the repository at this point in the history
closes #3
  • Loading branch information
thraxil committed Sep 17, 2017
1 parent 8759b28 commit f48f8d2
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 15 deletions.
5 changes: 5 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ Optionall, it also supports:
this metric if the fetch failed and a ~0~ otherwise. In other words,
this can be a simple availability check. If this isn't set, it will
simply ignore failed fetches.
- ~IgnoreMetrics~ - a list of metric names (corresponding to keys in
the expvar JSON) that will just be ignored and not passed along to
graphite. Handy if there are metrics that you just don't care about
and would rather that they don't take up space on your graphite
server.

~CheckInterval~ and ~Timeout~ can also be individually overridden for
each endpoint as well.
Expand Down
21 changes: 17 additions & 4 deletions endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type endpoint struct {
failureMetric string
checkInterval int
timeout int
ignoreMetrics map[string]struct{}
graphiteServer Submitable
fetcher fetcher
logger log.Logger
Expand All @@ -34,20 +35,28 @@ type metric struct {
Value float64
}

func newEndpoint(c endpointconfig, interval int, timeout int, g Submitable, fetcher fetcher, logger log.Logger) *endpoint {
func newEndpoint(c endpointconfig, interval int, timeout int, ignoreMetrics []string, g Submitable, fetcher fetcher, logger log.Logger) *endpoint {
if c.CheckInterval != 0 {
interval = c.CheckInterval
}
if c.Timeout != 0 {
timeout = c.Timeout
}
if len(c.IgnoreMetrics) > 0 {
ignoreMetrics = c.IgnoreMetrics
}
ignoreMap := make(map[string]struct{})
for _, m := range ignoreMetrics {
ignoreMap[m] = struct{}{}
}

return &endpoint{
url: c.URL,
prefix: c.Prefix,
checkInterval: interval,
failureMetric: c.FailureMetric,
timeout: timeout,
ignoreMetrics: ignoreMap,
graphiteServer: g,
fetcher: fetcher,
logger: logger,
Expand Down Expand Up @@ -93,7 +102,7 @@ func (e *endpoint) Gather(ctx context.Context) []metric {
}
e.logger.Log("msg", "good fetch")

metrics = metricsFromMap(m, e.prefix)
metrics = metricsFromMap(m, e.prefix, e.ignoreMetrics)
// success
if e.failureMetric != "" {
s := metric{Name: e.failureMetric, Value: 0.0}
Expand All @@ -103,15 +112,19 @@ func (e *endpoint) Gather(ctx context.Context) []metric {
return metrics
}

func metricsFromMap(m map[string]interface{}, prefix string) []metric {
func metricsFromMap(m map[string]interface{}, prefix string, ignore map[string]struct{}) []metric {
var metrics []metric
for k, v := range m {
_, exists := ignore[k]
if exists {
continue
}
key := fmt.Sprintf("%s.%s", prefix, k)
switch vv := v.(type) {
case float64:
metrics = append(metrics, metric{key, vv})
case map[string]interface{}:
nmetrics := metricsFromMap(vv, key)
nmetrics := metricsFromMap(vv, key, ignore)
for _, met := range nmetrics {
metrics = append(metrics, met)
}
Expand Down
42 changes: 32 additions & 10 deletions endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ func Test_metricsFromMap(t *testing.T) {
data := `{"foo": 10, "bar": 5}`
var f interface{}
json.Unmarshal([]byte(data), &f)
metrics := metricsFromMap(f.(map[string]interface{}), "")
ignore := make(map[string]struct{})
metrics := metricsFromMap(f.(map[string]interface{}), "", ignore)
if len(metrics) != 2 {
t.Error("wrong number of metrics found")
}

data = `{"foo": 10, "bar": 5, "baz": {"blah": 3}}`
json.Unmarshal([]byte(data), &f)
metrics = metricsFromMap(f.(map[string]interface{}), "")
metrics = metricsFromMap(f.(map[string]interface{}), "", ignore)
if len(metrics) != 3 {
t.Error("wrong number of metrics found")
}
Expand All @@ -46,7 +47,7 @@ func Test_newEndpoint(t *testing.T) {
Timeout: 60,
}
g := newGraphiteServer("1.2.3.4", 2003)
e := newEndpoint(c, 60, 60, g, httpFetcher{}, dummyLogger())
e := newEndpoint(c, 60, 60, []string{}, g, httpFetcher{}, dummyLogger())
if e.url != c.URL {
t.Error("lost the URL")
}
Expand All @@ -66,7 +67,7 @@ func Test_Submit(t *testing.T) {
Timeout: 60,
}
var g dummyGraphite
e := newEndpoint(c, 60, 60, g, httpFetcher{}, dummyLogger())
e := newEndpoint(c, 60, 60, []string{}, g, httpFetcher{}, dummyLogger())
var m []metric
err := e.Submit(m)
if err != nil {
Expand Down Expand Up @@ -116,15 +117,16 @@ func Test_Fetch(t *testing.T) {
Timeout: 60,
}
g := newGraphiteServer("1.2.3.4", 2003)
e := newEndpoint(c, 60, 60, g, dummyFetcher{}, dummyLogger())
e := newEndpoint(c, 60, 60, []string{}, g, dummyFetcher{}, dummyLogger())

ctx := context.TODO()

d, err := e.Fetch(ctx)
if err != nil {
t.Error("dummy fetcher shouldn't fail")
}
metrics := metricsFromMap(d, "")
ignore := make(map[string]struct{})
metrics := metricsFromMap(d, "", ignore)
if len(metrics) != 2 {
t.Error("wrong number of metrics found")
}
Expand All @@ -139,7 +141,7 @@ func Test_Gather(t *testing.T) {
Timeout: 60,
}
g := newGraphiteServer("1.2.3.4", 2003)
e := newEndpoint(c, 60, 60, g, dummyFetcher{}, dummyLogger())
e := newEndpoint(c, 60, 60, []string{}, g, dummyFetcher{}, dummyLogger())

ctx := context.TODO()
metrics := e.Gather(ctx)
Expand All @@ -150,6 +152,26 @@ func Test_Gather(t *testing.T) {

}

func Test_GatherIgnoreMetric(t *testing.T) {
c := endpointconfig{
URL: "http://example.com/",
Prefix: "test",
CheckInterval: 60,
Timeout: 60,
IgnoreMetrics: []string{"foo"},
}
g := newGraphiteServer("1.2.3.4", 2003)
e := newEndpoint(c, 60, 60, []string{}, g, dummyFetcher{}, dummyLogger())

ctx := context.TODO()
metrics := e.Gather(ctx)

if len(metrics) != 1 {
t.Error("wrong number of metrics found")
}

}

func Test_GatherWithFailureMetric(t *testing.T) {
c := endpointconfig{
URL: "http://example.com/",
Expand All @@ -159,7 +181,7 @@ func Test_GatherWithFailureMetric(t *testing.T) {
FailureMetric: "failure",
}
g := newGraphiteServer("1.2.3.4", 2003)
e := newEndpoint(c, 60, 60, g, dummyFetcher{}, dummyLogger())
e := newEndpoint(c, 60, 60, []string{}, g, dummyFetcher{}, dummyLogger())

ctx := context.TODO()
metrics := e.Gather(ctx)
Expand All @@ -178,7 +200,7 @@ func Test_Run(t *testing.T) {
Timeout: 60,
}
var g dummyGraphite
e := newEndpoint(c, 60, 60, g, dummyFetcher{}, dummyLogger())
e := newEndpoint(c, 60, 60, []string{}, g, dummyFetcher{}, dummyLogger())

ctx, cancel := context.WithCancel(context.TODO())

Expand All @@ -194,7 +216,7 @@ func Test_Run(t *testing.T) {
Timeout: 60,
}

e = newEndpoint(c, 60, 60, g, dummyFetcher{}, dummyLogger())
e = newEndpoint(c, 60, 60, []string{}, g, dummyFetcher{}, dummyLogger())

ctx, cancel = context.WithCancel(context.TODO())

Expand Down
4 changes: 3 additions & 1 deletion samlare.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ type endpointconfig struct {
CheckInterval int
Timeout int
FailureMetric string
IgnoreMetrics []string
}

type config struct {
CarbonHost string
CarbonPort int
CheckInterval int
Timeout int
IgnoreMetrics []string

Endpoints map[string]endpointconfig
}
Expand Down Expand Up @@ -88,7 +90,7 @@ func startEndpoints(conf *config, logger log.Logger) context.CancelFunc {

for k, endpoint := range conf.Endpoints {
elogger := log.With(logger, "endpoint", k)
e := newEndpoint(endpoint, conf.CheckInterval, conf.Timeout, g, httpFetcher{}, elogger)
e := newEndpoint(endpoint, conf.CheckInterval, conf.Timeout, conf.IgnoreMetrics, g, httpFetcher{}, elogger)
go e.Run(ctx)
}

Expand Down
1 change: 1 addition & 0 deletions sample-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ CarbonHost = "graphite.example.com"
CarbonPort = 2003
CheckInterval = 60000
Timeout = 3000
IgnoreMetrics = ["cmdline", "foo"]

[endpoints]

Expand Down

0 comments on commit f48f8d2

Please sign in to comment.