[hw-7] add metrics, tracing

This commit is contained in:
Никита Шубин
2025-07-26 14:15:40 +00:00
parent 342bd3f726
commit 4396bebe80
38 changed files with 717 additions and 36 deletions

View File

@@ -0,0 +1,52 @@
package metrics
import (
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var (
requestCounter = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "app",
Name: "requests_total",
Help: "Total amount of request by handler",
}, []string{"method", "path", "status_code"})
requestDurationHistogram = promauto.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "app",
Name: "request_duration_seconds",
Help: "Latency of handler processing, seconds",
Buckets: prometheus.DefBuckets,
}, []string{"method", "path", "status_code"})
outboundCounter = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "app",
Name: "outbound_requests_total",
Help: "Total HTTP requests to external services",
}, []string{"method", "url", "status_code"})
outboundDurationHistogram = promauto.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "app",
Name: "outbound_request_duration_seconds",
Help: "Latency of outbound HTTP requests, seconds",
Buckets: prometheus.DefBuckets,
}, []string{"method", "url", "status_code"})
)
func IncRequestHandlerCount(method, path, statusCode string) {
requestCounter.WithLabelValues(method, path, statusCode).Inc()
}
func StoreHandlerRequestDuration(method, path, statusCode string, d time.Duration) {
requestDurationHistogram.WithLabelValues(method, path, statusCode).Observe(d.Seconds())
}
func IncOutboundRequestCount(method, url, status string) {
outboundCounter.WithLabelValues(method, url, status).Inc()
}
func StoreOutboundRequestDuration(method, url, status string, d time.Duration) {
outboundDurationHistogram.WithLabelValues(method, url, status).Observe(d.Seconds())
}

View File

@@ -0,0 +1,16 @@
package metrics
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var inMemoryObjectsGauge = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "app",
Name: "inmemory_repo_objects",
Help: "Current in-memory repository size",
})
func SetInMemoryObjects(n int) {
inMemoryObjectsGauge.Set(float64(n))
}