Skip to content

Commit d82d958

Browse files
authored
Merge pull request #28 from FireTail-io/dev
Merge dev into main
2 parents bdcaca2 + 4ad226d commit d82d958

8 files changed

+92
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
aws-xray-sdk
2-
requests
31
firetail-lambda

examples/minimal-python/handler.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
import sys, base64, datetime, json, os, time
1+
import datetime
2+
import json
3+
import sys
24

35
# Deps in src/vendor
46
sys.path.insert(0, 'src/vendor')
5-
import requests
67

7-
from firetail_lambda import firetail_handler, firetail_app
8+
from firetail_lambda import firetail_handler, firetail_app # noqa: E402
89
app = firetail_app()
910

10-
from aws_xray_sdk.core import xray_recorder
11-
from aws_xray_sdk.core import patch_all
12-
patch_all()
1311

1412
@firetail_handler(app)
1513
def endpoint(event, context):
14+
current_time = datetime.datetime.now().time()
1615
return {
1716
"statusCode": 200,
1817
"body": json.dumps({
19-
"message": "Hello, the current time is %s" % datetime.datetime.now().time()
20-
})
21-
}
18+
"message": "Hello, the current time is %s" % current_time
19+
}),
20+
"headers": {
21+
"Current-Time": "%s" % current_time
22+
}
23+
}

