diff --git a/receiver/cloudfoundryreceiver/converter.go b/receiver/cloudfoundryreceiver/converter.go index cf56678771a5..b50854772883 100644 --- a/receiver/cloudfoundryreceiver/converter.go +++ b/receiver/cloudfoundryreceiver/converter.go @@ -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 } diff --git a/receiver/cloudfoundryreceiver/converter_test.go b/receiver/cloudfoundryreceiver/converter_test.go index 6c6da3903d20..0a2a83c7f9ac 100644 --- a/receiver/cloudfoundryreceiver/converter_test.go +++ b/receiver/cloudfoundryreceiver/converter_test.go @@ -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) + } + }) } }