Skip to content

Commit

Permalink
examples: Simple logging interceptor and filter
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinav committed May 10, 2016
1 parent a4051bd commit 735c6a1
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 4 deletions.
37 changes: 37 additions & 0 deletions examples/json/client/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package main

import (
"fmt"

"github.com/yarpc/yarpc-go/transport"

"golang.org/x/net/context"
)

type requestLogFilter struct{}

func (requestLogFilter) Apply(
ctx context.Context, request *transport.Request, out transport.Outbound) (*transport.Response, error) {
fmt.Printf("sending a request to %q\n", request.Procedure)
return out.Call(ctx, request)
}
1 change: 1 addition & 0 deletions examples/json/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func main() {
rpc := yarpc.New(yarpc.Config{
Name: "keyvalue-client",
Outbounds: transport.Outbounds{"keyvalue": outbound},
Filters: yarpc.Filters{requestLogFilter{}},
})

client := json.New(rpc.Channel("keyvalue"))
Expand Down
10 changes: 10 additions & 0 deletions examples/json/json.t
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ Test code:
> set baz qux
> get baz
> INPUT
sending a request to "get"
received a request to "get"
foo =
sending a request to "set"
received a request to "set"
sending a request to "get"
received a request to "get"
foo = bar
sending a request to "set"
received a request to "set"
sending a request to "get"
received a request to "get"
baz = qux
37 changes: 37 additions & 0 deletions examples/json/server/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package main

import (
"fmt"

"github.com/yarpc/yarpc-go/transport"

"golang.org/x/net/context"
)

type requestLogInterceptor struct{}

func (requestLogInterceptor) Apply(
ctx context.Context, req *transport.Request, resw transport.ResponseWriter, handler transport.Handler) error {
fmt.Printf("received a request to %q\n", req.Procedure)
return handler.Handle(ctx, req, resw)
}
1 change: 1 addition & 0 deletions examples/json/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func main() {
tch.NewInbound(channel, tch.ListenAddr(":28941")),
http.NewInbound(":24034"),
},
Interceptors: yarpc.Interceptors{requestLogInterceptor{}},
})

handler := handler{items: make(map[string]string)}
Expand Down
16 changes: 12 additions & 4 deletions rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,20 @@ type RPC interface {
Stop() error
}

// Filters is a collection of transport-level Filters.
type Filters []transport.Filter

// Interceptors is a collection of transport-level Interceptors.
type Interceptors []transport.Interceptor

// Config specifies the parameters of a new RPC constructed via New.
type Config struct {
Name string
Inbounds []transport.Inbound
Outbounds transport.Outbounds

Filters []transport.Filter
Interceptors []transport.Interceptor
Filters Filters
Interceptors Interceptors

// TODO FallbackHandler for catch-all endpoints
}
Expand All @@ -69,13 +75,15 @@ func New(cfg Config) RPC {
panic("a service name is required")
}

filters := []transport.Filter(cfg.Filters)
interceptors := []transport.Interceptor(cfg.Interceptors)
return rpc{
Name: cfg.Name,
Registry: transport.NewMapRegistry(cfg.Name),
Inbounds: cfg.Inbounds,
Outbounds: cfg.Outbounds,
Filter: transport.ChainFilters(cfg.Filters...),
Interceptor: transport.ChainInterceptors(cfg.Interceptors...),
Filter: transport.ChainFilters(filters...),
Interceptor: transport.ChainInterceptors(interceptors...),
}
}

Expand Down

0 comments on commit 735c6a1

Please sign in to comment.