Skip to content

Commit 572e5aa

Browse files
Merge pull request #24 from FireTail-io/fire-747/lenient-payload-parsing
Fire 747/lenient payload parsing
2 parents fd82561 + b471a32 commit 572e5aa

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

logsapi/extract_firetail_records.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,16 @@ func extractFiretailRecords(requestBody []byte) ([]firetail.Record, error) {
5151
func decodeFiretailRecord(record string) (*firetail.Record, error) {
5252
recordParts := strings.Split(record, ":")
5353

54-
if len(recordParts) != 3 {
54+
if len(recordParts) < 3 {
5555
return nil, fmt.Errorf("record had %d parts when split by ':'", len(recordParts))
5656
}
5757

58-
if recordParts[0] != "firetail" {
58+
// If there's more than 3 parts, we take the last three; we're assuming the last part of the log is the payload we want.
59+
if len(recordParts) > 3 {
60+
recordParts = recordParts[len(recordParts)-3:]
61+
}
62+
63+
if !strings.HasSuffix(recordParts[0], "firetail") {
5964
return nil, fmt.Errorf("record did not have firetail prefix")
6065
}
6166

logsapi/extract_firetail_records_test.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestDecodeFiretailRecordWithMissingPart(t *testing.T) {
4040
assert.Equal(t, "record had 2 parts when split by ':'", err.Error())
4141
}
4242

43-
func TestDecodeFiretailRecordWithExtraPart(t *testing.T) {
43+
func TestDecodeFiretailRecordWithExtraPartSuffixed(t *testing.T) {
4444
testRecord := firetail.Record{
4545
Response: firetail.RecordResponse{
4646
StatusCode: 200,
@@ -56,7 +56,25 @@ func TestDecodeFiretailRecordWithExtraPart(t *testing.T) {
5656
assert.Nil(t, decodedRecord)
5757
require.NotNil(t, err)
5858

59-
assert.Equal(t, "record had 4 parts when split by ':'", err.Error())
59+
assert.Equal(t, "record did not have firetail prefix", err.Error())
60+
}
61+
62+
func TestDecodeFiretailRecordWithExtraPartPrefixed(t *testing.T) {
63+
testRecord := firetail.Record{
64+
Response: firetail.RecordResponse{
65+
StatusCode: 200,
66+
Body: "Test Body",
67+
},
68+
}
69+
testPayloadBytes, err := json.Marshal(testRecord)
70+
require.Nil(t, err)
71+
72+
encodedRecord := "extra:firetail:log-ext:" + base64.StdEncoding.EncodeToString(testPayloadBytes) + ""
73+
74+
decodedRecord, err := decodeFiretailRecord(encodedRecord)
75+
require.Nil(t, err)
76+
77+
assert.Equal(t, testRecord.Response, *&decodedRecord.Response)
6078
}
6179

6280
func TestDecodeFiretailRecordWithInvalidPrefix(t *testing.T) {
@@ -78,6 +96,24 @@ func TestDecodeFiretailRecordWithInvalidPrefix(t *testing.T) {
7896
assert.Equal(t, "record did not have firetail prefix", err.Error())
7997
}
8098

99+
func TestDecodeFiretailRecordWithTimestampPrefix(t *testing.T) {
100+
testRecord := firetail.Record{
101+
Response: firetail.RecordResponse{
102+
StatusCode: 200,
103+
Body: "Test Body",
104+
},
105+
}
106+
testPayloadBytes, err := json.Marshal(testRecord)
107+
require.Nil(t, err)
108+
109+
encodedRecord := "2023-02-09T14:12:59.574Z 7b9025e7-228f-4f39-ab16-1cadba2bb3f6 INFO firetail:log-ext:" + base64.StdEncoding.EncodeToString(testPayloadBytes)
110+
111+
decodedRecord, err := decodeFiretailRecord(encodedRecord)
112+
require.Nil(t, err)
113+
114+
assert.Equal(t, testRecord.Response, *&decodedRecord.Response)
115+
}
116+
81117
func TestDecodeFiretailRecordWithInvalidToken(t *testing.T) {
82118
testRecord := firetail.Record{
83119
Response: firetail.RecordResponse{

0 commit comments

Comments
 (0)