Skip to content
This repository has been archived by the owner on Nov 5, 2021. It is now read-only.

Commit

Permalink
Provide an option to add a prefix to all prometheus metrics.
Browse files Browse the repository at this point in the history
This has been a much asked feature:
#195

PiperOrigin-RevId: 250729862
  • Loading branch information
manugarg committed May 31, 2019
1 parent bb0061b commit 95ecced
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 7 additions & 3 deletions surfacers/prometheus/prometheus.go
Expand Up @@ -101,6 +101,7 @@ type httpWriter struct {
// Data key represents a unique combination of metric name and labels.
type PromSurfacer struct {
c *configpb.SurfacerConf // Configuration
prefix string // Metrics prefix, e.g. "cloudprober_"
emChan chan *metrics.EventMetrics // Buffered channel to store incoming EventMetrics
metrics map[string]*promMetric // Metric name to promMetric mapping
metricNames []string // Metric names, to keep names ordered.
Expand Down Expand Up @@ -128,6 +129,7 @@ func New(config *configpb.SurfacerConf, l *logger.Logger) (*PromSurfacer, error)
emChan: make(chan *metrics.EventMetrics, config.GetMetricsBufferSize()),
queryChan: make(chan *httpWriter, queriesQueueSize),
metrics: make(map[string]*promMetric),
prefix: config.GetMetricsPrefix(),
metricNameRe: regexp.MustCompile(ValidMetricNameRegex),
labelNameRe: regexp.MustCompile(ValidLabelNameRegex),
l: l,
Expand Down Expand Up @@ -258,10 +260,12 @@ func (ps *PromSurfacer) checkLabelName(k string) string {
return labelName
}

// checkMetricName finds a prometheus metric name for an incoming metric. If metric
// promMetricName finds a prometheus metric name for an incoming metric. If metric
// is found to be invalid even after some basic conversions, a zero string is
// returned.
func (ps *PromSurfacer) checkMetricName(k string) string {
func (ps *PromSurfacer) promMetricName(k string) string {
k = ps.prefix + k

// Before checking with regex, see if this metric name is
// already known. This block will be entered only once per
// metric name.
Expand Down Expand Up @@ -309,7 +313,7 @@ func (ps *PromSurfacer) record(em *metrics.EventMetrics) {
}

for _, metricName := range em.MetricsKeys() {
pMetricName := ps.checkMetricName(metricName)
pMetricName := ps.promMetricName(metricName)
if pMetricName == "" {
// No prometheus metric name found for this metric.
continue
Expand Down
7 changes: 7 additions & 0 deletions surfacers/prometheus/proto/config.proto
Expand Up @@ -6,6 +6,8 @@ message SurfacerConf {
// How many metrics entries (EventMetrics) to buffer. Incoming metrics
// processing is paused while serving data to prometheus. This buffer is to
// make writes to prometheus surfacer non-blocking.
// NOTE: This field is confusing for users and will be removed from the config
// after v0.10.3.
optional int64 metrics_buffer_size = 1 [default = 10000];

// Whether to include timestamps in metrics. If enabled (default) each metric
Expand All @@ -17,4 +19,9 @@ message SurfacerConf {

// URL that prometheus scrapes metrics from.
optional string metrics_url = 3 [default = "/metrics"];

// Prefix to add to all metric names. For example setting this field to
// "cloudprober_" will result in metrics with names:
// cloudprober_total, cloudprober_success, cloudprober_latency, ..
optional string metrics_prefix = 4;
}

0 comments on commit 95ecced

Please sign in to comment.