Skip to content
Permalink
Browse files

Merge pull request #4 from prometherion/master

Providing namespace option
  • Loading branch information...
Depado committed Jan 8, 2019
2 parents 9c4f3cc + 7919166 commit ee66175a4606350f78ff590e745869c7846bd561
Showing with 41 additions and 3 deletions.
  1. +7 −2 README.md
  2. +17 −1 prom.go
  3. +17 −0 prom_test.go
@@ -52,9 +52,13 @@ func main() {
Specify the path on which the metrics are accessed
Default : "/metrics"

`Namespace(ns string)`
Specify the namespace
Default : "gin"

`Subsystem(sub string)`
Specify the subsystem
Default : "gin"
Default : "go"

`Engine(e *gin.Engine)`
Specify the Gin engine directly when initializing.
@@ -74,7 +78,8 @@ function after the initialization like this :

```go
p := ginprom.New(
ginprom.Subsystem("gin"),
ginprom.Namespace("gin"),
ginprom.Subsystem("gonic"),
ginprom.Path("/metrics"),
)
p.Use(r)
18 prom.go
@@ -11,7 +11,8 @@ import (
)

var defaultPath = "/metrics"
var defaultSys = "gin"
var defaultNs = "gin"
var defaultSys = "gonic"

type pmap struct {
sync.RWMutex
@@ -29,6 +30,7 @@ type Prometheus struct {
reqDur, reqSz, resSz prometheus.Summary

MetricsPath string
Namespace string
Subsystem string
Ignored pmapb
Engine *gin.Engine
@@ -63,6 +65,15 @@ func Subsystem(sub string) func(*Prometheus) {
}
}

// Namespace is an option allowing to set the namespace when intitializing
// with New.
// Example : ginprom.New(ginprom.Namespace("my_namespace"))
func Namespace(ns string) func(*Prometheus) {
return func(p *Prometheus) {
p.Namespace = ns
}
}

// Engine is an option allowing to set the gin engine when intializing with New.
// Example :
// r := gin.Default()
@@ -80,6 +91,7 @@ func Engine(e *gin.Engine) func(*Prometheus) {
func New(options ...func(*Prometheus)) *Prometheus {
p := &Prometheus{
MetricsPath: defaultPath,
Namespace: defaultNs,
Subsystem: defaultSys,
}
p.Ignored.values = make(map[string]bool)
@@ -124,6 +136,7 @@ func (p *Prometheus) get(handler string) (string, bool) {
func (p *Prometheus) register() {
p.reqCnt = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: p.Namespace,
Subsystem: p.Subsystem,
Name: "requests_total",
Help: "How many HTTP requests processed, partitioned by status code and HTTP method.",
@@ -134,6 +147,7 @@ func (p *Prometheus) register() {

p.reqDur = prometheus.NewSummary(
prometheus.SummaryOpts{
Namespace: p.Namespace,
Subsystem: p.Subsystem,
Name: "request_duration_seconds",
Help: "The HTTP request latencies in seconds.",
@@ -143,6 +157,7 @@ func (p *Prometheus) register() {

p.reqSz = prometheus.NewSummary(
prometheus.SummaryOpts{
Namespace: p.Namespace,
Subsystem: p.Subsystem,
Name: "request_size_bytes",
Help: "The HTTP request sizes in bytes.",
@@ -152,6 +167,7 @@ func (p *Prometheus) register() {

p.resSz = prometheus.NewSummary(
prometheus.SummaryOpts{
Namespace: p.Namespace,
Subsystem: p.Subsystem,
Name: "response_size_bytes",
Help: "The HTTP response sizes in bytes.",
@@ -59,6 +59,23 @@ func TestEngine(t *testing.T) {
unregister(p)
}

func TestNamespace(t *testing.T) {
p := New()
assert.Equal(t, p.Namespace, defaultNs, "namespace should be default")
unregister(p)

tests := []string{
"test",
"",
"_",
}
for _, test := range tests {
p = New(Namespace(test))
assert.Equal(t, p.Namespace, test, "should match")
unregister(p)
}
}

func TestSubsystem(t *testing.T) {
p := New()
assert.Equal(t, p.Subsystem, defaultSys, "subsystem should be default")

0 comments on commit ee66175

Please sign in to comment.
You can’t perform that action at this time.