feat: support prometheus metric url. (#168)

This commit is contained in:
Bo-Yi Wu
2017-01-19 16:56:30 +08:00
committed by GitHub
parent cbbbc4eef0
commit a669070ffb
9 changed files with 180 additions and 44 deletions

87
gorush/metrics.go Normal file
View File

@@ -0,0 +1,87 @@
package gorush
import (
"github.com/prometheus/client_golang/prometheus"
)
const namespace = "gorush_"
// Metrics implements the prometheus.Metrics interface and
// exposes gitea metrics for prometheus
type Metrics struct {
TotalPushCount *prometheus.Desc
IosSuccess *prometheus.Desc
IosError *prometheus.Desc
AndroidSuccess *prometheus.Desc
AndroidError *prometheus.Desc
}
// NewMetrics returns a new Metrics with all prometheus.Desc initialized
func NewMetrics() Metrics {
return Metrics{
TotalPushCount: prometheus.NewDesc(
namespace+"total_push_count",
"Number of push count",
nil, nil,
),
IosSuccess: prometheus.NewDesc(
namespace+"ios_success",
"Number of iOS success count",
nil, nil,
),
IosError: prometheus.NewDesc(
namespace+"ios_error",
"Number of iOS fail count",
nil, nil,
),
AndroidSuccess: prometheus.NewDesc(
namespace+"android_success",
"Number of android success count",
nil, nil,
),
AndroidError: prometheus.NewDesc(
namespace+"android_fail",
"Number of android fail count",
nil, nil,
),
}
}
// Describe returns all possible prometheus.Desc
func (c Metrics) Describe(ch chan<- *prometheus.Desc) {
ch <- c.TotalPushCount
ch <- c.IosSuccess
ch <- c.IosError
ch <- c.AndroidSuccess
ch <- c.AndroidError
}
// Collect returns the metrics with values
func (c Metrics) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(
c.TotalPushCount,
prometheus.GaugeValue,
float64(StatStorage.GetTotalCount()),
)
ch <- prometheus.MustNewConstMetric(
c.IosSuccess,
prometheus.GaugeValue,
float64(StatStorage.GetIosSuccess()),
)
ch <- prometheus.MustNewConstMetric(
c.IosError,
prometheus.GaugeValue,
float64(StatStorage.GetIosError()),
)
ch <- prometheus.MustNewConstMetric(
c.AndroidSuccess,
prometheus.GaugeValue,
float64(StatStorage.GetAndroidSuccess()),
)
ch <- prometheus.MustNewConstMetric(
c.AndroidError,
prometheus.GaugeValue,
float64(StatStorage.GetAndroidError()),
)
}

View File

@@ -2,11 +2,20 @@ package gorush
import (
"fmt"
"github.com/gin-gonic/gin"
api "gopkg.in/appleboy/gin-status-api.v1"
"net/http"
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
api "gopkg.in/appleboy/gin-status-api.v1"
)
func init() {
// Support metrics
m := NewMetrics()
prometheus.MustRegister(m)
}
func abortWithError(c *gin.Context, code int, message string) {
c.JSON(code, gin.H{
"code": code,
@@ -58,6 +67,10 @@ func configHandler(c *gin.Context) {
c.YAML(http.StatusCreated, PushConf)
}
func metricsHandler(c *gin.Context) {
promhttp.Handler().ServeHTTP(c.Writer, c.Request)
}
func routerEngine() *gin.Engine {
// set server mode
gin.SetMode(PushConf.Core.Mode)
@@ -76,6 +89,7 @@ func routerEngine() *gin.Engine {
r.GET(PushConf.API.ConfigURI, configHandler)
r.GET(PushConf.API.SysStatURI, sysStatsHandler)
r.POST(PushConf.API.PushURI, pushHandler)
r.GET(PushConf.API.MetricURI, metricsHandler)
r.GET("/", rootHandler)
return r

View File

@@ -233,3 +233,14 @@ func TestSysStatsHandler(t *testing.T) {
assert.Equal(t, http.StatusOK, r.Code)
})
}
func TestMetricsHandler(t *testing.T) {
initTest()
r := gofight.New()
r.GET("/metrics").
Run(routerEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code)
})
}

View File

@@ -2,6 +2,7 @@ package gorush
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
@@ -13,6 +14,9 @@ func TestStorageDriverExist(t *testing.T) {
}
func TestStatForMemoryEngine(t *testing.T) {
// wait android push notification response.
time.Sleep(5 * time.Second)
var val int64
PushConf.Stat.Engine = "memory"
InitAppStatus()