Switch to common/expfmt for extraction

This commit is contained in:
Fabian Reinartz 2015-08-20 19:02:29 +02:00
parent f237b0e2da
commit 11a577fcd0
1 changed files with 27 additions and 42 deletions

View File

@ -18,6 +18,7 @@ import (
"crypto/x509" "crypto/x509"
"errors" "errors"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"math/rand" "math/rand"
"net/http" "net/http"
@ -26,11 +27,10 @@ import (
"sync" "sync"
"time" "time"
"github.com/prometheus/client_golang/extraction"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/log" "github.com/prometheus/common/expfmt"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/prometheus/log"
"github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/storage"
@ -301,30 +301,6 @@ func (t *Target) String() string {
return t.url.Host return t.url.Host
} }
// Ingest implements an extraction.Ingester.
func (t *Target) Ingest(s model.Samples) error {
t.RLock()
deadline := t.deadline
t.RUnlock()
// Since the regular case is that ingestedSamples is ready to receive,
// first try without setting a timeout so that we don't need to allocate
// a timer most of the time.
select {
case t.ingestedSamples <- s:
return nil
default:
select {
case t.ingestedSamples <- s:
return nil
case <-time.After(deadline / 10):
return errIngestChannelFull
}
}
}
// Ensure that Target implements extraction.Ingester at compile time.
var _ extraction.Ingester = (*Target)(nil)
// RunScraper implements Target. // RunScraper implements Target.
func (t *Target) RunScraper(sampleAppender storage.SampleAppender) { func (t *Target) RunScraper(sampleAppender storage.SampleAppender) {
defer close(t.scraperStopped) defer close(t.scraperStopped)
@ -416,7 +392,7 @@ func (t *Target) scrape(sampleAppender storage.SampleAppender) (err error) {
defer func() { defer func() {
t.status.setLastError(err) t.status.setLastError(err)
recordScrapeHealth(sampleAppender, model.TimeFromTime(start), baseLabels, t.status.Health(), time.Since(start)) recordScrapeHealth(sampleAppender, start, baseLabels, t.status.Health(), time.Since(start))
}() }()
req, err := http.NewRequest("GET", t.URL().String(), nil) req, err := http.NewRequest("GET", t.URL().String(), nil)
@ -429,27 +405,30 @@ func (t *Target) scrape(sampleAppender storage.SampleAppender) (err error) {
if err != nil { if err != nil {
return err return err
} }
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return fmt.Errorf("server returned HTTP status %s", resp.Status) return fmt.Errorf("server returned HTTP status %s", resp.Status)
} }
processor, err := extraction.ProcessorForRequestHeader(resp.Header) dec, err := expfmt.NewDecoder(resp.Body, resp.Header)
if err != nil { if err != nil {
return err return err
} }
defer resp.Body.Close()
t.ingestedSamples = make(chan model.Samples, ingestedSamplesCap) sdec := expfmt.SampleDecoder{
Dec: dec,
processOptions := &extraction.ProcessOptions{ Opts: &expfmt.DecodeOptions{
Timestamp: model.TimeFromTime(start), Timestamp: model.TimeFromUnixNano(start.UnixNano()),
},
} }
go func() {
err = processor.ProcessSingle(resp.Body, t, processOptions)
close(t.ingestedSamples)
}()
for samples := range t.ingestedSamples { var samples model.Vector
for {
if err = sdec.Decode(&samples); err != nil {
break
}
for _, s := range samples { for _, s := range samples {
if honorLabels { if honorLabels {
// Merge the metric with the baseLabels for labels not already set in the // Merge the metric with the baseLabels for labels not already set in the
@ -485,6 +464,10 @@ func (t *Target) scrape(sampleAppender storage.SampleAppender) (err error) {
sampleAppender.Append(s) sampleAppender.Append(s)
} }
} }
if err == io.EOF {
return nil
}
return err return err
} }
@ -540,7 +523,7 @@ func (t *Target) MetaLabels() model.LabelSet {
func recordScrapeHealth( func recordScrapeHealth(
sampleAppender storage.SampleAppender, sampleAppender storage.SampleAppender,
timestamp model.Time, timestamp time.Time,
baseLabels model.LabelSet, baseLabels model.LabelSet,
health TargetHealth, health TargetHealth,
scrapeDuration time.Duration, scrapeDuration time.Duration,
@ -561,14 +544,16 @@ func recordScrapeHealth(
healthValue = model.SampleValue(1) healthValue = model.SampleValue(1)
} }
ts := model.TimeFromUnixNano(timestamp.UnixNano())
healthSample := &model.Sample{ healthSample := &model.Sample{
Metric: healthMetric, Metric: healthMetric,
Timestamp: timestamp, Timestamp: ts,
Value: healthValue, Value: healthValue,
} }
durationSample := &model.Sample{ durationSample := &model.Sample{
Metric: durationMetric, Metric: durationMetric,
Timestamp: timestamp, Timestamp: ts,
Value: model.SampleValue(float64(scrapeDuration) / float64(time.Second)), Value: model.SampleValue(float64(scrapeDuration) / float64(time.Second)),
} }