feat(observability): add product metrics for API, outbox and delivery
This commit is contained in:
1 parent
560724e2aa
commit
94488e2b57
51 files changed
+3655
-68
No files matched your search
@@ -0,0 +1,97 @@
|
||||
package httpadapter
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"lincodex/backend/internal/adapter/primary/http/problem"
|
||||
v1 "lincodex/backend/internal/adapter/primary/http/v1"
|
||||
metrics "lincodex/backend/internal/adapter/secondary/prometheus"
|
||||
"lincodex/backend/pkg/testutil"
|
||||
)
|
||||
|
||||
var requestIDField = regexp.MustCompile(`"request_id":"[^"]*"`)
|
||||
|
||||
// The request log is collected into Loki, so an address or an identifier that
|
||||
// a client puts into a path or a query must not reach it.
|
||||
func TestRequestLogNamesTheRouteTemplateAndNoRequestData(t *testing.T) {
|
||||
var logs bytes.Buffer
|
||||
logger := slog.New(slog.NewJSONHandler(&logs, nil))
|
||||
healthHandler := newTestHealthHandler(func(context.Context) error { return nil })
|
||||
responder := problem.NewResponder(logger)
|
||||
router, err := NewRouter(healthHandler, v1.NewHandler(healthHandler, responder), v1.NewDenyAllSecurity(), responder, metrics.New(), logger)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRouter() error = %v", err)
|
||||
}
|
||||
for _, target := range []string{
|
||||
"/api/v1/healthz?email=" + metricsFixtureEmail,
|
||||
"/api/v1/users/" + metricsFixtureEmail,
|
||||
"/api/v1/me/invitations/" + metricsFixtureID + "?email=" + metricsFixtureEmail + "&id=" + metricsFixtureID,
|
||||
"/api/" + metricsFixtureEmail,
|
||||
"/files/" + metricsFixtureID,
|
||||
// The operational routes are not logged at all.
|
||||
"/healthz?email=" + metricsFixtureEmail,
|
||||
"/readyz",
|
||||
} {
|
||||
router.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, target, nil))
|
||||
}
|
||||
|
||||
// The request ID is a random UUID per request that links log lines; it
|
||||
// identifies no person, so it is left out of the scan.
|
||||
scanned := requestIDField.ReplaceAllString(logs.String(), `"request_id":""`)
|
||||
if found := testutil.FindPersonalIdentifiers(scanned); len(found) != 0 {
|
||||
t.Fatalf("request log exposes identifiers %q:\n%s", found, logs.String())
|
||||
}
|
||||
for _, marker := range []string{metricsFixtureEmail, metricsFixtureID, "@"} {
|
||||
if strings.Contains(logs.String(), marker) {
|
||||
t.Fatalf("request log exposes %q:\n%s", marker, logs.String())
|
||||
}
|
||||
}
|
||||
type line struct {
|
||||
Msg string `json:"msg"`
|
||||
Route string `json:"route"`
|
||||
Status int `json:"status"`
|
||||
StatusClass string `json:"status_class"`
|
||||
ProblemCode string `json:"problem_code"`
|
||||
QueryParams int `json:"query_params"`
|
||||
RequestID string `json:"request_id"`
|
||||
}
|
||||
var got []line
|
||||
scanner := bufio.NewScanner(&logs)
|
||||
for scanner.Scan() {
|
||||
var entry line
|
||||
if err := json.Unmarshal(scanner.Bytes(), &entry); err != nil {
|
||||
t.Fatalf("decode log line %s: %v", scanner.Text(), err)
|
||||
}
|
||||
if strings.HasPrefix(entry.Msg, "http request") {
|
||||
got = append(got, entry)
|
||||
}
|
||||
}
|
||||
want := []line{
|
||||
{Msg: "http request", Route: "/api/v1/healthz", Status: 200, StatusClass: "2xx", QueryParams: 1},
|
||||
{Msg: "http request error", Route: "/api/v1/*", Status: 404, StatusClass: "4xx", ProblemCode: "not_found"},
|
||||
{Msg: "http request error", Route: "/api/v1/me/invitations/{invitation_id}", Status: 401, StatusClass: "4xx", ProblemCode: "unauthenticated", QueryParams: 2},
|
||||
{Msg: "http request error", Route: "/api/*", Status: 404, StatusClass: "4xx", ProblemCode: "not_found"},
|
||||
{Msg: "http request error", Route: "unmatched", Status: 404, StatusClass: "4xx"},
|
||||
}
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("logged %d request lines, want %d: %+v", len(got), len(want), got)
|
||||
}
|
||||
for index := range want {
|
||||
if got[index].RequestID == "" {
|
||||
t.Fatalf("line %d has no request ID", index)
|
||||
}
|
||||
got[index].RequestID = ""
|
||||
if got[index] != want[index] {
|
||||
t.Fatalf("line %d = %+v, want %+v", index, got[index], want[index])
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user