Добавил логи

This commit is contained in:
2026-06-14 19:37:09 +03:00
parent d4bf8a8cad
commit 81ed58ecff
28 changed files with 379 additions and 121 deletions
+26 -5
View File
@@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"time"
@@ -38,7 +39,7 @@ const maxBody = 4 << 20 // 4 MiB — потолок на тело ответа
// getJSON выполняет GET и декодирует JSON-ответ в out. headers — опц.
// дополнительные заголовки (напр. Authorization).
func getJSON(ctx context.Context, hc *http.Client, rawURL string, headers map[string]string, out any) error {
func getJSON(ctx context.Context, hc *http.Client, log *slog.Logger, rawURL string, headers map[string]string, out any) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil)
if err != nil {
return fmt.Errorf("metadata: build request: %w", err)
@@ -47,11 +48,11 @@ func getJSON(ctx context.Context, hc *http.Client, rawURL string, headers map[st
for k, v := range headers {
req.Header.Set(k, v)
}
return doJSON(hc, req, out)
return doJSON(hc, log, req, out)
}
// postJSON выполняет POST с JSON-телом и декодирует ответ.
func postJSON(ctx context.Context, hc *http.Client, rawURL string, body, out any) error {
func postJSON(ctx context.Context, hc *http.Client, log *slog.Logger, rawURL string, body, out any) error {
payload, err := json.Marshal(body)
if err != nil {
return fmt.Errorf("metadata: marshal body: %w", err)
@@ -62,26 +63,46 @@ func postJSON(ctx context.Context, hc *http.Client, rawURL string, body, out any
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
return doJSON(hc, req, out)
return doJSON(hc, log, req, out)
}
func doJSON(hc *http.Client, req *http.Request, out any) error {
// doJSON выполняет запрос и декодирует ответ, логируя исход. В лог идут только
// host и path (без query) — у TMDB api_key передаётся query-параметром, его
// нельзя светить в логах.
func doJSON(hc *http.Client, log *slog.Logger, req *http.Request, out any) error {
if log == nil {
log = slog.Default()
}
start := time.Now()
resp, err := hc.Do(req)
if err != nil {
log.Warn("metadata: request failed",
"method", req.Method, "host", req.URL.Host, "path", req.URL.Path,
"duration", time.Since(start), "err", err)
return fmt.Errorf("metadata: request: %w", err)
}
defer func() { _ = resp.Body.Close() }()
raw, err := io.ReadAll(io.LimitReader(resp.Body, maxBody))
if err != nil {
log.Warn("metadata: read body failed",
"host", req.URL.Host, "path", req.URL.Path, "err", err)
return fmt.Errorf("metadata: read body: %w", err)
}
if resp.StatusCode != http.StatusOK {
log.Warn("metadata: non-ok status",
"method", req.Method, "host", req.URL.Host, "path", req.URL.Path,
"status", resp.StatusCode, "duration", time.Since(start))
return fmt.Errorf("metadata: status %d: %s", resp.StatusCode, snippet(raw))
}
if err := json.Unmarshal(raw, out); err != nil {
log.Warn("metadata: decode failed",
"host", req.URL.Host, "path", req.URL.Path, "err", err)
return fmt.Errorf("metadata: decode: %w (body: %s)", err, snippet(raw))
}
log.Debug("metadata: request ok",
"method", req.Method, "host", req.URL.Host, "path", req.URL.Path,
"duration", time.Since(start))
return nil
}