|
| 1 | +package nrfasthttp |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/newrelic/go-agent/v3/internal" |
| 7 | + "github.com/newrelic/go-agent/v3/newrelic" |
| 8 | + "github.com/valyala/fasthttp" |
| 9 | +) |
| 10 | + |
| 11 | +type myError struct{} |
| 12 | + |
| 13 | +func (e myError) Error() string { return "my msg" } |
| 14 | + |
| 15 | +func myErrorHandlerFastHTTP(ctx *fasthttp.RequestCtx) { |
| 16 | + ctx.WriteString("noticing an error") |
| 17 | + txn := ctx.UserValue("transaction").(*newrelic.Transaction) |
| 18 | + txn.NoticeError(myError{}) |
| 19 | +} |
| 20 | + |
| 21 | +func TestWrapHandleFastHTTPFunc(t *testing.T) { |
| 22 | + singleCount := []float64{1, 0, 0, 0, 0, 0, 0} |
| 23 | + app := createTestApp(true) |
| 24 | + |
| 25 | + _, wrappedHandler := WrapHandleFunc(app.Application, "/hello", myErrorHandlerFastHTTP) |
| 26 | + |
| 27 | + if wrappedHandler == nil { |
| 28 | + t.Error("Error when creating a wrapped handler") |
| 29 | + } |
| 30 | + ctx := &fasthttp.RequestCtx{} |
| 31 | + ctx.Request.Header.SetMethod("GET") |
| 32 | + ctx.Request.SetRequestURI("/hello") |
| 33 | + wrappedHandler(ctx) |
| 34 | + app.ExpectErrors(t, []internal.WantError{{ |
| 35 | + TxnName: "WebTransaction/Go/GET /hello", |
| 36 | + Msg: "my msg", |
| 37 | + Klass: "nrfasthttp.myError", |
| 38 | + }}) |
| 39 | + |
| 40 | + app.ExpectMetrics(t, []internal.WantMetric{ |
| 41 | + {Name: "WebTransaction/Go/GET /hello", Scope: "", Forced: true, Data: nil}, |
| 42 | + {Name: "WebTransaction", Scope: "", Forced: true, Data: nil}, |
| 43 | + {Name: "WebTransactionTotalTime/Go/GET /hello", Scope: "", Forced: false, Data: nil}, |
| 44 | + {Name: "WebTransactionTotalTime", Scope: "", Forced: true, Data: nil}, |
| 45 | + {Name: "HttpDispatcher", Scope: "", Forced: true, Data: nil}, |
| 46 | + {Name: "Apdex", Scope: "", Forced: true, Data: nil}, |
| 47 | + {Name: "Apdex/Go/GET /hello", Scope: "", Forced: false, Data: nil}, |
| 48 | + {Name: "DurationByCaller/Unknown/Unknown/Unknown/Unknown/all", Scope: "", Forced: false, Data: nil}, |
| 49 | + {Name: "DurationByCaller/Unknown/Unknown/Unknown/Unknown/allWeb", Scope: "", Forced: false, Data: nil}, |
| 50 | + {Name: "Errors/all", Scope: "", Forced: true, Data: singleCount}, |
| 51 | + {Name: "Errors/allWeb", Scope: "", Forced: true, Data: singleCount}, |
| 52 | + {Name: "Errors/WebTransaction/Go/GET /hello", Scope: "", Forced: true, Data: singleCount}, |
| 53 | + {Name: "ErrorsByCaller/Unknown/Unknown/Unknown/Unknown/all", Scope: "", Forced: false, Data: nil}, |
| 54 | + {Name: "ErrorsByCaller/Unknown/Unknown/Unknown/Unknown/allWeb", Scope: "", Forced: false, Data: nil}, |
| 55 | + }) |
| 56 | +} |
0 commit comments