-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Support For FastHTTP
- Loading branch information
Showing
9 changed files
with
335 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2020 New Relic Corporation. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
newrelic "github.com/newrelic/go-agent/v3/newrelic" | ||
"github.com/valyala/fasthttp" | ||
) | ||
|
||
func doRequest(txn *newrelic.Transaction) error { | ||
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 := newrelic.StartExternalSegmentFastHTTP(txn, ctx) | ||
defer seg.End() | ||
|
||
err := fasthttp.Do(req, resp) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println("Response Code is ", resp.StatusCode()) | ||
return nil | ||
|
||
} | ||
|
||
func main() { | ||
app, err := newrelic.NewApplication( | ||
newrelic.ConfigAppName("Client App"), | ||
newrelic.ConfigLicense(os.Getenv("NEW_RELIC_LICENSE_KEY")), | ||
newrelic.ConfigDebugLogger(os.Stdout), | ||
newrelic.ConfigDistributedTracerEnabled(true), | ||
) | ||
|
||
if err := app.WaitForConnection(5 * time.Second); nil != err { | ||
fmt.Println(err) | ||
} | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
txn := app.StartTransaction("client-txn") | ||
err = doRequest(txn) | ||
if err != nil { | ||
txn.NoticeError(err) | ||
} | ||
txn.End() | ||
|
||
// Shut down the application to flush data to New Relic. | ||
app.Shutdown(10 * time.Second) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2020 New Relic Corporation. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
newrelic "github.com/newrelic/go-agent/v3/newrelic" | ||
|
||
"github.com/valyala/fasthttp" | ||
) | ||
|
||
func index(ctx *fasthttp.RequestCtx) { | ||
ctx.WriteString("Hello World") | ||
} | ||
|
||
func noticeError(ctx *fasthttp.RequestCtx) { | ||
ctx.WriteString("noticing an error") | ||
txn := ctx.UserValue("transaction").(*newrelic.Transaction) | ||
txn.NoticeError(errors.New("my error message")) | ||
} | ||
|
||
func main() { | ||
// Initialize New Relic | ||
app, err := newrelic.NewApplication( | ||
newrelic.ConfigAppName("FastHTTP App"), | ||
newrelic.ConfigLicense(os.Getenv("NEW_RELIC_LICENSE_KEY")), | ||
newrelic.ConfigDebugLogger(os.Stdout), | ||
newrelic.ConfigDistributedTracerEnabled(true), | ||
) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
if err := app.WaitForConnection(5 * time.Second); nil != err { | ||
fmt.Println(err) | ||
} | ||
_, helloRoute := newrelic.WrapHandleFuncFastHTTP(app, "/hello", index) | ||
_, errorRoute := newrelic.WrapHandleFuncFastHTTP(app, "/error", noticeError) | ||
handler := func(ctx *fasthttp.RequestCtx) { | ||
path := string(ctx.Path()) | ||
method := string(ctx.Method()) | ||
|
||
switch { | ||
case method == "GET" && path == "/hello": | ||
helloRoute(ctx) | ||
case method == "GET" && path == "/error": | ||
errorRoute(ctx) | ||
} | ||
} | ||
|
||
// Start the server with the instrumented handler | ||
fasthttp.ListenAndServe(":8080", handler) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module github.com/newrelic/go-agent/v3/integrations/nrfasthttp | ||
|
||
go 1.19 | ||
|
||
require ( | ||
github.com/newrelic/go-agent/v3 v3.23.1 | ||
github.com/stretchr/testify v1.8.4 | ||
github.com/valyala/fasthttp v1.48.0 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.