update readme

This commit is contained in:
Dmitry Vasiliev 2020-05-17 16:58:41 +03:00
parent cbb7a9f17a
commit 9827e1865a
No known key found for this signature in database
GPG Key ID: C9A6FF8856B941E3
4 changed files with 42 additions and 15 deletions

27
README.md Normal file
View File

@ -0,0 +1,27 @@
# prometheus-exporter-merger
Merges Prometheus metrics from multiple sources.
## But Why?!
> [prometheus/prometheus#3756](https://github.com/prometheus/prometheus/issues/3756)
To start the exporter:
```
prometheus-exporter-merger --config config.yaml
```
Config example:
```yaml
listen: :8080
scrap_timeout: 20s
sources:
- url: http://127.0.0.1:8081/metrics
labels:
key1: value1
- url: http://127.0.0.1:8082/metrics
labels:
key2: value2
```

View File

@ -13,9 +13,9 @@ type source struct {
} }
type config struct { type config struct {
Listen string `yaml:"listen"` Listen string `yaml:"listen"`
Timeout time.Duration `yaml:"timeout"` ScrapTimeout time.Duration `yaml:"scrap_timeout"`
Sources []*source `yaml:"sources"` Sources []*source `yaml:"sources"`
} }
func parseConfig(filename string) (*config, error) { func parseConfig(filename string) (*config, error) {
@ -24,8 +24,8 @@ func parseConfig(filename string) (*config, error) {
return nil, err return nil, err
} }
result := &config{ result := &config{
Listen: ":8080", Listen: ":8080",
Timeout: 15 * time.Second, ScrapTimeout: 15 * time.Second,
} }
return result, yaml.Unmarshal(data, result) return result, yaml.Unmarshal(data, result)
} }

View File

@ -24,7 +24,7 @@ func Execute() {
panic(err) panic(err)
} }
m := merger.New(c.Timeout) m := merger.New(c.ScrapTimeout)
for _, s := range c.Sources { for _, s := range c.Sources {
var labels []*prom.LabelPair var labels []*prom.LabelPair
for k, v := range s.Labels { for k, v := range s.Labels {

View File

@ -16,10 +16,10 @@ type Merger interface {
} }
type merger struct { type merger struct {
mu sync.Mutex mu sync.Mutex
timeout time.Duration scrapTimeout time.Duration
client *http.Client client *http.Client
sources []*source sources []*source
} }
type source struct { type source struct {
@ -27,7 +27,7 @@ type source struct {
labels []*prom.LabelPair labels []*prom.LabelPair
} }
func New(timeout time.Duration) Merger { func New(scrapTimeout time.Duration) Merger {
client := &http.Client{ client := &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
DisableKeepAlives: false, DisableKeepAlives: false,
@ -37,11 +37,11 @@ func New(timeout time.Duration) Merger {
MaxConnsPerHost: 10, MaxConnsPerHost: 10,
IdleConnTimeout: 5 * time.Minute, IdleConnTimeout: 5 * time.Minute,
}, },
Timeout: timeout, Timeout: scrapTimeout,
} }
return &merger{ return &merger{
timeout: timeout, scrapTimeout: scrapTimeout,
client: client, client: client,
} }
} }
@ -56,7 +56,7 @@ func (m *merger) AddSource(url string, labels []*prom.LabelPair) {
func (m *merger) Merge(w io.Writer) error { func (m *merger) Merge(w io.Writer) error {
m.mu.Lock() m.mu.Lock()
defer m.mu.Unlock() defer m.mu.Unlock()
ctx, cancel := context.WithTimeout(context.Background(), m.timeout) ctx, cancel := context.WithTimeout(context.Background(), m.scrapTimeout)
defer cancel() defer cancel()
return m.merge(ctx, w) return m.merge(ctx, w)
} }