verification/internal/controllers/admin/prometheus/prometheus.go

61 lines
1.9 KiB
Go
Raw Normal View History

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