Skip to content

Commit

Permalink
Expose record context response for v1 (#1185)
Browse files Browse the repository at this point in the history
* Exposing RecordContextResponse & ensuring to lock when accessing segment

* Moved code to create xray.Exception into a new function

* Explicitly initializing segment.HTTP when needed

* Added unit tests for RecordRequest, RecordResponse and race detection

* Also closing sub segment in race test
  • Loading branch information
dominicm authored and Raphaël Simon committed Apr 13, 2017
1 parent ae73ff1 commit 4f43967
Show file tree
Hide file tree
Showing 7 changed files with 344 additions and 119 deletions.
3 changes: 1 addition & 2 deletions middleware/xray/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ func (r *httpTracer) Do(req *http.Request) (*http.Response, error) {
sub := r.segment.NewSubsegment(req.URL.Host)
defer sub.Close()

sub.Namespace = "remote"
sub.HTTP = &HTTP{Request: requestData(req)}
sub.RecordRequest(req, "remote")

resp, err := r.client.Do(req)

Expand Down
88 changes: 9 additions & 79 deletions middleware/xray/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"net"
"net/http"
"strings"
"time"

"context"
Expand Down Expand Up @@ -67,7 +66,14 @@ func New(service, daemon string) (goa.Middleware, error) {
ctx = WithSegment(ctx, s)

defer func() {
go record(ctx, s, err)
go func() {
defer s.Close()

s.RecordContextResponse(ctx)
if err != nil {
s.RecordError(err)
}
}()
}()

err = h(ctx, rw, req)
Expand Down Expand Up @@ -112,11 +118,10 @@ func newSegment(ctx context.Context, traceID, name string, req *http.Request, c
var (
spanID = middleware.ContextSpanID(ctx)
parentID = middleware.ContextParentSpanID(ctx)
h = &HTTP{Request: requestData(req)}
)

s := NewSegment(name, traceID, spanID, c)
s.HTTP = h
s.RecordRequest(req, "")

if parentID != "" {
s.ParentID = parentID
Expand All @@ -126,81 +131,6 @@ func newSegment(ctx context.Context, traceID, name string, req *http.Request, c
return s
}

// record finalizes and sends the segment to the X-Ray daemon.
func record(ctx context.Context, s *Segment, err error) {
resp := goa.ContextResponse(ctx)
if resp != nil {
s.Lock()
switch {
case resp.Status == 429:
s.Throttle = true
case resp.Status >= 500:
s.Error = true
}
s.HTTP.Response = &Response{resp.Status, int64(resp.Length)}
s.Unlock()
}
if err != nil {
fault := false
if gerr, ok := err.(goa.ServiceError); ok {
fault = gerr.ResponseStatus() < http.StatusInternalServerError &&
gerr.ResponseStatus() != http.StatusTooManyRequests
}
s.Fault = fault
s.RecordError(err)
}
s.Close()
}

// requestData creates a Request from a http.Request.
func requestData(req *http.Request) *Request {
var (
scheme = "http"
host = req.Host
)
if len(req.URL.Scheme) > 0 {
scheme = req.URL.Scheme
}
if len(req.URL.Host) > 0 {
host = req.URL.Host
}
return &Request{
Method: req.Method,
URL: fmt.Sprintf("%s://%s%s", scheme, host, req.URL.Path),
ClientIP: getIP(req),
UserAgent: req.UserAgent(),
ContentLength: req.ContentLength,
}
}

// responseData creates a Response from a http.Response.
func responseData(resp *http.Response) *Response {
return &Response{
Status: resp.StatusCode,
ContentLength: resp.ContentLength,
}
}

// getIP implements a heuristic that returns an origin IP address for a request.
func getIP(req *http.Request) string {
for _, h := range []string{"X-Forwarded-For", "X-Real-Ip"} {
for _, ip := range strings.Split(req.Header.Get(h), ",") {
if len(ip) == 0 {
continue
}
realIP := net.ParseIP(strings.Replace(ip, " ", "", -1))
return realIP.String()
}
}

// not found in header
host, _, err := net.SplitHostPort(req.RemoteAddr)
if err != nil {
return req.RemoteAddr
}
return host
}

// now returns the current time as a float appropriate for X-Ray processing.
func now() float64 {
return float64(time.Now().Truncate(time.Millisecond).UnixNano()) / 1e9
Expand Down
3 changes: 1 addition & 2 deletions middleware/xray/middleware_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package xray

import (
"context"
"encoding/json"
"errors"
"net"
Expand All @@ -14,8 +15,6 @@ import (

"github.com/goadesign/goa"
"github.com/goadesign/goa/middleware"

"context"
)

const (
Expand Down
Loading

0 comments on commit 4f43967

Please sign in to comment.