Skip to content

Commit

Permalink
Improved observability (#869)
Browse files Browse the repository at this point in the history
- Added jaeger trace fields in all logs
- Moved few request log parameters from response to all logs
   - method
   - pathname
   - remoteAddr
   - host
  • Loading branch information
hbhoyar-uber committed May 18, 2023
1 parent 81403d8 commit f2aa266
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
4 changes: 4 additions & 0 deletions runtime/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ const (
const (
// thrift service::method of endpoint thrift spec
logFieldRequestMethod = "endpointThriftMethod"
logFieldRequestHTTPMethod = "method"
logFieldRequestURL = "url"
logFieldRequestPathname = "pathname"
logFieldRequestRemoteAddr = "remoteAddr"
logFieldRequestHost = "host"
logFieldRequestStartTime = "timestamp-started"
logFieldRequestFinishedTime = "timestamp-finished"
logFieldResponseStatusCode = "statusCode"
Expand Down
24 changes: 24 additions & 0 deletions runtime/server_http_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/uber-go/tally"
"github.com/uber/jaeger-client-go"
"github.com/uber/zanzibar/runtime/jsonwrapper"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -78,6 +79,10 @@ func NewServerHTTPRequest(
zap.String(logFieldEndpointID, endpoint.EndpointName),
zap.String(logFieldEndpointHandler, endpoint.HandlerName),
zap.String(logFieldRequestURL, r.URL.Path),
zap.String(logFieldRequestHTTPMethod, r.Method),
zap.String(logFieldRequestRemoteAddr, r.RemoteAddr),
zap.String(logFieldRequestPathname, r.URL.RequestURI()),
zap.String(logFieldRequestHost, r.Host),
}

// put request scope tags on context
Expand Down Expand Up @@ -204,6 +209,25 @@ func (req *ServerHTTPRequest) start() {
}
req.span = span
}
req.setupLogFields()
}

func (req *ServerHTTPRequest) setupLogFields() {
fields := GetLogFieldsFromCtx(req.Context())
fields = append(fields, zap.Time(logFieldRequestStartTime, req.startTime))
if span := req.GetSpan(); span != nil {
jc, ok := span.Context().(jaeger.SpanContext)
if ok {
fields = append(fields,
zap.String(TraceSpanKey, jc.SpanID().String()),
zap.String(TraceIDKey, jc.TraceID().String()),
zap.Bool(TraceSampledKey, jc.IsSampled()),
)
}
}

ctx := WithLogFields(req.Context(), fields...)
req.httpRequest = req.httpRequest.WithContext(ctx)
}

// CheckHeaders verifies that request contains required headers.
Expand Down
22 changes: 2 additions & 20 deletions runtime/server_http_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import (
"strings"
"time"

"github.com/uber/jaeger-client-go"

"github.com/buger/jsonparser"
"github.com/pkg/errors"
"github.com/uber-go/tally"
Expand Down Expand Up @@ -142,24 +140,8 @@ func (res *ServerHTTPResponse) finish(ctx context.Context) {

func serverHTTPLogFields(req *ServerHTTPRequest, res *ServerHTTPResponse) []zapcore.Field {
fields := []zapcore.Field{
zap.String("method", req.httpRequest.Method),
zap.String("remoteAddr", req.httpRequest.RemoteAddr),
zap.String("pathname", req.httpRequest.URL.RequestURI()),
zap.String("host", req.httpRequest.Host),
zap.Time("timestamp-started", req.startTime),
zap.Time("timestamp-finished", res.finishTime),
zap.Int("statusCode", res.StatusCode),
}

if span := req.GetSpan(); span != nil {
jc, ok := span.Context().(jaeger.SpanContext)
if ok {
fields = append(fields,
zap.String(TraceSpanKey, jc.SpanID().String()),
zap.String(TraceIDKey, jc.TraceID().String()),
zap.Bool(TraceSampledKey, jc.IsSampled()),
)
}
zap.Time(logFieldRequestFinishedTime, res.finishTime),
zap.Int(logFieldResponseStatusCode, res.StatusCode),
}

for k, v := range res.Headers() {
Expand Down
7 changes: 7 additions & 0 deletions test/endpoints/bar/bar_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ func TestCallMetrics(t *testing.T) {
"hostname",
"pid",
"requestUUID",
"host",
"remoteAddr",
"trace.traceId",
"trace.span",
"trace.sampled",
}
for _, dynamicValue := range dynamicHeaders {
assert.Contains(t, logMsg, dynamicValue)
Expand All @@ -221,6 +226,8 @@ func TestCallMetrics(t *testing.T) {
"level": "debug",
"msg": "Finished an outgoing client HTTP request",
"statusCode": float64(200),
"method": "POST",
"pathname": "/bar/bar-path",
//"clientID": "bar",
//"clientMethod": "Normal",
//"clientThriftMethod": "Bar::normal",
Expand Down

0 comments on commit f2aa266

Please sign in to comment.