mirror of
https://github.com/3ybactuk/marketplace-go-service-project.git
synced 2025-10-30 14:03:45 +03:00
[hw-7] add metrics, tracing
This commit is contained in:
37
cart/internal/infra/http/round_trippers/metrics.go
Normal file
37
cart/internal/infra/http/round_trippers/metrics.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package round_trippers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"route256/cart/internal/infra/http/metrics"
|
||||
)
|
||||
|
||||
type MetricsRoundTripper struct {
|
||||
rt http.RoundTripper
|
||||
}
|
||||
|
||||
func NewMetricsRoundTripper(rt http.RoundTripper) http.RoundTripper {
|
||||
return &MetricsRoundTripper{
|
||||
rt: rt,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MetricsRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
start := time.Now()
|
||||
resp, err := m.rt.RoundTrip(r)
|
||||
|
||||
status := "error"
|
||||
|
||||
if resp != nil {
|
||||
status = strconv.Itoa(resp.StatusCode)
|
||||
}
|
||||
|
||||
url := r.URL.Path
|
||||
|
||||
metrics.IncOutboundRequestCount(r.Method, url, status)
|
||||
metrics.StoreOutboundRequestDuration(r.Method, url, status, time.Since(start))
|
||||
|
||||
return resp, err
|
||||
}
|
||||
Reference in New Issue
Block a user