Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ formatters:
settings:
gofumpt:
extra-rules: true
goimports:
local-prefixes:
- github.com/grpc-ecosystem/go-grpc-middleware

issues:
# Maximum issues count per one linter.
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fmt: $(GOIMPORTS)
@for file in $(GO_FILES_TO_FMT) ; do \
./goimports.sh "$${file}"; \
done
@$(GOIMPORTS) -w $(GO_FILES_TO_FMT)
@$(GOIMPORTS) -local github.com/grpc-ecosystem/go-grpc-middleware -w $(GO_FILES_TO_FMT)

.PHONY: test
test:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This repository offers ready-to-use middlewares that implements gRPC interceptor

Additional great feature of interceptors is the fact we can chain those. For example below you can find example server side chain of interceptors with full observabiliy correlation, auth and panic recovery:

```go mdox-exec="sed -n '143,163p' examples/server/main.go"
```go mdox-exec="sed -n '144,164p' examples/server/main.go"
grpcSrv := grpc.NewServer(
grpc.StatsHandler(otelgrpc.NewServerHandler()),
grpc.ChainUnaryInterceptor(
Expand Down
11 changes: 6 additions & 5 deletions examples/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ import (
"syscall"
"time"

grpcprom "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/timeout"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/metadata"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
"github.com/oklog/run"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand All @@ -28,6 +23,12 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
grpcMetadata "google.golang.org/grpc/metadata"

grpcprom "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/timeout"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/metadata"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
)

const (
Expand Down
17 changes: 9 additions & 8 deletions examples/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ import (
"runtime/debug"
"syscall"

grpcprom "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/auth"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/recovery"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/selector"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/metadata"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
"github.com/oklog/run"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
Expand All @@ -34,6 +26,15 @@ import (
"google.golang.org/grpc/codes"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/status"

grpcprom "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/auth"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/recovery"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/selector"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/metadata"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
)

const (
Expand Down
3 changes: 2 additions & 1 deletion interceptors/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ package auth
import (
"context"

middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2"
"google.golang.org/grpc"

middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2"
)

// AuthFunc is the pluggable function that performs authentication.
Expand Down
7 changes: 4 additions & 3 deletions interceptors/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import (
"testing"
"time"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/auth"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/metadata"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"golang.org/x/oauth2"
Expand All @@ -20,6 +17,10 @@ import (
"google.golang.org/grpc/credentials/oauth"
grpcMetadata "google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/auth"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/metadata"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
)

type authedMarker struct{}
Expand Down
7 changes: 4 additions & 3 deletions interceptors/auth/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"context"
"log"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/auth"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/auth"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
)

type tokenInfoKey struct{}
Expand Down
3 changes: 2 additions & 1 deletion interceptors/auth/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"context"
"testing"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/metadata"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
grpcMetadata "google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/metadata"
)

func TestAuthFromMD(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion interceptors/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import (
"testing"
"time"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
)

type mockReport struct {
Expand Down
3 changes: 2 additions & 1 deletion interceptors/logging/examples/kit/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"google.golang.org/grpc"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
)

// InterceptorLogger adapts go-kit logger to interceptor logger.
Expand Down
3 changes: 2 additions & 1 deletion interceptors/logging/examples/log/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
"log"
"os"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"google.golang.org/grpc"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
)

// InterceptorLogger adapts standard Go logger to interceptor logger.
Expand Down
3 changes: 2 additions & 1 deletion interceptors/logging/examples/logr/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
"fmt"

"github.com/go-logr/logr"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"google.golang.org/grpc"
"k8s.io/klog/v2"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
)

// verbosity https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/logging.md#what-method-to-use
Expand Down
3 changes: 2 additions & 1 deletion interceptors/logging/examples/logrus/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"context"
"fmt"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
)

// InterceptorLogger adapts logrus logger to interceptor logger.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"context"
"fmt"

"google.golang.org/grpc"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/selector"
"google.golang.org/grpc"
)

func InterceptorLogger() logging.Logger {
Expand All @@ -25,7 +26,6 @@ func SkipHealthAndReflectionRequests(_ context.Context, c interceptors.CallMeta)
}

func ExampleInterceptorLogger() {

opts := []logging.Option{
logging.WithLogOnEvents(logging.StartCall, logging.FinishCall),
}
Expand Down
3 changes: 2 additions & 1 deletion interceptors/logging/examples/slog/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"log/slog"
"os"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"google.golang.org/grpc"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
)

// InterceptorLogger adapts slog logger to interceptor logger.
Expand Down
3 changes: 2 additions & 1 deletion interceptors/logging/examples/zap/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"context"
"fmt"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"go.uber.org/zap"
"google.golang.org/grpc"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
)

// InterceptorLogger adapts zap logger to interceptor logger.
Expand Down
3 changes: 2 additions & 1 deletion interceptors/logging/examples/zerolog/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
"fmt"
"os"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/rs/zerolog"
"google.golang.org/grpc"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
)

// InterceptorLogger adapts zerolog logger to interceptor logger.
Expand Down
3 changes: 2 additions & 1 deletion interceptors/logging/interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (
"io"
"time"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors"
"google.golang.org/grpc"
"google.golang.org/grpc/peer"
"google.golang.org/protobuf/proto"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors"
)

type reporter struct {
Expand Down
7 changes: 4 additions & 3 deletions interceptors/logging/interceptors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ import (
"testing"
"time"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
)

type testDisposableFields map[string]string
Expand Down
3 changes: 2 additions & 1 deletion interceptors/logging/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
"fmt"
"time"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors"
)

// LoggableEvent defines the events a log line can be added on.
Expand Down
3 changes: 2 additions & 1 deletion interceptors/protovalidate/example_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"net"

"buf.build/go/protovalidate"
"google.golang.org/grpc"

protovalidate_middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/protovalidate"
testvalidatev1 "github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testvalidate/v1"
"google.golang.org/grpc"
)

type StreamService struct {
Expand Down
3 changes: 2 additions & 1 deletion interceptors/protovalidate/example_unary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
"net"

"buf.build/go/protovalidate"
"google.golang.org/grpc"

protovalidate_middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/protovalidate"
testvalidatev1 "github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testvalidate/v1"
"google.golang.org/grpc"
)

type UnaryService struct {
Expand Down
7 changes: 4 additions & 3 deletions interceptors/protovalidate/protovalidate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ import (

"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate"
"buf.build/go/protovalidate"
protovalidate_middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/protovalidate"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testvalidate"
testvalidatev1 "github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testvalidate/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
Expand All @@ -24,6 +21,10 @@ import (
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"

protovalidate_middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/protovalidate"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testvalidate"
testvalidatev1 "github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testvalidate/v1"
)

func TestUnaryServerInterceptor(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion interceptors/ratelimit/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ package ratelimit_test
import (
"context"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/ratelimit"
"google.golang.org/grpc"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/ratelimit"
)

// alwaysPassLimiter is an example limiter which implements Limiter interface.
Expand Down
3 changes: 2 additions & 1 deletion interceptors/realip/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ package realip_test
import (
"net/netip"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/realip"
"google.golang.org/grpc"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/realip"
)

// Simple example of a unary server initialization code.
Expand Down
3 changes: 2 additions & 1 deletion interceptors/recovery/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
package recovery_test

import (
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/recovery"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/recovery"
)

var customFunc recovery.RecoveryHandlerFunc
Expand Down
Loading
Loading