Skip to content

Commit

Permalink
vet now passes and run from makefile (grpc-ecosystem#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
domgreen authored and yifanzz committed Nov 22, 2017
1 parent faf6812 commit d3bbb1b
Show file tree
Hide file tree
Showing 17 changed files with 112 additions and 441 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ install:
- dep ensure

script:
- ./test_all.sh
- make test

after_success:
- bash <(curl -s https://codecov.io/bash)
41 changes: 0 additions & 41 deletions auth/DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* [Overview](#pkg-overview)
* [Imported Packages](#pkg-imports)
* [Index](#pkg-index)
* [Examples](#pkg-examples)

## <a name="pkg-overview">Overview</a>
`grpc_auth` a generic server-side auth middleware for gRPC.
Expand All @@ -21,42 +20,6 @@ It also allows for per-service implementation overrides of `AuthFunc`. See `Serv

Please see examples for simple examples of use.

#### Example:

<details>
<summary>Click to expand code.</summary>

```go
token, err := grpc_auth.AuthFromMD(ctx, "bearer")
if err != nil {
return nil, err
}
tokenInfo, err := parseToken(token)
if err != nil {
return nil, grpc.Errorf(codes.Unauthenticated, "invalid auth token: %v", err)
}
grpc_ctxtags.Extract(ctx).Set("auth.sub", userClaimFromToken(tokenInfo))
newCtx := context.WithValue(ctx, "tokenInfo", tokenInfo)
return newCtx, nil
```

</details>

#### Example:

<details>
<summary>Click to expand code.</summary>

```go
server := grpc.NewServer(
grpc.StreamInterceptor(grpc_auth.StreamServerInterceptor(Example_authfunc)),
grpc.UnaryInterceptor(grpc_auth.UnaryServerInterceptor(Example_authfunc)),
)
return server
```

</details>

## <a name="pkg-imports">Imported Packages</a>

- [github.com/grpc-ecosystem/go-grpc-middleware](./..)
Expand All @@ -72,10 +35,6 @@ return server
* [type AuthFunc](#AuthFunc)
* [type ServiceAuthFuncOverride](#ServiceAuthFuncOverride)

#### <a name="pkg-examples">Examples</a>
* [Package (Authfunc)](#example__authfunc)
* [Package (Serverconfig)](#example__serverconfig)

#### <a name="pkg-files">Package files</a>
[auth.go](./auth.go) [doc.go](./doc.go) [metadata.go](./metadata.go)

Expand Down
44 changes: 20 additions & 24 deletions auth/examples_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Copyright 2016 Michal Witkowski. All Rights Reserved.
// See LICENSE for licensing terms.

package grpc_auth_test

import (
Expand All @@ -11,7 +8,9 @@ import (
"google.golang.org/grpc/codes"
)

var cc *grpc.ClientConn
var (
cc *grpc.ClientConn
)

func parseToken(token string) (struct{}, error) {
return struct{}{}, nil
Expand All @@ -21,27 +20,24 @@ func userClaimFromToken(struct{}) string {
return "foobar"
}

// Simple example of an `AuthFunc` that extracts, verifies the token and sets it in the handler
// contexts.
func Example_authfunc(ctx context.Context) (context.Context, error) {
token, err := grpc_auth.AuthFromMD(ctx, "bearer")
if err != nil {
return nil, err
}
tokenInfo, err := parseToken(token)
if err != nil {
return nil, grpc.Errorf(codes.Unauthenticated, "invalid auth token: %v", err)
// Simple example of server initialization code.
func Example_ServerConfig() {
exampleAuthFunc := func(ctx context.Context) (context.Context, error) {
token, err := grpc_auth.AuthFromMD(ctx, "bearer")
if err != nil {
return nil, err
}
tokenInfo, err := parseToken(token)
if err != nil {
return nil, grpc.Errorf(codes.Unauthenticated, "invalid auth token: %v", err)
}
grpc_ctxtags.Extract(ctx).Set("auth.sub", userClaimFromToken(tokenInfo))
newCtx := context.WithValue(ctx, "tokenInfo", tokenInfo)
return newCtx, nil
}
grpc_ctxtags.Extract(ctx).Set("auth.sub", userClaimFromToken(tokenInfo))
newCtx := context.WithValue(ctx, "tokenInfo", tokenInfo)
return newCtx, nil
}

// Simple example of server initialization code.
func Example_serverconfig() *grpc.Server {
server := grpc.NewServer(
grpc.StreamInterceptor(grpc_auth.StreamServerInterceptor(Example_authfunc)),
grpc.UnaryInterceptor(grpc_auth.UnaryServerInterceptor(Example_authfunc)),
_ = grpc.NewServer(
grpc.StreamInterceptor(grpc_auth.StreamServerInterceptor(exampleAuthFunc)),
grpc.UnaryInterceptor(grpc_auth.UnaryServerInterceptor(exampleAuthFunc)),
)
return server
}
21 changes: 0 additions & 21 deletions checkup.sh

This file was deleted.

85 changes: 0 additions & 85 deletions logging/logrus/DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* [Overview](#pkg-overview)
* [Imported Packages](#pkg-imports)
* [Index](#pkg-index)
* [Examples](#pkg-examples)

## <a name="pkg-overview">Overview</a>
`grpc_logrus` is a gRPC logging middleware backed by Logrus loggers
Expand All @@ -27,85 +26,6 @@ Logrus can also be made as a backend for gRPC library internals. For that use `R

Please see examples and tests for examples of use.

#### Example:

<details>
<summary>Click to expand code.</summary>

```go
x := func(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.PingResponse, error) {
// Add fields the ctxtags of the request which will be added to all extracted loggers.
grpc_ctxtags.Extract(ctx).Set("custom_tags.string", "something").Set("custom_tags.int", 1337)
// Extract a single request-scoped logrus.Logger and log messages.
l := grpc_logrus.Extract(ctx)
l.Info("some ping")
l.Info("another ping")
return &pb_testproto.PingResponse{Value: ping.Value}, nil
}
return x
```

</details>

#### Example:

<details>
<summary>Click to expand code.</summary>

```go
// Logrus entry is used, allowing pre-definition of certain fields by the user.
logrusEntry := logrus.NewEntry(logrusLogger)
// Shared options for the logger, with a custom gRPC code to log level function.
opts := []grpc_logrus.Option{
grpc_logrus.WithLevels(customFunc),
}
// Make sure that log statements internal to gRPC library are logged using the zapLogger as well.
grpc_logrus.ReplaceGrpcLogger(logrusEntry)
// Create a server, make sure we put the grpc_ctxtags context before everything else.
server := grpc.NewServer(
grpc_middleware.WithUnaryServerChain(
grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
grpc_logrus.UnaryServerInterceptor(logrusEntry, opts...),
),
grpc_middleware.WithStreamServerChain(
grpc_ctxtags.StreamServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
grpc_logrus.StreamServerInterceptor(logrusEntry, opts...),
),
)
return server
```

</details>

#### Example:

<details>
<summary>Click to expand code.</summary>

```go
// Logrus entry is used, allowing pre-definition of certain fields by the user.
logrusEntry := logrus.NewEntry(logrusLogger)
// Shared options for the logger, with a custom duration to log field function.
opts := []grpc_logrus.Option{
grpc_logrus.WithDurationField(func(duration time.Duration) (key string, value interface{}) {
return "grpc.time_ns", duration.Nanoseconds()
}),
}
server := grpc.NewServer(
grpc_middleware.WithUnaryServerChain(
grpc_ctxtags.UnaryServerInterceptor(),
grpc_logrus.UnaryServerInterceptor(logrusEntry, opts...),
),
grpc_middleware.WithStreamServerChain(
grpc_ctxtags.StreamServerInterceptor(),
grpc_logrus.StreamServerInterceptor(logrusEntry, opts...),
),
)
return server
```

</details>

## <a name="pkg-imports">Imported Packages</a>

- [github.com/golang/protobuf/jsonpb](https://godoc.org/github.com/golang/protobuf/jsonpb)
Expand Down Expand Up @@ -143,11 +63,6 @@ return server
* [func WithDurationField(f DurationToField) Option](#WithDurationField)
* [func WithLevels(f CodeToLevel) Option](#WithLevels)

#### <a name="pkg-examples">Examples</a>
* [Package (HandlerUsageUnaryPing)](#example__handlerUsageUnaryPing)
* [Package (Initialization)](#example__initialization)
* [Package (InitializationWithDurationFieldOverride)](#example__initializationWithDurationFieldOverride)

#### <a name="pkg-files">Package files</a>
[client_interceptors.go](./client_interceptors.go) [context.go](./context.go) [doc.go](./doc.go) [grpclogger.go](./grpclogger.go) [noop.go](./noop.go) [options.go](./options.go) [payload_interceptors.go](./payload_interceptors.go) [server_interceptors.go](./server_interceptors.go)

Expand Down
23 changes: 11 additions & 12 deletions logging/logrus/examples_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Copyright 2017 Michal Witkowski. All Rights Reserved.
// See LICENSE for licensing terms.

package grpc_logrus_test

import (
Expand All @@ -16,8 +13,13 @@ import (
"google.golang.org/grpc"
)

var (
logrusLogger *logrus.Logger
customFunc grpc_logrus.CodeToLevel
)

// Initialization shows a relatively complex initialization sequence.
func Example_initialization(logrusLogger *logrus.Logger, customFunc grpc_logrus.CodeToLevel) *grpc.Server {
func Example_Initialization() {
// Logrus entry is used, allowing pre-definition of certain fields by the user.
logrusEntry := logrus.NewEntry(logrusLogger)
// Shared options for the logger, with a custom gRPC code to log level function.
Expand All @@ -27,7 +29,7 @@ func Example_initialization(logrusLogger *logrus.Logger, customFunc grpc_logrus.
// Make sure that log statements internal to gRPC library are logged using the zapLogger as well.
grpc_logrus.ReplaceGrpcLogger(logrusEntry)
// Create a server, make sure we put the grpc_ctxtags context before everything else.
server := grpc.NewServer(
_ = grpc.NewServer(
grpc_middleware.WithUnaryServerChain(
grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
grpc_logrus.UnaryServerInterceptor(logrusEntry, opts...),
Expand All @@ -37,10 +39,9 @@ func Example_initialization(logrusLogger *logrus.Logger, customFunc grpc_logrus.
grpc_logrus.StreamServerInterceptor(logrusEntry, opts...),
),
)
return server
}

func Example_initializationWithDurationFieldOverride(logrusLogger *logrus.Logger) *grpc.Server {
func Example_InitializationWithDurationFieldOverride() {
// Logrus entry is used, allowing pre-definition of certain fields by the user.
logrusEntry := logrus.NewEntry(logrusLogger)
// Shared options for the logger, with a custom duration to log field function.
Expand All @@ -49,7 +50,7 @@ func Example_initializationWithDurationFieldOverride(logrusLogger *logrus.Logger
return "grpc.time_ns", duration.Nanoseconds()
}),
}
server := grpc.NewServer(
_ = grpc.NewServer(
grpc_middleware.WithUnaryServerChain(
grpc_ctxtags.UnaryServerInterceptor(),
grpc_logrus.UnaryServerInterceptor(logrusEntry, opts...),
Expand All @@ -59,12 +60,11 @@ func Example_initializationWithDurationFieldOverride(logrusLogger *logrus.Logger
grpc_logrus.StreamServerInterceptor(logrusEntry, opts...),
),
)
return server
}

// Simple unary handler that adds custom fields to the requests's context. These will be used for all log statements.
func Example_handlerUsageUnaryPing() interface{} {
x := func(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.PingResponse, error) {
func Example_HandlerUsageUnaryPing() {
_ = func(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.PingResponse, error) {
// Add fields the ctxtags of the request which will be added to all extracted loggers.
grpc_ctxtags.Extract(ctx).Set("custom_tags.string", "something").Set("custom_tags.int", 1337)
// Extract a single request-scoped logrus.Logger and log messages.
Expand All @@ -73,5 +73,4 @@ func Example_handlerUsageUnaryPing() interface{} {
l.Info("another ping")
return &pb_testproto.PingResponse{Value: ping.Value}, nil
}
return x
}
Loading

0 comments on commit d3bbb1b

Please sign in to comment.