Documentation
¶
Overview ¶
Package kitmw provides a collection fo useful go kit middlewares or options that inter-ops with other libraries for package core.
See https://github.com/go-kit/kit for usage.
Index ¶
- Constants
- func Async(logger log.Logger, concurrency int) endpoint.Middleware
- func Error(opt ErrorOption) endpoint.Middleware
- func Interceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, ...) (resp interface{}, err error)
- func Log(logger log.Logger, keyer contract.Keyer, printTrace bool) endpoint.Middleware
- func Metrics(his metrics.Histogram, keyer contract.Keyer) endpoint.Middleware
- func Retry(opt RetryOption) endpoint.Middleware
- func Timeout(duration time.Duration) endpoint.Middleware
- func TraceClient(tracer stdtracing.Tracer, operationName string, ...) endpoint.Middleware
- func TraceConsumer(tracer stdtracing.Tracer, operationName string, ...) endpoint.Middleware
- func TraceProducer(tracer stdtracing.Tracer, operationName string, ...) endpoint.Middleware
- func TraceServer(tracer stdtracing.Tracer, operationName string, ...) endpoint.Middleware
- func Validate() endpoint.Middleware
- type ErrorOption
- type LabeledMiddleware
- type RetryOption
Examples ¶
Constants ¶
const ( // HTTPKind stands for HTTP transport HTTPKind string = "HTTP" // GRPCKind stands for GRPC transport GRPCKind string = "GRPC" )
Variables ¶
This section is empty.
Functions ¶
func Async ¶
func Async(logger log.Logger, concurrency int) endpoint.Middleware
Async returns a go kit middleware that calls the next handler in a detached goroutine. Timeout and cancellation of the previous context no logger apply to the detached goroutine, the tracing context however is carried over. A concurrency limit can be passed into the middleware. If the limit is reached, next endpoint call will block until the level of concurrency is below the limit.
func Error ¶
func Error(opt ErrorOption) endpoint.Middleware
Error returns a middleware that wraps the returned error from next handler with a *unierr.Error. if a successful response is returned from the next handler, this is a no op. If the error returned by next handler is already a *unierr.Error, this decorates the *unierr.Error based on ErrorOption.
Example ¶
package main
import (
"context"
"errors"
"fmt"
"github.com/DoNewsCode/core-kit/mw"
"github.com/go-kit/kit/endpoint"
)
func main() {
var (
err error
original endpoint.Endpoint
wrapped endpoint.Endpoint
)
original = func(ctx context.Context, request interface{}) (response interface{}, err error) {
return nil, errors.New("error")
}
_, err = original(context.Background(), nil)
fmt.Printf("%T\n", err)
wrapped = mw.Error(mw.ErrorOption{})(original)
_, err = wrapped(context.Background(), nil)
fmt.Printf("%T\n", err)
}
Output: *errors.errorString *unierr.Error
func Interceptor ¶
func Interceptor( ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, ) (resp interface{}, err error)
Interceptor is a grpc UnaryInterceptor that injects the method name into context so it can be consumed by Go kit gRPC middlewares. The Interceptor typically is added at creation time of the grpc-go server. Like this: `grpc.NewServer(grpc.UnaryInterceptor(mw.Interceptor))`
func Log ¶
Log returns a middleware the logs every request and response at debug level.
Example ¶
package main
import (
"context"
"github.com/DoNewsCode/core-kit/mw"
"github.com/DoNewsCode/core/key"
"github.com/DoNewsCode/core/logging"
"github.com/go-kit/kit/endpoint"
)
func main() {
var (
original endpoint.Endpoint
wrapped endpoint.Endpoint
)
original = func(ctx context.Context, request interface{}) (response interface{}, err error) {
return "respData", nil
}
wrapped = mw.Log(
logging.NewLogger("json"),
key.New(),
false,
)(original)
wrapped(context.Background(), "reqData")
}
Output: {"request":"reqData","response":"respData"}
func Retry ¶
func Retry(opt RetryOption) endpoint.Middleware
Retry returns a middleware that retries the failed requests.
Example ¶
package main
import (
"context"
"errors"
"fmt"
"time"
"github.com/DoNewsCode/core-kit/mw"
"github.com/go-kit/kit/endpoint"
)
func main() {
var (
original endpoint.Endpoint
wrapped endpoint.Endpoint
)
original = func(ctx context.Context, request interface{}) (response interface{}, err error) {
fmt.Println("attempt")
return nil, errors.New("")
}
wrapped = mw.Retry(mw.RetryOption{
Max: 5,
Timeout: time.Second,
})(original)
wrapped(context.Background(), nil)
}
Output: attempt attempt attempt attempt attempt
func Timeout ¶
func Timeout(duration time.Duration) endpoint.Middleware
Timeout returns a middleware that timeouts the request when the timer expired.
Example ¶
package main
import (
"context"
"errors"
"fmt"
"time"
"github.com/DoNewsCode/core-kit/mw"
"github.com/go-kit/kit/endpoint"
)
func main() {
var (
original endpoint.Endpoint
wrapped endpoint.Endpoint
)
original = func(ctx context.Context, request interface{}) (response interface{}, err error) {
select {
case <-ctx.Done():
return nil, errors.New("timeout")
case <-time.After(100000 * time.Microsecond):
return nil, nil
}
}
wrapped = mw.Timeout(time.Microsecond)(original)
_, err := wrapped(context.Background(), nil)
fmt.Println(err)
}
Output: timeout
func TraceClient ¶ added in v0.2.0
func TraceClient(tracer stdtracing.Tracer, operationName string, opts ...opentracing.EndpointOption) endpoint.Middleware
TraceClient returns a Middleware that wraps the `next` Endpoint in an OpenTracing Span called `operationName`.
If `ctx` already has a Span, it is re-used and the operation name is overwritten. If `ctx` does not yet have a Span, one is created here.
func TraceConsumer ¶
func TraceConsumer(tracer stdtracing.Tracer, operationName string, opts ...opentracing.EndpointOption) endpoint.Middleware
TraceConsumer returns a Middleware that wraps the `next` Endpoint in an OpenTracing Span called `operationName`.
If `ctx` already has a Span, it is re-used and the operation name is overwritten. If `ctx` does not yet have a Span, one is created here.
func TraceProducer ¶
func TraceProducer(tracer stdtracing.Tracer, operationName string, opts ...opentracing.EndpointOption) endpoint.Middleware
TraceProducer returns a Middleware that wraps the `next` Endpoint in an OpenTracing Span called `operationName`.
func TraceServer ¶
func TraceServer(tracer stdtracing.Tracer, operationName string, opts ...opentracing.EndpointOption) endpoint.Middleware
TraceServer returns a Middleware that wraps the `next` Endpoint in an OpenTracing Span called `operationName`.
If `ctx` already has a Span, it is re-used and the operation name is overwritten. If `ctx` does not yet have a Span, one is created here.
func Validate ¶
func Validate() endpoint.Middleware
Validate returns a middleware that validates the request by calling Validate(). The request must implement the following interface, otherwise the middleware is a no-op:
type validator interface {
Validate() error
}
Types ¶
type ErrorOption ¶
ErrorOption is an option that tunes the middleware returned by Error
type LabeledMiddleware ¶
LabeledMiddleware is a mutated endpoint.Middleware. It receives an additional method name from the caller.
func LabeledLog ¶
LabeledLog returns a labeled version of logging middleware.
func LabeledMetrics ¶
func LabeledMetrics(his metrics.Histogram, keyer contract.Keyer) LabeledMiddleware
LabeledMetrics returns a LabeledMiddleware that collects histogram metrics.
func LabeledTraceServer ¶
func LabeledTraceServer(tracer stdtracing.Tracer, keyer contract.Keyer) LabeledMiddleware
LabeledTraceServer returns a LabeledMiddleware that wraps the `next` Endpoint in an OpenTracing Span. The name of the operation is defined by contract.Keyer.
type RetryOption ¶
RetryOption is the parameter to config the retry middleware.