2015-01-21 19:07:45 +00:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
2013-01-15 16:06:17 +00:00
|
|
|
// 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.
|
|
|
|
|
2018-02-01 09:55:07 +00:00
|
|
|
package scrape
|
2016-08-25 18:36:26 +00:00
|
|
|
|
|
|
|
import (
|
2019-04-10 12:20:00 +00:00
|
|
|
"net/http"
|
2018-09-26 09:20:56 +00:00
|
|
|
"strconv"
|
2016-08-25 18:36:26 +00:00
|
|
|
"testing"
|
2018-09-26 09:20:56 +00:00
|
|
|
"time"
|
2016-08-25 18:36:26 +00:00
|
|
|
|
2016-09-05 12:17:10 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2020-10-29 09:43:23 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-03-25 23:01:12 +00:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
|
|
|
2016-08-25 18:36:26 +00:00
|
|
|
"github.com/prometheus/prometheus/config"
|
2018-09-26 09:20:56 +00:00
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
2021-11-08 14:23:17 +00:00
|
|
|
"github.com/prometheus/prometheus/model/labels"
|
|
|
|
"github.com/prometheus/prometheus/model/relabel"
|
2016-08-25 18:36:26 +00:00
|
|
|
)
|
|
|
|
|
2016-09-05 12:17:10 +00:00
|
|
|
func TestPopulateLabels(t *testing.T) {
|
|
|
|
cases := []struct {
|
2016-12-29 08:27:30 +00:00
|
|
|
in labels.Labels
|
2016-09-05 12:17:10 +00:00
|
|
|
cfg *config.ScrapeConfig
|
2016-12-29 08:27:30 +00:00
|
|
|
res labels.Labels
|
|
|
|
resOrig labels.Labels
|
2020-10-22 09:00:08 +00:00
|
|
|
err string
|
2016-09-05 12:17:10 +00:00
|
|
|
}{
|
|
|
|
// Regular population of scrape config options.
|
|
|
|
{
|
2016-12-29 08:27:30 +00:00
|
|
|
in: labels.FromMap(map[string]string{
|
2016-09-05 12:17:10 +00:00
|
|
|
model.AddressLabel: "1.2.3.4:1000",
|
|
|
|
"custom": "value",
|
2016-12-29 08:27:30 +00:00
|
|
|
}),
|
2016-09-05 12:17:10 +00:00
|
|
|
cfg: &config.ScrapeConfig{
|
2021-08-31 15:37:32 +00:00
|
|
|
Scheme: "https",
|
|
|
|
MetricsPath: "/metrics",
|
|
|
|
JobName: "job",
|
|
|
|
ScrapeInterval: model.Duration(time.Second),
|
|
|
|
ScrapeTimeout: model.Duration(time.Second),
|
2016-09-05 12:17:10 +00:00
|
|
|
},
|
2016-12-29 08:27:30 +00:00
|
|
|
res: labels.FromMap(map[string]string{
|
2021-08-31 15:37:32 +00:00
|
|
|
model.AddressLabel: "1.2.3.4:1000",
|
|
|
|
model.InstanceLabel: "1.2.3.4:1000",
|
|
|
|
model.SchemeLabel: "https",
|
|
|
|
model.MetricsPathLabel: "/metrics",
|
|
|
|
model.JobLabel: "job",
|
|
|
|
model.ScrapeIntervalLabel: "1s",
|
|
|
|
model.ScrapeTimeoutLabel: "1s",
|
|
|
|
"custom": "value",
|
2016-12-29 08:27:30 +00:00
|
|
|
}),
|
|
|
|
resOrig: labels.FromMap(map[string]string{
|
2021-08-31 15:37:32 +00:00
|
|
|
model.AddressLabel: "1.2.3.4:1000",
|
|
|
|
model.SchemeLabel: "https",
|
|
|
|
model.MetricsPathLabel: "/metrics",
|
|
|
|
model.JobLabel: "job",
|
|
|
|
"custom": "value",
|
|
|
|
model.ScrapeIntervalLabel: "1s",
|
|
|
|
model.ScrapeTimeoutLabel: "1s",
|
2016-12-29 08:27:30 +00:00
|
|
|
}),
|
2016-09-05 12:17:10 +00:00
|
|
|
},
|
|
|
|
// Pre-define/overwrite scrape config labels.
|
|
|
|
// Leave out port and expect it to be defaulted to scheme.
|
|
|
|
{
|
2016-12-29 08:27:30 +00:00
|
|
|
in: labels.FromMap(map[string]string{
|
2021-08-31 15:37:32 +00:00
|
|
|
model.AddressLabel: "1.2.3.4",
|
|
|
|
model.SchemeLabel: "http",
|
|
|
|
model.MetricsPathLabel: "/custom",
|
|
|
|
model.JobLabel: "custom-job",
|
|
|
|
model.ScrapeIntervalLabel: "2s",
|
|
|
|
model.ScrapeTimeoutLabel: "2s",
|
2016-12-29 08:27:30 +00:00
|
|
|
}),
|
2016-09-05 12:17:10 +00:00
|
|
|
cfg: &config.ScrapeConfig{
|
2021-08-31 15:37:32 +00:00
|
|
|
Scheme: "https",
|
|
|
|
MetricsPath: "/metrics",
|
|
|
|
JobName: "job",
|
|
|
|
ScrapeInterval: model.Duration(time.Second),
|
|
|
|
ScrapeTimeout: model.Duration(time.Second),
|
2016-09-05 12:17:10 +00:00
|
|
|
},
|
2016-12-29 08:27:30 +00:00
|
|
|
res: labels.FromMap(map[string]string{
|
2021-08-31 15:37:32 +00:00
|
|
|
model.AddressLabel: "1.2.3.4:80",
|
|
|
|
model.InstanceLabel: "1.2.3.4:80",
|
|
|
|
model.SchemeLabel: "http",
|
|
|
|
model.MetricsPathLabel: "/custom",
|
|
|
|
model.JobLabel: "custom-job",
|
|
|
|
model.ScrapeIntervalLabel: "2s",
|
|
|
|
model.ScrapeTimeoutLabel: "2s",
|
2016-12-29 08:27:30 +00:00
|
|
|
}),
|
|
|
|
resOrig: labels.FromMap(map[string]string{
|
2021-08-31 15:37:32 +00:00
|
|
|
model.AddressLabel: "1.2.3.4",
|
|
|
|
model.SchemeLabel: "http",
|
|
|
|
model.MetricsPathLabel: "/custom",
|
|
|
|
model.JobLabel: "custom-job",
|
|
|
|
model.ScrapeIntervalLabel: "2s",
|
|
|
|
model.ScrapeTimeoutLabel: "2s",
|
2016-12-29 08:27:30 +00:00
|
|
|
}),
|
2016-09-05 12:17:10 +00:00
|
|
|
},
|
|
|
|
// Provide instance label. HTTPS port default for IPv6.
|
|
|
|
{
|
2016-12-29 08:27:30 +00:00
|
|
|
in: labels.FromMap(map[string]string{
|
2016-09-05 12:17:10 +00:00
|
|
|
model.AddressLabel: "[::1]",
|
|
|
|
model.InstanceLabel: "custom-instance",
|
2016-12-29 08:27:30 +00:00
|
|
|
}),
|
2016-09-05 12:17:10 +00:00
|
|
|
cfg: &config.ScrapeConfig{
|
2021-08-31 15:37:32 +00:00
|
|
|
Scheme: "https",
|
|
|
|
MetricsPath: "/metrics",
|
|
|
|
JobName: "job",
|
|
|
|
ScrapeInterval: model.Duration(time.Second),
|
|
|
|
ScrapeTimeout: model.Duration(time.Second),
|
2016-09-05 12:17:10 +00:00
|
|
|
},
|
2016-12-29 08:27:30 +00:00
|
|
|
res: labels.FromMap(map[string]string{
|
2021-08-31 15:37:32 +00:00
|
|
|
model.AddressLabel: "[::1]:443",
|
|
|
|
model.InstanceLabel: "custom-instance",
|
|
|
|
model.SchemeLabel: "https",
|
|
|
|
model.MetricsPathLabel: "/metrics",
|
|
|
|
model.JobLabel: "job",
|
|
|
|
model.ScrapeIntervalLabel: "1s",
|
|
|
|
model.ScrapeTimeoutLabel: "1s",
|
2016-12-29 08:27:30 +00:00
|
|
|
}),
|
|
|
|
resOrig: labels.FromMap(map[string]string{
|
2021-08-31 15:37:32 +00:00
|
|
|
model.AddressLabel: "[::1]",
|
|
|
|
model.InstanceLabel: "custom-instance",
|
|
|
|
model.SchemeLabel: "https",
|
|
|
|
model.MetricsPathLabel: "/metrics",
|
|
|
|
model.JobLabel: "job",
|
|
|
|
model.ScrapeIntervalLabel: "1s",
|
|
|
|
model.ScrapeTimeoutLabel: "1s",
|
2016-12-29 08:27:30 +00:00
|
|
|
}),
|
2016-09-05 12:17:10 +00:00
|
|
|
},
|
2017-06-09 15:18:19 +00:00
|
|
|
// Address label missing.
|
2016-09-05 12:17:10 +00:00
|
|
|
{
|
2017-06-23 11:15:44 +00:00
|
|
|
in: labels.FromStrings("custom", "value"),
|
2017-06-09 15:18:19 +00:00
|
|
|
cfg: &config.ScrapeConfig{
|
2021-08-31 15:37:32 +00:00
|
|
|
Scheme: "https",
|
|
|
|
MetricsPath: "/metrics",
|
|
|
|
JobName: "job",
|
|
|
|
ScrapeInterval: model.Duration(time.Second),
|
|
|
|
ScrapeTimeout: model.Duration(time.Second),
|
2017-06-09 15:18:19 +00:00
|
|
|
},
|
|
|
|
res: nil,
|
|
|
|
resOrig: nil,
|
2020-10-22 09:00:08 +00:00
|
|
|
err: "no address",
|
2017-06-09 15:18:19 +00:00
|
|
|
},
|
|
|
|
// Address label missing, but added in relabelling.
|
|
|
|
{
|
2017-06-23 11:15:44 +00:00
|
|
|
in: labels.FromStrings("custom", "host:1234"),
|
2017-06-09 15:18:19 +00:00
|
|
|
cfg: &config.ScrapeConfig{
|
2021-08-31 15:37:32 +00:00
|
|
|
Scheme: "https",
|
|
|
|
MetricsPath: "/metrics",
|
|
|
|
JobName: "job",
|
|
|
|
ScrapeInterval: model.Duration(time.Second),
|
|
|
|
ScrapeTimeout: model.Duration(time.Second),
|
2018-12-18 11:26:36 +00:00
|
|
|
RelabelConfigs: []*relabel.Config{
|
2017-06-09 15:18:19 +00:00
|
|
|
{
|
2018-12-18 11:26:36 +00:00
|
|
|
Action: relabel.Replace,
|
|
|
|
Regex: relabel.MustNewRegexp("(.*)"),
|
2017-06-09 15:18:19 +00:00
|
|
|
SourceLabels: model.LabelNames{"custom"},
|
|
|
|
Replacement: "${1}",
|
|
|
|
TargetLabel: string(model.AddressLabel),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-06-23 11:15:44 +00:00
|
|
|
res: labels.FromMap(map[string]string{
|
2021-08-31 15:37:32 +00:00
|
|
|
model.AddressLabel: "host:1234",
|
|
|
|
model.InstanceLabel: "host:1234",
|
|
|
|
model.SchemeLabel: "https",
|
|
|
|
model.MetricsPathLabel: "/metrics",
|
|
|
|
model.JobLabel: "job",
|
|
|
|
model.ScrapeIntervalLabel: "1s",
|
|
|
|
model.ScrapeTimeoutLabel: "1s",
|
|
|
|
"custom": "host:1234",
|
2017-06-23 11:15:44 +00:00
|
|
|
}),
|
|
|
|
resOrig: labels.FromMap(map[string]string{
|
2021-08-31 15:37:32 +00:00
|
|
|
model.SchemeLabel: "https",
|
|
|
|
model.MetricsPathLabel: "/metrics",
|
|
|
|
model.JobLabel: "job",
|
|
|
|
model.ScrapeIntervalLabel: "1s",
|
|
|
|
model.ScrapeTimeoutLabel: "1s",
|
|
|
|
"custom": "host:1234",
|
2016-12-29 08:27:30 +00:00
|
|
|
}),
|
2017-06-09 15:18:19 +00:00
|
|
|
},
|
|
|
|
// Address label missing, but added in relabelling.
|
|
|
|
{
|
2017-06-23 11:15:44 +00:00
|
|
|
in: labels.FromStrings("custom", "host:1234"),
|
2016-09-05 12:17:10 +00:00
|
|
|
cfg: &config.ScrapeConfig{
|
2021-08-31 15:37:32 +00:00
|
|
|
Scheme: "https",
|
|
|
|
MetricsPath: "/metrics",
|
|
|
|
JobName: "job",
|
|
|
|
ScrapeInterval: model.Duration(time.Second),
|
|
|
|
ScrapeTimeout: model.Duration(time.Second),
|
2018-12-18 11:26:36 +00:00
|
|
|
RelabelConfigs: []*relabel.Config{
|
2016-09-05 12:17:10 +00:00
|
|
|
{
|
2018-12-18 11:26:36 +00:00
|
|
|
Action: relabel.Replace,
|
|
|
|
Regex: relabel.MustNewRegexp("(.*)"),
|
2017-06-09 15:18:19 +00:00
|
|
|
SourceLabels: model.LabelNames{"custom"},
|
|
|
|
Replacement: "${1}",
|
|
|
|
TargetLabel: string(model.AddressLabel),
|
2016-09-05 12:17:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-06-23 11:15:44 +00:00
|
|
|
res: labels.FromMap(map[string]string{
|
2021-08-31 15:37:32 +00:00
|
|
|
model.AddressLabel: "host:1234",
|
|
|
|
model.InstanceLabel: "host:1234",
|
|
|
|
model.SchemeLabel: "https",
|
|
|
|
model.MetricsPathLabel: "/metrics",
|
|
|
|
model.JobLabel: "job",
|
|
|
|
model.ScrapeIntervalLabel: "1s",
|
|
|
|
model.ScrapeTimeoutLabel: "1s",
|
|
|
|
"custom": "host:1234",
|
2017-06-23 11:15:44 +00:00
|
|
|
}),
|
|
|
|
resOrig: labels.FromMap(map[string]string{
|
2021-08-31 15:37:32 +00:00
|
|
|
model.SchemeLabel: "https",
|
|
|
|
model.MetricsPathLabel: "/metrics",
|
|
|
|
model.JobLabel: "job",
|
|
|
|
model.ScrapeIntervalLabel: "1s",
|
|
|
|
model.ScrapeTimeoutLabel: "1s",
|
|
|
|
"custom": "host:1234",
|
2017-06-23 11:15:44 +00:00
|
|
|
}),
|
2017-06-09 15:18:19 +00:00
|
|
|
},
|
|
|
|
// Invalid UTF-8 in label.
|
|
|
|
{
|
2017-06-23 11:15:44 +00:00
|
|
|
in: labels.FromMap(map[string]string{
|
2017-06-09 15:18:19 +00:00
|
|
|
model.AddressLabel: "1.2.3.4:1000",
|
|
|
|
"custom": "\xbd",
|
2017-06-23 11:15:44 +00:00
|
|
|
}),
|
2017-06-09 15:18:19 +00:00
|
|
|
cfg: &config.ScrapeConfig{
|
2021-08-31 15:37:32 +00:00
|
|
|
Scheme: "https",
|
|
|
|
MetricsPath: "/metrics",
|
|
|
|
JobName: "job",
|
|
|
|
ScrapeInterval: model.Duration(time.Second),
|
|
|
|
ScrapeTimeout: model.Duration(time.Second),
|
2017-06-09 15:18:19 +00:00
|
|
|
},
|
2016-09-05 12:17:10 +00:00
|
|
|
res: nil,
|
|
|
|
resOrig: nil,
|
2020-10-22 09:00:08 +00:00
|
|
|
err: "invalid label value for \"custom\": \"\\xbd\"",
|
2016-09-05 12:17:10 +00:00
|
|
|
},
|
2021-08-31 15:37:32 +00:00
|
|
|
// Invalid duration in interval label.
|
|
|
|
{
|
|
|
|
in: labels.FromMap(map[string]string{
|
|
|
|
model.AddressLabel: "1.2.3.4:1000",
|
|
|
|
model.ScrapeIntervalLabel: "2notseconds",
|
|
|
|
}),
|
|
|
|
cfg: &config.ScrapeConfig{
|
|
|
|
Scheme: "https",
|
|
|
|
MetricsPath: "/metrics",
|
|
|
|
JobName: "job",
|
|
|
|
ScrapeInterval: model.Duration(time.Second),
|
|
|
|
ScrapeTimeout: model.Duration(time.Second),
|
|
|
|
},
|
|
|
|
res: nil,
|
|
|
|
resOrig: nil,
|
|
|
|
err: "error parsing scrape interval: not a valid duration string: \"2notseconds\"",
|
|
|
|
},
|
|
|
|
// Invalid duration in timeout label.
|
|
|
|
{
|
|
|
|
in: labels.FromMap(map[string]string{
|
|
|
|
model.AddressLabel: "1.2.3.4:1000",
|
|
|
|
model.ScrapeTimeoutLabel: "2notseconds",
|
|
|
|
}),
|
|
|
|
cfg: &config.ScrapeConfig{
|
|
|
|
Scheme: "https",
|
|
|
|
MetricsPath: "/metrics",
|
|
|
|
JobName: "job",
|
|
|
|
ScrapeInterval: model.Duration(time.Second),
|
|
|
|
ScrapeTimeout: model.Duration(time.Second),
|
|
|
|
},
|
|
|
|
res: nil,
|
|
|
|
resOrig: nil,
|
|
|
|
err: "error parsing scrape timeout: not a valid duration string: \"2notseconds\"",
|
|
|
|
},
|
|
|
|
// 0 interval in timeout label.
|
|
|
|
{
|
|
|
|
in: labels.FromMap(map[string]string{
|
|
|
|
model.AddressLabel: "1.2.3.4:1000",
|
|
|
|
model.ScrapeIntervalLabel: "0s",
|
|
|
|
}),
|
|
|
|
cfg: &config.ScrapeConfig{
|
|
|
|
Scheme: "https",
|
|
|
|
MetricsPath: "/metrics",
|
|
|
|
JobName: "job",
|
|
|
|
ScrapeInterval: model.Duration(time.Second),
|
|
|
|
ScrapeTimeout: model.Duration(time.Second),
|
|
|
|
},
|
|
|
|
res: nil,
|
|
|
|
resOrig: nil,
|
|
|
|
err: "scrape interval cannot be 0",
|
|
|
|
},
|
|
|
|
// 0 duration in timeout label.
|
|
|
|
{
|
|
|
|
in: labels.FromMap(map[string]string{
|
|
|
|
model.AddressLabel: "1.2.3.4:1000",
|
|
|
|
model.ScrapeTimeoutLabel: "0s",
|
|
|
|
}),
|
|
|
|
cfg: &config.ScrapeConfig{
|
|
|
|
Scheme: "https",
|
|
|
|
MetricsPath: "/metrics",
|
|
|
|
JobName: "job",
|
|
|
|
ScrapeInterval: model.Duration(time.Second),
|
|
|
|
ScrapeTimeout: model.Duration(time.Second),
|
|
|
|
},
|
|
|
|
res: nil,
|
|
|
|
resOrig: nil,
|
|
|
|
err: "scrape timeout cannot be 0",
|
|
|
|
},
|
|
|
|
// Timeout less than interval.
|
|
|
|
{
|
|
|
|
in: labels.FromMap(map[string]string{
|
|
|
|
model.AddressLabel: "1.2.3.4:1000",
|
|
|
|
model.ScrapeIntervalLabel: "1s",
|
|
|
|
model.ScrapeTimeoutLabel: "2s",
|
|
|
|
}),
|
|
|
|
cfg: &config.ScrapeConfig{
|
|
|
|
Scheme: "https",
|
|
|
|
MetricsPath: "/metrics",
|
|
|
|
JobName: "job",
|
|
|
|
ScrapeInterval: model.Duration(time.Second),
|
|
|
|
ScrapeTimeout: model.Duration(time.Second),
|
|
|
|
},
|
|
|
|
res: nil,
|
|
|
|
resOrig: nil,
|
|
|
|
err: "scrape timeout cannot be greater than scrape interval (\"2s\" > \"1s\")",
|
|
|
|
},
|
2016-09-05 12:17:10 +00:00
|
|
|
}
|
2018-04-27 12:11:16 +00:00
|
|
|
for _, c := range cases {
|
2017-03-08 14:37:12 +00:00
|
|
|
in := c.in.Copy()
|
|
|
|
|
2021-11-01 13:42:12 +00:00
|
|
|
res, orig, err := PopulateLabels(c.in, c.cfg)
|
2020-10-22 09:00:08 +00:00
|
|
|
if c.err != "" {
|
2020-10-29 09:43:23 +00:00
|
|
|
require.EqualError(t, err, c.err)
|
2020-10-22 09:00:08 +00:00
|
|
|
} else {
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, err)
|
2020-10-22 09:00:08 +00:00
|
|
|
}
|
2020-10-29 09:43:23 +00:00
|
|
|
require.Equal(t, c.in, in)
|
|
|
|
require.Equal(t, c.res, res)
|
|
|
|
require.Equal(t, c.resOrig, orig)
|
2016-09-05 12:17:10 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-19 11:36:21 +00:00
|
|
|
|
2019-02-13 13:24:22 +00:00
|
|
|
func loadConfiguration(t *testing.T, c string) *config.Config {
|
|
|
|
t.Helper()
|
2018-01-19 11:36:21 +00:00
|
|
|
|
2019-02-13 13:24:22 +00:00
|
|
|
cfg := &config.Config{}
|
|
|
|
if err := yaml.UnmarshalStrict([]byte(c), cfg); err != nil {
|
|
|
|
t.Fatalf("Unable to load YAML config: %s", err)
|
|
|
|
}
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
func noopLoop() loop {
|
|
|
|
return &testLoop{
|
|
|
|
startFunc: func(interval, timeout time.Duration, errc chan<- error) {},
|
|
|
|
stopFunc: func() {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestManagerApplyConfig(t *testing.T) {
|
|
|
|
// Valid initial configuration.
|
|
|
|
cfgText1 := `
|
2018-07-04 11:01:19 +00:00
|
|
|
scrape_configs:
|
2019-02-13 13:24:22 +00:00
|
|
|
- job_name: job1
|
2018-07-04 11:01:19 +00:00
|
|
|
static_configs:
|
|
|
|
- targets: ["foo:9090"]
|
|
|
|
`
|
2019-02-13 13:24:22 +00:00
|
|
|
// Invalid configuration.
|
|
|
|
cfgText2 := `
|
|
|
|
scrape_configs:
|
|
|
|
- job_name: job1
|
|
|
|
scheme: https
|
|
|
|
static_configs:
|
|
|
|
- targets: ["foo:9090"]
|
|
|
|
tls_config:
|
|
|
|
ca_file: /not/existing/ca/file
|
|
|
|
`
|
|
|
|
// Valid configuration.
|
|
|
|
cfgText3 := `
|
|
|
|
scrape_configs:
|
|
|
|
- job_name: job1
|
|
|
|
scheme: https
|
|
|
|
static_configs:
|
|
|
|
- targets: ["foo:9090"]
|
|
|
|
`
|
|
|
|
var (
|
|
|
|
cfg1 = loadConfiguration(t, cfgText1)
|
|
|
|
cfg2 = loadConfiguration(t, cfgText2)
|
|
|
|
cfg3 = loadConfiguration(t, cfgText3)
|
2018-01-19 11:36:21 +00:00
|
|
|
|
2019-02-13 13:24:22 +00:00
|
|
|
ch = make(chan struct{}, 1)
|
|
|
|
)
|
2018-07-04 11:01:19 +00:00
|
|
|
|
2021-08-24 12:31:14 +00:00
|
|
|
opts := Options{}
|
|
|
|
scrapeManager := NewManager(&opts, nil, nil)
|
2019-03-12 10:26:18 +00:00
|
|
|
newLoop := func(scrapeLoopOptions) loop {
|
2019-02-13 13:24:22 +00:00
|
|
|
ch <- struct{}{}
|
|
|
|
return noopLoop()
|
2018-01-19 11:36:21 +00:00
|
|
|
}
|
|
|
|
sp := &scrapePool{
|
2018-09-26 09:20:56 +00:00
|
|
|
appendable: &nopAppendable{},
|
|
|
|
activeTargets: map[uint64]*Target{},
|
2018-01-19 11:36:21 +00:00
|
|
|
loops: map[uint64]loop{
|
2019-02-13 13:24:22 +00:00
|
|
|
1: noopLoop(),
|
2018-01-19 11:36:21 +00:00
|
|
|
},
|
|
|
|
newLoop: newLoop,
|
|
|
|
logger: nil,
|
2019-02-13 13:24:22 +00:00
|
|
|
config: cfg1.ScrapeConfigs[0],
|
2019-04-10 12:20:00 +00:00
|
|
|
client: http.DefaultClient,
|
2018-01-19 11:36:21 +00:00
|
|
|
}
|
|
|
|
scrapeManager.scrapePools = map[string]*scrapePool{
|
2019-02-13 13:24:22 +00:00
|
|
|
"job1": sp,
|
2018-01-19 11:36:21 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 13:24:22 +00:00
|
|
|
// Apply the initial configuration.
|
|
|
|
if err := scrapeManager.ApplyConfig(cfg1); err != nil {
|
|
|
|
t.Fatalf("unable to apply configuration: %s", err)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
t.Fatal("reload happened")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply a configuration for which the reload fails.
|
|
|
|
if err := scrapeManager.ApplyConfig(cfg2); err == nil {
|
|
|
|
t.Fatalf("expecting error but got none")
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
t.Fatal("reload happened")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply a configuration for which the reload succeeds.
|
|
|
|
if err := scrapeManager.ApplyConfig(cfg3); err != nil {
|
|
|
|
t.Fatalf("unable to apply configuration: %s", err)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
default:
|
|
|
|
t.Fatal("reload didn't happen")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-applying the same configuration shouldn't trigger a reload.
|
|
|
|
if err := scrapeManager.ApplyConfig(cfg3); err != nil {
|
|
|
|
t.Fatalf("unable to apply configuration: %s", err)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
t.Fatal("reload happened")
|
|
|
|
default:
|
|
|
|
}
|
2018-01-19 11:36:21 +00:00
|
|
|
}
|
2018-09-26 09:20:56 +00:00
|
|
|
|
|
|
|
func TestManagerTargetsUpdates(t *testing.T) {
|
2021-08-24 12:31:14 +00:00
|
|
|
opts := Options{}
|
|
|
|
m := NewManager(&opts, nil, nil)
|
2018-09-26 09:20:56 +00:00
|
|
|
|
|
|
|
ts := make(chan map[string][]*targetgroup.Group)
|
|
|
|
go m.Run(ts)
|
2019-09-23 10:28:37 +00:00
|
|
|
defer m.Stop()
|
2018-09-26 09:20:56 +00:00
|
|
|
|
|
|
|
tgSent := make(map[string][]*targetgroup.Group)
|
|
|
|
for x := 0; x < 10; x++ {
|
|
|
|
|
|
|
|
tgSent[strconv.Itoa(x)] = []*targetgroup.Group{
|
2019-01-16 22:28:08 +00:00
|
|
|
{
|
2018-09-26 09:20:56 +00:00
|
|
|
Source: strconv.Itoa(x),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case ts <- tgSent:
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
|
|
|
t.Error("Scrape manager's channel remained blocked after the set threshold.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.mtxScrape.Lock()
|
|
|
|
tsetActual := m.targetSets
|
|
|
|
m.mtxScrape.Unlock()
|
|
|
|
|
|
|
|
// Make sure all updates have been received.
|
2020-10-29 09:43:23 +00:00
|
|
|
require.Equal(t, tgSent, tsetActual)
|
2018-09-26 09:20:56 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-m.triggerReload:
|
|
|
|
default:
|
|
|
|
t.Error("No scrape loops reload was triggered after targets update.")
|
|
|
|
}
|
|
|
|
}
|
2019-03-12 10:46:15 +00:00
|
|
|
|
|
|
|
func TestSetJitter(t *testing.T) {
|
|
|
|
getConfig := func(prometheus string) *config.Config {
|
|
|
|
cfgText := `
|
|
|
|
global:
|
|
|
|
external_labels:
|
|
|
|
prometheus: '` + prometheus + `'
|
|
|
|
`
|
|
|
|
|
|
|
|
cfg := &config.Config{}
|
|
|
|
if err := yaml.UnmarshalStrict([]byte(cfgText), cfg); err != nil {
|
|
|
|
t.Fatalf("Unable to load YAML config cfgYaml: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
2021-08-24 12:31:14 +00:00
|
|
|
opts := Options{}
|
|
|
|
scrapeManager := NewManager(&opts, nil, nil)
|
2019-03-12 10:46:15 +00:00
|
|
|
|
|
|
|
// Load the first config.
|
|
|
|
cfg1 := getConfig("ha1")
|
|
|
|
if err := scrapeManager.setJitterSeed(cfg1.GlobalConfig.ExternalLabels); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
jitter1 := scrapeManager.jitterSeed
|
|
|
|
|
|
|
|
if jitter1 == 0 {
|
|
|
|
t.Error("Jitter has to be a hash of uint64")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the first config.
|
|
|
|
cfg2 := getConfig("ha2")
|
|
|
|
if err := scrapeManager.setJitterSeed(cfg2.GlobalConfig.ExternalLabels); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
jitter2 := scrapeManager.jitterSeed
|
|
|
|
|
|
|
|
if jitter1 == jitter2 {
|
|
|
|
t.Error("Jitter should not be the same on different set of external labels")
|
|
|
|
}
|
|
|
|
}
|