2023-02-09 08:59:31 +00:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/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-06-28 08:57:31 +00:00
|
|
|
registry prometheus.Registerer
|
2023-04-10 08:40:58 +00:00
|
|
|
State *StateMetrics
|
|
|
|
Pool *poolMetricsCollector
|
|
|
|
Billing *billingMetrics
|
|
|
|
Stats *APIStatMetrics
|
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-02-09 08:59:31 +00:00
|
|
|
return &GateMetrics{
|
2023-04-10 08:40:58 +00:00
|
|
|
registry: registry,
|
|
|
|
State: stateMetric,
|
|
|
|
Pool: poolMetric,
|
|
|
|
Billing: billingMetric,
|
|
|
|
Stats: statsMetric,
|
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-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()
|
|
|
|
}
|