Skip to content

Commit ae1558d

Browse files
authored
Merge pull request slok#38 from slok/next-v1
2 parents 5eb46db + 98aa361 commit ae1558d

35 files changed

+1090
-442
lines changed

.github/workflows/ci.yml

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
11
name: CI
22

3-
on: [push]
3+
on: [push, pull_request]
44

55
jobs:
66
check:
77
name: Check
88
runs-on: ubuntu-latest
9-
# Execute the checks inside the contianer instead the VM.
9+
# Execute the checks inside the container instead the VM.
1010
container: golangci/golangci-lint:v1.27.0-alpine
1111
steps:
1212
- uses: actions/checkout@v1
1313
- run: golangci-lint run -E goimports
1414

15-
test:
16-
name: Test
15+
unit-test:
16+
name: Unit test
1717
runs-on: ubuntu-latest
1818
steps:
1919
- uses: actions/checkout@v1
2020
- uses: actions/setup-go@v1
2121
with:
2222
go-version: 1.14
2323
- run: make test
24+
25+
integration-test:
26+
name: Integration test
27+
runs-on: ubuntu-latest
28+
needs: [check, unit-test]
29+
steps:
30+
- uses: actions/checkout@v1
31+
- uses: actions/setup-go@v1
32+
with:
33+
go-version: 1.14
34+
- run: make integration-test

.golangci.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
3+
run:
4+
build-tags:
5+
- integration

CHANGELOG.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,23 @@
22

33
## [Unreleased]
44

5+
Breaking change: The library has been refactored to be more flexible when adding new framework/libraries.
6+
57
### Added
68

7-
- New middleware helper for the Echo framework
9+
- New middleware helper for the Echo framework.
10+
11+
### Changed
12+
13+
- Refactored internally how the Middleware works and gets the data to make it easier to extend and more reliable.
14+
- Added `Reporter` interface as the service responsible of getting the data to be measured.
15+
- All different framwork helpers now implement with the new Reporter way.
16+
- Fixed Gin returning duplicated data (#31).
17+
- (Breaking) Standard handler now is on `middleware/std` instead of `middleware`.
18+
19+
### Removed
20+
21+
- Middleware interface in favor of a struct.
822

923
## [0.6.1] - 2020-02-07
1024

Makefile

+16-11
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,46 @@
11

2-
UNIT_TEST_CMD := go test `go list ./... | grep -v vendor` -race
3-
INTEGRATION_TEST_CMD := go test `go list ./... | grep -v vendor` -race -tags='integration'
2+
UNIT_TEST_CMD := go test `go list ./... | grep -v test\/integration` -race
3+
INTEGRATION_TEST_CMD := go test ./test/integration -race -tags='integration'
44
BENCHMARK_CMD := go test `go list ./... | grep -v vendor` -benchmem -bench=.
55
CHECK_CMD = golangci-lint run -E goimports
66
DEPS_CMD := go mod tidy
77
MOCKS_CMD := go generate ./internal/mocks
88

9+
help: ## Show this help.
10+
@echo "Help"
11+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-20s\033[93m %s\n", $$1, $$2}'
12+
13+
914
.PHONY: default
1015
default: test
1116

1217
.PHONY: unit-test
13-
unit-test:
18+
unit-test: ## Execute unit tests.
1419
$(UNIT_TEST_CMD)
1520

1621
.PHONY: integration-test
17-
integration-test:
22+
integration-test: ## Execute unit tests.
1823
$(INTEGRATION_TEST_CMD)
1924

20-
.PHONY: test
21-
test: integration-test
25+
.PHONY: test ## Alias for unit tests.
26+
test: unit-test
2227

2328
.PHONY: benchmark
24-
benchmark:
29+
benchmark: ## Execute benchmarks.
2530
$(BENCHMARK_CMD)
2631

2732
.PHONY: check
28-
check:
33+
check: ## Execute check.
2934
$(CHECK_CMD)
3035

3136
.PHONY: deps
32-
deps:
37+
deps: ## Tidy dependencies.
3338
$(DEPS_CMD)
3439

3540
.PHONY: mocks
36-
mocks:
41+
mocks: ## Generates mocks.
3742
$(MOCKS_CMD)
3843

3944
.PHONY: docs
40-
docs:
45+
docs: ## Runs docs example on :6060.
4146
godoc -http=":6060"

Readme.md

+4-17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# go-http-metrics [![Build Status][github-actions-image]][github-actions-url] [![Go Report Card][goreport-image]][goreport-url] [![GoDoc][godoc-image]][godoc-url]
22

3-
go-http-metrics knows how to measure http metrics in different metric formats. The metrics measured are based on [RED] and/or [Four golden signals], follow standards and try to be measured in a efficient way.
4-
5-
It measures based on a middleware that is compatible with Go core net/http handler, if you are using a framework that isn't directly compatible with go's `http.Handler` interface, do not worry, there are multiple helpers available to get middlewares for the most used http Go frameworks. If there isn't you can open an issue or a PR.
3+
go-http-metrics knows how to measure http metrics in different metric formats a different Go HTTP framework/libs. The metrics measured are based on [RED] and/or [Four golden signals], follow standards and try to be measured in a efficient way.
64

75
## Table of contents
86

@@ -58,6 +56,7 @@ import (
5856
"github.com/prometheus/client_golang/prometheus/promhttp"
5957
metrics "github.com/slok/go-http-metrics/metrics/prometheus"
6058
"github.com/slok/go-http-metrics/middleware"
59+
middlewarestd "github.com/slok/go-http-metrics/middleware/std"
6160
)
6261

6362
func main() {
@@ -67,11 +66,11 @@ func main() {
6766
})
6867

6968
// Our handler.
70-
myHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
69+
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
7170
w.WriteHeader(http.StatusOK)
7271
w.Write([]byte("hello world!"))
7372
})
74-
h := mdlw.Handler("", myHandler)
73+
h = middlewarestd.Handler("", mdlw, h)
7574

