smtpbiz-exporter/internal/controller/metrics/prometheus.go

99 lines
4.0 KiB
Go

package metrics
import (
"gitea.pena/PenaDevops/smtpbiz-exporter/internal/worker/research"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
)
type Prometheus struct {
research *research.Research
metrics prometheusRegistry
}
type prometheusRegistry struct {
// Метрики отправленных писем и лимитов
hourlyEmailsSent prometheus.Gauge // Количество писем, отправленных за последний час
hourlyEmailLimit prometheus.Gauge // Лимит на количество писем, которые можно отправить за час
dailyEmailsSent prometheus.Gauge // Количество писем, отправленных за текущий день
dailyEmailLimit prometheus.Gauge // Лимит на количество писем, которые можно отправить за день
// Метрики квот и баланса
remainingQuota prometheus.Gauge // Оставшееся количество писем, которые можно отправить в рамках текущего тарифа
emailValidationLimit prometheus.Gauge // Лимит на количество проверок наверное емейл адресов
tariffQuota prometheus.Gauge // Общая квота на отправку писем, предоставляемая по тарифу
balance prometheus.Gauge // Баланс
}
func NewPrometheus(research *research.Research) *Prometheus {
registry := prometheusRegistry{
hourlyEmailsSent: prometheus.NewGauge(prometheus.GaugeOpts{
Name: "hourly_emails_sent",
Help: "Number of emails sent in the last hour",
}),
hourlyEmailLimit: prometheus.NewGauge(prometheus.GaugeOpts{
Name: "hourly_email_limit",
Help: "Limit on the number of emails that can be sent per hour",
}),
dailyEmailsSent: prometheus.NewGauge(prometheus.GaugeOpts{
Name: "daily_emails_sent",
Help: "Number of emails sent this day",
}),
dailyEmailLimit: prometheus.NewGauge(prometheus.GaugeOpts{
Name: "daily_email_limit",
Help: "Limit on the number of emails that can be sent per day",
}),
remainingQuota: prometheus.NewGauge(prometheus.GaugeOpts{
Name: "remaining_quota",
Help: "Remaining number of letters that can be sent within the current tariff",
}),
emailValidationLimit: prometheus.NewGauge(prometheus.GaugeOpts{
Name: "email_validation_limit",
Help: "The limit on the number of checks is probably email addresses",
}),
tariffQuota: prometheus.NewGauge(prometheus.GaugeOpts{
Name: "tariff_quota",
Help: "General quota for sending letters provided according to the tariff",
}),
balance: prometheus.NewGauge(prometheus.GaugeOpts{
Name: "balance",
Help: "Balance",
}),
}
prometheus.MustRegister(
registry.hourlyEmailsSent,
registry.hourlyEmailLimit,
registry.dailyEmailsSent,
registry.dailyEmailLimit,
registry.remainingQuota,
registry.emailValidationLimit,
registry.tariffQuota,
registry.balance,
)
return &Prometheus{
research: research,
metrics: registry,
}
}
func (receiver *Prometheus) Metrics(w http.ResponseWriter, r *http.Request) {
receiver.updateMetrics()
handler := promhttp.Handler()
handler.ServeHTTP(w, r)
}
func (receiver *Prometheus) updateMetrics() {
receiver.metrics.hourlyEmailsSent.Set(float64(receiver.research.Metrics.HourlyEmailsSent))
receiver.metrics.hourlyEmailLimit.Set(float64(receiver.research.Metrics.HourlyEmailLimit))
receiver.metrics.dailyEmailsSent.Set(float64(receiver.research.Metrics.DailyEmailsSent))
receiver.metrics.dailyEmailLimit.Set(float64(receiver.research.Metrics.DailyEmailLimit))
receiver.metrics.remainingQuota.Set(float64(receiver.research.Metrics.RemainingQuota))
receiver.metrics.emailValidationLimit.Set(float64(receiver.research.Metrics.EmailValidationLimit))
receiver.metrics.tariffQuota.Set(float64(receiver.research.Metrics.TariffQuota))
receiver.metrics.balance.Set(float64(receiver.research.Metrics.Balance))
}