-
Notifications
You must be signed in to change notification settings - Fork 3.1k
[chore] prom rw v2 exporter add support for batching #40051
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
Merged
andrzej-stencel
merged 17 commits into
open-telemetry:main
from
jmichalek132:jm-prom-rw-exporter-add-support-for-batching
Jun 9, 2025
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a5c993f
chore: prom rw v2 exporter add support for batching
jmichalek132 db78ecb
Merge remote-tracking branch 'origin/main' into jm-prom-rw-exporter-a…
jmichalek132 afa216b
chore: finished backport of changes from main
jmichalek132 d98789e
chore: added some TODOs
jmichalek132 89d00ba
Update exporter/prometheusremotewriteexporter/helper_v2.go
jmichalek132 81a1382
Update exporter/prometheusremotewriteexporter/helper_v2.go
jmichalek132 60415e6
Merge branch 'main' into jm-prom-rw-exporter-add-support-for-batching
jmichalek132 bcbd59d
chore: fix revert changes from review
jmichalek132 b3340c7
chore: remove old comment]
jmichalek132 16afdc3
feat: take symbolTable size into account
jmichalek132 a1e59dd
chore: added tests
jmichalek132 bfa3ba6
Merge branch 'main' into jm-prom-rw-exporter-add-support-for-batching
jmichalek132 1d7bb6a
chore: ran make generate
jmichalek132 7335264
chore: trigger tests again
jmichalek132 4395d97
Merge branch 'main' into jm-prom-rw-exporter-add-support-for-batching
jmichalek132 fe8fcf1
Merge branch 'main' into jm-prom-rw-exporter-add-support-for-batching
jmichalek132 efc9244
Merge branch 'main' into jm-prom-rw-exporter-add-support-for-batching
jmichalek132 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package prometheusremotewriteexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/prometheusremotewriteexporter" | ||
|
|
||
| import ( | ||
| "errors" | ||
| "sort" | ||
|
|
||
| writev2 "github.com/prometheus/prometheus/prompb/io/prometheus/write/v2" | ||
| ) | ||
|
|
||
| func batchTimeSeriesV2(tsMap map[string]*writev2.TimeSeries, symbolsTable writev2.SymbolsTable, maxBatchByteSize int, state *batchTimeSeriesState) ([]*writev2.Request, error) { | ||
| if len(tsMap) == 0 { | ||
| return nil, errors.New("invalid tsMap: cannot be empty map") | ||
| } | ||
|
|
||
| requests := make([]*writev2.Request, 0, max(10, state.nextRequestBufferSize)) | ||
| tsArray := make([]writev2.TimeSeries, 0, min(state.nextTimeSeriesBufferSize, len(tsMap))) | ||
jmichalek132 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Calculate symbols table size once since it's shared across batches | ||
| symbolsSize := 0 | ||
| for _, symbol := range symbolsTable.Symbols() { | ||
| symbolsSize += len(symbol) | ||
| } | ||
|
|
||
| sizeOfCurrentBatch := symbolsSize // Initialize with symbols table size | ||
| i := 0 | ||
|
|
||
| for _, v := range tsMap { | ||
| sizeOfSeries := v.Size() | ||
|
|
||
| if sizeOfCurrentBatch+sizeOfSeries >= maxBatchByteSize { | ||
jmichalek132 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| state.nextTimeSeriesBufferSize = max(10, 2*len(tsArray)) | ||
| wrapped := convertTimeseriesToRequestV2(tsArray, symbolsTable) | ||
| requests = append(requests, wrapped) | ||
|
|
||
| tsArray = make([]writev2.TimeSeries, 0, min(state.nextTimeSeriesBufferSize, len(tsMap)-i)) | ||
| sizeOfCurrentBatch = symbolsSize // Reset to symbols table size for new batch | ||
| } | ||
|
|
||
| tsArray = append(tsArray, *v) | ||
| sizeOfCurrentBatch += sizeOfSeries | ||
| i++ | ||
jmichalek132 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if len(tsArray) != 0 { | ||
| // TODO only sent necessary part of the symbolsTable | ||
| wrapped := convertTimeseriesToRequestV2(tsArray, symbolsTable) | ||
| requests = append(requests, wrapped) | ||
| } | ||
|
|
||
| state.nextRequestBufferSize = 2 * len(requests) | ||
| return requests, nil | ||
| } | ||
|
|
||
| func convertTimeseriesToRequestV2(tsArray []writev2.TimeSeries, symbolsTable writev2.SymbolsTable) *writev2.Request { | ||
| return &writev2.Request{ | ||
| // Prometheus requires time series to be sorted by Timestamp to avoid out of order problems. | ||
| // See: | ||
| // * https://github.com/open-telemetry/wg-prometheus/issues/10 | ||
| // * https://github.com/open-telemetry/opentelemetry-collector/issues/2315 | ||
| // TODO: try to sort while batching? | ||
jmichalek132 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Timeseries: orderBySampleTimestampV2(tsArray), | ||
jmichalek132 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Symbols: symbolsTable.Symbols(), | ||
| } | ||
| } | ||
|
|
||
| func orderBySampleTimestampV2(tsArray []writev2.TimeSeries) []writev2.TimeSeries { | ||
| for i := range tsArray { | ||
| sL := tsArray[i].Samples | ||
| sort.Slice(sL, func(i, j int) bool { | ||
| return sL[i].Timestamp < sL[j].Timestamp | ||
| }) | ||
| } | ||
| return tsArray | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.