Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
Resolves prometheus#77 add cmdline flag to drop unmapped
Browse files Browse the repository at this point in the history
* Adds `-statsd.drop-unmapped` cmdline flag which causes statsd metrics
  that don't match a mapping to be dropped. New flag defaults to "false"

Add test for dropUnmapped behavior

Convert dropUnmapped to a attribute of the Exporter

  * Removed references to dropUnmapped global var in favor of a boolean
    attribute of `Exporter`
  * Updated tests to reflect refactor
  * Reordered command line options per PR review
  • Loading branch information
Dave Rawks authored and robfig committed Oct 4, 2017
1 parent 57fd5de commit 66776a4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
27 changes: 16 additions & 11 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,12 @@ func (c *TimerEvent) Labels() map[string]string { return c.labels }
type Events []Event

type Exporter struct {
Counters *CounterContainer
Gauges *GaugeContainer
Summaries *SummaryContainer
Histograms *HistogramContainer
mapper *metricMapper
Counters *CounterContainer
Gauges *GaugeContainer
Summaries *SummaryContainer
Histograms *HistogramContainer
mapper *metricMapper
dropUnmapped bool
}

func escapeMetricName(metricName string) string {
Expand Down Expand Up @@ -257,6 +258,9 @@ func (b *Exporter) Listen(e <-chan Events) {
}
} else {
eventsUnmapped.Inc()
if b.dropUnmapped == true {
continue
}
metricName = escapeMetricName(event.MetricName())
}

Expand Down Expand Up @@ -351,13 +355,14 @@ func (b *Exporter) Listen(e <-chan Events) {
}
}

func NewExporter(mapper *metricMapper) *Exporter {
func NewExporter(mapper *metricMapper, dropUnmapped bool) *Exporter {
return &Exporter{
Counters: NewCounterContainer(),
Gauges: NewGaugeContainer(),
Summaries: NewSummaryContainer(),
Histograms: NewHistogramContainer(mapper),
mapper: mapper,
Counters: NewCounterContainer(),
Gauges: NewGaugeContainer(),
Summaries: NewSummaryContainer(),
Histograms: NewHistogramContainer(mapper),
mapper: mapper,
dropUnmapped: dropUnmapped,
}
}

Expand Down
42 changes: 40 additions & 2 deletions exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,30 @@ func TestNegativeCounter(t *testing.T) {
},
}
events <- c
ex := NewExporter(&metricMapper{})
ex := NewExporter(&metricMapper{}, false)

// Close channel to signify we are done with the listener after a short period.
go func() {
time.Sleep(time.Millisecond * 100)
close(events)
}()

ex.Listen(events)
}

// TestDropUnmapped validates that the dropUnmapped global will prevent unmapped
// metrics from being recorded when set true and that they are recorded when set
// false.
func TestDropUnmapped(t *testing.T) {
events := make(chan Events, 1)
c := Events{
&CounterEvent{
metricName: "foo",
value: 1,
},
}
events <- c
ex := NewExporter(&metricMapper{}, true, true)

// Close channel to signify we are done with the listener after a short period.
go func() {
Expand All @@ -53,14 +76,29 @@ func TestNegativeCounter(t *testing.T) {
}()

ex.Listen(events)
if len(ex.Counters.Elements) != 0 {
t.Fatalf("Unmapped metric recorded inspite of dropUnmapped being set true")
}
ex = NewExporter(&metricMapper{}, true, false)
events = make(chan Events, 1)
events <- c
// Close channel to signify we are done with the listener after a short period.
go func() {
time.Sleep(time.Millisecond * 100)
close(events)
}()
ex.Listen(events)
if len(ex.Counters.Elements) != 1 {
t.Fatalf("Unmapped metric not recorded inspite of dropUnmapped being set false")
}
}

// TestInvalidUtf8InDatadogTagValue validates robustness of exporter listener
// against datadog tags with invalid tag values.
// It sends the same tags first with a valid value, then with an invalid one.
// The exporter should not panic, but drop the invalid event
func TestInvalidUtf8InDatadogTagValue(t *testing.T) {
ex := NewExporter(&metricMapper{})
ex := NewExporter(&metricMapper{}, false)
for _, l := range []statsDPacketHandler{&StatsDUDPListener{}, &mockStatsDTCPListener{}} {
events := make(chan Events, 2)

Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var (
mappingConfig = flag.String("statsd.mapping-config", "", "Metric mapping configuration file name.")
readBuffer = flag.Int("statsd.read-buffer", 0, "Size (in bytes) of the operating system's transmit read buffer associated with the UDP connection. Please make sure the kernel parameters net.core.rmem_max is set to a value greater than the value specified.")
showVersion = flag.Bool("version", false, "Print version information.")
dropUnmapped = flag.Bool("statsd.drop-unmapped", false, "Drop statsd metrics which have no matched mapping configured.")
)

func serveHTTP() {
Expand Down Expand Up @@ -194,6 +195,6 @@ func main() {
}
go watchConfig(*mappingConfig, mapper)
}
exporter := NewExporter(mapper)
exporter := NewExporter(mapper, *dropUnmapped)
exporter.Listen(events)
}

0 comments on commit 66776a4

Please sign in to comment.