Skip to content
This repository has been archived by the owner on Jun 21, 2022. It is now read-only.

Commit

Permalink
PMM-3387 PMM-5464 Update code compied from Prometheus up to v2.15.2
Browse files Browse the repository at this point in the history
  • Loading branch information
artemgavrilov committed Feb 25, 2020
1 parent 7178038 commit 6366966
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 10 deletions.
35 changes: 31 additions & 4 deletions services/prometheus/internal/prometheus/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

// Original file: https://github.com/prometheus/prometheus/blob/v2.12.0/config/config.go
// Original file: https://github.com/prometheus/prometheus/blob/v2.15.2/config/config.go

// Copyright 2015 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -42,7 +42,7 @@ import (

"github.com/pkg/errors"
"github.com/prometheus/common/model"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"

config_util "github.com/percona/pmm-managed/services/prometheus/internal/common/config"
sd_config "github.com/percona/pmm-managed/services/prometheus/internal/prometheus/discovery/config"
Expand Down Expand Up @@ -263,15 +263,27 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
}
jobNames[scfg.JobName] = struct{}{}
}
rwNames := map[string]struct{}{}
for _, rwcfg := range c.RemoteWriteConfigs {
if rwcfg == nil {
return errors.New("empty or null remote write config section")
}
// Skip empty names, we fill their name with their config hash in remote write code.
if _, ok := rwNames[rwcfg.Name]; ok && rwcfg.Name != "" {
return errors.Errorf("found multiple remote write configs with job name %q", rwcfg.Name)
}
rwNames[rwcfg.Name] = struct{}{}
}
rrNames := map[string]struct{}{}
for _, rrcfg := range c.RemoteReadConfigs {
if rrcfg == nil {
return errors.New("empty or null remote read config section")
}
// Skip empty names, we fill their name with their config hash in remote read code.
if _, ok := rrNames[rrcfg.Name]; ok && rrcfg.Name != "" {
return errors.Errorf("found multiple remote read configs with job name %q", rrcfg.Name)
}
rrNames[rrcfg.Name] = struct{}{}
}
return nil
}
Expand Down Expand Up @@ -430,8 +442,8 @@ func (c *ScrapeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {

// AlertingConfig configures alerting and alertmanager related configs.
type AlertingConfig struct {
AlertRelabelConfigs []*relabel.Config `yaml:"alert_relabel_configs,omitempty"`
AlertmanagerConfigs []*AlertmanagerConfig `yaml:"alertmanagers,omitempty"`
AlertRelabelConfigs []*relabel.Config `yaml:"alert_relabel_configs,omitempty"`
AlertmanagerConfigs AlertmanagerConfigs `yaml:"alertmanagers,omitempty"`
}

// UnmarshalYAML implements the yaml.Unmarshaler interface.
Expand All @@ -452,6 +464,18 @@ func (c *AlertingConfig) UnmarshalYAML(unmarshal func(interface{}) error) error
return nil
}

// AlertmanagerConfigs is a slice of *AlertmanagerConfig.
type AlertmanagerConfigs []*AlertmanagerConfig

// ToMap converts a slice of *AlertmanagerConfig to a map.
func (a AlertmanagerConfigs) ToMap() map[string]*AlertmanagerConfig {
ret := make(map[string]*AlertmanagerConfig)
for i := range a {
ret[fmt.Sprintf("config-%d", i)] = a[i]
}
return ret
}

// AlertmanagerAPIVersion represents a version of the
// github.com/prometheus/alertmanager/api, e.g. 'v1' or 'v2'.
type AlertmanagerAPIVersion string
Expand Down Expand Up @@ -582,6 +606,7 @@ type RemoteWriteConfig struct {
URL *config_util.URL `yaml:"url"`
RemoteTimeout model.Duration `yaml:"remote_timeout,omitempty"`
WriteRelabelConfigs []*relabel.Config `yaml:"write_relabel_configs,omitempty"`
Name string `yaml:"name,omitempty"`

// We cannot do proper Go type embedding below as the parser will then parse
// values arbitrarily into the overflow maps of further-down types.
Expand Down Expand Up @@ -640,6 +665,8 @@ type RemoteReadConfig struct {
URL *config_util.URL `yaml:"url"`
RemoteTimeout model.Duration `yaml:"remote_timeout,omitempty"`
ReadRecent bool `yaml:"read_recent,omitempty"`
Name string `yaml:"name,omitempty"`

// We cannot do proper Go type embedding below as the parser will then parse
// values arbitrarily into the overflow maps of further-down types.
HTTPClientConfig config_util.HTTPClientConfig `yaml:",inline"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

// Original file: https://github.com/prometheus/prometheus/blob/v2.7.1/discovery/config/config.go
// Original file: https://github.com/prometheus/prometheus/blob/v2.15.2/discovery/config/config.go
// Only static_configs were kept, everything else was removed.

// Copyright 2016 The Prometheus Authors
Expand All @@ -33,7 +33,7 @@
package config

import (
"fmt"
"github.com/pkg/errors"

"github.com/percona/pmm-managed/services/prometheus/internal/prometheus/discovery/targetgroup"
)
Expand All @@ -50,7 +50,7 @@ type ServiceDiscoveryConfig struct {
func (c *ServiceDiscoveryConfig) Validate() error {
for _, cfg := range c.StaticConfigs {
if cfg == nil {
return fmt.Errorf("empty or null section in static_configs")
return errors.New("empty or null section in static_configs")
}
}
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

// Original file: https://github.com/prometheus/prometheus/blob/v2.12.0/discovery/targetgroup/targetgroup.go
// Original file: https://github.com/prometheus/prometheus/blob/v2.15.2/discovery/targetgroup/targetgroup.go

// Copyright 2013 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
37 changes: 36 additions & 1 deletion services/prometheus/internal/prometheus/pkg/labels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

// Original file: https://github.com/prometheus/prometheus/blob/v2.12.0/pkg/labels/labels.go
// Original file: https://github.com/prometheus/prometheus/blob/v2.15.2/pkg/labels/labels.go
// Hash calculation code was removed.

// Copyright 2017 The Prometheus Authors
Expand Down Expand Up @@ -98,6 +98,23 @@ func (ls *Labels) UnmarshalJSON(b []byte) error {
return nil
}

// MarshalYAML implements yaml.Marshaler.
func (ls Labels) MarshalYAML() (interface{}, error) {
return ls.Map(), nil
}

// UnmarshalYAML implements yaml.Unmarshaler.
func (ls *Labels) UnmarshalYAML(unmarshal func(interface{}) error) error {
var m map[string]string

if err := unmarshal(&m); err != nil {
return err
}

*ls = FromMap(m)
return nil
}

// Copy returns a copy of the labels.
func (ls Labels) Copy() Labels {
res := make(Labels, len(ls))
Expand Down Expand Up @@ -126,6 +143,24 @@ func (ls Labels) Has(name string) bool {
return false
}

// WithoutEmpty returns the labelset without empty labels.
// May return the same labelset.
func (ls Labels) WithoutEmpty() Labels {
for _, v := range ls {
if v.Value != "" {
continue
}
els := make(Labels, 0, len(ls)-1)
for _, v := range ls {
if v.Value != "" {
els = append(els, v)
}
}
return els
}
return ls
}

// Equal returns whether the two label sets are equal.
func Equal(ls, o Labels) bool {
if len(ls) != len(o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

// Original file: https://github.com/prometheus/prometheus/blob/v2.12.0/pkg/relabel/relabel.go
// Original file: https://github.com/prometheus/prometheus/blob/v2.15.2/pkg/relabel/relabel.go

// Copyright 2015 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down

0 comments on commit 6366966

Please sign in to comment.