mirror of
https://github.com/wahyd4/badger.git
synced 2026-08-09 05:15:58 +10:00
Add page view count metric
This commit is contained in:
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"image/color"
|
||||
"log"
|
||||
@@ -35,18 +36,20 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
queriesMap = map[string]queryParam{
|
||||
ErrNoSuchBadgeFound = errors.New("no such badge can be found, please make sure you have provided the correct information")
|
||||
queriesMap = map[string]queryParam{
|
||||
"mel": {Color: color.RGBA{49, 197, 83, 255}, Query: "air_index_iaqi_pm25_v{city_name=\"Melbourne CBD\"}", ValueField: "city_name"},
|
||||
"chengdu": {Color: color.RGBA{49, 197, 83, 255}, Query: "air_index_iaqi_pm25_v{city_name=\"Chengdu (成都)\"}", ValueField: "city_name"},
|
||||
"dockerhub": {Color: color.RGBA{60, 174, 163, 255}, Query: "docker_hub_pull_count{name=\"aria2-ui\"}", ValueField: "name"},
|
||||
"github": {Color: color.RGBA{32, 99, 155, 255}, Query: "github_repository_stars{name=\"aria2-ariang-docker\"}", ValueField: "name"},
|
||||
"badge-requests-count": {Color: color.RGBA{32, 99, 155, 255}, Query: "ceil(sum(increase(badge_requested_total[500d])))", Label: "Badge Req Count"},
|
||||
"junv-github-profile": {Color: color.RGBA{32, 99, 155, 255}, Query: "ceil(sum(increase(badge_requested_total{badge=\"junv-github-profile\"}[500d])))", Label: "Page Views Count"},
|
||||
}
|
||||
|
||||
badgeRequested = promauto.NewCounter(prometheus.CounterOpts{
|
||||
badgeRequested = promauto.NewCounterVec(prometheus.CounterOpts{
|
||||
Name: "badge_requested_total",
|
||||
Help: "The total request number of badges",
|
||||
})
|
||||
}, []string{"badge"})
|
||||
|
||||
badgeGenerationTime = promauto.NewHistogram(prometheus.HistogramOpts{
|
||||
Name: "badge_generation_time",
|
||||
@@ -89,6 +92,12 @@ func main() {
|
||||
r.GET("/svg/:name", func(c *gin.Context) {
|
||||
if err := generateSVG(client, c); err != nil {
|
||||
fmt.Println(err.Error())
|
||||
if err == ErrNoSuchBadgeFound {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "please try it again"})
|
||||
}
|
||||
})
|
||||
|
||||
@@ -112,7 +121,7 @@ func generateSVG(client api.Client, c *gin.Context) error {
|
||||
queryName := c.Param("name")
|
||||
query, ok := queriesMap[queryName]
|
||||
if !ok {
|
||||
query = queriesMap["mel"]
|
||||
return ErrNoSuchBadgeFound
|
||||
}
|
||||
metric := queryMetric(client, query)
|
||||
|
||||
@@ -141,7 +150,7 @@ func generateSVG(client api.Client, c *gin.Context) error {
|
||||
canvas.Text(badgeWidth*0.7, 15, valueText, textStyle)
|
||||
canvas.End()
|
||||
badgeGenerationTime.Observe(float64(time.Now().Sub(startTime).Microseconds()))
|
||||
badgeRequested.Inc()
|
||||
badgeRequested.With(map[string]string{"badge": queryName}).Inc()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user