Skip to content

Instantly share code, notes, and snippets.

@bbraunstein
Last active June 7, 2021 19:04
Show Gist options
  • Save bbraunstein/dc83aa0466800509ae45083500325888 to your computer and use it in GitHub Desktop.
Save bbraunstein/dc83aa0466800509ae45083500325888 to your computer and use it in GitHub Desktop.
New Relic Go Custom Instrumentation
package main
import (
"context"
"fmt"
"github.com/newrelic/go-agent/v3/integrations/nrlambda"
"github.com/newrelic/go-agent/v3/newrelic"
)
var (
app *newrelic.Application
err error
)
func init() {
app, err = newrelic.NewApplication(nrlambda.ConfigOption(), newrelic.ConfigDebugLogger(os.Stdout))
if nil != err {
fmt.Println("Error creating app (invalid config): ", err)
}
}
func handler(ctx context.Context) (string, error) {
// At this point, we're handling an invocation. Cold start is over; this code runs for each invocation.
// We'd like to add a custom event, and a custom attribute. For that, we need the transaction.
if txn := newrelic.FromContext(ctx); nil != txn {
// This is an example of a custom event. `FROM MyGoEvent SELECT *` in New Relic will find this event.
txn.Application().RecordCustomEvent("MyGoEvent", map[string]interface{}{
"zip": "zap",
})
// This attribute gets added to the normal AwsLambdaInvocation event
txn.AddAttribute("customAttribute", "customAttributeValue")
}
// As normal, anything you write to stdout ends up in CloudWatch
fmt.Println("hello world!")
return "Success!", nil
}
func main() {
nrlambda.Start(handler, app)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment