chore: initialize containerized Go learning workbook (2026 DevOps/SRE)

This commit is contained in:
soda64 committed 2026-11-30 23:03:31 +01:00
commit 84be3e5d77
64 files changed
+1382

No files matched your search

@@ -0,0 +1,10 @@
# 03 — SRE Observability (`/healthz`, `/readyz`, `log/slog`, Prometheus metrics)
## Zadania (`observability.go`)
1. `FormatPrometheusCounter(name, help string, value uint64) string` — wygeneruj poprawny tekst w formacie Prometheus exposition (`# HELP ...`, `# TYPE ... counter`, `<name> <value>\n`).
2. `ReadinessHandler(ready *atomic.Bool) http.HandlerFunc` — zwróć `200 OK` gdy `ready.Load()` jest `true`, inaczej `503 Service Unavailable`.
## Uruchomienie testów
```bash
make test PKG=./exercises/03-ecosystems/03-sre-health-and-metrics
```
@@ -0,0 +1,14 @@
package observability
import (
"net/http"
"sync/atomic"
)
func FormatPrometheusCounter(name, help string, value uint64) string {
panic("TODO: zwróć format Prometheus (# HELP, # TYPE counter, name value)")
}
func ReadinessHandler(ready *atomic.Bool) http.HandlerFunc {
panic("TODO: zwróć 200 OK gdy ready.Load() == true, inaczej 503 Service Unavailable")
}
@@ -0,0 +1,40 @@
package observability
import (
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
)
func TestFormatPrometheusCounter(t *testing.T) {
out := FormatPrometheusCounter("http_requests_total", "Total HTTP requests", 42)
for _, want := range []string{
"# HELP http_requests_total Total HTTP requests",
"# TYPE http_requests_total counter",
"http_requests_total 42",
} {
if !strings.Contains(out, want) {
t.Errorf("missing %q in:\n%s", want, out)
}
}
}
func TestReadinessHandler(t *testing.T) {
var ready atomic.Bool
h := ReadinessHandler(&ready)
rec1 := httptest.NewRecorder()
h(rec1, httptest.NewRequest(http.MethodGet, "/readyz", nil))
if rec1.Code != http.StatusServiceUnavailable {
t.Fatalf("got %d, want 503", rec1.Code)
}
ready.Store(true)
rec2 := httptest.NewRecorder()
h(rec2, httptest.NewRequest(http.MethodGet, "/readyz", nil))
if rec2.Code != http.StatusOK {
t.Fatalf("got %d, want 200", rec2.Code)
}
}