-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[exporter]prometheusremotewrite] Fix data race by introducing pool of…
… batch state (#36601) #### Description This is an alternative for #36524 and #36600 This PR does a couple of things: * Add a test written by @edma2 that shows a data race to the batch state when running multiple consumers. * Add a benchmark for PushMetrics, with options to run with a stable number of metrics or varying metrics. * Fix the data race by introducing a `sync.Pool` of batch states. #### Benchmark results results comparing `main`, #36600 and this PR: ```console arthursens$ benchstat main.txt withoutState.txt syncpool.txt goos: darwin goarch: arm64 pkg: github.com/open-telemetry/opentelemetry-collector-contrib/exporter/prometheusremotewriteexporter cpu: Apple M2 Pro │ main.txt │ withoutState.txt │ syncpool.txt │ │ sec/op │ sec/op vs base │ sec/op vs base │ PushMetricsVaryingMetrics-2 8.066m ± 5% 13.821m ± 9% +71.36% (p=0.002 n=6) 8.316m ± 6% ~ (p=0.065 n=6) │ main.txt │ withoutState.txt │ syncpool.txt │ │ B/op │ B/op vs base │ B/op vs base │ PushMetricsVaryingMetrics-2 5.216Mi ± 0% 34.436Mi ± 0% +560.17% (p=0.002 n=6) 5.548Mi ± 0% +6.36% (p=0.002 n=6) │ main.txt │ withoutState.txt │ syncpool.txt │ │ allocs/op │ allocs/op vs base │ allocs/op vs base │ PushMetricsVaryingMetrics-2 56.02k ± 0% 56.05k ± 0% ~ (p=0.721 n=6) 56.04k ± 0% ~ (p=0.665 n=6) ``` --------- Signed-off-by: Arthur Silva Sens <[email protected]>
- Loading branch information
1 parent
6bc4314
commit 33c5306
Showing
6 changed files
with
272 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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,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: exporter/prometheusremotewrite | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: In preparation to re-introducing multiple workers, we're removing a data-race when batching timeseries. | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [36601] | ||
|
||
# (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] |
This file contains 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
138 changes: 138 additions & 0 deletions
138
exporter/prometheusremotewriteexporter/exporter_concurrency_test.go
This file contains 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,138 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package prometheusremotewriteexporter | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"strconv" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/gogo/protobuf/proto" | ||
"github.com/golang/snappy" | ||
"github.com/prometheus/prometheus/prompb" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/component/componenttest" | ||
"go.opentelemetry.io/collector/config/confighttp" | ||
"go.opentelemetry.io/collector/config/configretry" | ||
"go.opentelemetry.io/collector/config/configtelemetry" | ||
"go.opentelemetry.io/collector/exporter/exportertest" | ||
"go.opentelemetry.io/collector/pdata/pmetric" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/testdata" | ||
) | ||
|
||
// Test everything works when there is more than one goroutine calling PushMetrics. | ||
// Today we only use 1 worker per exporter, but the intention of this test is to future-proof in case it changes. | ||
func Test_PushMetricsConcurrent(t *testing.T) { | ||
n := 1000 | ||
ms := make([]pmetric.Metrics, n) | ||
testIDKey := "test_id" | ||
for i := 0; i < n; i++ { | ||
m := testdata.GenerateMetricsOneMetric() | ||
dps := m.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).Sum().DataPoints() | ||
for j := 0; j < dps.Len(); j++ { | ||
dp := dps.At(j) | ||
dp.Attributes().PutInt(testIDKey, int64(i)) | ||
} | ||
ms[i] = m | ||
} | ||
received := make(map[int]prompb.TimeSeries) | ||
var mu sync.Mutex | ||
|
||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
body, err := io.ReadAll(r.Body) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
assert.NotNil(t, body) | ||
// Receives the http requests and unzip, unmarshalls, and extracts TimeSeries | ||
assert.Equal(t, "0.1.0", r.Header.Get("X-Prometheus-Remote-Write-Version")) | ||
assert.Equal(t, "snappy", r.Header.Get("Content-Encoding")) | ||
var unzipped []byte | ||
|
||
dest, err := snappy.Decode(unzipped, body) | ||
assert.NoError(t, err) | ||
|
||
wr := &prompb.WriteRequest{} | ||
ok := proto.Unmarshal(dest, wr) | ||
assert.NoError(t, ok) | ||
assert.Len(t, wr.Timeseries, 2) | ||
ts := wr.Timeseries[0] | ||
foundLabel := false | ||
for _, label := range ts.Labels { | ||
if label.Name == testIDKey { | ||
id, err := strconv.Atoi(label.Value) | ||
assert.NoError(t, err) | ||
mu.Lock() | ||
_, ok := received[id] | ||
assert.False(t, ok) // fail if we already saw it | ||
received[id] = ts | ||
mu.Unlock() | ||
foundLabel = true | ||
break | ||
} | ||
} | ||
assert.True(t, foundLabel) | ||
w.WriteHeader(http.StatusOK) | ||
})) | ||
|
||
defer server.Close() | ||
|
||
// Adjusted retry settings for faster testing | ||
retrySettings := configretry.BackOffConfig{ | ||
Enabled: true, | ||
InitialInterval: 100 * time.Millisecond, // Shorter initial interval | ||
MaxInterval: 1 * time.Second, // Shorter max interval | ||
MaxElapsedTime: 2 * time.Second, // Shorter max elapsed time | ||
} | ||
clientConfig := confighttp.NewDefaultClientConfig() | ||
clientConfig.Endpoint = server.URL | ||
clientConfig.ReadBufferSize = 0 | ||
clientConfig.WriteBufferSize = 512 * 1024 | ||
cfg := &Config{ | ||
Namespace: "", | ||
ClientConfig: clientConfig, | ||
MaxBatchSizeBytes: 3000000, | ||
RemoteWriteQueue: RemoteWriteQueue{NumConsumers: 1}, | ||
TargetInfo: &TargetInfo{ | ||
Enabled: true, | ||
}, | ||
CreatedMetric: &CreatedMetric{ | ||
Enabled: false, | ||
}, | ||
BackOffConfig: retrySettings, | ||
} | ||
|
||
assert.NotNil(t, cfg) | ||
set := exportertest.NewNopSettings() | ||
set.MetricsLevel = configtelemetry.LevelBasic | ||
|
||
prwe, nErr := newPRWExporter(cfg, set) | ||
|
||
require.NoError(t, nErr) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
require.NoError(t, prwe.Start(ctx, componenttest.NewNopHost())) | ||
defer func() { | ||
require.NoError(t, prwe.Shutdown(ctx)) | ||
}() | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(n) | ||
for _, m := range ms { | ||
go func() { | ||
err := prwe.PushMetrics(ctx, m) | ||
assert.NoError(t, err) | ||
wg.Done() | ||
}() | ||
} | ||
wg.Wait() | ||
assert.Len(t, received, n) | ||
} |
This file contains 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 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 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