Skip to content

Commit

Permalink
Update metrics with proper name
Browse files Browse the repository at this point in the history
  • Loading branch information
alinz committed Aug 29, 2024
1 parent 3a21e6b commit 7df45e5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
1 change: 1 addition & 0 deletions cmd/baker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ https://ella.to/baker
slog.SetLogLoggerLevel(parseLogLevel(logLevel))

metricsHandler := metrics.SetupHandler()
metrics.SetInfo(Version, GitCommit)

dockerGetter, err := httpclient.NewClient(
httpclient.WithUnixSock("/var/run/docker.sock", "http://localhost"),
Expand Down
41 changes: 30 additions & 11 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,37 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
)

var processedRequests = prometheus.NewCounterVec(
var websocketRequestCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "baker",
Name: "baker_pattern_requests_total",
Name: "websocket_request_count",
Help: "How many WebSocket requests processed, partitioned by status code, method and HTTP path.",
},
[]string{"domain", "path", "method", "code"},
)

var httpRequestCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "baker",
Name: "http_request_count",
Help: "How many HTTP requests processed, partitioned by status code, method and HTTP path (with patterns).",
},
[]string{"domain", "path", "method", "code"},
)

var requestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
var httpRequestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "baker",
Name: "baker_pattern_request_duration_seconds",
Name: "http_request_duration_seconds",
Help: "How long it took to process the request, partitioned by status code, method and HTTP path (with patterns).",
Buckets: []float64{.1, .3, 1, 1.5, 2, 5, 10},
},
[]string{"domain", "path", "method", "code"},
)

var infoGuage = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "bus",
Namespace: "baker",
Name: "info",
Help: "Information about the bus version and commit hash",
Help: "Information about the baker version and commit hash",
}, []string{"version", "commit"})

func SetInfo(version, commit string) {
Expand All @@ -40,8 +49,8 @@ func SetInfo(version, commit string) {
}).Set(1)
}

func HttpProcessedRequest(domain string, method string, path string, code int) {
processedRequests.With(prometheus.Labels{
func HttpRequestCount(domain string, method string, path string, code int) {
httpRequestCount.With(prometheus.Labels{
"domain": domain,
"method": method,
"path": path,
Expand All @@ -50,22 +59,32 @@ func HttpProcessedRequest(domain string, method string, path string, code int) {
}

func HttpRequestDuration(domain string, method string, path string, code int, duration float64) {
requestDuration.With(prometheus.Labels{
httpRequestDuration.With(prometheus.Labels{
"domain": domain,
"method": method,
"path": path,
"code": strconv.FormatInt(int64(code), 10),
}).Observe(duration)
}

func WebsocketRequest(domain string, method string, path string, code int) {
websocketRequestCount.With(prometheus.Labels{
"domain": domain,
"method": method,
"path": path,
"code": strconv.FormatInt(int64(code), 10),
}).Inc()
}

func SetupHandler() http.Handler {
req := prometheus.NewRegistry()

req.MustRegister(
collectors.NewGoCollector(),
infoGuage,
processedRequests,
requestDuration,
httpRequestCount,
httpRequestDuration,
websocketRequestCount,
)

// Create a custom http serve mux
Expand Down
11 changes: 7 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,6 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
tw := &trackResponseWriter{w: w}

start := time.Now()
defer func() {
metrics.HttpProcessedRequest(domain, path, method, tw.statusCode)
metrics.HttpRequestDuration(domain, path, method, tw.statusCode, float64(time.Since(start)))
}()

var container *Container
endpoint := &Endpoint{
Expand All @@ -236,8 +232,15 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

if isWebSocketRequest(r) {
defer func() {
metrics.WebsocketRequest(domain, path, method, tw.statusCode)
}()
s.handleWebSocket(tw, r, container)
} else {
defer func() {
metrics.HttpRequestCount(domain, path, method, tw.statusCode)
metrics.HttpRequestDuration(domain, path, method, tw.statusCode, float64(time.Since(start)))
}()
s.handleHTTP(tw, r, container, endpoint)
}
}
Expand Down

0 comments on commit 7df45e5

Please sign in to comment.