@ -15,6 +15,7 @@ package retrieval
import (
import (
"fmt"
"fmt"
"hash/fnv"
"io/ioutil"
"io/ioutil"
"net/http"
"net/http"
"net/url"
"net/url"
@ -117,24 +118,21 @@ type Target struct {
// The status object for the target. It is only set once on initialization.
// The status object for the target. It is only set once on initialization.
status * TargetStatus
status * TargetStatus
scrapeConfig * config . ScrapeConfig
// Mutex protects the members below.
sync . RWMutex
// Labels before any processing.
// Labels before any processing.
metaLabels model . LabelSet
metaLabels model . LabelSet
// Any labels that are added to this target and its metrics.
// Any labels that are added to this target and its metrics.
labels model . LabelSet
labels model . LabelSet
// Additional URL parmeters that are part of the target URL.
params url . Values
}
}
// NewTarget creates a reasonably configured target for querying.
// NewTarget creates a reasonably configured target for querying.
func NewTarget ( cfg * config . ScrapeConfig , labels, metaLabels model . LabelSet ) * Target {
func NewTarget ( labels, metaLabels model . LabelSet , params url . Values ) * Target {
return & Target {
return & Target {
status : & TargetStatus { } ,
status : & TargetStatus { } ,
scrapeConfig: cfg ,
labels: labels ,
labels: l abels,
metaLabels: metaL abels,
metaLabels: metaLabel s,
params: param s,
}
}
}
}
@ -188,15 +186,16 @@ func newHTTPClient(cfg *config.ScrapeConfig) (*http.Client, error) {
}
}
func ( t * Target ) String ( ) string {
func ( t * Target ) String ( ) string {
return t . host ( )
return t . URL( ) . String ( )
}
}
// fingerprint returns an identifying hash for the target.
// hash returns an identifying hash for the target.
func ( t * Target ) fingerprint ( ) model . Fingerprint {
func ( t * Target ) hash ( ) uint64 {
t . RLock ( )
h := fnv . New64a ( )
defer t . RUnlock ( )
h . Write ( [ ] byte ( t . labels . Fingerprint ( ) . String ( ) ) )
h . Write ( [ ] byte ( t . URL ( ) . String ( ) ) )
return t. labels . Fingerprint ( )
return h. Sum64 ( )
}
}
// offset returns the time until the next scrape cycle for the target.
// offset returns the time until the next scrape cycle for the target.
@ -205,7 +204,7 @@ func (t *Target) offset(interval time.Duration) time.Duration {
var (
var (
base = now % int64 ( interval )
base = now % int64 ( interval )
offset = uint64 ( t . fingerprint ( ) ) % uint64 ( interval )
offset = t . hash ( ) % uint64 ( interval )
next = base + int64 ( offset )
next = base + int64 ( offset )
)
)
@ -215,35 +214,27 @@ func (t *Target) offset(interval time.Duration) time.Duration {
return time . Duration ( next )
return time . Duration ( next )
}
}
func ( t * Target ) scheme ( ) string {
// Labels returns a copy of the set of all public labels of the target.
t . RLock ( )
func ( t * Target ) Labels ( ) model . LabelSet {
defer t . RUnlock ( )
lset := make ( model . LabelSet , len ( t . labels ) )
for ln , lv := range t . labels {
return string ( t . labels [ model . SchemeLabel ] )
if ! strings . HasPrefix ( string ( ln ) , model . ReservedLabelPrefix ) {
}
lset [ ln ] = lv
}
func ( t * Target ) host ( ) string {
}
t . RLock ( )
return lset
defer t . RUnlock ( )
return string ( t . labels [ model . AddressLabel ] )
}
}
func ( t * Target ) path ( ) string {
// MetaLabels returns a copy of the target's labels before any processing.
t . RLock ( )
func ( t * Target ) MetaLabels ( ) model . LabelSet {
defer t . RUnlock ( )
return t . metaLabels . Clone ( )
return string ( t . labels [ model . MetricsPathLabel ] )
}
}
// URL returns a copy of the target's URL.
// URL returns a copy of the target's URL.
func ( t * Target ) URL ( ) * url . URL {
func ( t * Target ) URL ( ) * url . URL {
t . RLock ( )
defer t . RUnlock ( )
params := url . Values { }
params := url . Values { }
for k , v := range t . scra peConfig. P arams {
for k , v := range t . params {
params [ k ] = make ( [ ] string , len ( v ) )
params [ k ] = make ( [ ] string , len ( v ) )
copy ( params [ k ] , v )
copy ( params [ k ] , v )
}
}
@ -329,36 +320,3 @@ func (app relabelAppender) Append(s *model.Sample) error {
return app . SampleAppender . Append ( s )
return app . SampleAppender . Append ( s )
}
}
// Labels returns a copy of the set of all public labels of the target.
func ( t * Target ) Labels ( ) model . LabelSet {
t . RLock ( )
defer t . RUnlock ( )
return t . unlockedLabels ( )
}
// unlockedLabels does the same as Labels but does not lock the mutex (useful
// for internal usage when the mutex is already locked).
func ( t * Target ) unlockedLabels ( ) model . LabelSet {
lset := make ( model . LabelSet , len ( t . labels ) )
for ln , lv := range t . labels {
if ! strings . HasPrefix ( string ( ln ) , model . ReservedLabelPrefix ) {
lset [ ln ] = lv
}
}
if _ , ok := lset [ model . InstanceLabel ] ; ! ok {
lset [ model . InstanceLabel ] = t . labels [ model . AddressLabel ]
}
return lset
}
// MetaLabels returns a copy of the target's labels before any processing.
func ( t * Target ) MetaLabels ( ) model . LabelSet {
t . RLock ( )
defer t . RUnlock ( )
return t . metaLabels . Clone ( )
}