Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[exporter/awss3] Make file format suffix configurable when using encoding #31833

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/awss3_encoding_file_extension.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: awss3exporter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Add support for specifying the file extension for files uploaded to S3 when using an encoding extension."

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [31818]

# (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:

# 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: [user]
1 change: 1 addition & 0 deletions exporter/awss3exporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The following exporter configuration parameters are supported.
| `file_prefix` | file prefix defined by user | |
| `marshaler` | marshaler used to produce output data | `otlp_json` |
| `encoding` | Encoding extension to use to marshal data. Overrides the `marshaler` configuration option if set. | |
| `encoding_file_extension` | file format extension suffix when using the `encoding` configuration option. May be left empty for no suffix to be appended. | |
| `endpoint` | overrides the endpoint used by the exporter instead of constructing it from `region` and `s3_bucket` | |
| `s3_force_path_style` | [set this to `true` to force the request to use path-style addressing](http://docs.aws.amazon.com/AmazonS3/latest/dev/VirtualHosting.html) | false |
| `disable_ssl` | set this to `true` to disable SSL when sending requests | false |
Expand Down
4 changes: 2 additions & 2 deletions exporter/awss3exporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ type Config struct {
S3Uploader S3UploaderConfig `mapstructure:"s3uploader"`
MarshalerName MarshalerType `mapstructure:"marshaler"`

FileFormat string `mapstructure:"file_format"`
// Encoding to apply. If present, overrides the marshaler configuration option.
Encoding *component.ID `mapstructure:"encoding"`
Encoding *component.ID `mapstructure:"encoding"`
EncodingFileExtension string `mapstructure:"encoding_file_extension"`
}

func (c *Config) Validate() error {
Expand Down
3 changes: 2 additions & 1 deletion exporter/awss3exporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ func TestLoadConfig(t *testing.T) {
encoding := component.MustNewIDWithName("foo", "bar")
assert.Equal(t, e,
&Config{
Encoding: &encoding,
Encoding: &encoding,
EncodingFileExtension: "baz",
S3Uploader: S3UploaderConfig{
Region: "us-east-1",
S3Bucket: "foo",
Expand Down
2 changes: 1 addition & 1 deletion exporter/awss3exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (e *s3Exporter) start(_ context.Context, host component.Host) error {
var m marshaler
var err error
if e.config.Encoding != nil {
if m, err = newMarshalerFromEncoding(e.config.Encoding, host, e.logger); err != nil {
if m, err = newMarshalerFromEncoding(e.config.Encoding, e.config.EncodingFileExtension, host, e.logger); err != nil {
return err
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions exporter/awss3exporter/marshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (
ErrUnknownMarshaler = errors.New("unknown marshaler")
)

func newMarshalerFromEncoding(encoding *component.ID, host component.Host, logger *zap.Logger) (marshaler, error) {
func newMarshalerFromEncoding(encoding *component.ID, fileFormat string, host component.Host, logger *zap.Logger) (marshaler, error) {
marshaler := &s3Marshaler{logger: logger}
e, ok := host.GetExtensions()[*encoding]
if !ok {
Expand All @@ -35,7 +35,7 @@ func newMarshalerFromEncoding(encoding *component.ID, host component.Host, logge
marshaler.logsMarshaler, _ = e.(plog.Marshaler)
marshaler.metricsMarshaler, _ = e.(pmetric.Marshaler)
marshaler.tracesMarshaler, _ = e.(ptrace.Marshaler)
marshaler.fileFormat = encoding.String()
marshaler.fileFormat = fileFormat
return marshaler, nil
}

Expand Down
6 changes: 3 additions & 3 deletions exporter/awss3exporter/marshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ func TestMarshalerFromEncoding(t *testing.T) {
host := hostWithExtensions{
encoding: encodingExtension{},
}
m, err := newMarshalerFromEncoding(&id, host, zap.NewNop())
m, err := newMarshalerFromEncoding(&id, "myext", host, zap.NewNop())
assert.NoError(t, err)
require.NotNil(t, m)
assert.Equal(t, "foo", m.format())
assert.Equal(t, "myext", m.format())
}
{
m, err := newMarshalerFromEncoding(&id, componenttest.NewNopHost(), zap.NewNop())
m, err := newMarshalerFromEncoding(&id, "", componenttest.NewNopHost(), zap.NewNop())
assert.EqualError(t, err, `unknown encoding "foo"`)
require.Nil(t, m)
}
Expand Down
8 changes: 6 additions & 2 deletions exporter/awss3exporter/s3_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ func randomInRange(low, hi int) int {
return low + rand.Intn(hi-low)
}

func getS3Key(time time.Time, keyPrefix string, partition string, filePrefix string, metadata string, fileformat string, compression configcompression.Type) string {
func getS3Key(time time.Time, keyPrefix string, partition string, filePrefix string, metadata string, fileFormat string, compression configcompression.Type) string {
timeKey := getTimeKey(time, partition)
randomID := randomInRange(100000000, 999999999)
suffix := ""
if fileFormat != "" {
suffix = "." + fileFormat
}

s3Key := keyPrefix + "/" + timeKey + "/" + filePrefix + metadata + "_" + strconv.Itoa(randomID) + "." + fileformat
s3Key := keyPrefix + "/" + timeKey + "/" + filePrefix + metadata + "_" + strconv.Itoa(randomID) + suffix

// add ".gz" extension to files if compression is enabled
if compression == configcompression.TypeGzip {
Expand Down
28 changes: 28 additions & 0 deletions exporter/awss3exporter/s3_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ func TestS3Key(t *testing.T) {
assert.Equal(t, true, matched)
}

func TestS3KeyEmptyFileFormat(t *testing.T) {
const layout = "2006-01-02"

tm, err := time.Parse(layout, "2022-06-05")

assert.NoError(t, err)
require.NotNil(t, tm)

re := regexp.MustCompile(`keyprefix/year=2022/month=06/day=05/hour=00/minute=00/fileprefixlogs_([0-9]+)`)
s3Key := getS3Key(tm, "keyprefix", "minute", "fileprefix", "logs", "", "")
matched := re.MatchString(s3Key)
assert.Equal(t, true, matched)
}

func TestS3KeyOfCompressedFile(t *testing.T) {
const layout = "2006-01-02"

Expand All @@ -55,6 +69,20 @@ func TestS3KeyOfCompressedFile(t *testing.T) {
assert.Equal(t, true, matched)
}

func TestS3KeyOfCompressedFileEmptyFileFormat(t *testing.T) {
const layout = "2006-01-02"

tm, err := time.Parse(layout, "2022-06-05")

assert.NoError(t, err)
require.NotNil(t, tm)

re := regexp.MustCompile(`keyprefix/year=2022/month=06/day=05/hour=00/minute=00/fileprefixlogs_([0-9]+).gz`)
s3Key := getS3Key(tm, "keyprefix", "minute", "fileprefix", "logs", "", "gzip")
matched := re.MatchString(s3Key)
assert.Equal(t, true, matched)
}

func TestGetSessionConfigWithEndpoint(t *testing.T) {
const endpoint = "https://endpoint.com"
const region = "region"
Expand Down
1 change: 1 addition & 0 deletions exporter/awss3exporter/testdata/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ receivers:
exporters:
awss3:
encoding: "foo/bar"
encoding_file_extension: "baz"
s3uploader:
s3_bucket: "foo"
region: 'us-east-1'
Expand Down