Skip to content

Commit

Permalink
Add connector/connectortest package
Browse files Browse the repository at this point in the history
  • Loading branch information
djaglowski committed Dec 8, 2022
1 parent 2016043 commit d7f6b56
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .chloggen/connectortest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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. otlpreceiver)
component: connectortest

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add connector/connectortest package

# One or more tracking issues or pull requests related to the change
issues: [6711]

# (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:
102 changes: 102 additions & 0 deletions connector/connectortest/connector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package componenttest // import "go.opentelemetry.io/collector/connector/connectortest"

import (
"context"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/connector"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumertest"
)

// NewNopCreateSettings returns a new nop settings for Create* functions.
func NewNopCreateSettings() connector.CreateSettings {
return connector.CreateSettings{
TelemetrySettings: componenttest.NewNopTelemetrySettings(),
BuildInfo: component.NewDefaultBuildInfo(),
}
}

type nopConfig struct {
config.ConnectorSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
}

// NewNopFactory returns a connector.Factory that constructs nop processors.
func NewNopFactory() connector.Factory {
return connector.NewFactory(
"nop",
func() component.Config {
return &nopConfig{
ConnectorSettings: config.NewConnectorSettings(component.NewID("nop")),
}
},
connector.WithTracesToTraces(createTracesToTracesConnector, component.StabilityLevelDevelopment),
connector.WithTracesToMetrics(createTracesToMetricsConnector, component.StabilityLevelDevelopment),
connector.WithTracesToLogs(createTracesToLogsConnector, component.StabilityLevelDevelopment),
connector.WithMetricsToTraces(createMetricsToTracesConnector, component.StabilityLevelDevelopment),
connector.WithMetricsToMetrics(createMetricsToMetricsConnector, component.StabilityLevelDevelopment),
connector.WithMetricsToLogs(createMetricsToLogsConnector, component.StabilityLevelDevelopment),
connector.WithLogsToTraces(createLogsToTracesConnector, component.StabilityLevelDevelopment),
connector.WithLogsToMetrics(createLogsToMetricsConnector, component.StabilityLevelDevelopment),
connector.WithLogsToLogs(createLogsToLogsConnector, component.StabilityLevelDevelopment),
)
}

func createTracesToTracesConnector(context.Context, connector.CreateSettings, component.Config, consumer.Traces) (connector.Traces, error) {
return &nopConnector{Consumer: consumertest.NewNop()}, nil
}

func createTracesToMetricsConnector(context.Context, connector.CreateSettings, component.Config, consumer.Metrics) (connector.Traces, error) {
return &nopConnector{Consumer: consumertest.NewNop()}, nil
}

func createTracesToLogsConnector(context.Context, connector.CreateSettings, component.Config, consumer.Logs) (connector.Traces, error) {
return &nopConnector{Consumer: consumertest.NewNop()}, nil
}

func createMetricsToTracesConnector(context.Context, connector.CreateSettings, component.Config, consumer.Traces) (connector.Metrics, error) {
return &nopConnector{Consumer: consumertest.NewNop()}, nil
}

func createMetricsToMetricsConnector(context.Context, connector.CreateSettings, component.Config, consumer.Metrics) (connector.Metrics, error) {
return &nopConnector{Consumer: consumertest.NewNop()}, nil
}

func createMetricsToLogsConnector(context.Context, connector.CreateSettings, component.Config, consumer.Logs) (connector.Metrics, error) {
return &nopConnector{Consumer: consumertest.NewNop()}, nil
}

func createLogsToTracesConnector(context.Context, connector.CreateSettings, component.Config, consumer.Traces) (connector.Logs, error) {
return &nopConnector{Consumer: consumertest.NewNop()}, nil
}

func createLogsToMetricsConnector(context.Context, connector.CreateSettings, component.Config, consumer.Metrics) (connector.Logs, error) {
return &nopConnector{Consumer: consumertest.NewNop()}, nil
}

func createLogsToLogsConnector(context.Context, connector.CreateSettings, component.Config, consumer.Logs) (connector.Logs, error) {
return &nopConnector{Consumer: consumertest.NewNop()}, nil
}

// nopConnector stores consumed traces and metrics for testing purposes.
type nopConnector struct {
component.StartFunc
component.ShutdownFunc
consumertest.Consumer
}
93 changes: 93 additions & 0 deletions connector/connectortest/connector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package componenttest

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
)

