@ -15,6 +15,7 @@ package retrieval
import (
"fmt"
"hash/fnv"
"io/ioutil"
"net/http"
"net/url"
@ -117,24 +118,21 @@ type Target struct {
// The status object for the target. It is only set once on initialization.
status * TargetStatus
scrapeConfig * config . ScrapeConfig
// Mutex protects the members below.
sync . RWMutex
// Labels before any processing.
metaLabels model . LabelSet
// Any labels that are added to this target and its metrics.
labels model . LabelSet
// Additional URL parmeters that are part of the target URL.
params url . Values
}
// 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 {
status : & TargetStatus { } ,
scrapeConfig : cfg ,
labels : l abels,
metaLabels : metaLabel s,
status : & TargetStatus { } ,
labels : labels ,
metaLabels : metaL abels,
params : param s,
}
}
@ -188,15 +186,16 @@ func newHTTPClient(cfg *config.ScrapeConfig) (*http.Client, error) {
}
func ( t * Target ) String ( ) string {
return t . host ( )
return t . URL ( ) . String ( )
}
// fingerprint returns an identifying hash for the target.
func ( t * Target ) fingerprint ( ) model . Fingerprint {
t . RLock ( )
defer t . RUnlock ( )
// hash returns an identifying hash for the target.
func ( t * Target ) hash ( ) uint64 {
h := fnv . New64a ( )
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.
@ -205,7 +204,7 @@ func (t *Target) offset(interval time.Duration) time.Duration {
var (
base = now % int64 ( interval )
offset = uint64 ( t . fingerprint ( ) ) % uint64 ( interval )
offset = t . hash ( ) % uint64 ( interval )
next = base + int64 ( offset )
)
@ -215,35 +214,27 @@ func (t *Target) offset(interval time.Duration) time.Duration {
return time . Duration ( next )
}
func ( t * Target ) scheme ( ) string {
t . RLock ( )
defer t . RUnlock ( )
return string ( t . labels [ model . SchemeLabel ] )
}
func ( t * Target ) host ( ) string {
t . RLock ( )
defer t . RUnlock ( )
return string ( t . labels [ model . AddressLabel ] )
// Labels returns a copy of the set of all public labels of the target.
func ( t * Target ) Labels ( ) 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
}
}
return lset
}
func ( t * Target ) path ( ) string {
t . RLock ( )
defer t . RUnlock ( )
return string ( t . labels [ model . MetricsPathLabel ] )
// MetaLabels returns a copy of the target's labels before any processing.
func ( t * Target ) MetaLabels ( ) model . LabelSet {
return t . metaLabels . Clone ( )
}
// URL returns a copy of the target's URL.
func ( t * Target ) URL ( ) * url . URL {
t . RLock ( )
defer t . RUnlock ( )
params := url . Values { }
for k , v := range t . scra peConfig . P arams {
for k , v := range t . params {
params [ k ] = make ( [ ] string , len ( v ) )
copy ( params [ k ] , v )
}
@ -329,36 +320,3 @@ func (app relabelAppender) Append(s *model.Sample) error {
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 ( )
}