Skip to content

Commit

Permalink
[receiver/pulsar] do not expose method (#27029)
Browse files Browse the repository at this point in the history
**Description:** 
Do not export functions `WithLogsUnmarshalers`,
`WithMetricsUnmarshalers`, `WithTracesUnmarshalers`, add tests for that
and pass checkapi

**Link to tracking Issue:**

#26304

**Testing:** 
go run cmd/checkapi/main.go .
make chlog-validate
go test for pulsarreceiver

**Documentation:**
  • Loading branch information
sakulali committed Oct 4, 2023
1 parent ed786a4 commit 64ae47c
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 7 deletions.
27 changes: 27 additions & 0 deletions .chloggen/pulsarreceiver-checkapi-26304.yaml
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: breaking

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: pulsarreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Do not export the functions `WithLogsUnmarshalers`, `WithMetricsUnmarshalers`, `WithTracesUnmarshalers`, add tests and pass checkapi.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [26304]

# (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: [api]
1 change: 0 additions & 1 deletion cmd/checkapi/allowlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ receiver/dockerstatsreceiver
receiver/journaldreceiver
receiver/kafkareceiver
receiver/podmanreceiver
receiver/pulsarreceiver
receiver/windowseventlogreceiver
12 changes: 6 additions & 6 deletions receiver/pulsarreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,26 @@ const (
// FactoryOption applies changes to PulsarExporterFactory.
type FactoryOption func(factory *pulsarReceiverFactory)

// WithTracesUnmarshalers adds Unmarshalers.
func WithTracesUnmarshalers(tracesUnmarshalers ...TracesUnmarshaler) FactoryOption {
// withTracesUnmarshalers adds Unmarshalers.
func withTracesUnmarshalers(tracesUnmarshalers ...TracesUnmarshaler) FactoryOption {
return func(factory *pulsarReceiverFactory) {
for _, unmarshaler := range tracesUnmarshalers {
factory.tracesUnmarshalers[unmarshaler.Encoding()] = unmarshaler
}
}
}

// WithMetricsUnmarshalers adds MetricsUnmarshalers.
func WithMetricsUnmarshalers(metricsUnmarshalers ...MetricsUnmarshaler) FactoryOption {
// withMetricsUnmarshalers adds MetricsUnmarshalers.
func withMetricsUnmarshalers(metricsUnmarshalers ...MetricsUnmarshaler) FactoryOption {
return func(factory *pulsarReceiverFactory) {
for _, unmarshaler := range metricsUnmarshalers {
factory.metricsUnmarshalers[unmarshaler.Encoding()] = unmarshaler
}
}
}

// WithLogsUnmarshalers adds LogsUnmarshalers.
func WithLogsUnmarshalers(logsUnmarshalers ...LogsUnmarshaler) FactoryOption {
// withLogsUnmarshalers adds LogsUnmarshalers.
func withLogsUnmarshalers(logsUnmarshalers ...LogsUnmarshaler) FactoryOption {
return func(factory *pulsarReceiverFactory) {
for _, unmarshaler := range logsUnmarshalers {
factory.logsUnmarshalers[unmarshaler.Encoding()] = unmarshaler
Expand Down
93 changes: 93 additions & 0 deletions receiver/pulsarreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.opentelemetry.io/collector/receiver/receivertest"
)

Expand Down Expand Up @@ -53,6 +56,25 @@ func Test_CreateTraceReceiver(t *testing.T) {
assert.NotNil(t, recv)
}

func TestWithTracesUnmarshalers(t *testing.T) {
unmarshaler := &customTracesUnmarshaler{}
f := NewFactory(withTracesUnmarshalers(unmarshaler))
cfg := createDefaultConfig().(*Config)

t.Run("custom_encoding", func(t *testing.T) {
cfg.Encoding = unmarshaler.Encoding()
receiver, err := f.CreateTracesReceiver(context.Background(), receivertest.NewNopCreateSettings(), cfg, nil)
require.NoError(t, err)
require.NotNil(t, receiver)
})
t.Run("default_encoding", func(t *testing.T) {
cfg.Encoding = defaultEncoding
receiver, err := f.CreateTracesReceiver(context.Background(), receivertest.NewNopCreateSettings(), cfg, nil)
require.NoError(t, err)
assert.NotNil(t, receiver)
})
}

// metrics
func TestCreateMetricsReceiver_err_addr(t *testing.T) {
cfg := createDefaultConfig().(*Config)
Expand Down Expand Up @@ -83,6 +105,25 @@ func Test_CreateMetricsReceiver(t *testing.T) {
assert.NotNil(t, recv)
}

func TestWithMetricsUnmarshalers(t *testing.T) {
unmarshaler := &customMetricsUnmarshaler{}
f := NewFactory(withMetricsUnmarshalers(unmarshaler))
cfg := createDefaultConfig().(*Config)

t.Run("custom_encoding", func(t *testing.T) {
cfg.Encoding = unmarshaler.Encoding()
receiver, err := f.CreateMetricsReceiver(context.Background(), receivertest.NewNopCreateSettings(), cfg, nil)
require.NoError(t, err)
require.NotNil(t, receiver)
})
t.Run("default_encoding", func(t *testing.T) {
cfg.Encoding = defaultEncoding
receiver, err := f.CreateMetricsReceiver(context.Background(), receivertest.NewNopCreateSettings(), cfg, nil)
require.NoError(t, err)
assert.NotNil(t, receiver)
})
}

// logs
func TestCreateLogsReceiver_err_addr(t *testing.T) {
cfg := createDefaultConfig().(*Config)
Expand Down Expand Up @@ -113,3 +154,55 @@ func Test_CreateLogsReceiver(t *testing.T) {
require.NoError(t, err)
assert.NotNil(t, recv)
}

func TestWithLogsUnmarshalers(t *testing.T) {
unmarshaler := &customLogsUnmarshaler{}
f := NewFactory(withLogsUnmarshalers(unmarshaler))
cfg := createDefaultConfig().(*Config)

t.Run("custom_encoding", func(t *testing.T) {
cfg.Encoding = unmarshaler.Encoding()
exporter, err := f.CreateLogsReceiver(context.Background(), receivertest.NewNopCreateSettings(), cfg, nil)
require.NoError(t, err)
require.NotNil(t, exporter)
})
t.Run("default_encoding", func(t *testing.T) {
cfg.Encoding = defaultEncoding
exporter, err := f.CreateLogsReceiver(context.Background(), receivertest.NewNopCreateSettings(), cfg, nil)
require.NoError(t, err)
assert.NotNil(t, exporter)
})
}

type customTracesUnmarshaler struct {
}

type customMetricsUnmarshaler struct {
}

type customLogsUnmarshaler struct {
}

func (c customTracesUnmarshaler) Unmarshal([]byte) (ptrace.Traces, error) {
panic("implement me")
}

func (c customTracesUnmarshaler) Encoding() string {
return "custom"
}

func (c customMetricsUnmarshaler) Unmarshal([]byte) (pmetric.Metrics, error) {
panic("implement me")
}

func (c customMetricsUnmarshaler) Encoding() string {
return "custom"
}

func (c customLogsUnmarshaler) Unmarshal([]byte) (plog.Logs, error) {
panic("implement me")
}

func (c customLogsUnmarshaler) Encoding() string {
return "custom"
}

0 comments on commit 64ae47c

Please sign in to comment.