Skip to content

Commit

Permalink
report metrics for bad requests (#52)
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Shmulevich <[email protected]>
  • Loading branch information
dmitsh authored Jan 7, 2025
1 parent e7001f3 commit 104a485
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
22 changes: 14 additions & 8 deletions pkg/server/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import (
"fmt"
"io"
"net/http"
"time"

"github.com/prometheus/client_golang/prometheus/promhttp"
"k8s.io/klog/v2"

"github.com/NVIDIA/topograph/pkg/config"
"github.com/NVIDIA/topograph/pkg/metrics"
"github.com/NVIDIA/topograph/pkg/registry"
"github.com/NVIDIA/topograph/pkg/topology"
)
Expand Down Expand Up @@ -103,22 +105,21 @@ func generate(w http.ResponseWriter, r *http.Request) {
}

func readRequest(w http.ResponseWriter, r *http.Request) *topology.Request {
start := time.Now()

if r.Method != http.MethodPost {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return nil
return httpError(w, "", "", "Invalid request method", http.StatusMethodNotAllowed, time.Since(start))
}

body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Unable to read request body", http.StatusInternalServerError)
return nil
return httpError(w, "", "", "Unable to read request body", http.StatusInternalServerError, time.Since(start))
}
defer func() { _ = r.Body.Close() }()

tr, err := topology.GetTopologyRequest(body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return nil
return httpError(w, "", "", err.Error(), http.StatusBadRequest, time.Since(start))
}

// If provider and engine are not passed in the payload, use the ones specified in the config
Expand All @@ -132,8 +133,7 @@ func readRequest(w http.ResponseWriter, r *http.Request) *topology.Request {
klog.Info(tr.String())

if err = validate(tr); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return nil
return httpError(w, tr.Provider.Name, tr.Engine.Name, err.Error(), http.StatusBadRequest, time.Since(start))
}

return tr
Expand Down Expand Up @@ -194,3 +194,9 @@ func getresult(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write(res.Ret.([]byte))
}
}

func httpError(w http.ResponseWriter, provider, engine, msg string, code int, duration time.Duration) *topology.Request {
metrics.Add(provider, engine, code, duration)
http.Error(w, msg, code)
return nil
}
5 changes: 5 additions & 0 deletions tests/configs/topograph-local-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
http:
port: 49021
ssl: false

request_aggregation_delay: 2s
7 changes: 7 additions & 0 deletions tests/configs/topograph-sim-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
http:
port: 49021
ssl: false

forward_service_url: localhost:49025

request_aggregation_delay: 2s

0 comments on commit 104a485

Please sign in to comment.