Skip to content

Commit 5509f85

Browse files
committed
Add header requirement for req ID
1 parent 2f6e0df commit 5509f85

File tree

1 file changed

+39
-23
lines changed

1 file changed

+39
-23
lines changed

module/apmlambda/lambda.go

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package apmlambda // import "go.elastic.co/apm/module/apmlambda/v2"
1919

2020
import (
2121
"bytes"
22+
"fmt"
2223
"log"
2324
"net"
2425
"net/http"
@@ -111,29 +112,8 @@ func (f *Function) Invoke(req *messages.InvokeRequest, response *messages.Invoke
111112
lambdaContext.Request = formatPayload(req.Payload)
112113
lambdaContext.Response = ""
113114

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)
137117
}
138118
err := f.client.Call("Function.Invoke", req, response)
139119
if err != nil {
@@ -154,6 +134,42 @@ func (f *Function) Invoke(req *messages.InvokeRequest, response *messages.Invoke
154134
return nil
155135
}
156136

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+
157173
type invokeResponseError struct {
158174
err *messages.InvokeResponse_Error
159175
}

0 commit comments

Comments
 (0)