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

add pushgateway related operations #15

Open
wants to merge 3 commits 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
53 changes: 47 additions & 6 deletions config/v2/configV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"github.com/fstab/grok_exporter/templates"
"gopkg.in/yaml.v2"
"regexp"
)

func Unmarshal(config []byte) (*Config, error) {
Expand All @@ -42,7 +43,8 @@ func (cfg *Config) String() string {
}

type GlobalConfig struct {
ConfigVersion int `yaml:"config_version,omitempty"`
ConfigVersion int `yaml:"config_version,omitempty"`
PushgatewayAddr string `yaml:"pushgateway_addr,omitempty"` // add pushgateway address <ip>:<port>
}

type InputConfig struct {
Expand All @@ -57,10 +59,19 @@ type GrokConfig struct {
}

type MetricConfig struct {
Type string `yaml:",omitempty"`
Name string `yaml:",omitempty"`
Help string `yaml:",omitempty"`
Match string `yaml:",omitempty"`
Type string `yaml:",omitempty"`
Name string `yaml:",omitempty"`
Help string `yaml:",omitempty"`
Match string `yaml:",omitempty"`

/**************pushgateway related configs*******************/
Pushgateway bool `yaml:",omitempty"` //flag for if push metric to pushgateway or not
JobName string `yaml:"job_name,omitempty"` //job name used to push metric to pushgateway
DeleteMatch string `yaml:"delete_match,omitempty"` //if match, metric will be deleted from pushgateway
GroupingKey map[string]string `yaml:"grouping_key,omitempty"` //grouping key used to push and delete metric from pushgateway
GroupTemplates []templates.Template `yaml:"-"`
/**************end of pushgateway related configs************/

Value string `yaml:",omitempty"`
Cumulative bool `yaml:",omitempty"`
Buckets []float64 `yaml:",flow,omitempty"`
Expand Down Expand Up @@ -138,7 +149,11 @@ func (c *ServerConfig) addDefaults() {
}

func (cfg *Config) validate() error {
err := cfg.Input.validate()
err := cfg.Global.validate()
if err != nil {
return err
}
err = cfg.Input.validate()
if err != nil {
return err
}
Expand All @@ -157,6 +172,17 @@ func (cfg *Config) validate() error {
return nil
}

func (c *GlobalConfig) validate() error {
//ignore version validation
if len(c.PushgatewayAddr) > 0 {
reg := regexp.MustCompile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}:[0-9]{1,5}")
result := reg.FindString(c.PushgatewayAddr)
if result == "" {
return fmt.Errorf("Not valid pushgateway address, usage: <ip>>:<port>.")
}
}
return nil
}
func (c *InputConfig) validate() error {
switch {
case c.Type == "stdin":
Expand Down Expand Up @@ -236,6 +262,11 @@ func (c *MetricConfig) validate() error {
return fmt.Errorf("Invalid metric configuration: 'metrics.buckets' cannot be used for %v metrics.", c.Type)
}
// Labels and value are validated in InitTemplates()
//validate pushgateway related configs
if len(c.DeleteMatch) > 0 && !c.Pushgateway {
return fmt.Errorf("Invalid metric configuration: 'delete_match' can only be defined when 'pushgateway = true'.")
}
//grouping key is validated in InitTemplates()
return nil
}

Expand Down Expand Up @@ -289,6 +320,16 @@ func (metric *MetricConfig) InitTemplates() error {
}
metric.LabelTemplates = append(metric.LabelTemplates, tmplt)
}
// validate grouping key
metric.GroupTemplates = make([]templates.Template, 0, len(metric.GroupingKey))
for name, templateString := range metric.GroupingKey {
tmplt, err = templates.New(name, templateString)
if err != nil {
return fmt.Errorf(msg, fmt.Sprintf("groupingKey %v", metric.Name), name, err.Error())
}
metric.GroupTemplates = append(metric.GroupTemplates, tmplt)
}

if len(metric.Value) > 0 {
metric.ValueTemplate, err = templates.New("__value__", metric.Value)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions exporter/grok.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ func VerifyFieldNames(m *v2.MetricConfig, regex *OnigurumaRegexp) error {
return nil
}

func VerifyGroupingKeyField(m *v2.MetricConfig, regex *OnigurumaRegexp) error {
for _, template := range m.GroupTemplates {
for _, grokFieldName := range template.ReferencedGrokFields() {
if !regex.HasCaptureGroup(grokFieldName) {
return fmt.Errorf("%v: error in label %v: grok field %v not found in match pattern", m.Name, template.Name(), grokFieldName)
}
}
}

return nil
}

// PATTERN_RE matches the %{..} patterns. There are three possibilities:
// 1) %{USER} - grok pattern
// 2) %{IP:clientip} - grok pattern with name
Expand Down
Loading