ezpromhttp

package module
v0.0.0-...-35db4d2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2018 License: MIT Imports: 9 Imported by: 2

README

Convenience Prometheus HTTP Metrics Middleware for Go

Build Status GitHub tag (latest SemVer)

Convenience middleware for adding of Prometheus HTTP metrics to a server that includes path, status, and method labels by default.

See this issue for potential performance issues.

Scope

This middleware automatically adds convenience labels to Prometheus HTTP metrics.

The following metrics are exposed:

  • http_request_total{method="${method}",path="/x/y/z",status="xxx"}
  • http_request_duration_ms{method="${method}",path="/x/y/z",status="xxx"}
  • http_response_size_bytes{method="${method}",path="/x/y/z",status="xxx"}
  • http_response_codes{status="xxx"}

xxx corresponds to a HTTP status code.

${method} corresponds to a HTTP verb such as get, post, put, delete, et cetera.

Installation

go get github.com/zephinzer/ezpromhttp

Usage

Import It

import (
  "github.com/prometheus/client_golang/prometheus/promhttp"
  // ...
  "github.com/zephinzer/ezpromhttp"
)

Summon the Middleware

Expose the Prometheus handler and instrument the main handler.

func main() {
  // ... assume a main handler named `handler`
  handler.Handle("/metrics", promhttp.Handler())
  // ... other setup
  handler = ezpromhttp.InstrumentHandler(handler)
  http.ListenAndServe("0.0.0.0:8080", handler)
}

License

This package is licensed under the MIT license. See the LICENSE file for the full text.

Contributing

You will need the following software to run the development environment based off this boilerplate at github.com/zephinzer/goboil:

  • Docker
  • Make

If you don't have Docker, you'll also need:

  • Go
  • Python
  • Dep (Go)
  • Realize (Go)

Development

Getting Started in Development

Copy ./sample.properties into ./Makefile.properties and set your required values there. These configurations are mostly for production image releasing so you may not need to change anything.

Testing

Append a .local to run the below on your host machine (test.local, testc.local, testw.local). Note that this may or may not work depending on what you have available on your host machine.

Standalone Tests

Run make test to run the tests once.

Standalone Tests with Live-Reload

Run make testw to run the tests in automated live-reload mode.

Requires python to be installed on your machine. The script is at ./.scripts/auto-run.py courtesy of GoConvey.

Standalone Tests with Coverage

Run make testc to run the tests once and output the coverage.

Releasing

To get the latest version of the application, use make version.get.

To bump the patch version, use make version.bump

To bump the minor version, use make version.bump BUMP=minor

To bump the major version, use make version.bump BUMP=major

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InstrumentHandler

func InstrumentHandler(next http.Handler) http.Handler

Types

type InstrumentedResponseWriter

type InstrumentedResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

func (*InstrumentedResponseWriter) Length

func (w *InstrumentedResponseWriter) Length() int

func (*InstrumentedResponseWriter) StatusCode

func (w *InstrumentedResponseWriter) StatusCode() int

func (*InstrumentedResponseWriter) Write

func (w *InstrumentedResponseWriter) Write(b []byte) (n int, err error)

func (*InstrumentedResponseWriter) WriteHeader

func (w *InstrumentedResponseWriter) WriteHeader(statusCode int)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL