1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
// +build go1.8
package awssupport
import (
"context"
"net/http"
"reflect"
"github.com/newrelic/go-agent/v3/internal/integrationsupport"
newrelic "github.com/newrelic/go-agent/v3/newrelic"
)
type contextKeyType struct{}
var segmentContextKey = contextKeyType(struct{}{})
type endable interface{ End() }
func getTableName(params interface{}) string {
var tableName string
v := reflect.ValueOf(params)
if v.IsValid() && v.Kind() == reflect.Ptr {
e := v.Elem()
if e.Kind() == reflect.Struct {
n := e.FieldByName("TableName")
if n.IsValid() {
if name, ok := n.Interface().(*string); ok {
if nil != name {
tableName = *name
}
}
}
}
}
return tableName
}
// GetRequestID looks for the AWS request ID header.
func GetRequestID(hdr http.Header) string {
id := hdr.Get("X-Amzn-Requestid")
if id == "" {
// Alternative version of request id in the header
id = hdr.Get("X-Amz-Request-Id")
}
return id
}
// StartSegmentInputs is used as the input to StartSegment.
type StartSegmentInputs struct {
HTTPRequest *http.Request
ServiceName string
Operation string
Region string
Params interface{}
}
// StartSegment starts a segment of either type DatastoreSegment or
// ExternalSegment given the serviceName provided. The segment is then added to
// the request context.
func StartSegment(input StartSegmentInputs) *http.Request {
httpCtx := input.HTTPRequest.Context()
txn := newrelic.FromContext(httpCtx)
var segment endable
// Service name capitalization is different for v1 and v2.
if input.ServiceName == "dynamodb" || input.ServiceName == "DynamoDB" || input.ServiceName == "dax" {
segment = &newrelic.DatastoreSegment{
Product: newrelic.DatastoreDynamoDB,
Collection: getTableName(input.Params),
Operation: input.Operation,
ParameterizedQuery: "",
QueryParameters: nil,
Host: input.HTTPRequest.URL.Host,
PortPathOrID: input.HTTPRequest.URL.Port(),
DatabaseName: "",
StartTime: txn.StartSegmentNow(),
}
} else {
segment = newrelic.StartExternalSegment(txn, input.HTTPRequest)
}
integrationsupport.AddAgentSpanAttribute(txn, newrelic.SpanAttributeAWSOperation, input.Operation)
integrationsupport.AddAgentSpanAttribute(txn, newrelic.SpanAttributeAWSRegion, input.Region)
ctx := context.WithValue(httpCtx, segmentContextKey, segment)
return input.HTTPRequest.WithContext(ctx)
}
// EndSegment will end any segment found in the given context.
func EndSegment(ctx context.Context, hdr http.Header) {
if segment, ok := ctx.Value(segmentContextKey).(endable); ok {
if id := GetRequestID(hdr); "" != id {
txn := newrelic.FromContext(ctx)
integrationsupport.AddAgentSpanAttribute(txn, newrelic.SpanAttributeAWSRequestID, id)
}
segment.End()
}
}
|