From d4ff85db5a0e0371baa82f9f98ebee1d392fb58b Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Wed, 24 Apr 2013 21:50:49 +0200 Subject: [PATCH] Add instance label to health (up) timeseries. --- model/sample.go | 14 ++++++++++++++ retrieval/target.go | 1 + retrieval/target_test.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/model/sample.go b/model/sample.go index 5c667c942..b29b19cd8 100644 --- a/model/sample.go +++ b/model/sample.go @@ -23,6 +23,20 @@ type Sample struct { Timestamp time.Time } +func (s Sample) Equal(sample Sample) bool { + if !NewFingerprintFromMetric(s.Metric).Equal(NewFingerprintFromMetric(sample.Metric)) { + return false + } + if !s.Timestamp.Equal(sample.Timestamp) { + return false + } + if !s.Value.Equal(sample.Value) { + return false + } + + return true +} + type Samples []Sample func (s Samples) Len() int { diff --git a/retrieval/target.go b/retrieval/target.go index c16cb28bb..1017d6ef4 100644 --- a/retrieval/target.go +++ b/retrieval/target.go @@ -142,6 +142,7 @@ func (t *target) recordScrapeHealth(results chan format.Result, timestamp time.T metric[label] = value } metric[model.MetricNameLabel] = model.ScrapeHealthMetricName + metric[model.InstanceLabel] = model.LabelValue(t.Address()) healthValue := model.SampleValue(0) if healthy { diff --git a/retrieval/target_test.go b/retrieval/target_test.go index b99845f48..8c8562f9a 100644 --- a/retrieval/target_test.go +++ b/retrieval/target_test.go @@ -14,6 +14,7 @@ package retrieval import ( + "github.com/prometheus/prometheus/model" "github.com/prometheus/prometheus/retrieval/format" "testing" "time" @@ -30,3 +31,35 @@ func TestTargetScrapeUpdatesState(t *testing.T) { t.Errorf("Expected target state %v, actual: %v", UNREACHABLE, testTarget.state) } } + +func TestTargetRecordScrapeHealth(t *testing.T) { + testTarget := target{ + scheduler: literalScheduler{}, + address: "http://example.url", + baseLabels: model.LabelSet{model.JobLabel: "testjob"}, + } + + now := time.Now() + results := make(chan format.Result) + go testTarget.recordScrapeHealth(results, now, true) + + result := <-results + actual := result.Sample + expected := model.Sample{ + Metric: model.Metric{ + model.MetricNameLabel: model.ScrapeHealthMetricName, + model.InstanceLabel: "http://example.url", + model.JobLabel: "testjob", + }, + Timestamp: now, + Value: 1, + } + + if result.Err != nil { + t.Fatalf("Got unexpected error: %v", result.Err) + } + + if !actual.Equal(expected) { + t.Fatalf("Expected and actual samples not equal. Expected: %v, actual: %v", expected, actual) + } +}