Skip to content

Commit

Permalink
add endpoint logging middleware to log total execution time of an end…
Browse files Browse the repository at this point in the history
…point
  • Loading branch information
lukasjarosch committed Jun 23, 2019
1 parent 40ef9a0 commit 9cabf64
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
4 changes: 2 additions & 2 deletions a_main-packr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions pkg/middleware/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package middleware

import (
"github.com/lukasjarosch/godin/pkg/log"
"github.com/go-kit/kit/endpoint"
"context"
"time"
)

func Logging(logger log.Logger, endpointName string) endpoint.Middleware {
return func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
defer func(begin time.Time) {
logger.Info("", "endpoint", endpointName, "took", time.Since(begin))
}(time.Now())

return next(ctx, request)
}
}
}
2 changes: 1 addition & 1 deletion templates/cmd_main.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func main() {

// initialize endpoint and transport layers
var (
endpoints = endpoint.Endpoints(svc)
endpoints = endpoint.Endpoints(svc, logger)
grpcHandler = svcGrpc.NewServer(endpoints, logger)
)

Expand Down
4 changes: 3 additions & 1 deletion templates/endpoint_set.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

godinMiddleware "github.com/lukasjarosch/godin/pkg/middleware"
"{{ .Service.Module }}/internal/service"
"github.com/lukasjarosch/godin/pkg/log"
)

type Set struct {
Expand All @@ -15,12 +16,13 @@ type Set struct {
{{- end }}
}

func Endpoints(service service.{{ title .Service.Name }}) Set {
func Endpoints(service service.{{ title .Service.Name }}, logger log.Logger) Set {
{{ range .Service.Methods }}
var {{ untitle .Name }} endpoint.Endpoint
{
{{ untitle .Name }} = {{ .Name }}Endpoint(service)
{{ untitle .Name }} = godinMiddleware.Prometheus("{{ .Name }}")({{ untitle .Name }})
{{ untitle .Name }} = godinMiddleware.Logging(logger, "{{ .Name }}")({{ untitle .Name }})
}
{{- end }}

Expand Down

0 comments on commit 9cabf64

Please sign in to comment.