infra: add logging wrapping
Resolves issues with gcp cloud logging quirks such as field names
This commit is contained in:
@@ -3,9 +3,10 @@ package pusher
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"sync"
|
||||
|
||||
"github.com/flowy-live/llink/internal/utils/flog"
|
||||
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
@@ -37,16 +38,16 @@ func (c *Conn) ReadPump(ctx context.Context, hub *Hub) {
|
||||
_, data, err := c.ws.Read(ctx)
|
||||
if err != nil {
|
||||
if ctx.Err() != nil {
|
||||
slog.Info("websocket context cancelled", "connId", c.id, "humanId", c.humanID, "error", ctx.Err())
|
||||
flog.Info("websocket context cancelled", "connId", c.id, "humanId", c.humanID, "error", ctx.Err())
|
||||
return
|
||||
}
|
||||
slog.Warn("websocket read error", "connId", c.id, "humanId", c.humanID, "error", err)
|
||||
flog.Warn("websocket read error", "connId", c.id, "humanId", c.humanID, "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
if string(data) == "ping" {
|
||||
if err := c.ws.Write(ctx, websocket.MessageText, []byte("pong")); err != nil {
|
||||
slog.Warn("websocket pong write error", "connId", c.id, "error", err)
|
||||
flog.Warn("websocket pong write error", "connId", c.id, "error", err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
@@ -92,7 +93,7 @@ func (c *Conn) WritePump(ctx context.Context) {
|
||||
return
|
||||
}
|
||||
if err := c.ws.Write(ctx, websocket.MessageText, data); err != nil {
|
||||
slog.Debug("websocket write error", "connId", c.id, "error", err)
|
||||
flog.Debug("websocket write error", "connId", c.id, "error", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -103,14 +104,14 @@ func (c *Conn) WritePump(ctx context.Context) {
|
||||
func (c *Conn) Send(msg ServerMessage) {
|
||||
data, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
slog.Error("failed to marshal server message", "error", err)
|
||||
flog.Error("failed to marshal server message", "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
select {
|
||||
case c.send <- data:
|
||||
default:
|
||||
slog.Warn("slow client, closing connection", "connId", c.id, "humanId", c.humanID)
|
||||
flog.Warn("slow client, closing connection", "connId", c.id, "humanId", c.humanID)
|
||||
c.Close()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,8 @@ package pusher
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
|
||||
"github.com/flowy-live/llink/internal/utils/flog"
|
||||
)
|
||||
|
||||
type subscribeRequest struct {
|
||||
@@ -110,7 +111,7 @@ func (h *Hub) handleSubscribe(ctx context.Context, req *subscribeRequest) {
|
||||
|
||||
presence, err := h.bridge.Subscribe(ctx, req.channelID, req.conn.id, req.conn.humanID)
|
||||
if err != nil {
|
||||
slog.Error("redis subscribe failed", "channelId", req.channelID, "error", err)
|
||||
flog.Error("redis subscribe failed", "channelId", req.channelID, "error", err)
|
||||
// Fall back to local-only presence.
|
||||
presence = ch.localHumanIDs()
|
||||
}
|
||||
@@ -144,7 +145,7 @@ func (h *Hub) handleUnsubscribe(ctx context.Context, req *unsubscribeRequest) {
|
||||
}
|
||||
|
||||
if err := h.bridge.Unsubscribe(ctx, req.channelID, req.conn.id, req.conn.humanID); err != nil {
|
||||
slog.Error("redis unsubscribe failed", "channelId", req.channelID, "error", err)
|
||||
flog.Error("redis unsubscribe failed", "channelId", req.channelID, "error", err)
|
||||
}
|
||||
|
||||
// Only emit leave once the humanID has no remaining tabs on this pod.
|
||||
@@ -198,7 +199,7 @@ func (h *Hub) handleDisconnect(ctx context.Context, conn *Conn) {
|
||||
ch.removeMember(conn)
|
||||
|
||||
if err := h.bridge.Unsubscribe(ctx, channelID, conn.id, conn.humanID); err != nil {
|
||||
slog.Error("redis unsubscribe on disconnect failed", "channelId", channelID, "error", err)
|
||||
flog.Error("redis unsubscribe on disconnect failed", "channelId", channelID, "error", err)
|
||||
}
|
||||
|
||||
if !ch.hasHumanID(conn.humanID) {
|
||||
|
||||
@@ -4,10 +4,11 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/flowy-live/llink/internal/utils/flog"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
@@ -200,7 +201,7 @@ func (rb *RedisBridge) handlePubSubMessage(msg *redis.Message) {
|
||||
|
||||
var event redisEvent
|
||||
if err := json.Unmarshal([]byte(msg.Payload), &event); err != nil {
|
||||
slog.Error("failed to parse pub/sub event", "error", err)
|
||||
flog.Error("failed to parse pub/sub event", "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -257,7 +258,7 @@ func (rb *RedisBridge) cleanupStalePods(ctx context.Context) {
|
||||
for {
|
||||
keys, nextCursor, err := rb.client.Scan(ctx, cursor, channelConnsPrefix+"*"+channelConnsSuffix, 100).Result()
|
||||
if err != nil {
|
||||
slog.Error("failed to scan channel keys", "error", err)
|
||||
flog.Error("failed to scan channel keys", "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -292,7 +293,7 @@ func (rb *RedisBridge) cleanupStalePods(ctx context.Context) {
|
||||
|
||||
for podID := range knownPods {
|
||||
if !alivePods[podID] {
|
||||
slog.Info("cleaning up stale pod", "podId", podID)
|
||||
flog.Info("cleaning up stale pod", "podId", podID)
|
||||
rb.cleanupPod(ctx, podID)
|
||||
}
|
||||
}
|
||||
@@ -344,11 +345,11 @@ func (rb *RedisBridge) connField(connID string) string {
|
||||
func (rb *RedisBridge) publishEvent(ctx context.Context, channelID string, event redisEvent) {
|
||||
data, err := json.Marshal(event)
|
||||
if err != nil {
|
||||
slog.Error("failed to marshal event", "error", err)
|
||||
flog.Error("failed to marshal event", "error", err)
|
||||
return
|
||||
}
|
||||
if err := rb.client.Publish(ctx, pubsubPrefix+channelID, data).Err(); err != nil {
|
||||
slog.Error("failed to publish event", "channelId", channelID, "error", err)
|
||||
flog.Error("failed to publish event", "channelId", channelID, "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,13 +2,14 @@ package pusher
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/flowy-live/llink/genproto/llink/pusher"
|
||||
"github.com/flowy-live/llink/internal/auth"
|
||||
"github.com/google/uuid"
|
||||
"nhooyr.io/websocket"
|
||||
|
||||
pbpusher "github.com/flowy-live/llink/genproto/llink/pusher"
|
||||
"github.com/flowy-live/llink/internal/auth"
|
||||
"github.com/flowy-live/llink/internal/utils/flog"
|
||||
)
|
||||
|
||||
// Server handles WebSocket upgrades and gRPC presence queries.
|
||||
@@ -49,14 +50,14 @@ func (s *Server) HandleWebSocket(w http.ResponseWriter, r *http.Request) {
|
||||
InsecureSkipVerify: true,
|
||||
})
|
||||
if err != nil {
|
||||
slog.Error("websocket accept failed", "error", err)
|
||||
flog.Error("websocket accept failed", "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
connID := uuid.New().String()
|
||||
conn := newConn(connID, session.HumanId, ws)
|
||||
|
||||
slog.Info("websocket connected", "connId", connID, "humanId", session.HumanId)
|
||||
flog.Info("websocket connected", "connId", connID, "humanId", session.HumanId)
|
||||
|
||||
// Use the server context, not r.Context(): after upgrade the HTTP request
|
||||
// context can be cancelled by load balancers and nhooyr/websocket would
|
||||
@@ -70,7 +71,7 @@ func (s *Server) HandleWebSocket(w http.ResponseWriter, r *http.Request) {
|
||||
go conn.WritePump(ctx)
|
||||
conn.ReadPump(ctx, s.hub)
|
||||
|
||||
slog.Info("websocket disconnected", "connId", connID, "humanId", session.HumanId)
|
||||
flog.Info("websocket disconnected", "connId", connID, "humanId", session.HumanId)
|
||||
}
|
||||
|
||||
func (s *Server) GetOnlineHumanIds(ctx context.Context, _ *pbpusher.GetOnlineHumanIdsRequest) (*pbpusher.GetOnlineHumanIdsResponse, error) {
|
||||
|
||||
Reference in New Issue
Block a user