diff --git a/README.md b/README.md index 93630f50..2a3f2ef0 100644 --- a/README.md +++ b/README.md @@ -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" + # 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 @@ -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 diff --git a/config.go b/config.go index 0b7549f2..0e5cdf15 100644 --- a/config.go +++ b/config.go @@ -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 { @@ -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 diff --git a/job.go b/job.go index e3f9ec8e..540a36d7 100644 --- a/job.go +++ b/job.go @@ -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 @@ -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 // @@ -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 {