Skip to content

Commit

Permalink
SetLogger and panic stacktrace. (#210)
Browse files Browse the repository at this point in the history
* Initialize opencensus for GKE

* update readme

* Generalize initialization of sd exporter

* fix sdExporterOptions

* do not initilize SD exporters and error reporting unless we are in the cloud or set proper environment variables

* Update doc

* address comments

* remove redundant if

* SetLogger to context. Log panic with a stacktrace.

* Func comment

* fix typo

* fix typo
  • Loading branch information
gaplyk authored and jprobinson committed Mar 16, 2019
1 parent 6c9c411 commit b51b914
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 3 additions & 2 deletions server/kit/kitserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net"
"net/http"
"net/http/pprof"
"runtime/debug"
"strings"

"cloud.google.com/go/errorreporting"
Expand Down Expand Up @@ -154,7 +155,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
err = e
}

s.logger.Log("error", err, "message", "the server encountered a panic")
s.logger.Log("error", err, "message", "the server encountered a panic", "stacktrace", string(debug.Stack()))

w.WriteHeader(http.StatusInternalServerError)
_, werr := w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
Expand Down Expand Up @@ -191,7 +192,7 @@ func (s *Server) register(svc Service) {
}),
// inject the server logger into every request context
httptransport.ServerBefore(func(ctx context.Context, _ *http.Request) context.Context {
return context.WithValue(ctx, logKey, AddLogKeyVals(ctx, s.logger))
return SetLogger(ctx, AddLogKeyVals(ctx, s.logger))
}),
}
opts = append(opts, svc.HTTPOptions()...)
Expand Down
5 changes: 5 additions & 0 deletions server/kit/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ func NewLogger(ctx context.Context, logID string) (log.Logger, func() error, err
return lg, cl, err
}

// SetLogger sets log.Logger to the context and returns new context with logger.
func SetLogger(ctx context.Context, logger log.Logger) context.Context {
return context.WithValue(ctx, logKey, logger)
}

// Logger will return a kit/log.Logger that has been injected into the context by the kit
// server. This logger has had request headers and metadata added as key values.
// This function will only work within the scope of a request initiated by the server.
Expand Down

0 comments on commit b51b914

Please sign in to comment.