Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: add customizable metric name prefix #111

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ For a more realistic example please have a look at [examples/kubernetes/configma
jobs:
# each job needs a unique name, it's used for logging and as an default label
- name: "example"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possible point of discussion: i considered using the job name as the prefix, but 2 things stopped me from that:

  1. it would change the existing behavior (sql_ prefix) in a non-backwards-compatible way. however, since this hasn't hit a 1.0 release, i wasn't sure if that would be ok
  2. the job name is already used as a default label

# prefix for all metric names; joined with Query name, separated by underscore.
# defaults to "sql". May be an empty string, which removes the name prefix and underscore entirely.
prefix: "pg"
# interval defined the pause between the runs of this job
interval: '5m'
# cron_schedule when to execute the job in the standard CRON syntax
Expand All @@ -134,7 +137,7 @@ jobs:
- 'SET idle_in_transaction_session_timeout = 100'
# queries is a map of Metric/Query mappings
queries:
# name is prefied with sql_ and used as the metric name
# name is prefixed with pg_ and used as the metric name; i.e. pg_running_queries
- name: "running_queries"
# help is a requirement of the Prometheus default registry, currently not
# used by the Prometheus server. Important: Must be the same for all metrics
Expand Down
21 changes: 11 additions & 10 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,16 @@ func (c *cronConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {

// Job is a collection of connections and queries
type Job struct {
log log.Logger
conns []*connection
Name string `yaml:"name"` // name of this job
KeepAlive bool `yaml:"keepalive"` // keep connection between runs?
Interval time.Duration `yaml:"interval"` // interval at which this job is run
CronSchedule cronConfig `yaml:"cron_schedule"` // if specified, the interval is ignored and the job will be executed at the specified time in CRON syntax
Connections []string `yaml:"connections"`
Queries []*Query `yaml:"queries"`
StartupSQL []string `yaml:"startup_sql"` // SQL executed on startup
log log.Logger
conns []*connection
Name string `yaml:"name"` // name of this job
MetricNamePrefix *string `yaml:"prefix"` // optional prefix for all metric names, may be empty string
KeepAlive bool `yaml:"keepalive"` // keep connection between runs?
Interval time.Duration `yaml:"interval"` // interval at which this job is run
CronSchedule cronConfig `yaml:"cron_schedule"` // if specified, the interval is ignored and the job will be executed at the specified time in CRON syntax
Connections []string `yaml:"connections"`
Queries []*Query `yaml:"queries"`
StartupSQL []string `yaml:"startup_sql"` // SQL executed on startup
}

type connection struct {
Expand All @@ -152,7 +153,7 @@ type Query struct {
metrics map[*connection][]prometheus.Metric
jobName string
AllowZeroRows bool `yaml:"allow_zero_rows"`
Name string `yaml:"name"` // the prometheus metric name
Name string `yaml:"name"` // the prometheus metric name, prefixed with Job.MetricPrefix
Help string `yaml:"help"` // the prometheus metric help text
Labels []string `yaml:"labels"` // expose these columns as labels per gauge
Values []string `yaml:"values"` // expose each of these as an gauge
Expand Down
16 changes: 15 additions & 1 deletion job.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import (
sqladmin "google.golang.org/api/sqladmin/v1beta4"
)

const (
defaultMetricNamePrefix string = "sql_"
)

var (
// MetricNameRE matches any invalid metric name
// characters, see github.com/prometheus/common/model.MetricNameRE
Expand Down Expand Up @@ -60,7 +64,7 @@ func (j *Job) Init(logger log.Logger, queries map[string]string) error {
q.metrics = make(map[*connection][]prometheus.Metric, len(j.Queries))
}
// try to satisfy prometheus naming restrictions
name := MetricNameRE.ReplaceAllString("sql_"+q.Name, "")
name := MetricNameRE.ReplaceAllString(j.metricName(q.Name), "")
help := q.Help
// prepare a new metrics descriptor
//
Expand All @@ -79,6 +83,16 @@ func (j *Job) Init(logger log.Logger, queries map[string]string) error {
return nil
}

func (j *Job) metricName(queryName string) string {
prefix := defaultMetricNamePrefix
if jmp := j.MetricNamePrefix; jmp != nil {
prefix = *jmp
}

name := prefix + queryName
return name
}

func (j *Job) updateConnections() {
// if there are no connection URLs for this job it can't be run
if j.Connections == nil {
Expand Down