2023-02-09 08:59:31 +00:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2023-09-26 11:26:33 +00:00
|
|
|
"git.frostfs.info/mbiryukova/frostfs-sdk-go/pool"
|
2023-02-09 08:59:31 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2023-04-10 08:40:58 +00:00
|
|
|
dto "github.com/prometheus/client_model/go"
|
2023-02-09 08:59:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const namespace = "frostfs_s3_gw"
|
|
|
|
|
|
|
|
type StatisticScraper interface {
|
|
|
|
Statistic() pool.Statistic
|
|
|
|
}
|
|
|
|
|
|
|
|
type GateMetrics struct {
|
2023-08-29 15:17:56 +00:00
|
|
|
registry prometheus.Registerer
|
|
|
|
State *StateMetrics
|
|
|
|
Pool *poolMetricsCollector
|
|
|
|
Billing *billingMetrics
|
|
|
|
Stats *APIStatMetrics
|
|
|
|
HTTPServer *httpServerMetrics
|
2023-02-09 08:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewGateMetrics(scraper StatisticScraper) *GateMetrics {
|
2023-06-28 08:57:31 +00:00
|
|
|
registry := prometheus.DefaultRegisterer
|
2023-04-10 08:40:58 +00:00
|
|
|
|
2023-02-09 08:59:31 +00:00
|
|
|
stateMetric := newStateMetrics()
|
2023-04-10 08:40:58 +00:00
|
|
|
registry.MustRegister(stateMetric)
|
2023-02-09 08:59:31 +00:00
|
|
|
|
|
|
|
poolMetric := newPoolMetricsCollector(scraper)
|
2023-04-10 08:40:58 +00:00
|
|
|
registry.MustRegister(poolMetric)
|
2023-02-09 08:59:31 +00:00
|
|
|
|
|
|
|
billingMetric := newBillingMetrics()
|
2023-04-10 08:40:58 +00:00
|
|
|
billingMetric.Register()
|
2023-02-09 08:59:31 +00:00
|
|
|
|
2023-04-07 14:28:21 +00:00
|
|
|
statsMetric := newAPIStatMetrics()
|
2023-04-10 08:40:58 +00:00
|
|
|
registry.MustRegister(statsMetric)
|
2023-04-07 14:28:21 +00:00
|
|
|
|
2023-08-29 15:17:56 +00:00
|
|
|
serverMetric := newHTTPServerMetrics()
|
|
|
|
registry.MustRegister(serverMetric)
|
|
|
|
|
2023-02-09 08:59:31 +00:00
|
|
|
return &GateMetrics{
|
2023-08-29 15:17:56 +00:00
|
|
|
registry: registry,
|
|
|
|
State: stateMetric,
|
|
|
|
Pool: poolMetric,
|
|
|
|
Billing: billingMetric,
|
|
|
|
Stats: statsMetric,
|
|
|
|
HTTPServer: serverMetric,
|
2023-02-09 08:59:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GateMetrics) Unregister() {
|
2023-04-10 08:40:58 +00:00
|
|
|
g.registry.Unregister(g.State)
|
|
|
|
g.registry.Unregister(g.Pool)
|
|
|
|
g.Billing.Unregister()
|
|
|
|
g.registry.Unregister(g.Stats)
|
2023-08-29 15:17:56 +00:00
|
|
|
g.registry.Unregister(g.HTTPServer)
|
2023-02-09 08:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GateMetrics) Handler() http.Handler {
|
|
|
|
handler := http.NewServeMux()
|
|
|
|
handler.Handle("/", promhttp.Handler())
|
2023-04-10 08:40:58 +00:00
|
|
|
handler.Handle("/metrics/billing", promhttp.HandlerFor(g.Billing.Gatherer(), promhttp.HandlerOpts{}))
|
2023-02-09 08:59:31 +00:00
|
|
|
return handler
|
|
|
|
}
|
2023-04-10 08:40:58 +00:00
|
|
|
|
|
|
|
func (g *GateMetrics) Gather() ([]*dto.MetricFamily, error) {
|
|
|
|
return prometheus.Gatherers([]prometheus.Gatherer{g.Billing.Gatherer()}).Gather()
|
|
|
|
}
|