2013-02-07 02:49:04 -08:00
|
|
|
// Copyright 2013 Prometheus Team
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2013-01-07 14:24:26 -08:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-04-30 11:20:14 -07:00
|
|
|
"regexp"
|
2013-01-07 14:24:26 -08:00
|
|
|
"time"
|
2014-12-10 07:16:49 -08:00
|
|
|
"code.google.com/p/goprotobuf/proto"
|
2013-08-13 07:14:18 -07:00
|
|
|
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
|
|
|
|
pb "github.com/prometheus/prometheus/config/generated"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/utility"
|
2013-01-07 14:24:26 -08:00
|
|
|
)
|
|
|
|
|
2013-04-30 11:20:14 -07:00
|
|
|
var jobNameRE = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_-]*$")
|
|
|
|
var labelNameRE = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$")
|
2013-01-07 14:24:26 -08:00
|
|
|
|
2013-04-30 11:20:14 -07:00
|
|
|
// Config encapsulates the configuration of a Prometheus instance. It wraps the
|
|
|
|
// raw configuration protocol buffer to be able to add custom methods to it.
|
|
|
|
type Config struct {
|
|
|
|
// The protobuf containing the actual configuration values.
|
|
|
|
pb.PrometheusConfig
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
|
2013-04-30 11:20:14 -07:00
|
|
|
// String returns an ASCII serialization of the loaded configuration protobuf.
|
|
|
|
func (c Config) String() string {
|
|
|
|
return proto.MarshalTextString(&c.PrometheusConfig)
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
|
2013-04-30 11:20:14 -07:00
|
|
|
// validateLabels validates whether label names have the correct format.
|
|
|
|
func (c Config) validateLabels(labels *pb.LabelPairs) error {
|
|
|
|
if labels == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, label := range labels.Label {
|
|
|
|
if !labelNameRE.MatchString(label.GetName()) {
|
2014-12-10 07:16:49 -08:00
|
|
|
return fmt.Errorf("invalid label name '%s'", label.GetName())
|
2013-04-30 11:20:14 -07:00
|
|
|
}
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
2013-04-30 11:20:14 -07:00
|
|
|
return nil
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
|
2013-04-30 11:20:14 -07:00
|
|
|
// Validate checks an entire parsed Config for the validity of its fields.
|
|
|
|
func (c Config) Validate() error {
|
|
|
|
// Check the global configuration section for validity.
|
|
|
|
global := c.Global
|
|
|
|
if _, err := utility.StringToDuration(global.GetScrapeInterval()); err != nil {
|
2014-12-10 07:16:49 -08:00
|
|
|
return fmt.Errorf("invalid global scrape interval: %s", err)
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
2013-04-30 11:20:14 -07:00
|
|
|
if _, err := utility.StringToDuration(global.GetEvaluationInterval()); err != nil {
|
2014-12-10 07:16:49 -08:00
|
|
|
return fmt.Errorf("invalid rule evaluation interval: %s", err)
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
2013-04-30 11:20:14 -07:00
|
|
|
if err := c.validateLabels(global.Labels); err != nil {
|
2014-12-10 07:16:49 -08:00
|
|
|
return fmt.Errorf("invalid global labels: %s", err)
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
2013-04-30 11:20:14 -07:00
|
|
|
|
|
|
|
// Check each job configuration for validity.
|
2013-12-03 02:59:38 -08:00
|
|
|
jobNames := map[string]bool{}
|
2013-04-30 11:20:14 -07:00
|
|
|
for _, job := range c.Job {
|
2013-12-03 02:59:38 -08:00
|
|
|
if jobNames[job.GetName()] {
|
2014-12-10 07:16:49 -08:00
|
|
|
return fmt.Errorf("found multiple jobs configured with the same name: '%s'", job.GetName())
|
2013-12-03 02:59:38 -08:00
|
|
|
}
|
|
|
|
jobNames[job.GetName()] = true
|
|
|
|
|
2013-04-30 11:20:14 -07:00
|
|
|
if !jobNameRE.MatchString(job.GetName()) {
|
2014-12-10 07:16:49 -08:00
|
|
|
return fmt.Errorf("invalid job name '%s'", job.GetName())
|
2013-04-30 11:20:14 -07:00
|
|
|
}
|
|
|
|
if _, err := utility.StringToDuration(job.GetScrapeInterval()); err != nil {
|
2014-12-10 07:16:49 -08:00
|
|
|
return fmt.Errorf("invalid scrape interval for job '%s': %s", job.GetName(), err)
|
2013-04-30 11:20:14 -07:00
|
|
|
}
|
2013-06-11 13:59:27 -07:00
|
|
|
if _, err := utility.StringToDuration(job.GetSdRefreshInterval()); err != nil {
|
2014-12-10 07:16:49 -08:00
|
|
|
return fmt.Errorf("invalid SD refresh interval for job '%s': %s", job.GetName(), err)
|
2013-06-11 13:59:27 -07:00
|
|
|
}
|
2013-08-16 09:17:48 -07:00
|
|
|
if _, err := utility.StringToDuration(job.GetScrapeTimeout()); err != nil {
|
2014-12-10 07:16:49 -08:00
|
|
|
return fmt.Errorf("invalid scrape timeout for job '%s': %s", job.GetName(), err)
|
2013-08-16 09:17:48 -07:00
|
|
|
}
|
2013-04-30 11:20:14 -07:00
|
|
|
for _, targetGroup := range job.TargetGroup {
|
|
|
|
if err := c.validateLabels(targetGroup.Labels); err != nil {
|
2014-12-10 07:16:49 -08:00
|
|
|
return fmt.Errorf("invalid labels for job '%s': %s", job.GetName(), err)
|
2013-04-30 11:20:14 -07:00
|
|
|
}
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
2013-06-11 13:59:27 -07:00
|
|
|
if job.SdName != nil && len(job.TargetGroup) > 0 {
|
2014-12-10 07:16:49 -08:00
|
|
|
return fmt.Errorf("specified both DNS-SD name and target group for job: %s", job.GetName())
|
2013-06-11 13:59:27 -07:00
|
|
|
}
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
2013-04-30 11:20:14 -07:00
|
|
|
|
2013-01-07 14:24:26 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-04-30 11:20:14 -07:00
|
|
|
// GetJobByName finds a job by its name in a Config object.
|
2013-05-15 22:38:31 -07:00
|
|
|
func (c Config) GetJobByName(name string) *JobConfig {
|
2013-04-30 11:20:14 -07:00
|
|
|
for _, job := range c.Job {
|
|
|
|
if job.GetName() == name {
|
2013-05-15 22:38:31 -07:00
|
|
|
return &JobConfig{*job}
|
2013-02-22 12:07:35 -08:00
|
|
|
}
|
|
|
|
}
|
2013-05-15 22:38:31 -07:00
|
|
|
return nil
|
2013-02-22 12:07:35 -08:00
|
|
|
}
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// GlobalLabels returns the global labels as a LabelSet.
|
2013-08-13 07:14:18 -07:00
|
|
|
func (c Config) GlobalLabels() clientmodel.LabelSet {
|
|
|
|
labels := clientmodel.LabelSet{}
|
|
|
|
if c.Global.Labels != nil {
|
|
|
|
for _, label := range c.Global.Labels.Label {
|
|
|
|
labels[clientmodel.LabelName(label.GetName())] = clientmodel.LabelValue(label.GetValue())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
|
2013-04-30 11:20:14 -07:00
|
|
|
// Jobs returns all the jobs in a Config object.
|
|
|
|
func (c Config) Jobs() (jobs []JobConfig) {
|
|
|
|
for _, job := range c.Job {
|
|
|
|
jobs = append(jobs, JobConfig{*job})
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
2013-02-13 08:41:35 -08:00
|
|
|
return
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
|
2013-04-30 11:20:14 -07:00
|
|
|
// stringToDuration converts a string to a duration and dies on invalid format.
|
|
|
|
func stringToDuration(intervalStr string) time.Duration {
|
|
|
|
duration, err := utility.StringToDuration(intervalStr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
2013-04-30 11:20:14 -07:00
|
|
|
return duration
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
|
2013-04-30 11:20:14 -07:00
|
|
|
// ScrapeInterval gets the default scrape interval for a Config.
|
|
|
|
func (c Config) ScrapeInterval() time.Duration {
|
|
|
|
return stringToDuration(c.Global.GetScrapeInterval())
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
|
2013-04-30 11:20:14 -07:00
|
|
|
// EvaluationInterval gets the default evaluation interval for a Config.
|
|
|
|
func (c Config) EvaluationInterval() time.Duration {
|
|
|
|
return stringToDuration(c.Global.GetEvaluationInterval())
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
|
2013-04-30 11:20:14 -07:00
|
|
|
// JobConfig encapsulates the configuration of a single job. It wraps the raw
|
|
|
|
// job protocol buffer to be able to add custom methods to it.
|
|
|
|
type JobConfig struct {
|
|
|
|
pb.JobConfig
|
|
|
|
}
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// ScrapeInterval gets the scrape interval for a job.
|
2013-04-30 11:20:14 -07:00
|
|
|
func (c JobConfig) ScrapeInterval() time.Duration {
|
|
|
|
return stringToDuration(c.GetScrapeInterval())
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
2013-08-16 09:17:48 -07:00
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// ScrapeTimeout gets the scrape timeout for a job.
|
2013-08-16 09:17:48 -07:00
|
|
|
func (c JobConfig) ScrapeTimeout() time.Duration {
|
|
|
|
return stringToDuration(c.GetScrapeInterval())
|
|
|
|
}
|