Skip to content

Commit

Permalink
Merge pull request #292 from strideynet/strideynet/slog
Browse files Browse the repository at this point in the history
Zap -> Slog
  • Loading branch information
strideynet authored Nov 22, 2024
2 parents 4392e67 + 30614ad commit 2e8d7bd
Show file tree
Hide file tree
Showing 26 changed files with 231 additions and 197 deletions.
19 changes: 10 additions & 9 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@ import (
"context"
"encoding/json"
"fmt"
"log/slog"
"net/http"

"connectrpc.com/connect"
"connectrpc.com/otelconnect"
"github.com/rs/cors"
"github.com/strideynet/bsky-furry-feed/bfflog"
"github.com/strideynet/bsky-furry-feed/feed"
"github.com/strideynet/bsky-furry-feed/proto/bff/v1/bffv1pbconnect"
"github.com/strideynet/bsky-furry-feed/store"
"go.uber.org/zap"
)

func handleErr(w http.ResponseWriter, log *zap.Logger, err error) {
log.Error("failed to handle request", zap.Error(err))
func handleErr(w http.ResponseWriter, log *slog.Logger, err error) {
log.Error("failed to handle request", bfflog.Err(err))
w.WriteHeader(500)
_, _ = w.Write([]byte(fmt.Sprintf("failed to handle request: %s", err)))
}

func jsonHandler(log *zap.Logger, h func(r *http.Request) (any, error)) http.HandlerFunc {
func jsonHandler(log *slog.Logger, h func(r *http.Request) (any, error)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
respBody, err := h(r)
if err != nil {
Expand All @@ -42,7 +43,7 @@ type feedService interface {

func New(
ctx context.Context,
log *zap.Logger,
log *slog.Logger,
hostname string,
listenAddr string,
feedService feedService,
Expand Down Expand Up @@ -116,20 +117,20 @@ func New(
}, nil
}

func unaryLoggingInterceptor(log *zap.Logger) connect.UnaryInterceptorFunc {
func unaryLoggingInterceptor(log *slog.Logger) connect.UnaryInterceptorFunc {
interceptor := func(next connect.UnaryFunc) connect.UnaryFunc {
return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
res, err := next(ctx, req)
if err != nil {
log.Error(
"gRPC request failed",
zap.String("procedure", req.Spec().Procedure),
zap.Error(err),
slog.String("procedure", req.Spec().Procedure),
bfflog.Err(err),
)
} else {
log.Info(
"gRPC request handled",
zap.String("procedure", req.Spec().Procedure),
slog.String("procedure", req.Spec().Procedure),
)
}
return res, err
Expand Down
3 changes: 2 additions & 1 deletion api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"net"
"net/http"
"testing"
Expand Down Expand Up @@ -52,7 +53,7 @@ func startAPIHarness(ctx context.Context, t *testing.T) *apiHarness {
_ = harness.PDS.MustNewUser(t, "bff.tpds")
srv, err := New(
context.Background(),
harness.Log,
slog.Default(),
"feed.test.furryli.st",
"",
&fakeFeedService{
Expand Down
9 changes: 5 additions & 4 deletions api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"strings"

"connectrpc.com/connect"
"github.com/bluesky-social/indigo/xrpc"
"github.com/strideynet/bsky-furry-feed/bfflog"
"github.com/strideynet/bsky-furry-feed/bluesky"
v1 "github.com/strideynet/bsky-furry-feed/proto/bff/v1"
"github.com/strideynet/bsky-furry-feed/store"
"go.uber.org/zap"
)

type actorGetter interface {
Expand Down Expand Up @@ -96,7 +97,7 @@ type AuthEngine struct {
// TokenValidator validates a given token and returns the DID associated
// with that token.
TokenValidator func(ctx context.Context, token string) (did string, err error)
Log *zap.Logger
Log *slog.Logger
}

type authContext struct {
Expand Down Expand Up @@ -157,8 +158,8 @@ func (a *AuthEngine) auth(ctx context.Context, req connect.AnyRequest) (*authCon
// Gracefully handle an unrecognized role
a.Log.Warn(
"unrecognized role",
zap.String("role", role),
zap.String("actor_did", did),
slog.String("role", role),
bfflog.ActorDID(did),
)
continue
}
Expand Down
13 changes: 7 additions & 6 deletions api/auth_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package api

import (
"connectrpc.com/connect"
"context"
"log/slog"
"reflect"
"testing"
"unsafe"

"connectrpc.com/connect"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
v1 "github.com/strideynet/bsky-furry-feed/proto/bff/v1"
"github.com/strideynet/bsky-furry-feed/store"
"go.uber.org/zap/zaptest"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/testing/protocmp"
"reflect"
"testing"
"unsafe"
)

type memoryActorGetter map[string]*v1.Actor
Expand Down Expand Up @@ -106,7 +107,7 @@ func TestAuthEngine(t *testing.T) {
TokenValidator: func(ctx context.Context, token string) (did string, err error) {
return token, nil
},
Log: zaptest.NewLogger(t),
Log: slog.Default(),
}

req := connect.NewRequest(&v1.PingRequest{})
Expand Down
7 changes: 4 additions & 3 deletions api/describe_feed_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package api

import (
"fmt"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.uber.org/zap"
"log/slog"
"net/http"

"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

type describeFeedGeneratorResponseFeed struct {
Expand All @@ -17,7 +18,7 @@ type describeFeedGeneratorResponse struct {
}

func describeFeedGeneratorHandler(
log *zap.Logger,
log *slog.Logger,
hostname string,
feedService feedService,
) (string, http.Handler) {
Expand Down
4 changes: 2 additions & 2 deletions api/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package api

import (
"fmt"
"go.uber.org/zap"
"log/slog"
"net/http"

"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
Expand All @@ -24,7 +24,7 @@ type WebDID struct {
Service []WebDIDService `json:"service"`
}

func didHandler(log *zap.Logger, hostname string) (string, http.Handler, error) {
func didHandler(log *slog.Logger, hostname string) (string, http.Handler, error) {
h := jsonHandler(log, func(r *http.Request) (any, error) {
return WebDID{
Context: []string{"https://www.w3.org/ns/did/v1"},
Expand Down
13 changes: 7 additions & 6 deletions api/get_feed_skeleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package api

import (
"fmt"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.uber.org/zap"
"log/slog"
"net/http"
"net/url"
"strconv"
"strings"

"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

type getFeedSkeletonParams struct {
Expand Down Expand Up @@ -63,7 +64,7 @@ type getFeedSkeletonResponse struct {
}

func getFeedSkeletonHandler(
log *zap.Logger, feedService feedService,
log *slog.Logger, feedService feedService,
) (string, http.Handler) {
h := jsonHandler(log, func(r *http.Request) (any, error) {
ctx := r.Context()
Expand All @@ -74,9 +75,9 @@ func getFeedSkeletonHandler(
}
log.Debug(
"get feed skeleton request",
zap.String("feed", params.feed),
zap.String("cursor", params.cursor),
zap.Int("limit", params.limit),
slog.String("feed", params.feed),
slog.String("cursor", params.cursor),
slog.Int("limit", params.limit),
)

posts, err := feedService.GetFeedPosts(ctx, params.feed, params.cursor, params.limit)
Expand Down
7 changes: 4 additions & 3 deletions api/moderation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ package api
import (
"context"
"fmt"
"log/slog"
"time"

"github.com/strideynet/bsky-furry-feed/bfflog"
"github.com/strideynet/bsky-furry-feed/bluesky"
"golang.org/x/exp/slices"
"google.golang.org/protobuf/types/known/timestamppb"

"connectrpc.com/connect"
v1 "github.com/strideynet/bsky-furry-feed/proto/bff/v1"
"github.com/strideynet/bsky-furry-feed/store"
"go.uber.org/zap"
)

type ModerationServiceHandler struct {
store *store.PGXStore
log *zap.Logger
log *slog.Logger
authEngine *AuthEngine
}

Expand Down Expand Up @@ -253,7 +254,7 @@ func (m *ModerationServiceHandler) Ping(ctx context.Context, req *connect.Reques
return nil, fmt.Errorf("authenticating: %w", err)
}
// Temporary log message - useful for debugging.
m.log.Info("received authenticated ping!", zap.String("did", authCtx.DID))
m.log.Info("received authenticated ping!", bfflog.ActorDID(authCtx.DID))

return connect.NewResponse(&v1.PingResponse{}), nil
}
Expand Down
9 changes: 5 additions & 4 deletions api/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ package api

import (
_ "embed"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.uber.org/zap"
"log/slog"
"net/http"

"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

//go:embed html/root.html
var rootPage []byte

func rootHandler(log *zap.Logger) (string, http.Handler) {
func rootHandler(log *slog.Logger) (string, http.Handler) {
var h http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
log.Info("request to non-existent path", zap.Any("path", r.URL.Path))
log.Info("request to non-existent path", slog.Any("path", r.URL.Path))
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte("Not Found"))
return
Expand Down
38 changes: 38 additions & 0 deletions bfflog/bfflog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package bfflog

import (
"fmt"
"log/slog"
)

func Err(e error) slog.Attr {
return slog.String("error", e.Error())
}

func Component(c string) slog.Attr {
return slog.String("component", c)
}

func ActorDID(did string) slog.Attr {
return slog.String("actor_did", did)
}

func ChildLogger(parent *slog.Logger, component string) *slog.Logger {
return parent.With(slog.String("component", component))
}

type PyroscopeSlogAdapter struct {
Slog *slog.Logger
}

func (p *PyroscopeSlogAdapter) Infof(a string, b ...interface{}) {
p.Slog.Info(fmt.Sprintf(a, b...))
}

func (p *PyroscopeSlogAdapter) Debugf(a string, b ...interface{}) {
p.Slog.Debug(fmt.Sprintf(a, b...))
}

func (p *PyroscopeSlogAdapter) Errorf(a string, b ...interface{}) {
p.Slog.Error(fmt.Sprintf(a, b...))
}
12 changes: 6 additions & 6 deletions cmd/bffctl/bsky.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package main

import (
"fmt"
"log/slog"
"os"
"time"

"github.com/bluesky-social/indigo/api/bsky"
"github.com/strideynet/bsky-furry-feed/bluesky"
"github.com/strideynet/bsky-furry-feed/feed"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
)

func bskyCmd(log *zap.Logger, env *environment) *cli.Command {
func bskyCmd(log *slog.Logger, env *environment) *cli.Command {
return &cli.Command{
Name: "bsky",
Subcommands: []*cli.Command{
Expand Down Expand Up @@ -54,7 +54,7 @@ func bskyCmd(log *zap.Logger, env *environment) *cli.Command {
for _, meta := range feeds.Metas() {
meta := meta

log.Info("upserting feed", zap.String("rkey", meta.ID))
log.Info("upserting feed", slog.String("rkey", meta.ID))
err = client.PutRecord(cctx.Context, "app.bsky.feed.generator", meta.ID, &bsky.FeedGenerator{
Avatar: blob,
Did: fmt.Sprintf("did:web:%s", hostname),
Expand All @@ -65,10 +65,10 @@ func bskyCmd(log *zap.Logger, env *environment) *cli.Command {
if err != nil {
return fmt.Errorf("putting feed record: %w", err)
}
log.Info("upserted feed", zap.String("rkey", meta.ID))
log.Info("upserted feed", slog.String("rkey", meta.ID))
}

log.Info("blob", zap.String("ref", blob.Ref.String()))
log.Info("blob", slog.String("ref", blob.Ref.String()))
return nil
},
},
Expand All @@ -83,7 +83,7 @@ func bskyCmd(log *zap.Logger, env *environment) *cli.Command {
if err != nil {
return err
}
log.Info("found did", zap.String("did", did.Did))
log.Info("found did", slog.String("did", did.Did))
return nil
},
},
Expand Down
Loading

0 comments on commit 2e8d7bd

Please sign in to comment.