-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[remotetapprocessor] use 'time/rate' to limit traffic (#32481)
bug: The remotetapprocessor `limit` configure doesn't work. how to fix: use `time/rate` to limit traffic. Resolves #32385 --------- Co-authored-by: Andrzej Stencel <[email protected]>
- Loading branch information
1 parent
f4a3147
commit 497fed7
Showing
4 changed files
with
223 additions
and
15 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,30 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: breaking | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: remotetapprocessor | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Make the `limit` configuration work properly. | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [32385] | ||
|
||
# (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: | | ||
The `limit` configuration was ignored previously, but now it works according to the configuration and documentation. | ||
Nothing is required of users. | ||
See the remotetapprocessor's `README.md` for details. | ||
# 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: [] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package remotetapprocessor | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/collector/pdata/plog" | ||
"go.opentelemetry.io/collector/pdata/pmetric" | ||
"go.opentelemetry.io/collector/pdata/ptrace" | ||
"go.opentelemetry.io/collector/processor/processortest" | ||
"golang.org/x/time/rate" | ||
) | ||
|
||
func TestConsumeMetrics(t *testing.T) { | ||
metric := pmetric.NewMetrics() | ||
metric.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetName("foo") | ||
|
||
cases := []struct { | ||
name string | ||
limit int | ||
}{ | ||
{name: "limit_0", limit: 0}, | ||
{name: "limit_1", limit: 1}, | ||
{name: "limit_10", limit: 10}, | ||
{name: "limit_50", limit: 50}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
conf := &Config{ | ||
Limit: rate.Limit(c.limit), | ||
} | ||
|
||
processor := newProcessor(processortest.NewNopCreateSettings(), conf) | ||
|
||
ch := make(chan []byte) | ||
idx := processor.cs.add(ch) | ||
receiveNum := 0 | ||
wg := &sync.WaitGroup{} | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
for range ch { | ||
receiveNum++ | ||
} | ||
}() | ||
|
||
for i := 0; i < c.limit*2; i++ { | ||
// send metric to chan c.limit*2 per sec. | ||
metric2, err := processor.ConsumeMetrics(context.Background(), metric) | ||
assert.Nil(t, err) | ||
assert.Equal(t, metric, metric2) | ||
} | ||
|
||
processor.cs.closeAndRemove(idx) | ||
wg.Wait() | ||
assert.Equal(t, receiveNum, c.limit) | ||
|
||
}) | ||
} | ||
} | ||
|
||
func TestConsumeLogs(t *testing.T) { | ||
log := plog.NewLogs() | ||
log.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty().Body().SetStr("foo") | ||
|
||
cases := []struct { | ||
name string | ||
limit int | ||
}{ | ||
{name: "limit_0", limit: 0}, | ||
{name: "limit_1", limit: 1}, | ||
{name: "limit_10", limit: 10}, | ||
{name: "limit_50", limit: 50}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
conf := &Config{ | ||
Limit: rate.Limit(c.limit), | ||
} | ||
|
||
processor := newProcessor(processortest.NewNopCreateSettings(), conf) | ||
|
||
ch := make(chan []byte) | ||
idx := processor.cs.add(ch) | ||
receiveNum := 0 | ||
wg := &sync.WaitGroup{} | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
for range ch { | ||
receiveNum++ | ||
} | ||
}() | ||
|
||
// send log to chan c.limit*2 per sec. | ||
for i := 0; i < c.limit*2; i++ { | ||
log2, err := processor.ConsumeLogs(context.Background(), log) | ||
assert.Nil(t, err) | ||
assert.Equal(t, log, log2) | ||
} | ||
|
||
processor.cs.closeAndRemove(idx) | ||
wg.Wait() | ||
t.Log(receiveNum) | ||
assert.Equal(t, receiveNum, c.limit) | ||
}) | ||
} | ||
} | ||
|
||
func TestConsumeTraces(t *testing.T) { | ||
trace := ptrace.NewTraces() | ||
trace.ResourceSpans().AppendEmpty().ScopeSpans().AppendEmpty().Spans().AppendEmpty().SetName("foo") | ||
|
||
cases := []struct { | ||
name string | ||
limit int | ||
}{ | ||
{name: "limit_0", limit: 0}, | ||
{name: "limit_1", limit: 1}, | ||
{name: "limit_10", limit: 10}, | ||
{name: "limit_50", limit: 50}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
conf := &Config{ | ||
Limit: rate.Limit(c.limit), | ||
} | ||
|
||
processor := newProcessor(processortest.NewNopCreateSettings(), conf) | ||
|
||
ch := make(chan []byte) | ||
idx := processor.cs.add(ch) | ||
receiveNum := 0 | ||
wg := &sync.WaitGroup{} | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
for range ch { | ||
receiveNum++ | ||
} | ||
}() | ||
|
||
for i := 0; i < c.limit*2; i++ { | ||
// send trace to chan c.limit*2 per sec. | ||
trace2, err := processor.ConsumeTraces(context.Background(), trace) | ||
assert.Nil(t, err) | ||
assert.Equal(t, trace, trace2) | ||
} | ||
|
||
processor.cs.closeAndRemove(idx) | ||
wg.Wait() | ||
assert.Equal(t, receiveNum, c.limit) | ||
}) | ||
} | ||
} |
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