@@ -13,8 +13,9 @@ import (
1313 "strings"
1414 "time"
1515
16- "github.com/go-kit/kit/log"
17- "github.com/go-kit/kit/log/level"
16+ "github.com/GoogleCloudPlatform/functions-framework-go/functions"
17+ "github.com/go-kit/log"
18+ "github.com/go-kit/log/level"
1819 "github.com/prometheus/blackbox_exporter/config"
1920 "github.com/prometheus/blackbox_exporter/prober"
2021 "github.com/prometheus/client_golang/prometheus"
@@ -45,11 +46,15 @@ var (
4546 })
4647)
4748
48- // Handler is a http.Handler which will be called by the
49+ func init () {
50+ functions .HTTP ("Handler" , handler )
51+ }
52+
53+ // handler is a http.handler which will be called by the
4954// GCP Cloud Function on every request.
5055// It has been adapted from the original blackbox_exporter probeHandler:
5156// https://github.com/prometheus/blackbox_exporter/blob/63678419a6a274ac6d43d3d4088cad2a1d06371f/main.go#L70
52- func Handler (w http.ResponseWriter , r * http.Request ) {
57+ func handler (w http.ResponseWriter , r * http.Request ) {
5358 ctx , cancel := context .WithTimeout (r .Context (), timeoutSeconds * time .Second )
5459 defer cancel ()
5560 r = r .WithContext (ctx )
@@ -92,7 +97,7 @@ func Handler(w http.ResponseWriter, r *http.Request) {
9297 }
9398 }
9499
95- level .Info (logger ).Log ("msg" , "Beginning probe" , "probe" , module .Prober , "timeout_seconds" , timeoutSeconds )
100+ _ = level .Info (logger ).Log ("msg" , "Beginning probe" , "probe" , module .Prober , "timeout_seconds" , timeoutSeconds )
96101
97102 probeSuccessGauge := prometheus .NewGauge (prometheus.GaugeOpts {
98103 Name : "probe_success" ,
@@ -113,14 +118,14 @@ func Handler(w http.ResponseWriter, r *http.Request) {
113118 probeDurationGauge .Set (duration )
114119 if success {
115120 probeSuccessGauge .Set (1 )
116- level .Info (logger ).Log ("msg" , "Probe succeeded" , "duration_seconds" , duration )
121+ _ = level .Info (logger ).Log ("msg" , "Probe succeeded" , "duration_seconds" , duration )
117122 } else {
118- level .Error (logger ).Log ("msg" , "Probe failed" , "duration_seconds" , duration )
123+ _ = level .Error (logger ).Log ("msg" , "Probe failed" , "duration_seconds" , duration )
119124 }
120125
121126 mfs , err := registry .Gather ()
122127 if err != nil {
123- level .Debug (logger ).Log ("msg" , err .Error ())
128+ _ = level .Debug (logger ).Log ("msg" , err .Error ())
124129 http .Error (w , err .Error (), http .StatusInternalServerError )
125130 return
126131 }
@@ -134,15 +139,15 @@ func Handler(w http.ResponseWriter, r *http.Request) {
134139 enc := expfmt .NewEncoder (buf , contentType )
135140 for _ , mf := range mfs {
136141 if err := enc .Encode (mf ); err != nil {
137- level .Debug (logger ).Log ("msg" , err .Error ())
142+ _ = level .Debug (logger ).Log ("msg" , err .Error ())
138143 http .Error (w , err .Error (), http .StatusInternalServerError )
139144 return
140145 }
141146 }
142147
143148 w .Header ().Add ("Content-Type" , string (contentType ))
144149 w .Header ().Add ("Content-Length" , fmt .Sprint (buf .Len ()))
145- fmt .Fprint (w , buf )
150+ _ , _ = fmt .Fprint (w , buf )
146151}
147152
148153// If a google cloud function is experiencing a cold start, TLS handshakes were observed
@@ -157,7 +162,7 @@ func coldStartRequest(target string) error {
157162 return nil
158163 }
159164 cloudFunctionColdStartGauge .Set (1 )
160- level .Info (logger ).Log ("msg" , "cold start detected, making an initial request to the target" , "target" , target )
165+ _ = level .Info (logger ).Log ("msg" , "cold start detected, making an initial request to the target" , "target" , target )
161166 client = & http.Client {
162167 Timeout : 10 * time .Second ,
163168 }
@@ -213,23 +218,21 @@ func overwriteHTTPModuleParams(params url.Values, conf *config.HTTPProbe) error
213218// parseStatusCodes parses the given comma separated status codes
214219// it is possible to use shortcuts like 2xx, 3xx, etc which will be expanded
215220// to the corresponding whole range of status codes
221+ var statusCodeRangeRe = regexp .MustCompile ("[0-9]xx" )
222+
216223func parseStatusCodes (codes string ) ([]int , error ) {
217224 var result []int
218- splittedCodes := strings .Split (codes , "," )
219- for _ , code := range splittedCodes {
225+ splitCodes := strings .Split (codes , "," )
226+ for _ , code := range splitCodes {
220227 code := strings .TrimSpace (code )
221- match , err := regexp .MatchString ("[0-9]xx" , code )
222- if err != nil {
223- return nil , err
224- }
225- if match {
228+ if statusCodeRangeRe .MatchString (code ) {
226229 mainCode , _ := strconv .Atoi (string (code [0 ]))
227230 result = append (result , generateCodeRange (mainCode * 100 , mainCode * 100 + 99 )... )
228231 continue
229232 }
230233 parsed , err := strconv .Atoi (code )
231234 if err != nil {
232- return nil , fmt .Errorf ("Can not convert status code \" %s \" to a number" , code )
235+ return nil , fmt .Errorf ("can not convert status code %q to a number" , code )
233236 }
234237 result = append (result , parsed )
235238 }
0 commit comments