github.com/newrelic/go-agent@v3.26.0+incompatible/_integrations/nrawssdk/v2/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-v2 requests. 5 package nrawssdk 6 7 import ( 8 "github.com/aws/aws-sdk-go-v2/aws" 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-v2") } 14 15 func startSegment(req *aws.Request) { 16 input := internal.StartSegmentInputs{ 17 HTTPRequest: req.HTTPRequest, 18 ServiceName: req.Metadata.ServiceName, 19 Operation: req.Operation.Name, 20 Region: req.Metadata.SigningRegion, 21 Params: req.Params, 22 } 23 req.HTTPRequest = internal.StartSegment(input) 24 } 25 26 func endSegment(req *aws.Request) { 27 ctx := req.HTTPRequest.Context() 28 internal.EndSegment(ctx, req.HTTPResponse.Header) 29 } 30 31 // InstrumentHandlers will add instrumentation to the given *aws.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 a Config and see segments created for each 43 // invocation that uses that Config, call InstrumentHandlers with the config's 44 // Handlers and add the current Transaction to the `http.Request`'s Context: 45 // 46 // cfg, _ := external.LoadDefaultAWSConfig() 47 // cfg.Region = "us-west-2" 48 // // Add instrumentation to handlers 49 // nrawssdk.InstrumentHandlers(&cfg.Handlers) 50 // lambdaClient = lambda.New(cfg) 51 // 52 // req := lambdaClient.InvokeRequest(&lambda.InvokeInput{ 53 // ClientContext: aws.String("MyApp"), 54 // FunctionName: aws.String("Function"), 55 // InvocationType: lambda.InvocationTypeEvent, 56 // LogType: lambda.LogTypeTail, 57 // Payload: []byte("{}"), 58 // } 59 // // Add txn to http.Request's context 60 // ctx := newrelic.NewContext(req.Context(), txn) 61 // resp, err := req.Send(ctx) 62 // 63 // To add instrumentation to a Request and see a segment created just for the 64 // individual request, call InstrumentHandlers with the `aws.Request`'s 65 // Handlers and add the current Transaction to the `http.Request`'s Context: 66 // 67 // req := lambdaClient.InvokeRequest(&lambda.InvokeInput{ 68 // ClientContext: aws.String("MyApp"), 69 // FunctionName: aws.String("Function"), 70 // InvocationType: lambda.InvocationTypeEvent, 71 // LogType: lambda.LogTypeTail, 72 // Payload: []byte("{}"), 73 // } 74 // // Add instrumentation to handlers 75 // nrawssdk.InstrumentHandlers(&req.Handlers) 76 // // Add txn to http.Request's context 77 // ctx := newrelic.NewContext(req.Context(), txn) 78 // resp, err := req.Send(ctx) 79 func InstrumentHandlers(handlers *aws.Handlers) { 80 handlers.Send.SetFrontNamed(aws.NamedHandler{ 81 Name: "StartNewRelicSegment", 82 Fn: startSegment, 83 }) 84 handlers.Send.SetBackNamed(aws.NamedHandler{ 85 Name: "EndNewRelicSegment", 86 Fn: endSegment, 87 }) 88 }