55 "crypto/tls"
66 "encoding/json"
77 "fmt"
8- "log"
98 "net"
109 "net/http"
1110 "net/url"
@@ -295,12 +294,15 @@ func registerPod(pod *corev1.Pod, p *Prometheus) {
295294 if p .kubernetesPods == nil {
296295 p .kubernetesPods = map [string ]URLAndAddress {}
297296 }
298- targetURL := getScrapeURL (pod )
299- if targetURL == nil {
297+ targetURL , err := getScrapeURL (pod )
298+ if err != nil {
299+ p .Log .Errorf ("could not parse URL: %s" , err )
300+ return
301+ } else if targetURL == nil {
300302 return
301303 }
302304
303- log . Printf ( "D! [inputs.prometheus] will scrape metrics from %q" , * targetURL )
305+ p . Log . Debugf ( " will scrape metrics from %q" , targetURL . String () )
304306 // add annotation as metrics tags
305307 tags := pod .Annotations
306308 if tags == nil {
@@ -312,12 +314,7 @@ func registerPod(pod *corev1.Pod, p *Prometheus) {
312314 for k , v := range pod .Labels {
313315 tags [k ] = v
314316 }
315- URL , err := url .Parse (* targetURL )
316- if err != nil {
317- log .Printf ("E! [inputs.prometheus] could not parse URL %q: %s" , * targetURL , err .Error ())
318- return
319- }
320- podURL := p .AddressToURL (URL , URL .Hostname ())
317+ podURL := p .AddressToURL (targetURL , targetURL .Hostname ())
321318
322319 // Locks earlier if using cAdvisor calls - makes a new list each time
323320 // rather than updating and removing from the same list
@@ -327,22 +324,22 @@ func registerPod(pod *corev1.Pod, p *Prometheus) {
327324 }
328325 p .kubernetesPods [podURL .String ()] = URLAndAddress {
329326 URL : podURL ,
330- Address : URL .Hostname (),
331- OriginalURL : URL ,
327+ Address : targetURL .Hostname (),
328+ OriginalURL : targetURL ,
332329 Tags : tags ,
333330 }
334331}
335332
336- func getScrapeURL (pod * corev1.Pod ) * string {
333+ func getScrapeURL (pod * corev1.Pod ) ( * url. URL , error ) {
337334 ip := pod .Status .PodIP
338335 if ip == "" {
339336 // return as if scrape was disabled, we will be notified again once the pod
340337 // has an IP
341- return nil
338+ return nil , nil
342339 }
343340
344341 scheme := pod .Annotations ["prometheus.io/scheme" ]
345- path := pod .Annotations ["prometheus.io/path" ]
342+ pathAndQuery := pod .Annotations ["prometheus.io/path" ]
346343 port := pod .Annotations ["prometheus.io/port" ]
347344
348345 if scheme == "" {
@@ -351,34 +348,36 @@ func getScrapeURL(pod *corev1.Pod) *string {
351348 if port == "" {
352349 port = "9102"
353350 }
354- if path == "" {
355- path = "/metrics"
351+ if pathAndQuery == "" {
352+ pathAndQuery = "/metrics"
356353 }
357354
358- u := & url.URL {
359- Scheme : scheme ,
360- Host : net .JoinHostPort (ip , port ),
361- Path : path ,
355+ base , err := url .Parse (pathAndQuery )
356+ if err != nil {
357+ return nil , err
362358 }
363359
364- x := u .String ()
360+ base .Scheme = scheme
361+ base .Host = net .JoinHostPort (ip , port )
365362
366- return & x
363+ return base , nil
367364}
368365
369366func unregisterPod (pod * corev1.Pod , p * Prometheus ) {
370- url := getScrapeURL (pod )
371- if url == nil {
367+ targetURL , err := getScrapeURL (pod )
368+ if err != nil {
369+ p .Log .Errorf ("failed to parse url: %s" , err )
370+ return
371+ } else if targetURL == nil {
372372 return
373373 }
374374
375- log .Printf ("D! [inputs.prometheus] registered a delete request for %q in namespace %q" ,
376- pod .Name , pod .Namespace )
375+ p .Log .Debugf ("registered a delete request for %q in namespace %q" , pod .Name , pod .Namespace )
377376
378377 p .lock .Lock ()
379378 defer p .lock .Unlock ()
380- if _ , ok := p .kubernetesPods [* url ]; ok {
381- delete (p .kubernetesPods , * url )
382- log . Printf ( "D! [inputs.prometheus] will stop scraping for %q" , * url )
379+ if _ , ok := p .kubernetesPods [targetURL . String () ]; ok {
380+ delete (p .kubernetesPods , targetURL . String () )
381+ p . Log . Debugf ( " will stop scraping for %q" , targetURL . String () )
383382 }
384383}
0 commit comments