-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature: instrumentation for gofiber #356
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package xray | ||
|
||
import ( | ||
"context" | ||
"github.com/aws/aws-xray-sdk-go/header" | ||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a description here? |
||
type FiberHandler interface { | ||
Handler(SegmentNamer, fiber.Handler) fiber.Handler | ||
} | ||
|
||
type fiberHandler struct { | ||
cfg *Config | ||
} | ||
|
||
// NewFiberInstrumentor returns a new FiberHandler that | ||
// provides a Handler function to satisfy the fiber.Handler | ||
// interface. | ||
func NewFiberInstrumentor(cfg *Config) FiberHandler { | ||
return &fiberHandler{ | ||
cfg: cfg, | ||
} | ||
} | ||
|
||
// Handler wraps the provided fiber.Handler. | ||
func (h *fiberHandler) Handler(sn SegmentNamer, handler fiber.Handler) fiber.Handler { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think probably a good idea to provide context as an argument of this method to provide abilities to cancel the context or pass the context with a deadline. |
||
return func(ctx *fiber.Ctx) error { | ||
auxCtx := context.Background() | ||
if h.cfg != nil { | ||
auxCtx = context.WithValue(context.Background(), fasthttpContextConfigKey, h.cfg) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not super familiar with gofiber but what I can tell is it's built on |
||
ctx.Locals(fasthttpContextConfigKey, h.cfg) | ||
} | ||
|
||
name := sn.Name(ctx.Hostname()) | ||
traceHeader := header.FromString(ctx.Get(TraceIDHeaderKey)) | ||
|
||
req, err := fasthttpToNetHTTPRequest(ctx.Context()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, seg := NewSegmentFromHeader(auxCtx, name, req, traceHeader) | ||
defer seg.Close(nil) | ||
|
||
ctx.Locals(fasthttpContextKey, seg) | ||
httpCaptureRequest(seg, req) | ||
return fiberTrace(seg, handler, ctx, traceHeader) | ||
} | ||
} | ||
|
||
func fiberTrace(seg *Segment, handler fiber.Handler, ctx *fiber.Ctx, traceHeader *header.Header) error { | ||
ctx.Set(TraceIDHeaderKey, generateTraceIDHeaderValue(seg, traceHeader)) | ||
handlerErr := handler(ctx) | ||
|
||
seg.Lock() | ||
seg.GetHTTP().GetResponse().ContentLength = ctx.Context().Response.Header.ContentLength() | ||
seg.Unlock() | ||
HttpCaptureResponse(seg, ctx.Response().StatusCode()) | ||
return handlerErr | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package xray | ||
|
||
import ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should add a benchmark here. |
||
"github.com/gofiber/fiber/v2" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestFiberHandler(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment explaining what this test case is testing would be good for readability. |
||
ctx1, td := NewTestDaemon() | ||
cfg := GetRecorder(ctx1) | ||
defer td.Close() | ||
|
||
fh := NewFiberInstrumentor(cfg) | ||
handler := fh.Handler(NewFixedSegmentNamer("test"), func(ctx *fiber.Ctx) error { | ||
return nil | ||
}) | ||
rc := genericFiberRequestCtx() | ||
if err := handler(rc); err != nil { | ||
t.Error("Error calling handler:", err) | ||
} | ||
|
||
seg, err := td.Recv() | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
assert.Equal(t, fiber.StatusOK, rc.Response().StatusCode()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably a good idea to validate all the three error flags |
||
assert.Equal(t, fiber.MethodPost, seg.HTTP.Request.Method) | ||
assert.Equal(t, "http://localhost/path", seg.HTTP.Request.URL) | ||
assert.Equal(t, "1.2.3.5", seg.HTTP.Request.ClientIP) | ||
assert.Equal(t, "UA_test", seg.HTTP.Request.UserAgent) | ||
} | ||
|
||
func genericFiberRequestCtx() *fiber.Ctx { | ||
return fiber.New().AcquireCtx(genericRequestCtx()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we run
gofmt -w -s ./...
to format the files?