mirror of https://github.com/rocboss/paopao-ce
commit
30d268a269
@ -0,0 +1,57 @@
|
||||
// Copyright 2023 ROC. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package prometheus
|
||||
|
||||
import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/rocboss/paopao-ce/internal/conf"
|
||||
"github.com/rocboss/paopao-ce/internal/core"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type metrics struct {
|
||||
siteInfo *prometheus.GaugeVec
|
||||
ds core.DataService
|
||||
wc core.WebCache
|
||||
}
|
||||
|
||||
func (m *metrics) updateSiteInfo() {
|
||||
if onlineUserKeys, err := m.wc.Keys(conf.PrefixOnlineUser + "*"); err == nil {
|
||||
maxOnline := len(onlineUserKeys)
|
||||
m.siteInfo.With(prometheus.Labels{"name": "max_online"}).Set(float64(maxOnline))
|
||||
} else {
|
||||
logrus.Warnf("update promethues metrics[site_info_max_online] occurs error: %s", err)
|
||||
}
|
||||
if registerUserCount, err := m.ds.GetRegisterUserCount(); err == nil {
|
||||
m.siteInfo.With(prometheus.Labels{"name": "register_user_count"}).Set(float64(registerUserCount))
|
||||
} else {
|
||||
logrus.Warnf("update promethues metrics[site_info_register_user_count] occurs error: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *metrics) onUpdate() {
|
||||
logrus.Debugf("update promethues metrics job running")
|
||||
m.updateSiteInfo()
|
||||
}
|
||||
|
||||
func newMetrics(reg prometheus.Registerer, ds core.DataService, wc core.WebCache) *metrics {
|
||||
m := &metrics{
|
||||
siteInfo: prometheus.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "paopao",
|
||||
Subsystem: "site",
|
||||
Name: "simple_info",
|
||||
Help: "paopao-ce site simple information.",
|
||||
},
|
||||
[]string{
|
||||
// metric name
|
||||
"name",
|
||||
}),
|
||||
ds: ds,
|
||||
wc: wc,
|
||||
}
|
||||
reg.MustRegister(m.siteInfo)
|
||||
return m
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
// Copyright 2023 ROC. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package prometheus
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/collectors"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"github.com/robfig/cron/v3"
|
||||
"github.com/rocboss/paopao-ce/internal/conf"
|
||||
"github.com/rocboss/paopao-ce/internal/core"
|
||||
"github.com/rocboss/paopao-ce/internal/events"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func scheduleJobs(metrics *metrics) {
|
||||
spec := conf.JobManagerSetting.UpdateMetricsInterval
|
||||
schedule, err := cron.ParseStandard(spec)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
events.OnTask(schedule, metrics.onUpdate)
|
||||
logrus.Debug("shedule prometheus metrics update jobs complete")
|
||||
}
|
||||
|
||||
func NewHandler(ds core.DataService, wc core.WebCache) http.Handler {
|
||||
// Create non-global registry.
|
||||
registry := prometheus.NewRegistry()
|
||||
// Add go runtime metrics and process collectors.
|
||||
registry.MustRegister(
|
||||
collectors.NewGoCollector(),
|
||||
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
|
||||
)
|
||||
metrics := newMetrics(registry, ds, wc)
|
||||
scheduleJobs(metrics)
|
||||
return promhttp.HandlerFor(registry, promhttp.HandlerOpts{EnableOpenMetrics: true})
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
// Copyright 2023 ROC. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/Masterminds/semver/v3"
|
||||
"github.com/fatih/color"
|
||||
"github.com/rocboss/paopao-ce/internal/conf"
|
||||
"github.com/rocboss/paopao-ce/internal/dao"
|
||||
"github.com/rocboss/paopao-ce/internal/dao/cache"
|
||||
"github.com/rocboss/paopao-ce/internal/metrics/prometheus"
|
||||
)
|
||||
|
||||
var (
|
||||
_ Service = (*metricsService)(nil)
|
||||
)
|
||||
|
||||
type metricsService struct {
|
||||
*baseHttpService
|
||||
}
|
||||
|
||||
func (s *metricsService) Name() string {
|
||||
return "MetricsService"
|
||||
}
|
||||
|
||||
func (s *metricsService) Version() *semver.Version {
|
||||
return semver.MustParse("v0.1.0")
|
||||
}
|
||||
|
||||
func (s *metricsService) OnInit() error {
|
||||
s.registerRoute(s, nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *metricsService) String() string {
|
||||
return fmt.Sprintf("listen on %s\n", color.GreenString("http://%s:%s", conf.MetricsServerSetting.HttpIp, conf.MetricsServerSetting.HttpPort))
|
||||
}
|
||||
|
||||
func newMetricsService() Service {
|
||||
addr := conf.MetricsServerSetting.HttpIp + ":" + conf.MetricsServerSetting.HttpPort
|
||||
// notice this step just to register pprof server to start. don't share server with pprof.
|
||||
server := httpServers.from(addr, func() *httpServer {
|
||||
ds, wc := dao.DataService(), cache.NewWebCache()
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/metrics", prometheus.NewHandler(ds, wc))
|
||||
return &httpServer{
|
||||
baseServer: newBaseServe(),
|
||||
server: &http.Server{
|
||||
Addr: addr,
|
||||
Handler: mux,
|
||||
},
|
||||
}
|
||||
})
|
||||
return &metricsService{
|
||||
baseHttpService: &baseHttpService{
|
||||
server: server,
|
||||
},
|
||||
}
|
||||
}
|
Loading…
Reference in new issue