https://github.com/jmcfarlane/httprwd
A golang http.ResponseWriter delegate that keeps track of the response code
https://github.com/jmcfarlane/httprwd
Last synced: 12 months ago
JSON representation
A golang http.ResponseWriter delegate that keeps track of the response code
- Host: GitHub
- URL: https://github.com/jmcfarlane/httprwd
- Owner: jmcfarlane
- Created: 2016-10-29T22:04:03.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-05-07T01:11:21.000Z (about 3 years ago)
- Last Synced: 2025-03-22T11:44:21.044Z (about 1 year ago)
- Language: Go
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# httprwd
A simple [http.ResponseWriter](https://golang.org/pkg/net/http/#ResponseWriter)
delegate that keeps track of the response code.
## Installation
```
go get github.com/jmcfarlane/httprwd/...
```
## Usage
The typical use case is when you want to emit metrics about an http
route, and include the response code.
```go
d := &httprwd.ResponseWriterDelegate{ResponseWriter: w}
start := time.Now()
handler.ServeHTTP(d, r)
handlerDuration.WithLabelValues(
r.URL.Path,
r.Method,
strconv.Itoa(d.Code),
).Observe(time.Since(start).Seconds())
```
## Working example
```
go run example/main.go
curl -s localhost:8080
curl -s localhost:8080/metrics
```
## Tests
```
$ go test -v -cover -race
=== RUN TestResponseWriterDelegateStatusOK
--- PASS: TestResponseWriterDelegateStatusOK (0.00s)
=== RUN TestResponseWriterDelegateStatusNotFound
--- PASS: TestResponseWriterDelegateStatusNotFound (0.00s)
=== RUN TestResponseWriterDelegateStatusUnset
--- PASS: TestResponseWriterDelegateStatusUnset (0.00s)
PASS
coverage: 100.0% of statements
ok github.com/jmcfarlane/httprwd 1.012s
```