verification/internal/controllers/admin/prometheus/prometheus.go
2024-12-10 12:04:47 +03:00

61 lines
1.9 KiB
Go

package prometheus
import (
"gitea.pena/PenaSide/verification/internal/repository"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
)
type Prometheus struct {
verificationRepo *repository.VerificationRepository
metrics prometheusRegistry
}
type prometheusRegistry struct {
totalBytes prometheus.Gauge // Общее количество загруженной памяти
totalTime prometheus.Gauge // Общее затраченное время на 1 загрузку
totalUploads prometheus.Gauge // Количество всего загрузок файлов
}
func NewPrometheus(verificationRepo *repository.VerificationRepository) *Prometheus {
registry := prometheusRegistry{
totalBytes: prometheus.NewGauge(prometheus.GaugeOpts{
Name: "s3_total_uploaded_bytes",
Help: "Total memory loaded in bytes to S3",
}),
totalTime: prometheus.NewGauge(prometheus.GaugeOpts{
Name: "s3_total_upload_time_ns",
Help: "Total time spent downloading files to S3",
}),
totalUploads: prometheus.NewGauge(prometheus.GaugeOpts{
Name: "s3_total_upload_count",
Help: "Number of total file downloads to S3",
}),
}
prometheus.MustRegister(
registry.totalBytes,
registry.totalTime,
registry.totalUploads,
)
return &Prometheus{
verificationRepo: verificationRepo,
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.totalBytes.Set(float64(receiver.verificationRepo.Metrics.TotalBytes))
receiver.metrics.totalTime.Set(float64(receiver.verificationRepo.Metrics.TotalTime))
receiver.metrics.totalUploads.Set(float64(receiver.verificationRepo.Metrics.TotalUploads))
}