Skip to content

Commit c55bb63

Browse files
Add metrics middleware and wire into API
Introduce business/web/v1/mid/metrics.go to track requests, errors, panics and goroutine samples. Call metrics.AddPanics in the Panics middleware and include mid.Metrics() in APIMux. Update hack handler to return an HTTP 400 response error using response.NewError. Add a Makefile 'load' target to run hey load tests
1 parent 975a36c commit c55bb63

5 files changed

Lines changed: 46 additions & 4 deletions

File tree

app/services/sales-api/v1/handlers/hackgrp/hackgrp.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@ package hackgrp
22

33
import (
44
"context"
5+
"errors"
56
"math/rand"
67
"net/http"
78

9+
"github.com/standard-librarian/gosale/business/web/v1/response"
810
"github.com/standard-librarian/gosale/foundation/web"
911
)
1012

1113
func Hack(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
12-
if n := rand.Intn(2); n == 0 {
13-
return web.NewShutdownError("hack group initiated shutdown")
14+
if n := rand.Intn(100) % 2; n == 0 {
15+
return response.NewError(errors.New("TRUST ERROR"), http.StatusBadRequest)
1416
}
17+
1518
status := struct {
1619
Status string
1720
}{

business/web/v1/mid/metrics.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package mid
2+
3+
import (
4+
"context"
5+
"net/http"
6+
7+
metrics "github.com/standard-librarian/gosale/business/web/v1/mertrics"
8+
"github.com/standard-librarian/gosale/foundation/web"
9+
)
10+
11+
// Metrics updates program counters.
12+
func Metrics() web.Middleware {
13+
m := func(handler web.Handler) web.Handler {
14+
h := func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
15+
ctx = metrics.Set(ctx)
16+
17+
err := handler(ctx, w, r)
18+
19+
n := metrics.AddRequests(ctx)
20+
if n%1000 == 0 {
21+
metrics.AddGoroutines(ctx)
22+
}
23+
24+
if err != nil {
25+
metrics.AddErrors(ctx)
26+
}
27+
28+
return err
29+
}
30+
31+
return h
32+
}
33+
34+
return m
35+
}

business/web/v1/mid/panics.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77
"runtime/debug"
88

9+
metrics "github.com/standard-librarian/gosale/business/web/v1/mertrics"
910
"github.com/standard-librarian/gosale/foundation/web"
1011
)
1112

@@ -22,7 +23,7 @@ func Panics() web.Middleware {
2223
trace := debug.Stack()
2324
err = fmt.Errorf("PANIC [%v] TRACE[%s]", rec, string(trace))
2425

25-
// metrics.AddPanics(ctx)
26+
metrics.AddPanics(ctx)
2627
}
2728
}()
2829

business/web/v1/v1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type RouteAdder interface {
2323

2424
// APIMux constructs a http.Handler with all application routes defined.
2525
func APIMux(cfg APIMuxConfig, routeAdder RouteAdder) *web.App {
26-
app := web.NewApp(cfg.Shutdown, mid.Logger(cfg.Log), mid.Errors(cfg.Log), mid.Panics())
26+
app := web.NewApp(cfg.Shutdown, mid.Logger(cfg.Log), mid.Errors(cfg.Log), mid.Metrics(), mid.Panics())
2727

2828
routeAdder.Add(app, cfg)
2929

makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ run-help:
1111
curl:
1212
curl -il http://localhost:3000/hack
1313

14+
load:
15+
hey -m GET -c 100 -n 100000 "http://localhost:3000/hack"
16+
1417
# ==============================================================================
1518
# Define dependencies
1619

0 commit comments

Comments
 (0)