Skip to content

Commit

Permalink
[receiver/cloudfoundryreceiver] Fix parseLogLine function when last c…
Browse files Browse the repository at this point in the history
…har was ". Update tests

Co-authored-by: Sam Clulow <[email protected]>
Co-authored-by: Cem Deniz Kabakci <[email protected]>
  • Loading branch information
3 people committed May 29, 2024
1 parent 361fd8e commit 6487d4e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 36 deletions.
6 changes: 5 additions & 1 deletion receiver/cloudfoundryreceiver/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,14 @@ func parseLogLine(s string) ([]string, map[string]string) {
sb.WriteRune(ch)
}
if sb.Len() > 0 {
wordList = append(wordList, sb.String())
word := sb.String()
if isMap {
wordMap[mapKey] = mapValue.String()
} else if strings.HasPrefix(word, `"`) && strings.HasSuffix(word, `"`) {
// remove " if the item is not a keyMap and starts and ends with it
word = strings.Trim(word, `"`)
}
wordList = append(wordList, word)
}
return wordList, wordMap
}
84 changes: 49 additions & 35 deletions receiver/cloudfoundryreceiver/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,45 +146,59 @@ func TestConvertGaugeEnvelope(t *testing.T) {
}

func TestParseLogLine(t *testing.T) {
logLines := []string{
`www.example.com - [2024-05-21T15:40:13.892179798Z] "GET /articles/ssdfws HTTP/1.1" 200 0 110563 "-" "python-requests/2.26.0" "20.191.2.244:52238" "10.88.195.81:61222" x_forwarded_for:"18.21.57.150, 10.28.45.29, 35.16.25.46, 20.191.2.244" x_forwarded_proto:"https" vcap_request_id:"766afb19-1779-4bb9-65d4-f01306f9f912" response_time:0.191835 gorouter_time:0.000139 app_id:"e3267823-0938-43ce-85ff-003e3e3a5a23" app_index:"4" instance_id:"918dd283-a0ed-48be-7f0c-253b" x_cf_routererror:"-" x_forwarded_host:"www.example.com" x_b3_traceid:"766afb1917794bb965d4f01306f9f912" x_b3_spanid:"65d4f01306f9f912" x_b3_parentspanid:"-" b3:"766afb1917794bb965d4f01306f9f912-65d4f01306f9f912" traceparent:"00-766afb1917794bb965d4f01306f9f912-65d4f01306f9f912-01" tracestate:"gorouter=65d4f01306f9f912"`,
}
wordListExpected := [][]string{
{"www.example.com", "-", "2024-05-21T15:40:13.892179798Z", "GET /articles/ssdfws HTTP/1.1", "200", "0", "110563", "-", "python-requests/2.26.0", "20.191.2.244:52238", "10.88.195.81:61222", `x_forwarded_for:"18.21.57.150, 10.28.45.29, 35.16.25.46, 20.191.2.244"`, `x_forwarded_proto:"https"`, `vcap_request_id:"766afb19-1779-4bb9-65d4-f01306f9f912"`, `response_time:0.191835`, `gorouter_time:0.000139`, `app_id:"e3267823-0938-43ce-85ff-003e3e3a5a23"`, `app_index:"4"`, `instance_id:"918dd283-a0ed-48be-7f0c-253b"`, `x_cf_routererror:"-"`, `x_forwarded_host:"www.example.com"`, `x_b3_traceid:"766afb1917794bb965d4f01306f9f912"`, `x_b3_spanid:"65d4f01306f9f912"`, `x_b3_parentspanid:"-"`, `b3:"766afb1917794bb965d4f01306f9f912-65d4f01306f9f912"`, `traceparent:"00-766afb1917794bb965d4f01306f9f912-65d4f01306f9f912-01"`, `tracestate:"gorouter=65d4f01306f9f912"`},
}
wordMapExpected := []map[string]string{
t.Parallel()
tests := []struct {
id string
logLine string
expectedWordList []string
expectedWordMap map[string]string
}{
{
id: "good-rtr",
logLine: `www.example.com - [2024-05-21T15:40:13.892179798Z] "GET /articles/ssdfws HTTP/1.1" 200 0 110563 "-" "python-requests/2.26.0" "20.191.2.244:52238" "10.88.195.81:61222" x_forwarded_for:"18.21.57.150, 10.28.45.29, 35.16.25.46, 20.191.2.244" x_forwarded_proto:"https" vcap_request_id:"766afb19-1779-4bb9-65d4-f01306f9f912" response_time:0.191835 gorouter_time:0.000139 app_id:"e3267823-0938-43ce-85ff-003e3e3a5a23" app_index:"4" instance_id:"918dd283-a0ed-48be-7f0c-253b" x_cf_routererror:"-" x_forwarded_host:"www.example.com" x_b3_traceid:"766afb1917794bb965d4f01306f9f912" x_b3_spanid:"65d4f01306f9f912" x_b3_parentspanid:"-" b3:"766afb1917794bb965d4f01306f9f912-65d4f01306f9f912" traceparent:"00-766afb1917794bb965d4f01306f9f912-65d4f01306f9f912-01" tracestate:"gorouter=65d4f01306f9f912"`,
expectedWordList: []string{"www.example.com", "-", "2024-05-21T15:40:13.892179798Z", "GET /articles/ssdfws HTTP/1.1", "200", "0", "110563", "-", "python-requests/2.26.0", "20.191.2.244:52238", "10.88.195.81:61222", `x_forwarded_for:"18.21.57.150, 10.28.45.29, 35.16.25.46, 20.191.2.244"`, `x_forwarded_proto:"https"`, `vcap_request_id:"766afb19-1779-4bb9-65d4-f01306f9f912"`, `response_time:0.191835`, `gorouter_time:0.000139`, `app_id:"e3267823-0938-43ce-85ff-003e3e3a5a23"`, `app_index:"4"`, `instance_id:"918dd283-a0ed-48be-7f0c-253b"`, `x_cf_routererror:"-"`, `x_forwarded_host:"www.example.com"`, `x_b3_traceid:"766afb1917794bb965d4f01306f9f912"`, `x_b3_spanid:"65d4f01306f9f912"`, `x_b3_parentspanid:"-"`, `b3:"766afb1917794bb965d4f01306f9f912-65d4f01306f9f912"`, `traceparent:"00-766afb1917794bb965d4f01306f9f912-65d4f01306f9f912-01"`, `tracestate:"gorouter=65d4f01306f9f912"`},
expectedWordMap: map[string]string{
"x_forwarded_for": "18.21.57.150, 10.28.45.29, 35.16.25.46, 20.191.2.244",
"x_forwarded_proto": "https",
"vcap_request_id": "766afb19-1779-4bb9-65d4-f01306f9f912",
"response_time": "0.191835",
"gorouter_time": "0.000139",
"app_id": "e3267823-0938-43ce-85ff-003e3e3a5a23",
"app_index": "4",
"instance_id": "918dd283-a0ed-48be-7f0c-253b",
"x_cf_routererror": "-",
"x_forwarded_host": "www.example.com",
"x_b3_traceid": "766afb1917794bb965d4f01306f9f912",
"x_b3_spanid": "65d4f01306f9f912",
"x_b3_parentspanid": "-",
"b3": "766afb1917794bb965d4f01306f9f912-65d4f01306f9f912",
"traceparent": "00-766afb1917794bb965d4f01306f9f912-65d4f01306f9f912-01",
"tracestate": "gorouter=65d4f01306f9f912",
},
},
{
"x_forwarded_for": "18.21.57.150, 10.28.45.29, 35.16.25.46, 20.191.2.244",
"x_forwarded_proto": "https",
"vcap_request_id": "766afb19-1779-4bb9-65d4-f01306f9f912",
"response_time": "0.191835",
"gorouter_time": "0.000139",
"app_id": "e3267823-0938-43ce-85ff-003e3e3a5a23",
"app_index": "4",
"instance_id": "918dd283-a0ed-48be-7f0c-253b",
"x_cf_routererror": "-",
"x_forwarded_host": "www.example.com",
"x_b3_traceid": "766afb1917794bb965d4f01306f9f912",
"x_b3_spanid": "65d4f01306f9f912",
"x_b3_parentspanid": "-",
"b3": "766afb1917794bb965d4f01306f9f912-65d4f01306f9f912",
"traceparent": "00-766afb1917794bb965d4f01306f9f912-65d4f01306f9f912-01",
"tracestate": "gorouter=65d4f01306f9f912",
id: "empty-wordmap-rtr",
logLine: `www.example.com - [2024-05-21T15:40:13.892179798Z] "GET /articles/ssdfws HTTP/1.1" 200 0 110563 "-" "python-requests/2.26.0" "20.191.2.244:52238" "10.88.195.81:61222"`,
expectedWordList: []string{"www.example.com", "-", "2024-05-21T15:40:13.892179798Z", "GET /articles/ssdfws HTTP/1.1", "200", "0", "110563", "-", "python-requests/2.26.0", "20.191.2.244:52238", "10.88.195.81:61222"},
expectedWordMap: map[string]string{},
},
}
for index, logLine := range logLines {
wordList, wordMap := parseLogLine(logLine)
require.Equal(t, len(wordList), len(wordListExpected[index]))
require.Equal(t, len(wordMap), len(wordMapExpected[index]))
for _, tt := range tests {
t.Run(tt.id, func(t *testing.T) {
wordList, wordMap := parseLogLine(tt.logLine)

require.Equal(t, len(wordList), len(tt.expectedWordList))
require.Equal(t, len(wordMap), len(tt.expectedWordMap))

for wordExpectedIndex, wordExpected := range wordListExpected[index] {
assert.Equal(t, wordExpected, wordList[wordExpectedIndex], "List Item %s value", wordList[wordExpectedIndex])
}
for k, v := range wordMapExpected[index] {
value, present := wordMap[k]
assert.True(t, present, "Map Item %s presence", k)
assert.Equal(t, v, value, "Map Item %s value", v)
}
for wordExpectedIndex, wordExpected := range tt.expectedWordList {
assert.Equal(t, wordExpected, wordList[wordExpectedIndex], "List Item %s value", wordList[wordExpectedIndex])
}
for wordExpectedKey, wordExpectedValue := range tt.expectedWordMap {
value, present := wordMap[wordExpectedKey]
assert.True(t, present, "Map Item %s presence", wordExpectedKey)
assert.Equal(t, wordExpectedValue, value, "Map Item %s value", wordExpectedValue)
}
})
}
}

Expand Down

0 comments on commit 6487d4e

Please sign in to comment.