Fix target update error handling.

Fixes https://github.com/prometheus/prometheus/issues/1378
pull/1379/head
Julius Volz 9 years ago
parent c0df1c7e81
commit 3728b5872f

@ -184,7 +184,7 @@ type Target struct {
}
// NewTarget creates a reasonably configured target for querying.
func NewTarget(cfg *config.ScrapeConfig, baseLabels, metaLabels model.LabelSet) *Target {
func NewTarget(cfg *config.ScrapeConfig, baseLabels, metaLabels model.LabelSet) (*Target, error) {
t := &Target{
url: &url.URL{
Scheme: string(baseLabels[model.SchemeLabel]),
@ -194,8 +194,8 @@ func NewTarget(cfg *config.ScrapeConfig, baseLabels, metaLabels model.LabelSet)
scraperStopping: make(chan struct{}),
scraperStopped: make(chan struct{}),
}
t.Update(cfg, baseLabels, metaLabels)
return t
err := t.Update(cfg, baseLabels, metaLabels)
return t, err
}
// Status returns the status of the target.
@ -205,14 +205,13 @@ func (t *Target) Status() *TargetStatus {
// Update overwrites settings in the target that are derived from the job config
// it belongs to.
func (t *Target) Update(cfg *config.ScrapeConfig, baseLabels, metaLabels model.LabelSet) {
func (t *Target) Update(cfg *config.ScrapeConfig, baseLabels, metaLabels model.LabelSet) error {
t.Lock()
defer t.Unlock()
httpClient, err := newHTTPClient(cfg)
if err != nil {
log.Errorf("cannot create HTTP client: %v", err)
return
return fmt.Errorf("cannot create HTTP client: %v", err)
}
t.httpClient = httpClient
@ -257,6 +256,7 @@ func (t *Target) Update(cfg *config.ScrapeConfig, baseLabels, metaLabels model.L
t.baseLabels[model.InstanceLabel] = model.LabelValue(t.InstanceIdentifier())
}
t.metricRelabelConfigs = cfg.MetricRelabelConfigs
return nil
}
func newHTTPClient(cfg *config.ScrapeConfig) (*http.Client, error) {

@ -423,7 +423,7 @@ func TestURLParams(t *testing.T) {
t.Fatal(err)
}
target := NewTarget(
target, err := NewTarget(
&config.ScrapeConfig{
JobName: "test_job1",
ScrapeInterval: model.Duration(1 * time.Minute),
@ -439,6 +439,9 @@ func TestURLParams(t *testing.T) {
"__param_foo": "bar",
},
nil)
if err != nil {
t.Fatal(err)
}
app := &collectResultAppender{}
if err = target.scrape(app); err != nil {
t.Fatal(err)
@ -639,3 +642,18 @@ func newTLSConfig(t *testing.T) *tls.Config {
tlsConfig.BuildNameToCertificate()
return tlsConfig
}
func TestNewTargetWithBadTLSConfig(t *testing.T) {
cfg := &config.ScrapeConfig{
ScrapeTimeout: model.Duration(1 * time.Second),
TLSConfig: config.TLSConfig{
CAFile: "testdata/nonexistent_ca.cer",
CertFile: "testdata/nonexistent_client.cer",
KeyFile: "testdata/nonexistent_client.key",
},
}
_, err := NewTarget(cfg, nil, nil)
if err == nil {
t.Fatalf("Expected error, got nil.")
}
}

@ -278,7 +278,9 @@ func (tm *TargetManager) updateTargetGroup(tgroup *config.TargetGroup, cfg *conf
// to build up.
wg.Add(1)
go func(t *Target) {
match.Update(cfg, t.fullLabels(), t.metaLabels)
if err := match.Update(cfg, t.fullLabels(), t.metaLabels); err != nil {
log.Errorf("Error updating target %v: %v", t, err)
}
wg.Done()
}(tnew)
newTargets[i] = match
@ -516,7 +518,10 @@ func (tm *TargetManager) targetsFromGroup(tg *config.TargetGroup, cfg *config.Sc
delete(labels, ln)
}
}
tr := NewTarget(cfg, labels, preRelabelLabels)
tr, err := NewTarget(cfg, labels, preRelabelLabels)
if err != nil {
return nil, fmt.Errorf("error while creating instance %d in target group %s: %s", i, tg, err)
}
targets = append(targets, tr)
}

Loading…
Cancel
Save