Skip to content

Commit fba19d2

Browse files
committed
fix(cloudflarereceiver): preserve array-typed log fields as slice attrs
Cloudflare's http_requests dataset carries several array-typed fields (BotDetectionIDs, SecurityActions, SecurityRuleIDs, BotDetectionTags, BotTags, SecuritySources, ContentScanObj{Results,Sizes,Types}, FraudDetection{IDs,Tags}, ...). The receiver's type switch in processLogs only handled scalar values and maps, so any []any -- which is what encoding/json unmarshals a JSON array into -- fell through to the default branch and was dropped with an "unsupported type" warning. Handle []any explicitly via pcommon.Value.FromRaw(v) on a new empty slice attribute; do the same in the flatten-map subcase so nested array fields aren't lost either. Add a regression test (TestArrayAttributes) that feeds a payload with BotDetectionIDs/SecurityActions/SecurityRuleIDs and asserts each survives as a pcommon.ValueTypeSlice attribute. Fixes #46716.
1 parent 92c205a commit fba19d2

3 files changed

Lines changed: 83 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. receiver/filelog)
7+
component: cloudflarereceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Preserve array-typed log fields (e.g. BotDetectionIDs, SecurityActions, SecurityRuleIDs) as pcommon.Slice attributes instead of dropping them with an "unsupported type" warning.
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [46716]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: []

receiver/cloudflarereceiver/logs.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,16 @@ func (l *logsReceiver) processLogs(now pcommon.Timestamp, logs []map[string]any)
361361
attrs.PutDouble(attrName, v)
362362
case bool:
363363
attrs.PutBool(attrName, v)
364+
case []any:
365+
// Preserve array-typed fields (e.g. BotDetectionIDs,
366+
// SecurityActions) as a pcommon.Slice attribute
367+
// instead of dropping them with a warning.
368+
if err := attrs.PutEmptySlice(attrName).FromRaw(v); err != nil {
369+
l.logger.Warn("unable to translate array field to attribute",
370+
zap.String("field", field),
371+
zap.Any("value", v),
372+
zap.Error(err))
373+
}
364374
case map[string]any:
365375
// Flatten the map and add each field with a prefixed key
366376
flattened := make(map[string]any)
@@ -377,6 +387,13 @@ func (l *logsReceiver) processLogs(now pcommon.Timestamp, logs []map[string]any)
377387
attrs.PutDouble(k, v)
378388
case bool:
379389
attrs.PutBool(k, v)
390+
case []any:
391+
if err := attrs.PutEmptySlice(k).FromRaw(v); err != nil {
392+
l.logger.Warn("unable to translate flattened array field to attribute",
393+
zap.String("field", k),
394+
zap.Any("value", v),
395+
zap.Error(err))
396+
}
380397
default:
381398
l.logger.Warn("unable to translate flattened field to attribute, unsupported type",
382399
zap.String("field", k),

receiver/cloudflarereceiver/logs_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,3 +732,44 @@ func newReceiver(t *testing.T, cfg *Config, nextConsumer consumer.Logs) *logsRec
732732
require.NoError(t, err)
733733
return r
734734
}
735+
736+
// TestArrayAttributes covers #46716: Cloudflare log records carry several
737+
// array-typed fields (BotDetectionIDs, SecurityActions, SecurityRuleIDs, …)
738+
// that used to be dropped with "unsupported type" warnings. They should now
739+
// round-trip as pcommon.Slice attributes.
740+
func TestArrayAttributes(t *testing.T) {
741+
payload := `{"ClientIP":"1.2.3.4","EdgeStartTimestamp":"2023-03-03T05:29:05Z","BotDetectionIDs":[1,2,3],"SecurityActions":["block","log"],"SecurityRuleIDs":["rule-a","rule-b"]}`
742+
743+
recv := newReceiver(t, &Config{
744+
Logs: LogsConfig{
745+
Endpoint: "localhost:0",
746+
TLS: &configtls.ServerConfig{},
747+
MaxRequestBodySize: 1024,
748+
TimestampField: "EdgeStartTimestamp",
749+
Attributes: map[string]string{},
750+
Separator: ".",
751+
},
752+
}, &consumertest.LogsSink{})
753+
754+
rawLogs, err := parsePayload([]byte(payload))
755+
require.NoError(t, err)
756+
logs := recv.processLogs(pcommon.NewTimestampFromTime(time.Now()), rawLogs)
757+
require.Equal(t, 1, logs.ResourceLogs().Len())
758+
759+
attrs := logs.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0).Attributes()
760+
761+
botIDs, ok := attrs.Get("BotDetectionIDs")
762+
require.True(t, ok, "BotDetectionIDs must be preserved as an attribute")
763+
require.Equal(t, pcommon.ValueTypeSlice, botIDs.Type())
764+
require.Equal(t, 3, botIDs.Slice().Len())
765+
766+
secActions, ok := attrs.Get("SecurityActions")
767+
require.True(t, ok, "SecurityActions must be preserved as an attribute")
768+
require.Equal(t, pcommon.ValueTypeSlice, secActions.Type())
769+
require.Equal(t, []any{"block", "log"}, secActions.Slice().AsRaw())
770+
771+
secRuleIDs, ok := attrs.Get("SecurityRuleIDs")
772+
require.True(t, ok, "SecurityRuleIDs must be preserved as an attribute")
773+
require.Equal(t, pcommon.ValueTypeSlice, secRuleIDs.Type())
774+
require.Equal(t, []any{"rule-a", "rule-b"}, secRuleIDs.Slice().AsRaw())
775+
}

0 commit comments

Comments
 (0)