Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

report metrics for bad requests #52

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions pkg/server/http_server.go
Original file line number Diff line number Diff line change
@@ -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"
)
@@ -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
@@ -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
@@ -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