added logic worker and metric controller
This commit is contained in:
parent
a86754de71
commit
d8ab8bd121
@ -35,7 +35,7 @@ func Run(ctx context.Context, logger *zap.Logger, cfg initialize.Config) error {
|
||||
|
||||
go researchWorker.Start(ctx)
|
||||
|
||||
prometheusController := metrics.NewPrometheus()
|
||||
prometheusController := metrics.NewPrometheus(researchWorker)
|
||||
|
||||
adminServer := http.NewServer(http.ServerConfig{
|
||||
Logger: logger,
|
||||
|
@ -1,15 +1,82 @@
|
||||
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
|
||||
}
|
||||
|
||||
func NewPrometheus() *Prometheus {
|
||||
return &Prometheus{}
|
||||
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) {
|
||||
@ -20,5 +87,12 @@ func (receiver *Prometheus) Metrics(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
|
@ -2,8 +2,11 @@ package research
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"gitea.pena/PenaDevops/smtpbiz-exporter/internal/client"
|
||||
"gitea.pena/PenaDevops/smtpbiz-exporter/internal/models"
|
||||
"go.uber.org/zap"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -17,6 +20,21 @@ type Research struct {
|
||||
client *client.SMTPClient
|
||||
logger *zap.Logger
|
||||
frequency int64
|
||||
Metrics Metrics
|
||||
}
|
||||
|
||||
type Metrics struct {
|
||||
// Метрики отправленных писем и лимитов
|
||||
HourlyEmailsSent int64 // Количество писем, отправленных за последний час
|
||||
HourlyEmailLimit int64 // Лимит на количество писем, которые можно отправить за час
|
||||
DailyEmailsSent int64 // Количество писем, отправленных за текущий день
|
||||
DailyEmailLimit int64 // Лимит на количество писем, которые можно отправить за день
|
||||
|
||||
// Метрики квот и баланса
|
||||
RemainingQuota int64 // Оставшееся количество писем, которые можно отправить в рамках текущего тарифа
|
||||
EmailValidationLimit int64 // Лимит на количество проверок наверное емейл адресов
|
||||
TariffQuota int64 // Общая квота на отправку писем, предоставляемая по тарифу
|
||||
Balance int64 // Баланс
|
||||
}
|
||||
|
||||
func NewResearch(deps Deps) *Research {
|
||||
@ -40,4 +58,25 @@ func (r *Research) Start(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
}
|
||||
func (r *Research) StartFetching(ctx context.Context) {}
|
||||
func (r *Research) StartFetching(ctx context.Context) {
|
||||
data, err := r.client.UseGetMethod(models.UserDataEndpoint, nil)
|
||||
if err != nil {
|
||||
r.logger.Error("failed to fetch user data", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
var userData models.UserDataResponse
|
||||
if err := json.Unmarshal(data, &userData); err != nil {
|
||||
r.logger.Error("failed to parse user data response", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
atomic.StoreInt64(&r.Metrics.HourlyEmailsSent, userData.HSent)
|
||||
atomic.StoreInt64(&r.Metrics.HourlyEmailLimit, userData.HLimit)
|
||||
atomic.StoreInt64(&r.Metrics.DailyEmailsSent, userData.DSent)
|
||||
atomic.StoreInt64(&r.Metrics.DailyEmailLimit, userData.DLimit)
|
||||
atomic.StoreInt64(&r.Metrics.RemainingQuota, userData.Quota)
|
||||
atomic.StoreInt64(&r.Metrics.EmailValidationLimit, userData.Validate)
|
||||
atomic.StoreInt64(&r.Metrics.TariffQuota, userData.TariffQuota)
|
||||
atomic.StoreInt64(&r.Metrics.Balance, int64(userData.Balance))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user