mirror of https://github.com/prometheus/prometheus
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
233 lines
6.7 KiB
233 lines
6.7 KiB
10 years ago
|
// Copyright 2013 The Prometheus Authors
|
||
12 years ago
|
// 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.
|
||
|
|
||
|
package retrieval
|
||
8 years ago
|
|
||
|
import (
|
||
8 years ago
|
"fmt"
|
||
8 years ago
|
"reflect"
|
||
8 years ago
|
"testing"
|
||
|
|
||
8 years ago
|
"github.com/prometheus/common/model"
|
||
8 years ago
|
"github.com/prometheus/prometheus/config"
|
||
8 years ago
|
"github.com/prometheus/prometheus/pkg/labels"
|
||
8 years ago
|
)
|
||
|
|
||
8 years ago
|
func mustNewRegexp(s string) config.Regexp {
|
||
|
re, err := config.NewRegexp(s)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
return re
|
||
|
}
|
||
|
|
||
|
func TestPopulateLabels(t *testing.T) {
|
||
|
cases := []struct {
|
||
8 years ago
|
in labels.Labels
|
||
8 years ago
|
cfg *config.ScrapeConfig
|
||
8 years ago
|
res labels.Labels
|
||
|
resOrig labels.Labels
|
||
8 years ago
|
err error
|
||
8 years ago
|
}{
|
||
|
// Regular population of scrape config options.
|
||
|
{
|
||
8 years ago
|
in: labels.FromMap(map[string]string{
|
||
8 years ago
|
model.AddressLabel: "1.2.3.4:1000",
|
||
|
"custom": "value",
|
||
8 years ago
|
}),
|
||
8 years ago
|
cfg: &config.ScrapeConfig{
|
||
|
Scheme: "https",
|
||
|
MetricsPath: "/metrics",
|
||
|
JobName: "job",
|
||
|
},
|
||
8 years ago
|
res: labels.FromMap(map[string]string{
|
||
8 years ago
|
model.AddressLabel: "1.2.3.4:1000",
|
||
|
model.InstanceLabel: "1.2.3.4:1000",
|
||
|
model.SchemeLabel: "https",
|
||
|
model.MetricsPathLabel: "/metrics",
|
||
|
model.JobLabel: "job",
|
||
|
"custom": "value",
|
||
8 years ago
|
}),
|
||
|
resOrig: labels.FromMap(map[string]string{
|
||
8 years ago
|
model.AddressLabel: "1.2.3.4:1000",
|
||
|
model.SchemeLabel: "https",
|
||
|
model.MetricsPathLabel: "/metrics",
|
||
|
model.JobLabel: "job",
|
||
|
"custom": "value",
|
||
8 years ago
|
}),
|
||
8 years ago
|
},
|
||
|
// Pre-define/overwrite scrape config labels.
|
||
|
// Leave out port and expect it to be defaulted to scheme.
|
||
|
{
|
||
8 years ago
|
in: labels.FromMap(map[string]string{
|
||
8 years ago
|
model.AddressLabel: "1.2.3.4",
|
||
|
model.SchemeLabel: "http",
|
||
|
model.MetricsPathLabel: "/custom",
|
||
|
model.JobLabel: "custom-job",
|
||
8 years ago
|
}),
|
||
8 years ago
|
cfg: &config.ScrapeConfig{
|
||
|
Scheme: "https",
|
||
|
MetricsPath: "/metrics",
|
||
|
JobName: "job",
|
||
|
},
|
||
8 years ago
|
res: labels.FromMap(map[string]string{
|
||
8 years ago
|
model.AddressLabel: "1.2.3.4:80",
|
||
|
model.InstanceLabel: "1.2.3.4:80",
|
||
|
model.SchemeLabel: "http",
|
||
|
model.MetricsPathLabel: "/custom",
|
||
|
model.JobLabel: "custom-job",
|
||
8 years ago
|
}),
|
||
|
resOrig: labels.FromMap(map[string]string{
|
||
8 years ago
|
model.AddressLabel: "1.2.3.4",
|
||
|
model.SchemeLabel: "http",
|
||
|
model.MetricsPathLabel: "/custom",
|
||
|
model.JobLabel: "custom-job",
|
||
8 years ago
|
}),
|
||
8 years ago
|
},
|
||
|
// Provide instance label. HTTPS port default for IPv6.
|
||
|
{
|
||
8 years ago
|
in: labels.FromMap(map[string]string{
|
||
8 years ago
|
model.AddressLabel: "[::1]",
|
||
|
model.InstanceLabel: "custom-instance",
|
||
8 years ago
|
}),
|
||
8 years ago
|
cfg: &config.ScrapeConfig{
|
||
|
Scheme: "https",
|
||
|
MetricsPath: "/metrics",
|
||
|
JobName: "job",
|
||
|
},
|
||
8 years ago
|
res: labels.FromMap(map[string]string{
|
||
8 years ago
|
model.AddressLabel: "[::1]:443",
|
||
|
model.InstanceLabel: "custom-instance",
|
||
|
model.SchemeLabel: "https",
|
||
|
model.MetricsPathLabel: "/metrics",
|
||
|
model.JobLabel: "job",
|
||
8 years ago
|
}),
|
||
|
resOrig: labels.FromMap(map[string]string{
|
||
8 years ago
|
model.AddressLabel: "[::1]",
|
||
|
model.InstanceLabel: "custom-instance",
|
||
|
model.SchemeLabel: "https",
|
||
|
model.MetricsPathLabel: "/metrics",
|
||
|
model.JobLabel: "job",
|
||
8 years ago
|
}),
|
||
8 years ago
|
},
|
||
8 years ago
|
// Address label missing.
|
||
8 years ago
|
{
|
||
8 years ago
|
in: labels.FromStrings("custom", "value"),
|
||
8 years ago
|
cfg: &config.ScrapeConfig{
|
||
|
Scheme: "https",
|
||
|
MetricsPath: "/metrics",
|
||
|
JobName: "job",
|
||
|
},
|
||
|
res: nil,
|
||
|
resOrig: nil,
|
||
|
err: fmt.Errorf("no address"),
|
||
|
},
|
||
|
// Address label missing, but added in relabelling.
|
||
|
{
|
||
8 years ago
|
in: labels.FromStrings("custom", "host:1234"),
|
||
8 years ago
|
cfg: &config.ScrapeConfig{
|
||
|
Scheme: "https",
|
||
|
MetricsPath: "/metrics",
|
||
|
JobName: "job",
|
||
|
RelabelConfigs: []*config.RelabelConfig{
|
||
|
{
|
||
|
Action: config.RelabelReplace,
|
||
|
Regex: mustNewRegexp("(.*)"),
|
||
|
SourceLabels: model.LabelNames{"custom"},
|
||
|
Replacement: "${1}",
|
||
|
TargetLabel: string(model.AddressLabel),
|
||
|
},
|
||
|
},
|
||
|
},
|
||
8 years ago
|
res: labels.FromMap(map[string]string{
|
||
8 years ago
|
model.AddressLabel: "host:1234",
|
||
|
model.InstanceLabel: "host:1234",
|
||
|
model.SchemeLabel: "https",
|
||
|
model.MetricsPathLabel: "/metrics",
|
||
|
model.JobLabel: "job",
|
||
|
"custom": "host:1234",
|
||
8 years ago
|
}),
|
||
|
resOrig: labels.FromMap(map[string]string{
|
||
8 years ago
|
model.SchemeLabel: "https",
|
||
|
model.MetricsPathLabel: "/metrics",
|
||
|
model.JobLabel: "job",
|
||
|
"custom": "host:1234",
|
||
8 years ago
|
}),
|
||
8 years ago
|
},
|
||
|
// Address label missing, but added in relabelling.
|
||
|
{
|
||
8 years ago
|
in: labels.FromStrings("custom", "host:1234"),
|
||
8 years ago
|
cfg: &config.ScrapeConfig{
|
||
|
Scheme: "https",
|
||
|
MetricsPath: "/metrics",
|
||
|
JobName: "job",
|
||
|
RelabelConfigs: []*config.RelabelConfig{
|
||
|
{
|
||
8 years ago
|
Action: config.RelabelReplace,
|
||
|
Regex: mustNewRegexp("(.*)"),
|
||
|
SourceLabels: model.LabelNames{"custom"},
|
||
|
Replacement: "${1}",
|
||
|
TargetLabel: string(model.AddressLabel),
|
||
8 years ago
|
},
|
||
|
},
|
||
|
},
|
||
8 years ago
|
res: labels.FromMap(map[string]string{
|
||
8 years ago
|
model.AddressLabel: "host:1234",
|
||
|
model.InstanceLabel: "host:1234",
|
||
|
model.SchemeLabel: "https",
|
||
|
model.MetricsPathLabel: "/metrics",
|
||
|
model.JobLabel: "job",
|
||
|
"custom": "host:1234",
|
||
8 years ago
|
}),
|
||
|
resOrig: labels.FromMap(map[string]string{
|
||
8 years ago
|
model.SchemeLabel: "https",
|
||
|
model.MetricsPathLabel: "/metrics",
|
||
|
model.JobLabel: "job",
|
||
|
"custom": "host:1234",
|
||
8 years ago
|
}),
|
||
8 years ago
|
},
|
||
|
// Invalid UTF-8 in label.
|
||
|
{
|
||
8 years ago
|
in: labels.FromMap(map[string]string{
|
||
8 years ago
|
model.AddressLabel: "1.2.3.4:1000",
|
||
|
"custom": "\xbd",
|
||
8 years ago
|
}),
|
||
8 years ago
|
cfg: &config.ScrapeConfig{
|
||
|
Scheme: "https",
|
||
|
MetricsPath: "/metrics",
|
||
|
JobName: "job",
|
||
|
},
|
||
8 years ago
|
res: nil,
|
||
|
resOrig: nil,
|
||
8 years ago
|
err: fmt.Errorf("invalid label value for \"custom\": \"\\xbd\""),
|
||
8 years ago
|
},
|
||
|
}
|
||
|
for i, c := range cases {
|
||
8 years ago
|
in := c.in.Copy()
|
||
|
|
||
8 years ago
|
res, orig, err := populateLabels(c.in, c.cfg)
|
||
8 years ago
|
if !reflect.DeepEqual(err, c.err) {
|
||
|
t.Fatalf("case %d: wanted %v error, got %v", i, c.err, err)
|
||
8 years ago
|
}
|
||
8 years ago
|
if !reflect.DeepEqual(c.in, in) {
|
||
|
t.Errorf("case %d: input lset was changed was\n\t%+v\n now\n\t%+v", i, in, c.in)
|
||
|
}
|
||
8 years ago
|
if !reflect.DeepEqual(res, c.res) {
|
||
|
t.Errorf("case %d: expected res\n\t%+v\n got\n\t%+v", i, c.res, res)
|
||
|
}
|
||
|
if !reflect.DeepEqual(orig, c.resOrig) {
|
||
|
t.Errorf("case %d: expected resOrig\n\t%+v\n got\n\t%+v", i, c.resOrig, orig)
|
||
|
}
|
||
|
}
|
||
|
}
|