Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
name: CI

on: [push]
on: [push, pull_request]

jobs:
check:
name: Check
runs-on: ubuntu-latest
# Execute the checks inside the contianer instead the VM.
# Execute the checks inside the container instead the VM.
container: golangci/golangci-lint:v1.27.0-alpine
steps:
- uses: actions/checkout@v1
- run: golangci-lint run -E goimports

test:
name: Test
unit-test:
name: Unit test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-go@v1
with:
go-version: 1.14
- run: make test

integration-test:
name: Integration test
runs-on: ubuntu-latest
needs: [check, unit-test]
steps:
- uses: actions/checkout@v1
- uses: actions/setup-go@v1
with:
go-version: 1.14
- run: make integration-test
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---

run:
build-tags:
- integration
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,23 @@

## [Unreleased]

Breaking change: The library has been refactored to be more flexible when adding new framework/libraries.

### Added

- New middleware helper for the Echo framework
- New middleware helper for the Echo framework.

### Changed

- Refactored internally how the Middleware works and gets the data to make it easier to extend and more reliable.
- Added `Reporter` interface as the service responsible of getting the data to be measured.
- All different framwork helpers now implement with the new Reporter way.
- Fixed Gin returning duplicated data (#31).
- (Breaking) Standard handler now is on `middleware/std` instead of `middleware`.

### Removed

- Middleware interface in favor of a struct.

## [0.6.1] - 2020-02-07

Expand Down
27 changes: 16 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,41 +1,46 @@

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

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


.PHONY: default
default: test

.PHONY: unit-test
unit-test:
unit-test: ## Execute unit tests.
$(UNIT_TEST_CMD)

.PHONY: integration-test
integration-test:
integration-test: ## Execute unit tests.
$(INTEGRATION_TEST_CMD)

.PHONY: test
test: integration-test
.PHONY: test ## Alias for unit tests.
test: unit-test

.PHONY: benchmark
benchmark:
benchmark: ## Execute benchmarks.
$(BENCHMARK_CMD)

.PHONY: check
check:
check: ## Execute check.
$(CHECK_CMD)

.PHONY: deps
deps:
deps: ## Tidy dependencies.
$(DEPS_CMD)

.PHONY: mocks
mocks:
mocks: ## Generates mocks.
$(MOCKS_CMD)

.PHONY: docs
docs:
docs: ## Runs docs example on :6060.
godoc -http=":6060"
21 changes: 4 additions & 17 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# go-http-metrics [![Build Status][github-actions-image]][github-actions-url] [![Go Report Card][goreport-image]][goreport-url] [![GoDoc][godoc-image]][godoc-url]

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.

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.
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.

## Table of contents

Expand Down Expand Up @@ -58,6 +56,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
metrics "github.com/slok/go-http-metrics/metrics/prometheus"
"github.com/slok/go-http-metrics/middleware"
middlewarestd "github.com/slok/go-http-metrics/middleware/std"
)

func main() {
Expand All @@ -67,11 +66,11 @@ func main() {
})

// Our handler.
myHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("hello world!"))
})
h := mdlw.Handler("", myHandler)
h = middlewarestd.Handler("", mdlw, h)

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

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.

## Benchmarks

```text
pkg: github.com/slok/go-http-metrics/middleware

BenchmarkMiddlewareHandler/benchmark_with_default_settings.-4 1000000 1206 ns/op 256 B/op 6 allocs/op
BenchmarkMiddlewareHandler/benchmark_disabling_measuring_size.-4 1000000 1198 ns/op 256 B/op 6 allocs/op
BenchmarkMiddlewareHandler/benchmark_disabling_inflights.-4 1000000 1139 ns/op 256 B/op 6 allocs/op
BenchmarkMiddlewareHandler/benchmark_with_grouped_status_code.-4 1000000 1534 ns/op 256 B/op 7 allocs/op
BenchmarkMiddlewareHandler/benchmark_with_predefined_handler_ID-4 1000000 1258 ns/op 256 B/op 6 allocs/op
```

