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

Add support for compression options in Kafka exporter #63

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions exporter/kafkaexporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ The following settings can be optionally configured:
User should calculate this as `num_seconds * requests_per_second` where:
- `num_seconds` is the number of seconds to buffer in case of a backend outage
- `requests_per_second` is the average number of requests per seconds.
- `compression` (default = none): The compression codec to compress the messages sent to Kafka. Support four compression methods: `gzip`, `snappy`, `lz4` and `zstd`.
Saber-W marked this conversation as resolved.
Show resolved Hide resolved
- `compression_level` (default = -1000): The corresponding compression level.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the compression_level value mean? Is there a scale? Is it the same for all supported compression methods? Does a lower value indicate more compression, or a higher value? What is the range of this scale?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the compression_level value mean? Is there a scale?

Yes, it is a scale. It measures the compression degree to the files.

Is it the same for all supported compression methods?

No, each compression method has different range. For example, the valid range for gzip is 1 - 9.

Does a lower value indicate more compression, or a higher value?

Higher value indicates more compression.

What is the range of this scale?

The valid range for each method is as below:
gzip: 1 - 9
snappy: do not support
lz4: 1 - 17
zstd: 1 - 22

I would put all these info into README. Thanks!


Example configuration:

Expand Down
41 changes: 41 additions & 0 deletions exporter/kafkaexporter/compression.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package kafkaexporter

import (
"fmt"
"strings"

"github.com/Shopify/sarama"
)

func ConfigureCompression(compression string, saramaConfig *sarama.Config) error {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be exported? If so, it needs godoc describing its use. It probably doesn't, though, and thus should be unexported.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also don't see why this would be exported, but I saw that ConfigureAuthentication is also an exported method. Not sure why that would be, and if the same reasons would apply to ConfigureCompression?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I agree it doesn't need to - will do!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also don't see why this would be exported, but I saw that ConfigureAuthentication is also an exported method. Not sure why that would be, and if the same reasons would apply to ConfigureCompression?

ConfigureAuthentication is exported because it is called by the Kafka receiver. I think configureCompression doesn't need to at this point.

switch strings.ToLower(compression) {
case "none":
saramaConfig.Producer.Compression = sarama.CompressionNone
case "gzip":
saramaConfig.Producer.Compression = sarama.CompressionGZIP
case "snappy":
saramaConfig.Producer.Compression = sarama.CompressionSnappy
case "lz4":
saramaConfig.Producer.Compression = sarama.CompressionLZ4
case "zstd":
saramaConfig.Producer.Compression = sarama.CompressionZSTD
default:
return fmt.Errorf("invalid compression %q: can be either \"none\" , \"gzip\", \"snappy\", \"lz4\" or \"zstd\"", compression)
Saber-W marked this conversation as resolved.
Show resolved Hide resolved
}

return nil
}
59 changes: 59 additions & 0 deletions exporter/kafkaexporter/compression_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package kafkaexporter

import (
"testing"

"github.com/Shopify/sarama"
)

func TestConfigureCompression(t *testing.T) {
saramaSample := &sarama.Config{}

tests := []struct {
compression string
saramaConfig *sarama.Config
wantErr bool
}{
{
compression: "none",
saramaConfig: saramaSample,
},
{
compression: "gzip",
saramaConfig: saramaSample,
},
{
compression: "snappy",
saramaConfig: saramaSample,
},
{
compression: "lz4",
saramaConfig: saramaSample,
},
{
compression: "zstd",
saramaConfig: saramaSample,
},
Saber-W marked this conversation as resolved.
Show resolved Hide resolved
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
if err := ConfigureCompression(tt.compression, tt.saramaConfig); (err != nil) != tt.wantErr {
t.Errorf("ConfigureCompression() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
6 changes: 6 additions & 0 deletions exporter/kafkaexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ type Config struct {

// Authentication defines used authentication mechanism.
Authentication Authentication `mapstructure:"auth"`

// CompressionCodec is the chosen compression method
Saber-W marked this conversation as resolved.
Show resolved Hide resolved
CompressionCodec string `mapstructure:"compression"`

// CompressionCodec is the chosen compression level, if applicable
Saber-W marked this conversation as resolved.
Show resolved Hide resolved
Saber-W marked this conversation as resolved.
Show resolved Hide resolved
CompressionLevel int `mapstructure:"compression_level"`
}

// Metadata defines configuration for retrieving metadata from the broker.
Expand Down
2 changes: 2 additions & 0 deletions exporter/kafkaexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,7 @@ func TestLoadConfig(t *testing.T) {
Backoff: defaultMetadataRetryBackoff,
},
},
CompressionCodec: defaultCompression,
CompressionLevel: defaultCompressionLevel,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be good to try some non-default values for these in the testdata config?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

why did you decide to make compression and compression_level separate settings?

The Kafka exporter basically uses sarama as the producer to produce messages to Kafka. So I think it would be the best to follow the sarama's specification about the compression options.

Maybe it would be good to try some non-default values for these in the testdata config?

Yes, I should add more tests.

}, c)
}
8 changes: 8 additions & 0 deletions exporter/kafkaexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"context"
"time"

"github.com/Shopify/sarama"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/consumer"
Expand All @@ -37,6 +39,10 @@ const (
defaultMetadataRetryBackoff = time.Millisecond * 250
// default from sarama.NewConfig()
defaultMetadataFull = true
// By default, don't use compression.
defaultCompression = "none"
// default from sarama.NewConfig()
defaultCompressionLevel = sarama.CompressionLevelDefault
)

// FactoryOption applies changes to kafkaExporterFactory.
Expand Down Expand Up @@ -87,6 +93,8 @@ func createDefaultConfig() config.Exporter {
Backoff: defaultMetadataRetryBackoff,
},
},
CompressionCodec: defaultCompression,
CompressionLevel: defaultCompressionLevel,
}
}

Expand Down
4 changes: 4 additions & 0 deletions exporter/kafkaexporter/kafka_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ func newSaramaProducer(config Config) (sarama.SyncProducer, error) {
c.Metadata.Full = config.Metadata.Full
c.Metadata.Retry.Max = config.Metadata.Retry.Max
c.Metadata.Retry.Backoff = config.Metadata.Retry.Backoff
if err := ConfigureCompression(config.CompressionCodec, c); err != nil {
return nil, err
}
c.Producer.CompressionLevel = config.CompressionLevel
if config.ProtocolVersion != "" {
version, err := sarama.ParseKafkaVersion(config.ProtocolVersion)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions exporter/kafkaexporter/kafka_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ func TestNewExporter_err_auth_type(t *testing.T) {
Metadata: Metadata{
Full: false,
},
CompressionCodec: defaultCompression,
CompressionLevel: defaultCompressionLevel,
Comment on lines +104 to +105

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these needed in this test? I couldn't find where it would change anything being tested here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this test will fail if not.

}
texp, err := newTracesExporter(c, component.ExporterCreateParams{Logger: zap.NewNop()}, tracesMarshalers())
assert.Error(t, err)
Expand Down