Add ability to capture some more info about the api value request

This commit is contained in:
2021-10-13 16:29:30 +11:00
parent 9eef92902d
commit 33becf1aa6
2 changed files with 41 additions and 2 deletions
+12 -2
View File
@@ -22,6 +22,7 @@ import (
"github.com/prometheus/common/model"
"github.com/sirupsen/logrus"
"github.com/wahyd4/badger/models"
"github.com/wahyd4/badger/utils"
)
var (
@@ -32,6 +33,11 @@ var (
Help: "The total request number of badges",
}, []string{"badge"})
aria2UIImageCounter = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "aria2_ui_docker_counter",
Help: "The counter for junv's aria2-ui docker image updates checker",
}, []string{"badge", "version", "arch", "ip"})
badgeGenerationTime = promauto.NewHistogram(prometheus.HistogramOpts{
Name: "badge_generation_time",
Help: "The time consumed for badge generation",
@@ -181,7 +187,6 @@ func (handler *BadgesHandler) saveBadgeValue(request *APIValueRequest, badge *mo
}
func (handler *BadgesHandler) generateBadgeValue(c *gin.Context) error {
startTime := time.Now()
_, internalRequest := c.GetQuery("i")
queryName := c.Param("name")
@@ -214,8 +219,13 @@ func (handler *BadgesHandler) generateBadgeValue(c *gin.Context) error {
}
if !internalRequest {
badgeGenerationTime.Observe(float64(time.Since(startTime).Microseconds()))
badgeRequested.With(map[string]string{"badge": queryName}).Inc()
aria2UIImageCounter.With(map[string]string{
"badge": queryName,
"version": c.Query("version"),
"arch": c.Request.UserAgent(),
"ip": utils.GetUserIP(c.Request),
}).Inc()
}
c.String(http.StatusOK, "%s", metric.TextValue)
return nil
+29
View File
@@ -0,0 +1,29 @@
package utils
import (
"fmt"
"net/http"
"net/http/httputil"
)
func GetUserIP(r *http.Request) string {
IPAddress := r.Header.Get("X-Real-Ip")
if IPAddress == "" {
IPAddress = r.Header.Get("X-Forwarded-For")
}
if IPAddress == "" {
IPAddress = r.RemoteAddr
}
return IPAddress
}
func DumpRequest(r *http.Request) {
if r == nil {
return
}
dump, err := httputil.DumpRequest(r, true)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(dump)
}