Skip to content

Commit

Permalink
Added test for external segment/minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mirackara committed Sep 14, 2023
1 parent ef22a97 commit aed3dde
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
6 changes: 5 additions & 1 deletion v3/newrelic/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,9 @@ func transactionFromRequestContextFastHTTP(ctx *fasthttp.RequestCtx) *Transactio
txn := ctx.UserValue("transaction").(*Transaction)
return txn
}
return txn

if txn != nil {
return txn
}
return nil
}
29 changes: 29 additions & 0 deletions v3/newrelic/internal_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/newrelic/go-agent/v3/internal"
"github.com/valyala/fasthttp"
)

func TestWrapHandlerContext(t *testing.T) {
Expand Down Expand Up @@ -36,6 +37,34 @@ func TestWrapHandlerContext(t *testing.T) {
{Name: "Custom/mySegment", Scope: scope, Forced: false, Data: nil},
})
}
func TestExternalSegmentFastHTTP(t *testing.T) {
app := testApp(nil, ConfigDistributedTracerEnabled(false), t)
txn := app.StartTransaction("myTxn")

req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseRequest(req)
defer fasthttp.ReleaseResponse(resp)

req.SetRequestURI("http://localhost:8080/hello")
req.Header.SetMethod("GET")

ctx := &fasthttp.RequestCtx{}
seg := StartExternalSegmentFastHTTP(txn, ctx)
defer seg.End()

err := fasthttp.Do(req, resp)
txn.End()
app.ExpectMetrics(t, []internal.WantMetric{
{Name: "OtherTransaction/Go/myTxn", Scope: "", Forced: true, Data: nil},
{Name: "OtherTransaction/all", Scope: "", Forced: true, Data: nil},
{Name: "OtherTransactionTotalTime/Go/myTxn", Scope: "", Forced: false, Data: nil},
{Name: "OtherTransactionTotalTime", Scope: "", Forced: true, Data: nil},
})
if err != nil {
t.Error(err)
}
}

func TestStartExternalSegmentNilTransaction(t *testing.T) {
// Test that StartExternalSegment pulls the transaction from the
Expand Down
10 changes: 8 additions & 2 deletions v3/newrelic/segments.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,14 +348,20 @@ func StartExternalSegmentFastHTTP(txn *Transaction, ctx *fasthttp.RequestCtx) *E
StartTime: txn.StartSegmentNow(),
Request: request,
}
s.secureAgentEvent = secureAgent.SendEvent("OUTBOUND", request)
if IsSecurityAgentPresent() {
s.secureAgentEvent = secureAgent.SendEvent("OUTBOUND", request)
}

if request != nil && request.Header != nil {
for key, values := range s.outboundHeaders() {
for _, value := range values {
request.Header.Set(key, value)
}
}
secureAgent.DistributedTraceHeaders(request, s.secureAgentEvent)

if IsSecurityAgentPresent() {
secureAgent.DistributedTraceHeaders(request, s.secureAgentEvent)
}
}

return s
Expand Down

0 comments on commit aed3dde

Please sign in to comment.