@@ -19,6 +19,7 @@ package apmlambda // import "go.elastic.co/apm/module/apmlambda/v2"
19
19
20
20
import (
21
21
"bytes"
22
+ "fmt"
22
23
"log"
23
24
"net"
24
25
"net/http"
@@ -111,29 +112,8 @@ func (f *Function) Invoke(req *messages.InvokeRequest, response *messages.Invoke
111
112
lambdaContext .Request = formatPayload (req .Payload )
112
113
lambdaContext .Response = ""
113
114
114
- if ! ignoreTxnRegistration {
115
- defer jsonw .Reset ()
116
- if err := createPartialTransactionJSON (tx , & jsonw ); err != nil {
117
- log .Printf ("failed to create partial transaction for registration: %v" , err )
118
- } else {
119
- resp , err := http .Post (
120
- // TODO: @lahsivjar better way to get base URI
121
- "http://localhost:8200/register/transaction" ,
122
- "application/vnd.elastic.apm.transaction+json" ,
123
- bytes .NewReader (jsonw .Bytes ()),
124
- )
125
- // Don't attempt registration for next invocations if network
126
- // error or the registration endpoint is not found.
127
- if err != nil || resp .StatusCode == 404 {
128
- ignoreTxnRegistration = true
129
- }
130
- if err != nil {
131
- log .Printf ("failed to register transaction, req failed with error: %v" , err )
132
- }
133
- if resp .StatusCode / 100 != 2 {
134
- log .Printf ("failed to register transaction, req failed with status code: %d" , resp .StatusCode )
135
- }
136
- }
115
+ if err := registerTxn (tx , req .RequestId ); err != nil {
116
+ log .Printf ("failed to register txn: %v" , err )
137
117
}
138
118
err := f .client .Call ("Function.Invoke" , req , response )
139
119
if err != nil {
@@ -154,6 +134,42 @@ func (f *Function) Invoke(req *messages.InvokeRequest, response *messages.Invoke
154
134
return nil
155
135
}
156
136
137
+ func registerTxn (tx * apm.Transaction , requestID string ) error {
138
+ if ignoreTxnRegistration {
139
+ return nil
140
+ }
141
+
142
+ defer jsonw .Reset ()
143
+ if err := createPartialTransactionJSON (tx , & jsonw ); err != nil {
144
+ return fmt .Errorf ("failed to create txn registration body: %v" , err )
145
+ }
146
+ req , err := http .NewRequest (
147
+ http .MethodPost ,
148
+ // TODO: @lahsivjar better way to get base URI
149
+ "http://localhost:8200/register/transaction" ,
150
+ bytes .NewReader (jsonw .Bytes ()),
151
+ )
152
+ if err != nil {
153
+ return fmt .Errorf ("failed to create txn registration request: %v" , err )
154
+ }
155
+ req .Header .Set ("Content-Type" , "application/vnd.elastic.apm.transaction+json" )
156
+ req .Header .Set ("x-elastic-aws-request-id" , requestID )
157
+
158
+ resp , err := http .DefaultClient .Do (req )
159
+ // Don't attempt registration for next invocations if network
160
+ // error or the registration endpoint is not found.
161
+ if err != nil || resp .StatusCode == 404 {
162
+ ignoreTxnRegistration = true
163
+ }
164
+ if err != nil {
165
+ return fmt .Errorf ("failed to register transaction, req failed with error: %v" , err )
166
+ }
167
+ if resp .StatusCode / 100 != 2 {
168
+ return fmt .Errorf ("failed to register transaction, req failed with status code: %d" , resp .StatusCode )
169
+ }
170
+ return nil
171
+ }
172
+
157
173
type invokeResponseError struct {
158
174
err * messages.InvokeResponse_Error
159
175
}
0 commit comments