mirror of
https://github.com/multica-ai/multica.git
synced 2026-06-17 03:38:32 +02:00
* feat: identify clients via X-Client-Platform/Version/OS
Adds client identification headers (and matching WS query params) across
all first-party clients so the server can split logs/metrics/gating by
caller without parsing User-Agent.
- HTTP: X-Client-Platform, X-Client-Version, X-Client-OS
- WS: client_platform, client_version, client_os query params
- Platform ∈ {web, desktop, cli, daemon}; OS ∈ {macos, windows, linux}
Wired through the shared TS ApiClient/WSClient via a new identity option
on CoreProvider. Web reads its version from package.json/env; Desktop
captures version + OS synchronously in preload via sendSync IPC. Go CLI
and daemon clients populate the same headers using runtime.GOOS
(normalized darwin → macos).
Server-side adds a ClientMetadata middleware that stashes the headers in
request context; the request logger and logger.RequestAttrs surface them
on every access log and handler-level log. Realtime hub logs the same
fields on websocket connect.
CORS allowlist extended for the new headers.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* test: address client-identity PR nits
- Memoize the CoreProvider identity object on Web and Desktop, and key
WSProvider's effect on identity primitives instead of the object
reference, so unrelated parent re-renders no longer tear down and
reconnect the WebSocket.
- Add direct header-injection tests for the CLI and daemon Go HTTP
clients (X-Client-Platform/Version/OS) and a normalizeGOOS unit test
on both packages.
- Add a TS test for WSClient that asserts client_platform/client_version/
client_os land on the upgrade URL and never leak the auth token.
- Add a hub test that dials the WS endpoint with client_* query params
and asserts the "websocket connected" log entry surfaces them as
structured attributes.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
89 lines
2.5 KiB
Go
89 lines
2.5 KiB
Go
package logger
|
|
|
|
import (
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
|
|
chimw "github.com/go-chi/chi/v5/middleware"
|
|
"github.com/lmittmann/tint"
|
|
|
|
"github.com/multica-ai/multica/server/internal/middleware"
|
|
)
|
|
|
|
// isTerminal reports whether the given file descriptor is connected to a
|
|
// terminal. Used to suppress ANSI color escapes when stderr is redirected
|
|
// to a file (e.g. daemon.log), so log files stay clean.
|
|
func isTerminal(f *os.File) bool {
|
|
fi, err := f.Stat()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return fi.Mode()&os.ModeCharDevice != 0
|
|
}
|
|
|
|
// Init initializes the global slog logger. Colors are enabled when stderr
|
|
// is a terminal and disabled otherwise. Reads LOG_LEVEL env var (debug,
|
|
// info, warn, error). Default: debug.
|
|
func Init() {
|
|
level := parseLevel(os.Getenv("LOG_LEVEL"))
|
|
handler := tint.NewHandler(os.Stderr, &tint.Options{
|
|
Level: level,
|
|
TimeFormat: "15:04:05.000",
|
|
NoColor: !isTerminal(os.Stderr),
|
|
})
|
|
slog.SetDefault(slog.New(handler))
|
|
}
|
|
|
|
// NewLogger creates a named slog logger. Colors follow the same
|
|
// TTY-detection rule as Init. Useful for standalone processes (daemon,
|
|
// migrate) that want a component prefix.
|
|
func NewLogger(component string) *slog.Logger {
|
|
level := parseLevel(os.Getenv("LOG_LEVEL"))
|
|
handler := tint.NewHandler(os.Stderr, &tint.Options{
|
|
Level: level,
|
|
TimeFormat: "15:04:05.000",
|
|
NoColor: !isTerminal(os.Stderr),
|
|
})
|
|
return slog.New(handler).With("component", component)
|
|
}
|
|
|
|
// RequestAttrs extracts request_id, user_id, and X-Client-* metadata from
|
|
// an HTTP request for use in handler-level structured logging. Mirrors the
|
|
// global request logger so handler logs end up with the same observability
|
|
// dimensions as the access log.
|
|
func RequestAttrs(r *http.Request) []any {
|
|
attrs := make([]any, 0, 10)
|
|
if rid := chimw.GetReqID(r.Context()); rid != "" {
|
|
attrs = append(attrs, "request_id", rid)
|
|
}
|
|
if uid := r.Header.Get("X-User-ID"); uid != "" {
|
|
attrs = append(attrs, "user_id", uid)
|
|
}
|
|
platform, version, os := middleware.ClientMetadataFromContext(r.Context())
|
|
if platform != "" {
|
|
attrs = append(attrs, "client_platform", platform)
|
|
}
|
|
if version != "" {
|
|
attrs = append(attrs, "client_version", version)
|
|
}
|
|
if os != "" {
|
|
attrs = append(attrs, "client_os", os)
|
|
}
|
|
return attrs
|
|
}
|
|
|
|
func parseLevel(s string) slog.Level {
|
|
switch strings.ToLower(strings.TrimSpace(s)) {
|
|
case "info":
|
|
return slog.LevelInfo
|
|
case "warn", "warning":
|
|
return slog.LevelWarn
|
|
case "error":
|
|
return slog.LevelError
|
|
default:
|
|
return slog.LevelDebug
|
|
}
|
|
}
|