[github-actions-image]: https://github.com/slok/go-http-metrics/workflows/CI/badge.svg
[github-actions-url]: https://github.com/slok/go-http-metrics/actions
[goreport-image]: https://goreportcard.com/badge/github.com/slok/go-http-metrics
Expand Down
3 changes: 2 additions & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ the main Go net/http handler:
"github.com/prometheus/client_golang/prometheus/promhttp"
httpmetrics "github.com/slok/go-http-metrics/metrics/prometheus"
httpmiddleware "github.com/slok/go-http-metrics/middleware"
httpstdmiddleware "github.com/slok/go-http-metrics/middleware/std"
)

func main() {
Expand All @@ -24,7 +25,7 @@ the main Go net/http handler:
w.WriteHeader(http.StatusOK)
w.Write([]byte("hello world!"))
})
h := mdlw.Handler("", myHandler)
h := httpstdmiddleware.Handler("", mdlw, myHandler)

// Serve metrics.
log.Printf("serving metrics at: %s", ":9090")
Expand Down
9 changes: 5 additions & 4 deletions examples/custom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
metrics "github.com/slok/go-http-metrics/metrics/prometheus"
"github.com/slok/go-http-metrics/middleware"
"github.com/slok/go-http-metrics/middleware/std"
)

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

// Serve our handler.
go func() {
Expand Down
3 changes: 2 additions & 1 deletion examples/default/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
metrics "github.com/slok/go-http-metrics/metrics/prometheus"
"github.com/slok/go-http-metrics/middleware"
"github.com/slok/go-http-metrics/middleware/std"
)

const (
Expand Down Expand Up @@ -45,7 +46,7 @@ func main() {

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

// Serve our handler.
go func() {
Expand Down
7 changes: 5 additions & 2 deletions examples/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

metrics "github.com/slok/go-http-metrics/metrics/prometheus"
"github.com/slok/go-http-metrics/middleware"
echoMiddleware "github.com/slok/go-http-metrics/middleware/echo"
echomiddleware "github.com/slok/go-http-metrics/middleware/echo"
)

const (
Expand All @@ -28,12 +28,15 @@ func main() {

// Create Echo instance and global middleware.
e := echo.New()
e.Use(echoMiddleware.Handler("", mdlw))
e.Use(echomiddleware.Handler("", mdlw))

// Add our handler.
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello world")
})
e.GET("/json", func(c echo.Context) error {
return c.JSON(http.StatusAccepted, map[string]string{"hello": "world"})
})
e.GET("/wrong", func(c echo.Context) error {
return c.String(http.StatusTooManyRequests, "oops")
})
Expand Down
8 changes: 7 additions & 1 deletion examples/gin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ func main() {

// Add our handler.
engine.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "Hello world")
c.String(http.StatusOK, "Hello %s", "world")
})
engine.GET("/json", func(c *gin.Context) {
c.JSON(http.StatusAccepted, map[string]string{"hello": "world"})
})
engine.GET("/yaml", func(c *gin.Context) {
c.YAML(http.StatusCreated, map[string]string{"hello": "world"})
})
engine.GET("/wrong", func(c *gin.Context) {
c.String(http.StatusTooManyRequests, "oops")
Expand Down
6 changes: 4 additions & 2 deletions examples/opencensus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (

ocprometheus "contrib.go.opencensus.io/exporter/prometheus"
ocmmetrics "github.com/slok/go-http-metrics/metrics/opencensus"
"github.com/slok/go-http-metrics/middleware"
"go.opencensus.io/stats/view"

"github.com/slok/go-http-metrics/middleware"
"github.com/slok/go-http-metrics/middleware/std"
)

const (
Expand Down Expand Up @@ -52,7 +54,7 @@ func main() {

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

// Serve our handler.
go func() {
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ module github.com/slok/go-http-metrics

require (
contrib.go.opencensus.io/exporter/prometheus v0.1.0
github.com/emicklei/go-restful v2.11.1+incompatible
github.com/gin-gonic/gin v1.5.0
github.com/emicklei/go-restful v2.12.0+incompatible
github.com/gin-gonic/gin v1.6.3
github.com/julienschmidt/httprouter v1.3.0
github.com/labstack/echo/v4 v4.1.16
github.com/prometheus/client_golang v1.2.1
github.com/stretchr/testify v1.4.0
github.com/prometheus/client_golang v1.6.0
github.com/stretchr/testify v1.5.1
github.com/urfave/negroni v1.0.0
go.opencensus.io v0.22.0
go.opencensus.io v0.22.3
)

go 1.13
Loading