github.com/newrelic/go-agent@v3.26.0+incompatible/_integrations/nrawssdk/v1/nrawssdk.go (about) 1 // Copyright 2020 New Relic Corporation. All rights reserved. 2 // SPDX-License-Identifier: Apache-2.0 3 4 // Package nrawssdk instruments https://github.com/aws/aws-sdk-go requests. 5 package nrawssdk 6 7 import ( 8 "github.com/aws/aws-sdk-go/aws/request" 9 internal "github.com/newrelic/go-agent/_integrations/nrawssdk/internal" 10 agentinternal "github.com/newrelic/go-agent/internal" 11 ) 12 13 func init() { agentinternal.TrackUsage("integration", "library", "aws-sdk-go") } 14 15 func startSegment(req *request.Request) { 16 input := internal.StartSegmentInputs{ 17 HTTPRequest: req.HTTPRequest, 18 ServiceName: req.ClientInfo.ServiceName, 19 Operation: req.Operation.Name, 20 Region: req.ClientInfo.SigningRegion, 21 Params: req.Params, 22 } 23 req.HTTPRequest = internal.StartSegment(input) 24 } 25 26 func endSegment(req *request.Request) { 27 ctx := req.HTTPRequest.Context() 28 internal.EndSegment(ctx, req.HTTPResponse.Header) 29 } 30 31 // InstrumentHandlers will add instrumentation to the given *request.Handlers. 32 // 33 // A Segment will be created for each out going request. The Transaction must 34 // be added to the `http.Request`'s Context in order for the segment to be 35 // recorded. For DynamoDB calls, these segments will be 36 // `newrelic.DatastoreSegment` type and for all others they will be 37 // `newrelic.ExternalSegment` type. 38 // 39 // Additional attributes will be added to Transaction Trace Segments and Span 40 // Events: aws.region, aws.requestId, and aws.operation. 41 // 42 // To add instrumentation to the Session and see segments created for each 43 // invocation that uses the Session, call InstrumentHandlers with the session's 44 // Handlers and add the current Transaction to the `http.Request`'s Context: 45 // 46 // ses := session.New() 47 // // Add instrumentation to handlers 48 // nrawssdk.InstrumentHandlers(&ses.Handlers) 49 // lambdaClient = lambda.New(ses, aws.NewConfig()) 50 // 51 // req, out := lambdaClient.InvokeRequest(&lambda.InvokeInput{ 52 // ClientContext: aws.String("MyApp"), 53 // FunctionName: aws.String("Function"), 54 // InvocationType: aws.String("Event"), 55 // LogType: aws.String("Tail"), 56 // Payload: []byte("{}"), 57 // } 58 // // Add txn to http.Request's context 59 // req.HTTPRequest = newrelic.RequestWithTransactionContext(req.HTTPRequest, txn) 60 // err := req.Send() 61 // 62 // To add instrumentation to a Request and see a segment created just for the 63 // individual request, call InstrumentHandlers with the `request.Request`'s 64 // Handlers and add the current Transaction to the `http.Request`'s Context: 65 // 66 // req, out := lambdaClient.InvokeRequest(&lambda.InvokeInput{ 67 // ClientContext: aws.String("MyApp"), 68 // FunctionName: aws.String("Function"), 69 // InvocationType: aws.String("Event"), 70 // LogType: aws.String("Tail"), 71 // Payload: []byte("{}"), 72 // } 73 // // Add instrumentation to handlers 74 // nrawssdk.InstrumentHandlers(&req.Handlers) 75 // // Add txn to http.Request's context 76 // req.HTTPRequest = newrelic.RequestWithTransactionContext(req.HTTPRequest, txn) 77 // err := req.Send() 78 func InstrumentHandlers(handlers *request.Handlers) { 79 handlers.Send.SetFrontNamed(request.NamedHandler{ 80 Name: "StartNewRelicSegment", 81 Fn: startSegment, 82 }) 83 handlers.Send.SetBackNamed(request.NamedHandler{ 84 Name: "EndNewRelicSegment", 85 Fn: endSegment, 86 }) 87 }