diff --git a/.chloggen/fix-awss3reader-prefix-trim.yaml b/.chloggen/fix-awss3reader-prefix-trim.yaml new file mode 100644 index 0000000000000..025b297477b03 --- /dev/null +++ b/.chloggen/fix-awss3reader-prefix-trim.yaml @@ -0,0 +1,29 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. receiver/filelog) +component: receiver/awss3 + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: "Fix S3 prefix trimming logic in awss3reader to correctly handle empty, single slash '/', and double slash '//' prefixes." + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [43587] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: | + This fix ensures the S3 object prefix is generated consistently for all prefix formats (e.g., `""`, `/`, `//`, `/logs/`, `//raw//`), + preventing malformed S3 paths when reading from buckets with non-standard prefixes. + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/receiver/awss3receiver/s3reader.go b/receiver/awss3receiver/s3reader.go index e6d39bd276d0a..2f95f53298954 100644 --- a/receiver/awss3receiver/s3reader.go +++ b/receiver/awss3receiver/s3reader.go @@ -7,6 +7,7 @@ import ( "context" "errors" "fmt" + "strings" "time" "github.com/aws/aws-sdk-go-v2/service/s3" @@ -157,10 +158,21 @@ func (s3Reader *s3TimeBasedReader) getObjectPrefixForTime(t time.Time, telemetry case S3PartitionHour: timeKey = getTimeKeyPartitionHour(t) } - if s3Reader.s3Prefix != "" { - return fmt.Sprintf("%s/%s/%s%s_", s3Reader.s3Prefix, timeKey, s3Reader.filePrefix, telemetryType) + // Retrieve the configured S3 prefix (may be empty, "/", "//", "logs/", etc.) + prefix := s3Reader.s3Prefix + + // Case 1: No prefix provided → use only timeKey + filePrefix + if prefix == "" { + return fmt.Sprintf("%s/%s%s_", timeKey, s3Reader.filePrefix, telemetryType) + } + // Case 2: Prefix contains only slashes (e.g., "/", "//", "///") + // Keep the exact number of slashes and directly append timeKey without adding an extra "/" + if strings.Trim(prefix, "/") == "" { + return fmt.Sprintf("%s%s/%s%s_", prefix, timeKey, s3Reader.filePrefix, telemetryType) } - return fmt.Sprintf("%s/%s%s_", timeKey, s3Reader.filePrefix, telemetryType) + // Case 3: Normal prefix (e.g., "logs", "logs/", "/logs/", "//raw//") + // Always add a "/" between prefix and timeKey to build a valid S3 path + return fmt.Sprintf("%s/%s/%s%s_", prefix, timeKey, s3Reader.filePrefix, telemetryType) } func (s3Reader *s3TimeBasedReader) sendStatus(ctx context.Context, status statusNotification) { diff --git a/receiver/awss3receiver/s3reader_test.go b/receiver/awss3receiver/s3reader_test.go index e0bf46f335d79..e61a6fb7809d1 100644 --- a/receiver/awss3receiver/s3reader_test.go +++ b/receiver/awss3receiver/s3reader_test.go @@ -83,6 +83,46 @@ func Test_s3Reader_getObjectPrefixForTime(t *testing.T) { }, want: "year=2021/month=02/day=01/hour=17/minute=32/metrics_", }, + { + name: "prefix is / (should preserve leading slash)", + args: args{ + s3Prefix: "/", + s3Partition: "minute", + filePrefix: "file", + telemetryType: "logs", + }, + want: "/year=2021/month=02/day=01/hour=17/minute=32/filelogs_", + }, + { + name: "prefix is // (should preserve double leading slashes)", + args: args{ + s3Prefix: "//", + s3Partition: "hour", + filePrefix: "file", + telemetryType: "metrics", + }, + want: "//year=2021/month=02/day=01/hour=17/filemetrics_", + }, + { + name: "prefix starts and ends with slash /logs/", + args: args{ + s3Prefix: "/logs/", + s3Partition: "hour", + filePrefix: "file", + telemetryType: "traces", + }, + want: "/logs//year=2021/month=02/day=01/hour=17/filetraces_", + }, + { + name: "prefix starts and ends with double slash //raw//", + args: args{ + s3Prefix: "//raw//", + s3Partition: "minute", + filePrefix: "file", + telemetryType: "logs", + }, + want: "//raw///year=2021/month=02/day=01/hour=17/minute=32/filelogs_", + }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) {