Skip to content

Commit

Permalink
Migrate to slog and go-1.22 (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 authored Mar 4, 2024
1 parent 7ad9fe7 commit 88d2feb
Show file tree
Hide file tree
Showing 14 changed files with 442 additions and 491 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/master.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Set up Go 1.21
uses: actions/setup-go@v3
- name: Set up Go 1.22
uses: actions/setup-go@v5
with:
go-version: "1.21.x"
go-version: "1.22.x"

- name: Lint
uses: golangci/golangci-lint-action@v3
Expand All @@ -33,12 +33,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Set up Go 1.21
uses: actions/setup-go@v3
- name: Set up Go 1.22
uses: actions/setup-go@v5
with:
go-version: "1.21.x"
go-version: "1.22.x"

- name: Run integration tests
run: make test-integration
57 changes: 29 additions & 28 deletions auditing/auditing-interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"

"connectrpc.com/connect"
"github.com/emicklei/go-restful/v3"
"github.com/google/uuid"
"github.com/metal-stack/metal-lib/rest"
"github.com/metal-stack/security"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand All @@ -25,9 +26,9 @@ const (
Exclude string = "exclude-from-auditing"
)

func UnaryServerInterceptor(a Auditing, logger *zap.SugaredLogger, shouldAudit func(fullMethod string) bool) grpc.UnaryServerInterceptor {
func UnaryServerInterceptor(a Auditing, logger *slog.Logger, shouldAudit func(fullMethod string) bool) (grpc.UnaryServerInterceptor, error) {
if a == nil {
logger.Fatal("cannot use nil auditing to create unary server interceptor")
return nil, fmt.Errorf("cannot use nil auditing to create unary server interceptor")
}
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
if !shouldAudit(info.FullMethod) {
Expand Down Expand Up @@ -73,19 +74,19 @@ func UnaryServerInterceptor(a Auditing, logger *zap.SugaredLogger, shouldAudit f
auditReqContext.Error = err
err2 := a.Index(auditReqContext)
if err2 != nil {
logger.Errorf("unable to index error: %v", err2)
logger.Error("unable to index", "error", err2)
}
return nil, err
}

err = a.Index(auditReqContext)
return resp, err
}
}, nil
}

func StreamServerInterceptor(a Auditing, logger *zap.SugaredLogger, shouldAudit func(fullMethod string) bool) grpc.StreamServerInterceptor {
func StreamServerInterceptor(a Auditing, logger *slog.Logger, shouldAudit func(fullMethod string) bool) (grpc.StreamServerInterceptor, error) {
if a == nil {
logger.Fatal("cannot use nil auditing to create stream server interceptor")
return nil, fmt.Errorf("cannot use nil auditing to create stream server interceptor")
}
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
if !shouldAudit(info.FullMethod) {
Expand Down Expand Up @@ -131,7 +132,7 @@ func StreamServerInterceptor(a Auditing, logger *zap.SugaredLogger, shouldAudit
auditReqContext.Error = err
err2 := a.Index(auditReqContext)
if err2 != nil {
logger.Errorf("unable to index error: %v", err2)
logger.Error("unable to index", "error", err2)
}
return err
}
Expand All @@ -140,12 +141,12 @@ func StreamServerInterceptor(a Auditing, logger *zap.SugaredLogger, shouldAudit
err = a.Index(auditReqContext)

return err
}
}, nil
}

type auditingConnectInterceptor struct {
auditing Auditing
logger *zap.SugaredLogger
logger *slog.Logger
shouldAudit func(fullMethod string) bool
}

Expand Down Expand Up @@ -180,7 +181,7 @@ func (a auditingConnectInterceptor) WrapStreamingClient(next connect.StreamingCl

err := a.auditing.Index(auditReqContext)
if err != nil {
a.logger.Errorf("unable to index error: %v", err)
a.logger.Error("unable to index", "error", err)
}

auditReqContext.prepareForNextPhase()
Expand All @@ -191,7 +192,7 @@ func (a auditingConnectInterceptor) WrapStreamingClient(next connect.StreamingCl

err = a.auditing.Index(auditReqContext)
if err != nil {
a.logger.Errorf("unable to index error: %v", err)
a.logger.Error("unable to index", "error", err)
}

return scc
Expand Down Expand Up @@ -235,7 +236,7 @@ func (a auditingConnectInterceptor) WrapStreamingHandler(next connect.StreamingH

err := a.auditing.Index(auditReqContext)
if err != nil {
a.logger.Errorf("unable to index error: %v", err)
a.logger.Error("unable to index", "error", err)
}

auditReqContext.prepareForNextPhase()
Expand All @@ -246,15 +247,15 @@ func (a auditingConnectInterceptor) WrapStreamingHandler(next connect.StreamingH
auditReqContext.Error = err
err2 := a.auditing.Index(auditReqContext)
if err2 != nil {
a.logger.Errorf("unable to index error: %v", err2)
a.logger.Error("unable to index", "error", err2)
}
return err
}

auditReqContext.Phase = EntryPhaseClosed
err = a.auditing.Index(auditReqContext)
if err != nil {
a.logger.Errorf("unable to index error: %v", err)
a.logger.Error("unable to index", "error", err)
}

return err
Expand Down Expand Up @@ -313,7 +314,7 @@ func (i auditingConnectInterceptor) WrapUnary(next connect.UnaryFunc) connect.Un
auditReqContext.Error = err
err2 := i.auditing.Index(auditReqContext)
if err2 != nil {
i.logger.Errorf("unable to index error: %v", err2)
i.logger.Error("unable to index", "error", err2)
}
return nil, err
}
Expand All @@ -323,20 +324,20 @@ func (i auditingConnectInterceptor) WrapUnary(next connect.UnaryFunc) connect.Un
}
}

func NewConnectInterceptor(a Auditing, logger *zap.SugaredLogger, shouldAudit func(fullMethod string) bool) connect.Interceptor {
func NewConnectInterceptor(a Auditing, logger *slog.Logger, shouldAudit func(fullMethod string) bool) (connect.Interceptor, error) {
if a == nil {
logger.Fatal("cannot use nil auditing to create connect interceptor")
return nil, fmt.Errorf("cannot use nil auditing to create connect interceptor")
}
return auditingConnectInterceptor{
auditing: a,
logger: logger,
shouldAudit: shouldAudit,
}
}, nil
}

func HttpFilter(a Auditing, logger *zap.SugaredLogger) restful.FilterFunction {
func HttpFilter(a Auditing, logger *slog.Logger) (restful.FilterFunction, error) {
if a == nil {
logger.Fatal("cannot use nil auditing to create http middleware")
return nil, fmt.Errorf("cannot use nil auditing to create http middleware")
}
return func(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
r := request.Request
Expand All @@ -346,7 +347,7 @@ func HttpFilter(a Auditing, logger *zap.SugaredLogger) restful.FilterFunction {
break
default:
if request.SelectedRoute() == nil {
logger.Debugw("selected route is not defined, continue request processing")
logger.Debug("selected route is not defined, continue request processing")
chain.ProcessFilter(request, response)
return
}
Expand All @@ -360,14 +361,14 @@ func HttpFilter(a Auditing, logger *zap.SugaredLogger) restful.FilterFunction {
}

if request.SelectedRoute() == nil {
logger.Debugw("selected route is not defined, continue request processing")
logger.Debug("selected route is not defined, continue request processing")
chain.ProcessFilter(request, response)
return
}

excluded, ok := request.SelectedRoute().Metadata()[Exclude].(bool)
if ok && excluded {
logger.Debugw("excluded route from auditing through metadata annotation", "path", request.SelectedRoute().Path())
logger.Debug("excluded route from auditing through metadata annotation", "path", request.SelectedRoute().Path())
chain.ProcessFilter(request, response)
return
}
Expand Down Expand Up @@ -399,7 +400,7 @@ func HttpFilter(a Auditing, logger *zap.SugaredLogger) restful.FilterFunction {
body, err := io.ReadAll(bodyReader)
r.Body = io.NopCloser(bytes.NewReader(body))
if err != nil {
logger.Errorf("unable to read request body: %v", err)
logger.Error("unable to read request body", "error", err)
response.WriteHeader(http.StatusInternalServerError)
return
}
Expand All @@ -411,7 +412,7 @@ func HttpFilter(a Auditing, logger *zap.SugaredLogger) restful.FilterFunction {

err := a.Index(auditReqContext)
if err != nil {
logger.Errorf("unable to index error: %v", err)
logger.Error("unable to index", "error", err)
response.WriteHeader(http.StatusInternalServerError)
return
}
Expand All @@ -436,11 +437,11 @@ func HttpFilter(a Auditing, logger *zap.SugaredLogger) restful.FilterFunction {

err = a.Index(auditReqContext)
if err != nil {
logger.Errorf("unable to index error: %v", err)
logger.Error("unable to index", "error", err)
response.WriteHeader(http.StatusInternalServerError)
return
}
}
}, nil
}

type bufferedHttpResponseWriter struct {
Expand Down
5 changes: 2 additions & 3 deletions auditing/auditing.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package auditing

import (
"log/slog"
"time"

"go.uber.org/zap"
)

type Config struct {
Expand All @@ -13,7 +12,7 @@ type Config struct {
IndexPrefix string
RotationInterval Interval
Keep int64
Log *zap.SugaredLogger
Log *slog.Logger
}

type Interval string
Expand Down
30 changes: 15 additions & 15 deletions auditing/meilisearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"regexp"
Expand All @@ -14,13 +15,12 @@ import (

"github.com/google/uuid"
"github.com/meilisearch/meilisearch-go"
"go.uber.org/zap"
)

type meiliAuditing struct {
component string
client *meilisearch.Client
log *zap.SugaredLogger
log *slog.Logger
indexPrefix string
rotationInterval Interval
keep int64
Expand Down Expand Up @@ -56,12 +56,12 @@ func New(c Config) (Auditing, error) {
if err != nil {
return nil, fmt.Errorf("unable to connect to meilisearch at:%s %w", c.URL, err)
}
c.Log.Infow("meilisearch", "connected to", v, "index rotated", c.RotationInterval, "index keep", c.Keep)
c.Log.Info("meilisearch", "connected to", v, "index rotated", c.RotationInterval, "index keep", c.Keep)

a := &meiliAuditing{
component: c.Component,
client: client,
log: c.Log.Named("auditing"),
log: c.Log.WithGroup("auditing"),
indexPrefix: c.IndexPrefix,
rotationInterval: c.RotationInterval,
keep: c.Keep,
Expand All @@ -78,7 +78,7 @@ func (a *meiliAuditing) Flush() error {
if err != nil {
return err
}
a.log.Debugw("flush, waiting for", "tasks", len(taskResult.Results))
a.log.Debug("flush, waiting for", "tasks", len(taskResult.Results))

var errs []error
for _, task := range taskResult.Results {
Expand Down Expand Up @@ -114,10 +114,10 @@ func (a *meiliAuditing) Index(entry Entry) error {

task, err := index.AddDocuments(documents, "id")
if err != nil {
a.log.Errorw("index", "error", err)
a.log.Error("index", "error", err)
return err
}
a.log.Debugw("index", "task", task.TaskUID, "index", index.UID)
a.log.Debug("index", "task", task.TaskUID, "index", index.UID)
return nil
}

Expand Down Expand Up @@ -356,7 +356,7 @@ func (a *meiliAuditing) getLatestIndex() (*meilisearch.Index, error) {
return a.index, nil
}

a.log.Debugw("auditing", "create new index", a.rotationInterval, "index", indexUid)
a.log.Debug("auditing", "create new index", a.rotationInterval, "index", indexUid)
creationTask, err := a.client.CreateIndex(&meilisearch.IndexConfig{
Uid: indexUid,
PrimaryKey: "id",
Expand Down Expand Up @@ -384,7 +384,7 @@ func (a *meiliAuditing) getLatestIndex() (*meilisearch.Index, error) {
go func() {
err = a.cleanUpIndexes()
if err != nil {
a.log.Errorw("auditing", "failed to clean up indexes", err)
a.log.Error("auditing", "failed to clean up indexes", err)
}
}()
return a.index, nil
Expand Down Expand Up @@ -479,11 +479,11 @@ func (a *meiliAuditing) cleanUpIndexes() error {

indexListResponse, err := a.getAllIndexes()
if err != nil {
a.log.Errorw("unable to list indexes", "err", err)
a.log.Error("unable to list indexes", "err", err)
return err
}

a.log.Debugw("indexes listed", "count", indexListResponse.Total, "keep", a.keep)
a.log.Debug("indexes listed", "count", indexListResponse.Total, "keep", a.keep)

// Sort the indexes descending by creation date
slices.SortStableFunc(indexListResponse.Results, func(a, b meilisearch.Index) int {
Expand All @@ -501,7 +501,7 @@ func (a *meiliAuditing) cleanUpIndexes() error {
seen := 0
var errs []error
for _, index := range indexListResponse.Results {
a.log.Debugw("inspect index for deletion", "uid", index.UID)
a.log.Debug("inspect index for deletion", "uid", index.UID)
if !strings.HasPrefix(index.UID, a.indexPrefix) {
continue
}
Expand All @@ -511,14 +511,14 @@ func (a *meiliAuditing) cleanUpIndexes() error {
}
deleteInfo, err := a.client.DeleteIndex(index.UID)
if err != nil {
a.log.Errorw("unable to delete index", "uid", index.UID, "created", index.CreatedAt)
a.log.Error("unable to delete index", "uid", index.UID, "created", index.CreatedAt)
errs = append(errs, err)
continue
}
deleted++
a.log.Debugw("deleted index", "uid", index.UID, "created", index.CreatedAt, "info", deleteInfo)
a.log.Debug("deleted index", "uid", index.UID, "created", index.CreatedAt, "info", deleteInfo)
}
a.log.Infow("cleanup finished", "deletes", deleted, "keep", a.keep, "errs", len(errs))
a.log.Info("cleanup finished", "deletes", deleted, "keep", a.keep, "errs", len(errs))
if len(errs) > 0 {
return errors.Join(errs...)
}
Expand Down
Loading

0 comments on commit 88d2feb

Please sign in to comment.