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/prometheusremotewrite] Make maxBatchByteSize configurable #23447

20 changes: 20 additions & 0 deletions .chloggen/prometheus-remote-write-max-batch-byte-size.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use this changelog template to create an entry for release notes.
# If your change doesn't affect end users, such as a test fix or a tooling change,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: addition of `max_batch_byte_size` configurable parameter, to allow users to adjust it based on the capabilities of their specific remote storage

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

# (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:
3 changes: 3 additions & 0 deletions exporter/prometheusremotewriteexporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ The following settings can be optionally configured:
- `enabled` (default = false): If `enabled` is `true`, a `_created` metric is
exported for Summary, Histogram, and Monotonic Sum metric points if
`StartTimeUnixNano` is set.
- `max_batch_byte_size` (default = `3000000` -> `~2.861 mb`): Maximum size of a batch of
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: could we use max_batch_size_bytes instead? reference for similar property in an unrelated place https://prometheus.io/docs/prometheus/latest/feature_flags/#extra-scrape-metrics
scrape_body_size_bytes

Usually the suffix denotes the unit.

another option is to use something like https://pkg.go.dev/github.com/docker/go-units#FromHumanSize

go-units is used in this project, but I think it is not worth it.

Otherwise I think this is a reasonable change and LGTM.

Copy link
Contributor

Choose a reason for hiding this comment

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

ping @yotamloe

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rapphil Sorry for the delay just refactored to max_batch_size_bytes

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you!

samples to be sent to the remote write endpoint. If the batch size is larger
than this value, it will be split into multiple batches.

Example:

Expand Down
11 changes: 11 additions & 0 deletions exporter/prometheusremotewriteexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type Config struct {

HTTPClientSettings confighttp.HTTPClientSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct.

// maximum size in bytes of time series batch sent to remote storage
MaxBatchByteSize int `mapstructure:"max_batch_byte_size"`

// ResourceToTelemetrySettings is the option for converting resource attributes to telemetry attributes.
// "Enabled" - A boolean field to enable/disable this option. Default is `false`.
// If enabled, all the resource attributes will be converted to metric labels by default.
Expand Down Expand Up @@ -97,5 +100,13 @@ func (cfg *Config) Validate() error {
Enabled: false,
}
}
if cfg.MaxBatchByteSize < 0 {
return fmt.Errorf("max_batch_byte_size must be greater than 0")
}
if cfg.MaxBatchByteSize == 0 {
// Defaults to ~2.81MB
cfg.MaxBatchByteSize = 3000000
}

return nil
}
3 changes: 2 additions & 1 deletion exporter/prometheusremotewriteexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func TestLoadConfig(t *testing.T) {
{
id: component.NewIDWithName(metadata.Type, "2"),
expected: &Config{
TimeoutSettings: exporterhelper.NewDefaultTimeoutSettings(),
MaxBatchByteSize: 3000000,
TimeoutSettings: exporterhelper.NewDefaultTimeoutSettings(),
RetrySettings: exporterhelper.RetrySettings{
Enabled: true,
InitialInterval: 10 * time.Second,
Expand Down
36 changes: 18 additions & 18 deletions exporter/prometheusremotewriteexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,17 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/prometheusremotewrite"
)

const maxBatchByteSize = 3000000

// prwExporter converts OTLP metrics to Prometheus remote write TimeSeries and sends them to a remote endpoint.
type prwExporter struct {
endpointURL *url.URL
client *http.Client
wg *sync.WaitGroup
closeChan chan struct{}
concurrency int
userAgentHeader string
clientSettings *confighttp.HTTPClientSettings
settings component.TelemetrySettings
endpointURL *url.URL
client *http.Client
wg *sync.WaitGroup
closeChan chan struct{}
concurrency int
userAgentHeader string
maxBatchByteSize int
clientSettings *confighttp.HTTPClientSettings
settings component.TelemetrySettings

wal *prweWAL
exporterSettings prometheusremotewrite.Settings
Expand All @@ -61,13 +60,14 @@ func newPRWExporter(cfg *Config, set exporter.CreateSettings) (*prwExporter, err
userAgentHeader := fmt.Sprintf("%s/%s", strings.ReplaceAll(strings.ToLower(set.BuildInfo.Description), " ", "-"), set.BuildInfo.Version)

prwe := &prwExporter{
endpointURL: endpointURL,
wg: new(sync.WaitGroup),
closeChan: make(chan struct{}),
userAgentHeader: userAgentHeader,
concurrency: cfg.RemoteWriteQueue.NumConsumers,
clientSettings: &cfg.HTTPClientSettings,
settings: set.TelemetrySettings,
endpointURL: endpointURL,
wg: new(sync.WaitGroup),
closeChan: make(chan struct{}),
userAgentHeader: userAgentHeader,
maxBatchByteSize: cfg.MaxBatchByteSize,
concurrency: cfg.RemoteWriteQueue.NumConsumers,
clientSettings: &cfg.HTTPClientSettings,
settings: set.TelemetrySettings,
exporterSettings: prometheusremotewrite.Settings{
Namespace: cfg.Namespace,
ExternalLabels: sanitizedLabels,
Expand Down Expand Up @@ -154,7 +154,7 @@ func (prwe *prwExporter) handleExport(ctx context.Context, tsMap map[string]*pro
}

// Calls the helper function to convert and batch the TsMap to the desired format
requests, err := batchTimeSeries(tsMap, maxBatchByteSize)
requests, err := batchTimeSeries(tsMap, prwe.maxBatchByteSize)
if err != nil {
return err
}
Expand Down
10 changes: 6 additions & 4 deletions exporter/prometheusremotewriteexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,11 @@ func Test_NewPRWExporter(t *testing.T) {
// Test_Start checks if the client is properly created as expected.
func Test_Start(t *testing.T) {
cfg := &Config{
TimeoutSettings: exporterhelper.TimeoutSettings{},
RetrySettings: exporterhelper.RetrySettings{},
Namespace: "",
ExternalLabels: map[string]string{},
TimeoutSettings: exporterhelper.TimeoutSettings{},
RetrySettings: exporterhelper.RetrySettings{},
MaxBatchByteSize: 3000000,
Namespace: "",
ExternalLabels: map[string]string{},
TargetInfo: &TargetInfo{
Enabled: true,
},
Expand Down Expand Up @@ -675,6 +676,7 @@ func Test_PushMetrics(t *testing.T) {
ReadBufferSize: 0,
WriteBufferSize: 512 * 1024,
},
MaxBatchByteSize: 3000000,
RemoteWriteQueue: RemoteWriteQueue{NumConsumers: 1},
TargetInfo: &TargetInfo{
Enabled: true,
Expand Down
7 changes: 4 additions & 3 deletions exporter/prometheusremotewriteexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ func createMetricsExporter(ctx context.Context, set exporter.CreateSettings,

func createDefaultConfig() component.Config {
return &Config{
Namespace: "",
ExternalLabels: map[string]string{},
TimeoutSettings: exporterhelper.NewDefaultTimeoutSettings(),
Namespace: "",
ExternalLabels: map[string]string{},
MaxBatchByteSize: 3000000,
TimeoutSettings: exporterhelper.NewDefaultTimeoutSettings(),
RetrySettings: exporterhelper.RetrySettings{
Enabled: true,
InitialInterval: 50 * time.Millisecond,
Expand Down