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 ¶
- func ErrorEncoder(_ context.Context, err error, w http.ResponseWriter)
- func IpToGRPCContext() grpctransport.ServerRequestFunc
- func IpToHTTPContext() httptransport.RequestFunc
- func MakeAsyncMiddleware(logger log.Logger, concurrency int) endpoint.Middleware
- func MakeErrorConversionMiddleware(opt ErrorOption) endpoint.Middleware
- func MakeLoggingMiddleware(logger log.Logger, keyer contract.Keyer, printTrace bool) endpoint.Middleware
- func MakeMetricsMiddleware(his metrics.Histogram, keyer contract.Keyer) endpoint.Middleware
- func MakeRetryMiddleware(max int, timeout time.Duration) endpoint.Middleware
- func MakeTimeoutMiddleware(duration time.Duration) endpoint.Middleware
- func MakeTraceServerMiddleware(tracer stdtracing.Tracer, keyer contract.Keyer) endpoint.Middleware
- func MakeValidationMiddleware() endpoint.Middleware
- func TraceConsumer(tracer stdtracing.Tracer, operationName string, kind ext.SpanKindEnum) endpoint.Middleware
- func TraceProducer(tracer stdtracing.Tracer, operationName string, kind ext.SpanKindEnum) endpoint.Middleware
- type ErrorOption
- type LabeledMiddleware
- func MakeLabeledLoggingMiddleware(logger log.Logger, keyer contract.Keyer, printTrace bool) LabeledMiddleware
- func MakeLabeledMetricsMiddleware(his metrics.Histogram, keyer contract.Keyer) LabeledMiddleware
- func MakeLabeledTraceServerMiddleware(tracer stdtracing.Tracer, keyer contract.Keyer) LabeledMiddleware
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ErrorEncoder ¶
func ErrorEncoder(_ context.Context, err error, w http.ResponseWriter)
ErrorEncoder is a go kit style http error encoder. Internally it uses srvhttp.ResponseEncoder to encode the error.
func IpToGRPCContext ¶
func IpToGRPCContext() grpctransport.ServerRequestFunc
IpToGRPCContext is a grpctransport.ServerRequestFunc that put client ip to context, under contract.IpKey.
func IpToHTTPContext ¶
func IpToHTTPContext() httptransport.RequestFunc
IpToHTTPContext is a httptransport.RequestFun that put client ip to context, under contract.IpKey.
func MakeAsyncMiddleware ¶
func MakeAsyncMiddleware(logger log.Logger, concurrency int) endpoint.Middleware
MakeAsyncMiddleware 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 MakeErrorConversionMiddleware ¶
func MakeErrorConversionMiddleware(opt ErrorOption) endpoint.Middleware
MakeErrorConversionMiddleware 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/kitmw"
"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 = kitmw.MakeErrorConversionMiddleware(kitmw.ErrorOption{})(original)
_, err = wrapped(context.Background(), nil)
fmt.Printf("%T\n", err)
}
Output: *errors.errorString *unierr.Error
func MakeLoggingMiddleware ¶
func MakeLoggingMiddleware(logger log.Logger, keyer contract.Keyer, printTrace bool) endpoint.Middleware
MakeLoggingMiddleware returns a middleware the logs every request and response at debug level.
Example ¶
package main
import (
"context"
"github.com/DoNewsCode/core/key"
"github.com/DoNewsCode/core/kitmw"
"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 = kitmw.MakeLoggingMiddleware(
logging.NewLogger("json"),
key.New(),
false,
)(original)
wrapped(context.Background(), "reqData")
}
Output: {"request":"reqData","response":"respData"}
func MakeMetricsMiddleware ¶
MakeMetricsMiddleware returns a middleware that collects histogram metrics.
func MakeRetryMiddleware ¶
func MakeRetryMiddleware(max int, timeout time.Duration) endpoint.Middleware
MakeRetryMiddleware returns a middleware that retries the failed requests.
Example ¶
package main
import (
"context"
"errors"
"fmt"
"time"
"github.com/DoNewsCode/core/kitmw"
"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 = kitmw.MakeRetryMiddleware(5, time.Hour)(original)
wrapped(context.Background(), nil)
}
Output: attempt attempt attempt attempt attempt
func MakeTimeoutMiddleware ¶
func MakeTimeoutMiddleware(duration time.Duration) endpoint.Middleware
MakeTimeoutMiddleware returns a middleware that timeouts the request when the timer expired.
Example ¶
package main
import (
"context"
"errors"
"fmt"
"time"
"github.com/DoNewsCode/core/kitmw"
"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 = kitmw.MakeTimeoutMiddleware(time.Microsecond)(original)
_, err := wrapped(context.Background(), nil)
fmt.Println(err)
}
Output: timeout
func MakeTraceServerMiddleware ¶
func MakeTraceServerMiddleware(tracer stdtracing.Tracer, keyer contract.Keyer) endpoint.Middleware
MakeTraceServerMiddleware returns a Middleware that wraps the `next` Endpoint in an OpenTracing Span. The name of the operation is defined by contract.Keyer.
func MakeValidationMiddleware ¶
func MakeValidationMiddleware() endpoint.Middleware
MakeValidationMiddleware 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
}
func TraceConsumer ¶
func TraceConsumer(tracer stdtracing.Tracer, operationName string, kind ext.SpanKindEnum) 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, kind ext.SpanKindEnum) endpoint.Middleware
TraceProducer returns a Middleware that wraps the `next` Endpoint in an OpenTracing Span called `operationName`.
Types ¶
type ErrorOption ¶
ErrorOption is an option that tunes the middleware returned by MakeErrorConversionMiddleware
type LabeledMiddleware ¶
LabeledMiddleware is a mutated endpoint.Middleware. It receives an additional method name from the caller.
func MakeLabeledMetricsMiddleware ¶
func MakeLabeledMetricsMiddleware(his metrics.Histogram, keyer contract.Keyer) LabeledMiddleware
MakeLabeledMetricsMiddleware returns a LabeledMiddleware that collects histogram metrics.
func MakeLabeledTraceServerMiddleware ¶
func MakeLabeledTraceServerMiddleware(tracer stdtracing.Tracer, keyer contract.Keyer) LabeledMiddleware
MakeLabeledTraceServerMiddleware returns a LabeledMiddleware that wraps the `next` Endpoint in an OpenTracing Span. The name of the operation is defined by contract.Keyer.