Skip to content

Commit

Permalink
fix(prometheus_sink): log errors and fallback to default metric mapper
Browse files Browse the repository at this point in the history
Signed-off-by: harpunius <[email protected]>
  • Loading branch information
harpunius committed Oct 8, 2024
1 parent 28b1629 commit 088ab19
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/stats/prom/prometheus_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,24 @@ func NewPrometheusSink(opts ...prometheusSinkOption) gostats.Sink {
logrus.Infof("Starting prometheus sink on %s%s", sink.config.addr, sink.config.path)
_ = http.ListenAndServe(sink.config.addr, nil)
}()
if sink.config.mapperYamlPath != "" {
_ = sink.mapper.InitFromFile(sink.config.mapperYamlPath)
} else {
_ = sink.mapper.InitFromYAMLString(defaultMapper)

useDefaultMapper := sink.config.mapperYamlPath == ""
if !useDefaultMapper {
err := sink.mapper.InitFromFile(sink.config.mapperYamlPath)
if err != nil {
logrus.Errorf("Failed parsing metric mapper from path %s", sink.config.mapperYamlPath)

// Fallback to using the default mapper
useDefaultMapper = true
}
}

if useDefaultMapper {
logrus.Infof("Using default metric mapper")
err := sink.mapper.InitFromYAMLString(defaultMapper)
if err != nil {
logrus.Error("Failed parsing default metric mapper")
}
}

sink.exp = exporter.NewExporter(promRegistry,
Expand Down
25 changes: 25 additions & 0 deletions src/stats/prom/prometheus_sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,28 @@ func TestFlushTimer(t *testing.T) {
return true
}, time.Second, time.Millisecond)
}

func TestPrometheusSinkMetricMapperDefaultHandling(t *testing.T) {
// Pass a metric mapper that doesn't exist
s := NewPrometheusSink(WithMapperYamlPath("file_not_found.yaml"), WithPath("/otherMetrics"))

s.FlushTimer("ratelimit.service.rate_limit.rds.database_users.within_limit", 1)
assert.Eventually(t, func() bool {
metricFamilies, err := prometheus.DefaultGatherer.Gather()
require.NoError(t, err)

metrics := make(map[string]*dto.MetricFamily)
for _, metricFamily := range metricFamilies {
metrics[*metricFamily.Name] = metricFamily
}

m, ok := metrics["ratelimit_service_rate_limit_within_limit"]
require.True(t, ok)
require.Len(t, m.Metric, 1)
require.Equal(t, map[string]string{
"domain": "rds",
"key1": "database_users",
}, toMap(m.Metric[0].Label))
return true
}, time.Second, time.Millisecond)
}

0 comments on commit 088ab19

Please sign in to comment.