A Gin middleware for reporting to statsd daemon.
You can download the middleware using
go get github.com/amalfra/gin-statsd/v3
Next, import the package
import (
statsdMiddleware "github.com/amalfra/gin-statsd/v3/middleware"
)
- Attach middleware to all routes
r := gin.New()
r.Use(statsdMiddleware.New(statsdMiddleware.Options{}))
or
- Attach middleware to specific route
r := gin.New()
r.GET("/", statsdMiddleware.New(statsdMiddleware.Options{}), func(c *gin.Context) {})
By default, the middleware will send status_code and response_time stats.
All stats are namespaced under statsdKey
key by default. This can be configured using options.
The middleware allows configuring using Options struct. The struct with config should be passed when initializing with New method. Supported configurations are:
- Host - statsd daemon's host; defaults to 127.0.0.1
- Port - statsd daemon's port; defaults to 8125
- RequestKey - the namespace for stats; defaults to statsdKey
Configuring namespace:
r := gin.New()
r.Use(statsdMiddleware.New(statsdMiddleware.Options{RequestKey: "myNamespace"}))
Configuring statsd host and port:
r := gin.New()
r.Use(statsdMiddleware.New(statsdMiddleware.Options{Host: "myhost.statsd", Port: 8089}))
Questions, problems or suggestions? Please post them on the issue tracker.
You can contribute changes by forking the project and submitting a pull request. Feel free to contribute 😍
The MIT License (MIT)
Copyright (c) 2018 Amal Francis
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.