7675
// Serve metrics.
7776
log.Printf("serving metrics at: %s", ":9090")
@@ -196,18 +195,6 @@ Same options as the Prometheus recorder.
196195

197196
This Option is used to unregister the Recorder views before are being registered, this is option is mainly due to the nature of OpenCensus implementation and the huge usage fo global state making impossible to run multiple tests. On regular usage of the library this setting is very rare that needs to be used.
198197

199-
## Benchmarks
200-
201-
```text
202-
pkg: github.com/slok/go-http-metrics/middleware
203-
204-
BenchmarkMiddlewareHandler/benchmark_with_default_settings.-4 1000000 1206 ns/op 256 B/op 6 allocs/op
205-
BenchmarkMiddlewareHandler/benchmark_disabling_measuring_size.-4 1000000 1198 ns/op 256 B/op 6 allocs/op
206-
BenchmarkMiddlewareHandler/benchmark_disabling_inflights.-4 1000000 1139 ns/op 256 B/op 6 allocs/op
207-
BenchmarkMiddlewareHandler/benchmark_with_grouped_status_code.-4 1000000 1534 ns/op 256 B/op 7 allocs/op
208-
BenchmarkMiddlewareHandler/benchmark_with_predefined_handler_ID-4 1000000 1258 ns/op 256 B/op 6 allocs/op
209-
```
210-
211198
[github-actions-image]: https://github.com/slok/go-http-metrics/workflows/CI/badge.svg
212199
[github-actions-url]: https://github.com/slok/go-http-metrics/actions
213200
[goreport-image]: https://goreportcard.com/badge/github.com/slok/go-http-metrics

doc.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ the main Go net/http handler:
1111
"github.com/prometheus/client_golang/prometheus/promhttp"
1212
httpmetrics "github.com/slok/go-http-metrics/metrics/prometheus"
1313
httpmiddleware "github.com/slok/go-http-metrics/middleware"
14+
httpstdmiddleware "github.com/slok/go-http-metrics/middleware/std"
1415
)
1516
1617
func main() {
@@ -24,7 +25,7 @@ the main Go net/http handler:
2425
w.WriteHeader(http.StatusOK)
2526
w.Write([]byte("hello world!"))
2627
})
27-
h := mdlw.Handler("", myHandler)
28+
h := httpstdmiddleware.Handler("", mdlw, myHandler)
2829
2930
// Serve metrics.
3031
log.Printf("serving metrics at: %s", ":9090")

examples/custom/main.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/prometheus/client_golang/prometheus/promhttp"
1212
metrics "github.com/slok/go-http-metrics/metrics/prometheus"
1313
"github.com/slok/go-http-metrics/middleware"
14+
"github.com/slok/go-http-metrics/middleware/std"
1415
)
1516

1617
const (
@@ -50,10 +51,10 @@ func main() {
5051
// Wrape our middleware on each of the different handlers with the ID of the handler
5152
// this way we reduce the cardinality, for example: `/test/2` and `/test/4` will
5253
// have the same `handler` label on the metric: `/test/:testID`
53-
mux.Handle("/", mdlw.Handler("/", rooth))
54-
mux.Handle("/test/1", mdlw.Handler("/test/:testID", testh))
55-
mux.Handle("/test/2", mdlw.Handler("/test/:testID", testh2))
56-
mux.Handle("/other-test", mdlw.Handler("/other-test", othetesth))
54+
mux.Handle("/", std.Handler("/", mdlw, rooth))
55+
mux.Handle("/test/1", std.Handler("/test/:testID", mdlw, testh))
56+
mux.Handle("/test/2", std.Handler("/test/:testID", mdlw, testh2))
57+
mux.Handle("/other-test", std.Handler("/other-test", mdlw, othetesth))
5758

5859
// Serve our handler.
5960
go func() {

examples/default/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/prometheus/client_golang/prometheus/promhttp"
1212
metrics "github.com/slok/go-http-metrics/metrics/prometheus"
1313
"github.com/slok/go-http-metrics/middleware"
14+
"github.com/slok/go-http-metrics/middleware/std"
1415
)
1516

1617
const (
@@ -45,7 +46,7 @@ func main() {
4546

4647
// Wrap our main handler, we pass empty handler ID so the middleware inferes
4748
// the handler label from the URL.
48-
h := mdlw.Handler("", mux)
49+
h := std.Handler("", mdlw, mux)
4950

5051
// Serve our handler.
5152
go func() {

examples/echo/main.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
metrics "github.com/slok/go-http-metrics/metrics/prometheus"
1414
"github.com/slok/go-http-metrics/middleware"
15-
echoMiddleware "github.com/slok/go-http-metrics/middleware/echo"
15+
echomiddleware "github.com/slok/go-http-metrics/middleware/echo"
1616
)
1717

1818
const (
@@ -28,12 +28,15 @@ func main() {
2828

2929
// Create Echo instance and global middleware.
3030
e := echo.New()
31-
e.Use(echoMiddleware.Handler("", mdlw))
31+
e.Use(echomiddleware.Handler("", mdlw))
3232

3333
// Add our handler.
3434
e.GET("/", func(c echo.Context) error {
3535
return c.String(http.StatusOK, "Hello world")
3636
})
37+
e.GET("/json", func(c echo.Context) error {
38+
return c.JSON(http.StatusAccepted, map[string]string{"hello": "world"})
39+
})
3740
e.GET("/wrong", func(c echo.Context) error {
3841
return c.String(http.StatusTooManyRequests, "oops")
3942
})

examples/gin/main.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ func main() {
3131

3232
// Add our handler.
3333
engine.GET("/", func(c *gin.Context) {
34-
c.String(http.StatusOK, "Hello world")
34+
c.String(http.StatusOK, "Hello %s", "world")
35+
})
36+
engine.GET("/json", func(c *gin.Context) {
37+
c.JSON(http.StatusAccepted, map[string]string{"hello": "world"})
38+
})
39+
engine.GET("/yaml", func(c *gin.Context) {
40+
c.YAML(http.StatusCreated, map[string]string{"hello": "world"})
3541
})
3642
engine.GET("/wrong", func(c *gin.Context) {
3743
c.String(http.StatusTooManyRequests, "oops")

examples/opencensus/main.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import (
1010

1111
ocprometheus "contrib.go.opencensus.io/exporter/prometheus"
1212
ocmmetrics "github.com/slok/go-http-metrics/metrics/opencensus"
13-
"github.com/slok/go-http-metrics/middleware"
1413
"go.opencensus.io/stats/view"
14+
15+
"github.com/slok/go-http-metrics/middleware"
16+
"github.com/slok/go-http-metrics/middleware/std"
1517
)
1618

1719
const (
@@ -52,7 +54,7 @@ func main() {
5254

5355
// Wrap our main handler, we pass empty handler ID so the middleware inferes
5456
// the handler label from the URL.
55-
h := mdlw.Handler("", mux)
57+
h := std.Handler("", mdlw, mux)
5658

5759
// Serve our handler.
5860
go func() {

go.mod

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ module github.com/slok/go-http-metrics
22

33
require (
44
contrib.go.opencensus.io/exporter/prometheus v0.1.0
5-
github.com/emicklei/go-restful v2.11.1+incompatible
6-
github.com/gin-gonic/gin v1.5.0
5+
github.com/emicklei/go-restful v2.12.0+incompatible
6+
github.com/gin-gonic/gin v1.6.3
77
github.com/julienschmidt/httprouter v1.3.0
88
github.com/labstack/echo/v4 v4.1.16
9-
github.com/prometheus/client_golang v1.2.1
10-
github.com/stretchr/testify v1.4.0
9+
github.com/prometheus/client_golang v1.6.0
10+
github.com/stretchr/testify v1.5.1
1111
github.com/urfave/negroni v1.0.0
12-
go.opencensus.io v0.22.0
12+
go.opencensus.io v0.22.3
1313
)
1414

1515
go 1.13

0 commit comments

Comments
 (0)