func TestNewNopConnectorFactory(t *testing.T) {
factory := NewNopFactory()
require.NotNil(t, factory)
assert.Equal(t, component.Type("nop"), factory.Type())
cfg := factory.CreateDefaultConfig()
assert.Equal(t, &nopConfig{ConnectorSettings: config.NewConnectorSettings(component.NewID("nop"))}, cfg)

tracesToTraces, err := factory.CreateTracesToTraces(context.Background(), NewNopCreateSettings(), cfg, consumertest.NewNop())
require.NoError(t, err)
assert.NoError(t, tracesToTraces.Start(context.Background(), componenttest.NewNopHost()))
assert.NoError(t, tracesToTraces.ConsumeTraces(context.Background(), ptrace.NewTraces()))
assert.NoError(t, tracesToTraces.Shutdown(context.Background()))

tracesToMetrics, err := factory.CreateTracesToMetrics(context.Background(), NewNopCreateSettings(), cfg, consumertest.NewNop())
require.NoError(t, err)
assert.NoError(t, tracesToMetrics.Start(context.Background(), componenttest.NewNopHost()))
assert.NoError(t, tracesToMetrics.ConsumeTraces(context.Background(), ptrace.NewTraces()))
assert.NoError(t, tracesToMetrics.Shutdown(context.Background()))

tracesToLogs, err := factory.CreateTracesToLogs(context.Background(), NewNopCreateSettings(), cfg, consumertest.NewNop())
require.NoError(t, err)
assert.NoError(t, tracesToLogs.Start(context.Background(), componenttest.NewNopHost()))
assert.NoError(t, tracesToLogs.ConsumeTraces(context.Background(), ptrace.NewTraces()))
assert.NoError(t, tracesToLogs.Shutdown(context.Background()))

metricsToTraces, err := factory.CreateMetricsToTraces(context.Background(), NewNopCreateSettings(), cfg, consumertest.NewNop())
require.NoError(t, err)
assert.NoError(t, metricsToTraces.Start(context.Background(), componenttest.NewNopHost()))
assert.NoError(t, metricsToTraces.ConsumeMetrics(context.Background(), pmetric.NewMetrics()))
assert.NoError(t, metricsToTraces.Shutdown(context.Background()))

metricsToMetrics, err := factory.CreateMetricsToMetrics(context.Background(), NewNopCreateSettings(), cfg, consumertest.NewNop())
require.NoError(t, err)
assert.NoError(t, metricsToMetrics.Start(context.Background(), componenttest.NewNopHost()))
assert.NoError(t, metricsToMetrics.ConsumeMetrics(context.Background(), pmetric.NewMetrics()))
assert.NoError(t, metricsToMetrics.Shutdown(context.Background()))

metricsToLogs, err := factory.CreateMetricsToLogs(context.Background(), NewNopCreateSettings(), cfg, consumertest.NewNop())
require.NoError(t, err)
assert.NoError(t, metricsToLogs.Start(context.Background(), componenttest.NewNopHost()))
assert.NoError(t, metricsToLogs.ConsumeMetrics(context.Background(), pmetric.NewMetrics()))
assert.NoError(t, metricsToLogs.Shutdown(context.Background()))

logsToTraces, err := factory.CreateLogsToTraces(context.Background(), NewNopCreateSettings(), cfg, consumertest.NewNop())
require.NoError(t, err)
assert.NoError(t, logsToTraces.Start(context.Background(), componenttest.NewNopHost()))
assert.NoError(t, logsToTraces.ConsumeLogs(context.Background(), plog.NewLogs()))
assert.NoError(t, logsToTraces.Shutdown(context.Background()))

logsToMetrics, err := factory.CreateLogsToMetrics(context.Background(), NewNopCreateSettings(), cfg, consumertest.NewNop())
require.NoError(t, err)
assert.NoError(t, logsToMetrics.Start(context.Background(), componenttest.NewNopHost()))
assert.NoError(t, logsToMetrics.ConsumeLogs(context.Background(), plog.NewLogs()))
assert.NoError(t, logsToMetrics.Shutdown(context.Background()))

logsToLogs, err := factory.CreateLogsToLogs(context.Background(), NewNopCreateSettings(), cfg, consumertest.NewNop())
require.NoError(t, err)
assert.NoError(t, logsToLogs.Start(context.Background(), componenttest.NewNopHost()))
assert.NoError(t, logsToLogs.ConsumeLogs(context.Background(), plog.NewLogs()))
assert.NoError(t, logsToLogs.Shutdown(context.Background()))
}

0 comments on commit d7f6b56

Please sign in to comment.