firetail/record.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ type Record struct {
2727

2828
// RecordResponse represents the response contained within a Firetail log Record
2929
type RecordResponse struct {
30-
StatusCode int64 `json:"statusCode"`
31-
Body string `json:"body"`
30+
StatusCode int64 `json:"statusCode"`
31+
Body string `json:"body"`
32+
Headers map[string]string `json:"headers"`
3233
}
3334

3435
// getLogEntryRequest returns the value for the request field of a Firetail SaaS LogEntry based upon the value of the firetail Record's Event value,

firetail/record_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ func TestEncodeAndDecodeRecord(t *testing.T) {
109109
Response: RecordResponse{
110110
StatusCode: 200,
111111
Body: "{\"Description\":\"This is a test response body\"}",
112+
Headers: map[string]string{
113+
"Test-Header-Name": "Test-Header-Value",
114+
},
112115
},
113116
ExecutionTime: 50,
114117
}
@@ -135,6 +138,9 @@ func TestGetLogEntryRequestAPIGatewayProxyRequest(t *testing.T) {
135138
Response: RecordResponse{
136139
StatusCode: 200,
137140
Body: "{\"Description\":\"This is a test response body\"}",
141+
Headers: map[string]string{
142+
"Test-Header-Name": "Test-Header-Value",
143+
},
138144
},
139145
ExecutionTime: 50,
140146
}
@@ -164,6 +170,9 @@ func TestGetLogEntryRequestAPIGatewayProxyRequestWithNoRequestHeaders(t *testing
164170
Response: RecordResponse{
165171
StatusCode: 200,
166172
Body: "{\"Description\":\"This is a test response body\"}",
173+
Headers: map[string]string{
174+
"Test-Header-Name": "Test-Header-Value",
175+
},
167176
},
168177
ExecutionTime: 50,
169178
}
@@ -191,6 +200,9 @@ func TestGetLogEntryRequestAPIGatewayV2HTTPRequest(t *testing.T) {
191200
Response: RecordResponse{
192201
StatusCode: 200,
193202
Body: "{\"Description\":\"This is a test response body\"}",
203+
Headers: map[string]string{
204+
"Test-Header-Name": "Test-Header-Value",
205+
},
194206
},
195207
ExecutionTime: 50,
196208
}
@@ -228,6 +240,9 @@ func TestGetLogEntryRequestUnsupportedPayload(t *testing.T) {
228240
Response: RecordResponse{
229241
StatusCode: 200,
230242
Body: "{\"Description\":\"This is a test response body\"}",
243+
Headers: map[string]string{
244+
"Test-Header-Name": "Test-Header-Value",
245+
},
231246
},
232247
ExecutionTime: 50,
233248
}

firetail/send_records_to_saas.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,18 @@ func SendRecordsToSaaS(records []Record, apiUrl, apiKey string) (int, error) {
2525
continue
2626
}
2727

28+
responseHeaders := map[string][]string{}
29+
for headerName, headerValue := range record.Response.Headers {
30+
responseHeaders[headerName] = []string{headerValue}
31+
}
32+
2833
logEntryBytes, err := json.Marshal(LogEntry{
2934
DateCreated: requestTime,
3035
ExecutionTime: record.ExecutionTime,
3136
Request: *logEntryRequest,
3237
Response: LogEntryResponse{
3338
Body: record.Response.Body,
34-
Headers: map[string][]string{},
39+
Headers: responseHeaders,
3540
StatusCode: record.Response.StatusCode,
3641
},
3742
Version: The100Alpha,

firetail/send_records_to_saas_test.go

+23-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package firetail
33
import (
44
"encoding/json"
55
"fmt"
6+
"io"
67
"net/http"
78
"net/http/httptest"
89
"testing"
@@ -19,6 +20,9 @@ func getValidRecord(t *testing.T) Record {
1920
Response: RecordResponse{
2021
StatusCode: 200,
2122
Body: "{\"Description\":\"This is a test response body\"}",
23+
Headers: map[string]string{
24+
"Test-Header-Name": "Test-Header-Value",
25+
},
2226
},
2327
ExecutionTime: 50,
2428
}
@@ -38,14 +42,20 @@ func getInvalidRecord(t *testing.T) Record {
3842
Response: RecordResponse{
3943
StatusCode: 200,
4044
Body: "{\"Description\":\"This is a test response body\"}",
45+
Headers: map[string]string{
46+
"Test-Header-Name": "Test-Header-Value",
47+
},
4148
},
4249
ExecutionTime: 50,
4350
}
4451
}
4552

46-
func getTestServer() *httptest.Server {
53+
func getTestServer(t *testing.T, body *[]byte) *httptest.Server {
4754
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
4855
fmt.Fprintf(w, `{"message":"success"}`)
56+
var err error
57+
*body, err = io.ReadAll(r.Body)
58+
assert.Nil(t, err)
4959
}))
5060
}
5161

@@ -56,18 +66,24 @@ func getBrokenTestServer() *httptest.Server {
5666
}
5767

5868
func TestSendRecordToSaas(t *testing.T) {
59-
testServer := getTestServer()
69+
var receivedBody []byte
70+
testServer := getTestServer(t, &receivedBody)
6071
defer testServer.Close()
6172

6273
testRecord := getValidRecord(t)
6374

6475
recordsSent, err := SendRecordsToSaaS([]Record{testRecord}, testServer.URL, "")
6576
assert.Nil(t, err)
6677
assert.Equal(t, 1, recordsSent)
78+
assert.Equal(t,
79+
"{\"dateCreated\":1668685315222,\"executionTime\":50,\"request\":{\"body\":\"\",\"headers\":{\"Content-Length\":[\"0\"],\"Host\":[\"5iagptskg6.execute-api.eu-west-2.amazonaws.com\"],\"Postman-Token\":[\"8639a798-d0e7-420a-bd98-0c5cb16c6115\"],\"User-Agent\":[\"PostmanRuntime/7.28.4\"],\"X-Amzn-Trace-Id\":[\"Root=1-63761e03-7bc79fb21f90dbbe66feba18\"],\"X-Forwarded-For\":[\"37.228.214.117\"],\"X-Forwarded-Port\":[\"443\"],\"X-Forwarded-Proto\":[\"https\"],\"accept\":[\"*/*\"],\"accept-encoding\":[\"gzip, deflate, br\"]},\"httpProtocol\":\"HTTP/1.1\",\"ip\":\"37.228.214.117\",\"method\":\"GET\",\"uri\":\"https://5iagptskg6.execute-api.eu-west-2.amazonaws.com/hi\",\"resource\":\"/hi\"},\"response\":{\"body\":\"{\\\"Description\\\":\\\"This is a test response body\\\"}\",\"headers\":{\"Test-Header-Name\":[\"Test-Header-Value\"]},\"statusCode\":200},\"version\":\"1.0.0-alpha\"}\n",
80+
string(receivedBody),
81+
)
6782
}
6883

6984
func TestSendInvalidRecordToSaas(t *testing.T) {
70-
testServer := getTestServer()
85+
var receivedBody []byte
86+
testServer := getTestServer(t, &receivedBody)
7187
defer testServer.Close()
7288

7389
invalidRecord := getInvalidRecord(t)
@@ -77,6 +93,7 @@ func TestSendInvalidRecordToSaas(t *testing.T) {
7793
require.NotNil(t, err)
7894
assert.Contains(t, err.Error(), "json: cannot unmarshal string into Go struct field APIGatewayProxyRequest.headers of type map[string]string")
7995
assert.Contains(t, err.Error(), "json: cannot unmarshal string into Go struct field APIGatewayV2HTTPRequest.headers of type map[string]string")
96+
assert.Nil(t, receivedBody)
8097
}
8198

8299
func TestSendRecordToInvalidApiUrl(t *testing.T) {
@@ -87,7 +104,8 @@ func TestSendRecordToInvalidApiUrl(t *testing.T) {
87104
}
88105

89106
func TestSendRecordToUnavailableSaas(t *testing.T) {
90-
testServer := getTestServer()
107+
var receivedBody []byte
108+
testServer := getTestServer(t, &receivedBody)
91109
testServer.Close()
92110

93111
testRecord := getValidRecord(t)
@@ -97,6 +115,7 @@ func TestSendRecordToUnavailableSaas(t *testing.T) {
97115
require.NotNil(t, err)
98116
assert.Contains(t, err.Error(), "connect: connection refused")
99117
assert.Contains(t, err.Error(), "Failed to make log request, err: Post")
118+
assert.Nil(t, receivedBody)
100119
}
101120

102121
func TestSendRecordWithSaasFailure(t *testing.T) {

logsapi/extract_firetail_records_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ func TestDecodeFiretailRecordResponse(t *testing.T) {
1717
Response: firetail.RecordResponse{
1818
StatusCode: 200,
1919
Body: "Test Body",
20+
Headers: map[string]string{
21+
"Test-Header-Name": "Test-Header-Value",
22+
},
2023
},
2124
}
2225
testPayloadBytes, err := json.Marshal(testRecord)
@@ -45,6 +48,9 @@ func TestDecodeFiretailRecordWithExtraPartSuffixed(t *testing.T) {
4548
Response: firetail.RecordResponse{
4649
StatusCode: 200,
4750
Body: "Test Body",
51+
Headers: map[string]string{
52+
"Test-Header-Name": "Test-Header-Value",
53+
},
4854
},
4955
}
5056
testPayloadBytes, err := json.Marshal(testRecord)
@@ -64,6 +70,9 @@ func TestDecodeFiretailRecordWithExtraPartPrefixed(t *testing.T) {
6470
Response: firetail.RecordResponse{
6571
StatusCode: 200,
6672
Body: "Test Body",
73+
Headers: map[string]string{
74+
"Test-Header-Name": "Test-Header-Value",
75+
},
6776
},
6877
}
6978
testPayloadBytes, err := json.Marshal(testRecord)
@@ -82,6 +91,9 @@ func TestDecodeFiretailRecordWithInvalidPrefix(t *testing.T) {
8291
Response: firetail.RecordResponse{
8392
StatusCode: 200,
8493
Body: "Test Body",
94+
Headers: map[string]string{
95+
"Test-Header-Name": "Test-Header-Value",
96+
},
8597
},
8698
}
8799
testPayloadBytes, err := json.Marshal(testRecord)
@@ -101,6 +113,9 @@ func TestDecodeFiretailRecordWithTimestampPrefix(t *testing.T) {
101113
Response: firetail.RecordResponse{
102114
StatusCode: 200,
103115
Body: "Test Body",
116+
Headers: map[string]string{
117+
"Test-Header-Name": "Test-Header-Value",
118+
},
104119
},
105120
}
106121
testPayloadBytes, err := json.Marshal(testRecord)
@@ -119,6 +134,9 @@ func TestDecodeFiretailRecordWithInvalidToken(t *testing.T) {
119134
Response: firetail.RecordResponse{
120135
StatusCode: 200,
121136
Body: "Test Body",
137+
Headers: map[string]string{
138+
"Test-Header-Name": "Test-Header-Value",
139+
},
122140
},
123141
}
124142
testPayloadBytes, err := json.Marshal(testRecord)
@@ -138,6 +156,9 @@ func TestDecodeFiretailRecordWithInvalidPayloadEncoding(t *testing.T) {
138156
Response: firetail.RecordResponse{
139157
StatusCode: 200,
140158
Body: "Test Body",
159+
Headers: map[string]string{
160+
"Test-Header-Name": "Test-Header-Value",
161+
},
141162
},
142163
}
143164
testPayloadBytes, err := json.Marshal(testRecord)
@@ -180,6 +201,9 @@ func TestExtractSingleRecord(t *testing.T) {
180201
Response: firetail.RecordResponse{
181202
StatusCode: 200,
182203
Body: "Test Body",
204+
Headers: map[string]string{
205+
"Test-Header-Name": "Test-Header-Value",
206+
},
183207
},
184208
}
185209
testPayloadBytes, err := json.Marshal(testRecord)

logsapi/options_test.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,19 @@ func TestDefaultBatchCallback(t *testing.T) {
129129
Response: firetail.RecordResponse{
130130
StatusCode: 200,
131131
Body: `{"description":"test response body"}`,
132+
Headers: map[string]string{
133+
"Test-Header-Name": "Test-Header-Value",
134+
},
132135
},
133136
ExecutionTime: 3.142,
134137
},
135138
})
136139
require.Nil(t, err)
137140

138-
assert.Equal(t, "{\"dateCreated\":0,\"executionTime\":3.142,\"request\":{\"body\":\"\",\"headers\":{},\"httpProtocol\":\"\",\"ip\":\"\",\"method\":\"\",\"uri\":\"https://\",\"resource\":\"\"},\"response\":{\"body\":\"{\\\"description\\\":\\\"test response body\\\"}\",\"headers\":{},\"statusCode\":200},\"version\":\"1.0.0-alpha\"}\n", string(requestBody))
141+
assert.Equal(t,
142+
"{\"dateCreated\":0,\"executionTime\":3.142,\"request\":{\"body\":\"\",\"headers\":{},\"httpProtocol\":\"\",\"ip\":\"\",\"method\":\"\",\"uri\":\"https://\",\"resource\":\"\"},\"response\":{\"body\":\"{\\\"description\\\":\\\"test response body\\\"}\",\"headers\":{\"Test-Header-Name\":[\"Test-Header-Value\"]},\"statusCode\":200},\"version\":\"1.0.0-alpha\"}\n",
143+
string(requestBody),
144+
)
139145
}
140146

141147
func TestDefaultBatchCallbackFail(t *testing.T) {
@@ -151,6 +157,9 @@ func TestDefaultBatchCallbackFail(t *testing.T) {
151157
Response: firetail.RecordResponse{
152158
StatusCode: 200,
153159
Body: `{"description":"test response body"}`,
160+
Headers: map[string]string{
161+
"Test-Header-Name": "Test-Header-Value",
162+
},
154163
},
155164
ExecutionTime: 3.142,
156165
},

0 commit comments

Comments
 (0)