forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added metric and log support (open-telemetry#30019)
This is the 3rd PR for the failover connector. This PR adds support for metric and log pipelines Link to tracking Issue: open-telemetry#20766 cc: @djaglowski @fatsheep9146
- Loading branch information
Showing
5 changed files
with
504 additions
and
17 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: new_component | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: failoverconnector | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: PR extends failover connector for metric and log pipelines | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [20766] | ||
|
||
# (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: [] |
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,192 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package failoverconnector // import "github.com/open-telemetry/opentelemetry-collector-contrib/connector/failoverconnector" | ||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/connector/connectortest" | ||
"go.opentelemetry.io/collector/consumer" | ||
"go.opentelemetry.io/collector/consumer/consumertest" | ||
"go.opentelemetry.io/collector/pdata/plog" | ||
) | ||
|
||
var errLogsConsumer = errors.New("Error from ConsumeLogs") | ||
|
||
func TestLogsRegisterConsumers(t *testing.T) { | ||
var sinkFirst, sinkSecond, sinkThird consumertest.LogsSink | ||
logsFirst := component.NewIDWithName(component.DataTypeLogs, "logs/first") | ||
logsSecond := component.NewIDWithName(component.DataTypeLogs, "logs/second") | ||
logsThird := component.NewIDWithName(component.DataTypeLogs, "logs/third") | ||
|
||
cfg := &Config{ | ||
PipelinePriority: [][]component.ID{{logsFirst}, {logsSecond}, {logsThird}}, | ||
RetryInterval: 5 * time.Minute, | ||
RetryGap: 10 * time.Second, | ||
MaxRetries: 5, | ||
} | ||
|
||
router := connectortest.NewLogsRouter( | ||
connectortest.WithLogsSink(logsFirst, &sinkFirst), | ||
connectortest.WithLogsSink(logsSecond, &sinkSecond), | ||
connectortest.WithLogsSink(logsThird, &sinkThird), | ||
) | ||
|
||
conn, err := NewFactory().CreateLogsToLogs(context.Background(), | ||
connectortest.NewNopCreateSettings(), cfg, router.(consumer.Logs)) | ||
|
||
failoverConnector := conn.(*logsFailover) | ||
defer func() { | ||
assert.NoError(t, failoverConnector.Shutdown(context.Background())) | ||
}() | ||
|
||
require.NoError(t, err) | ||
require.NotNil(t, conn) | ||
|
||
tc, idx, ok := failoverConnector.failover.getCurrentConsumer() | ||
tc1 := failoverConnector.failover.GetConsumerAtIndex(1) | ||
tc2 := failoverConnector.failover.GetConsumerAtIndex(2) | ||
|
||
assert.True(t, ok) | ||
require.Equal(t, idx, 0) | ||
require.Implements(t, (*consumer.Logs)(nil), tc) | ||
require.Implements(t, (*consumer.Logs)(nil), tc1) | ||
require.Implements(t, (*consumer.Logs)(nil), tc2) | ||
} | ||
|
||
func TestLogsWithValidFailover(t *testing.T) { | ||
var sinkSecond, sinkThird consumertest.LogsSink | ||
logsFirst := component.NewIDWithName(component.DataTypeLogs, "logs/first") | ||
logsSecond := component.NewIDWithName(component.DataTypeLogs, "logs/second") | ||
logsThird := component.NewIDWithName(component.DataTypeLogs, "logs/third") | ||
|
||
cfg := &Config{ | ||
PipelinePriority: [][]component.ID{{logsFirst}, {logsSecond}, {logsThird}}, | ||
RetryInterval: 5 * time.Minute, | ||
RetryGap: 10 * time.Second, | ||
MaxRetries: 5, | ||
} | ||
|
||
router := connectortest.NewLogsRouter( | ||
connectortest.WithNopLogs(logsFirst), | ||
connectortest.WithLogsSink(logsSecond, &sinkSecond), | ||
connectortest.WithLogsSink(logsThird, &sinkThird), | ||
) | ||
|
||
conn, err := NewFactory().CreateLogsToLogs(context.Background(), | ||
connectortest.NewNopCreateSettings(), cfg, router.(consumer.Logs)) | ||
|
||
require.NoError(t, err) | ||
|
||
failoverConnector := conn.(*logsFailover) | ||
failoverConnector.failover.ModifyConsumerAtIndex(0, consumertest.NewErr(errLogsConsumer)) | ||
defer func() { | ||
assert.NoError(t, failoverConnector.Shutdown(context.Background())) | ||
}() | ||
|
||
tr := sampleLog() | ||
|
||
require.NoError(t, conn.ConsumeLogs(context.Background(), tr)) | ||
_, idx, ok := failoverConnector.failover.getCurrentConsumer() | ||
assert.True(t, ok) | ||
require.Equal(t, idx, 1) | ||
} | ||
|
||
func TestLogsWithFailoverError(t *testing.T) { | ||
var sinkSecond, sinkThird consumertest.LogsSink | ||
logsFirst := component.NewIDWithName(component.DataTypeLogs, "logs/first") | ||
logsSecond := component.NewIDWithName(component.DataTypeLogs, "logs/second") | ||
logsThird := component.NewIDWithName(component.DataTypeLogs, "logs/third") | ||
|
||
cfg := &Config{ | ||
PipelinePriority: [][]component.ID{{logsFirst}, {logsSecond}, {logsThird}}, | ||
RetryInterval: 5 * time.Minute, | ||
RetryGap: 10 * time.Second, | ||
MaxRetries: 5, | ||
} | ||
|
||
router := connectortest.NewLogsRouter( | ||
connectortest.WithNopLogs(logsFirst), | ||
connectortest.WithLogsSink(logsSecond, &sinkSecond), | ||
connectortest.WithLogsSink(logsThird, &sinkThird), | ||
) | ||
|
||
conn, err := NewFactory().CreateLogsToLogs(context.Background(), | ||
connectortest.NewNopCreateSettings(), cfg, router.(consumer.Logs)) | ||
|
||
require.NoError(t, err) | ||
|
||
failoverConnector := conn.(*logsFailover) | ||
failoverConnector.failover.ModifyConsumerAtIndex(0, consumertest.NewErr(errLogsConsumer)) | ||
failoverConnector.failover.ModifyConsumerAtIndex(1, consumertest.NewErr(errLogsConsumer)) | ||
failoverConnector.failover.ModifyConsumerAtIndex(2, consumertest.NewErr(errLogsConsumer)) | ||
defer func() { | ||
assert.NoError(t, failoverConnector.Shutdown(context.Background())) | ||
}() | ||
|
||
tr := sampleLog() | ||
|
||
assert.EqualError(t, conn.ConsumeLogs(context.Background(), tr), "All provided pipelines return errors") | ||
} | ||
|
||
func TestLogsWithFailoverRecovery(t *testing.T) { | ||
var sinkSecond, sinkThird consumertest.LogsSink | ||
logsFirst := component.NewIDWithName(component.DataTypeLogs, "logs/first") | ||
logsSecond := component.NewIDWithName(component.DataTypeLogs, "logs/second") | ||
logsThird := component.NewIDWithName(component.DataTypeLogs, "logs/third") | ||
|
||
cfg := &Config{ | ||
PipelinePriority: [][]component.ID{{logsFirst}, {logsSecond}, {logsThird}}, | ||
RetryInterval: 50 * time.Millisecond, | ||
RetryGap: 10 * time.Millisecond, | ||
MaxRetries: 1000, | ||
} | ||
|
||
router := connectortest.NewLogsRouter( | ||
connectortest.WithNopLogs(logsFirst), | ||
connectortest.WithLogsSink(logsSecond, &sinkSecond), | ||
connectortest.WithLogsSink(logsThird, &sinkThird), | ||
) | ||
|
||
conn, err := NewFactory().CreateLogsToLogs(context.Background(), | ||
connectortest.NewNopCreateSettings(), cfg, router.(consumer.Logs)) | ||
|
||
require.NoError(t, err) | ||
|
||
failoverConnector := conn.(*logsFailover) | ||
failoverConnector.failover.ModifyConsumerAtIndex(0, consumertest.NewErr(errLogsConsumer)) | ||
defer func() { | ||
assert.NoError(t, failoverConnector.Shutdown(context.Background())) | ||
}() | ||
|
||
tr := sampleLog() | ||
|
||
require.NoError(t, conn.ConsumeLogs(context.Background(), tr)) | ||
_, idx, ok := failoverConnector.failover.getCurrentConsumer() | ||
|
||
assert.True(t, ok) | ||
require.Equal(t, idx, 1) | ||
|
||
// Simulate recovery of exporter | ||
failoverConnector.failover.ModifyConsumerAtIndex(0, consumertest.NewNop()) | ||
|
||
time.Sleep(100 * time.Millisecond) | ||
|
||
_, idx, ok = failoverConnector.failover.getCurrentConsumer() | ||
assert.True(t, ok) | ||
require.Equal(t, idx, 0) | ||
} | ||
|
||
func sampleLog() plog.Logs { | ||
l := plog.NewLogs() | ||
rl := l.ResourceLogs().AppendEmpty() | ||
rl.Resource().Attributes().PutStr("test", "logs-test") | ||
rl.ScopeLogs().AppendEmpty().LogRecords().AppendEmpty() | ||
return l | ||
} |
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
Oops, something went wrong.