Skip to content
Open
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
15 changes: 15 additions & 0 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ginprometheus

import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"os"
Expand Down Expand Up @@ -101,6 +102,8 @@ type Prometheus struct {

// gin.Context string to use as a prometheus URL label
URLLabelFromContext string
//read value to override api status ( first match in array)
StatusOverrideFromContext []string
}

// PrometheusPushGateway contains the configuration for pushing to a Prometheus pushgateway (optional)
Expand Down Expand Up @@ -367,6 +370,18 @@ func (p *Prometheus) HandlerFunc() gin.HandlerFunc {
c.Next()

status := strconv.Itoa(c.Writer.Status())

//iterate over each context key to find first match
if len(p.StatusOverrideFromContext) > 0 {
for _, v := range p.StatusOverrideFromContext {
statusFromContext, found := c.Get(v)
if found {
status = fmt.Sprintf("%v", statusFromContext)
break
}
}
}

elapsed := float64(time.Since(start)) / float64(time.Second)
resSz := float64(c.Writer.Size())

Expand Down