-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add client grpc interceptor * add draft for slog logger * fix lint * fix lint * fix lint * fix lint * fix lint * fix error * fix register * change call string * Update metrics/grpc_mw_client.go Co-authored-by: Mikhail Barshev <[email protected]> * fix mrs --------- Co-authored-by: Mikhail Barshev <[email protected]>
- Loading branch information
1 parent
91cb24a
commit 46b9e11
Showing
14 changed files
with
422 additions
and
1,577 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
GOLANGCI_BIN=$(LOCAL_BIN)/golangci-lint | ||
$(GOLANGCI_BIN): | ||
GOBIN=$(LOCAL_BIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint | ||
GOBIN=$(LOCAL_BIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,46 @@ | ||
module github.com/glebnaz/witcher | ||
|
||
go 1.15 | ||
go 1.21 | ||
|
||
require ( | ||
github.com/antonfisher/nested-logrus-formatter v1.3.1 | ||
github.com/google/uuid v1.3.0 | ||
github.com/kelseyhightower/envconfig v1.4.0 | ||
github.com/klauspost/compress v1.16.5 // indirect | ||
github.com/labstack/echo/v4 v4.10.2 | ||
github.com/mattn/go-isatty v0.0.19 // indirect | ||
github.com/montanaflynn/stats v0.7.1 // indirect | ||
github.com/oklog/run v1.1.0 | ||
github.com/pkg/errors v0.9.1 | ||
github.com/prometheus/client_golang v1.15.1 | ||
github.com/sirupsen/logrus v1.9.2 | ||
go.mongodb.org/mongo-driver v1.11.6 | ||
google.golang.org/grpc v1.55.0 | ||
) | ||
|
||
require ( | ||
github.com/beorn7/perks v1.0.1 // indirect | ||
github.com/cespare/xxhash/v2 v2.2.0 // indirect | ||
github.com/fatih/color v1.15.0 // indirect | ||
github.com/golang/protobuf v1.5.3 // indirect | ||
github.com/golang/snappy v0.0.1 // indirect | ||
github.com/klauspost/compress v1.16.5 // indirect | ||
github.com/labstack/gommon v0.4.0 // indirect | ||
github.com/mattn/go-colorable v0.1.13 // indirect | ||
github.com/mattn/go-isatty v0.0.19 // indirect | ||
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect | ||
github.com/montanaflynn/stats v0.7.1 // indirect | ||
github.com/prometheus/client_model v0.4.0 // indirect | ||
github.com/prometheus/common v0.44.0 // indirect | ||
github.com/prometheus/procfs v0.10.0 // indirect | ||
github.com/sirupsen/logrus v1.9.2 | ||
github.com/valyala/bytebufferpool v1.0.0 // indirect | ||
github.com/valyala/fasttemplate v1.2.2 // indirect | ||
github.com/xdg-go/pbkdf2 v1.0.0 // indirect | ||
github.com/xdg-go/scram v1.1.2 // indirect | ||
github.com/xdg-go/stringprep v1.0.4 // indirect | ||
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect | ||
go.mongodb.org/mongo-driver v1.11.6 | ||
golang.org/x/crypto v0.9.0 // indirect | ||
golang.org/x/net v0.10.0 // indirect | ||
golang.org/x/sync v0.2.0 // indirect | ||
golang.org/x/sys v0.8.0 // indirect | ||
golang.org/x/text v0.9.0 // indirect | ||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230526015343-6ee61e4f9d5f // indirect | ||
google.golang.org/grpc v1.55.0 | ||
google.golang.org/protobuf v1.30.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package log | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
) | ||
|
||
type ctxKeySLOGAttr string | ||
|
||
var ctxKeySlOGAttr = ctxKeySLOGAttr("attr") | ||
|
||
// AddSLOGAttrToCTX adds slog attr to ctx | ||
func AddSLOGAttrToCTX(ctx context.Context, attrs []slog.Attr) context.Context { | ||
oldAttrs := GetSLOGAttrFromCTX(ctx) | ||
if oldAttrs != nil { | ||
attrs = append(oldAttrs, attrs...) | ||
} | ||
return context.WithValue(ctx, ctxKeySlOGAttr, attrs) | ||
} | ||
|
||
// GetSLOGAttrFromCTX returns slog attrs from ctx | ||
func GetSLOGAttrFromCTX(ctx context.Context) []slog.Attr { | ||
attrs, ok := ctx.Value(ctxKeySlOGAttr).([]slog.Attr) | ||
if !ok { | ||
attrs = nil | ||
} | ||
return attrs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package log | ||
|
||
import ( | ||
"context" | ||
|
||
"google.golang.org/grpc" | ||
) | ||
|
||
// ClientLoggerUnaryInterceptor returns a new unary client interceptor for | ||
// log from what to | ||
func ClientLoggerUnaryInterceptor(from, to string) grpc.UnaryClientInterceptor { | ||
return func( | ||
ctx context.Context, | ||
method string, | ||
req, reply interface{}, | ||
cc *grpc.ClientConn, | ||
invoker grpc.UnaryInvoker, | ||
opts ...grpc.CallOption) error { | ||
///////////////////////////////////////////////// | ||
|
||
Debugf(ctx, "GRPC UNARY REQUEST[%s] from %s to %s", method, from, to) | ||
|
||
return invoker(ctx, method, req, reply, cc, opts...) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package log | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
) | ||
|
||
type CTXAttrHandler struct { | ||
slog.Handler | ||
} | ||
|
||
// NewCTXAttrHandler | ||
// | ||
// is a slog.Handler that adds slog.Attr to slog.Record from context.Context | ||
func NewCTXAttrHandler(serviceName string, mainHandler slog.Handler) CTXAttrHandler { | ||
mainHandler.WithAttrs([]slog.Attr{ | ||
slog.String("service", serviceName), | ||
}, | ||
) | ||
|
||
return CTXAttrHandler{ | ||
Handler: mainHandler, | ||
} | ||
} | ||
|
||
func (p CTXAttrHandler) Handle(ctx context.Context, record slog.Record) error { | ||
attrs := GetSLOGAttrFromCTX(ctx) | ||
if attrs == nil { | ||
return p.Handler.Handle(ctx, record) | ||
} | ||
|
||
record.AddAttrs(attrs...) | ||
|
||
return p.Handler.Handle(ctx, record) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package log | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"log/slog" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestCTXAttrHandler(t *testing.T) { | ||
type kv struct { | ||
key string | ||
value string | ||
} | ||
|
||
type args struct { | ||
kv []kv | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
}{ | ||
{ | ||
name: "simple test with one attr in ctx", | ||
args: args{ | ||
kv: []kv{ | ||
{ | ||
key: "req-id", | ||
value: "123", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "simple test with two attr in ctx", | ||
args: args{ | ||
kv: []kv{ | ||
{ | ||
key: "req-id", | ||
value: "123", | ||
}, | ||
{ | ||
key: "name", | ||
value: "John Doe", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctx := context.Background() | ||
for _, kv := range tt.args.kv { | ||
ctx = AddSLOGAttrToCTX(ctx, []slog.Attr{slog.String(kv.key, kv.value)}) | ||
} | ||
|
||
buf := &bytes.Buffer{} | ||
defer buf.Truncate(len(buf.Bytes())) | ||
|
||
handler := NewCTXAttrHandler("test", slog.NewJSONHandler(buf, nil)) | ||
logger := slog.New(handler) | ||
logger.InfoContext(ctx, "test") | ||
str := buf.String() | ||
|
||
for _, kv := range tt.args.kv { | ||
if !strings.Contains(str, kv.key) { | ||
t.Errorf("JSONCTXHandlerBase() = %v, want %v", str, kv.key) | ||
} | ||
if !strings.Contains(str, kv.value) { | ||
t.Errorf("JSONCTXHandlerBase() = %v, want %v", str, kv.value) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.