-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from bweston92/feature/status-code-extractor
Add status code extractor.
- Loading branch information
Showing
2 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/linki/instrumented_http" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
) | ||
|
||
func main() { | ||
go func() { | ||
http.Handle("/metrics", promhttp.Handler()) | ||
log.Fatal(http.ListenAndServe("127.0.0.1:9099", nil)) | ||
}() | ||
|
||
client := instrumented_http.NewClient(nil, &instrumented_http.Callbacks{ | ||
PathProcessor: instrumented_http.IdentityProcessor, | ||
QueryProcessor: instrumented_http.IdentityProcessor, | ||
CodeProcessor: func(code int) string { | ||
// export all status codes >= 400 as label status=failure instead of their individual values. | ||
if code >= http.StatusBadRequest { | ||
return "failure" | ||
} | ||
return "success" | ||
}, | ||
}) | ||
|
||
for { | ||
func() { | ||
resp, err := client.Get("https://kubernetes.io/docs/search/?q=pods") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
fmt.Printf("%d\n", resp.StatusCode) | ||
}() | ||
|
||
time.Sleep(10 * time.Second) | ||
} | ||
} | ||
|
||
// expected result: | ||
// | ||
// $ curl -Ss 127.0.0.1:9099/metrics | grep http | ||
// | ||
// http_request_duration_seconds{handler="instrumented_http",host="kubernetes.io",method="GET",path="/docs/search/",query="q=pods",scheme="https",status="success",quantile="0.5"} 0.662351 | ||
// http_request_duration_seconds{handler="instrumented_http",host="kubernetes.io",method="GET",path="/docs/search/",query="q=pods",scheme="https",status="success",quantile="0.9"} 1.000437 | ||
// http_request_duration_seconds{handler="instrumented_http",host="kubernetes.io",method="GET",path="/docs/search/",query="q=pods",scheme="https",status="success",quantile="0.99"} 1.000437 | ||
// http_request_duration_seconds_sum{handler="instrumented_http",host="kubernetes.io",method="GET",path="/docs/search/",query="q=pods",scheme="https",status="success"} 1.662788442 | ||
// http_request_duration_seconds_count{handler="instrumented_http",host="kubernetes.io",method="GET",path="/docs/search/",query="q=pods",scheme="https",status="success"} 2 |