Skip to content

Commit

Permalink
[receiver/opencensus] refactor the code for maintenance and generate …
Browse files Browse the repository at this point in the history
…lifecycle tests
  • Loading branch information
atoulme committed Mar 8, 2024
1 parent cccb429 commit 05c88d9
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 211 deletions.
27 changes: 27 additions & 0 deletions .chloggen/opencensusreceiver_lifecycle.yaml
@@ -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: bug_fix

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Refactor the opencensusreceiver to pass lifecycle tests and avoid leaking gRPC connections.

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

# (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: []
16 changes: 2 additions & 14 deletions receiver/opencensusreceiver/factory.go
Expand Up @@ -47,16 +47,10 @@ func createTracesReceiver(
cfg component.Config,
nextConsumer consumer.Traces,
) (receiver.Traces, error) {
var err error
r := receivers.GetOrAdd(cfg, func() component.Component {
rCfg := cfg.(*Config)
var recv *ocReceiver
recv, err = newOpenCensusReceiver(rCfg.NetAddr.Transport, rCfg.NetAddr.Endpoint, nil, nil, set, rCfg.buildOptions()...)
return recv
return newOpenCensusReceiver(rCfg, nil, nil, set, rCfg.buildOptions()...)
})
if err != nil {
return nil, err
}
r.Unwrap().(*ocReceiver).traceConsumer = nextConsumer

return r, nil
Expand All @@ -68,16 +62,10 @@ func createMetricsReceiver(
cfg component.Config,
nextConsumer consumer.Metrics,
) (receiver.Metrics, error) {
var err error
r := receivers.GetOrAdd(cfg, func() component.Component {
rCfg := cfg.(*Config)
var recv *ocReceiver
recv, err = newOpenCensusReceiver(rCfg.NetAddr.Transport, rCfg.NetAddr.Endpoint, nil, nil, set, rCfg.buildOptions()...)
return recv
return newOpenCensusReceiver(rCfg, nil, nil, set, rCfg.buildOptions()...)
})
if err != nil {
return nil, err
}
r.Unwrap().(*ocReceiver).metricsConsumer = nextConsumer

return r, nil
Expand Down
17 changes: 8 additions & 9 deletions receiver/opencensusreceiver/factory_test.go
Expand Up @@ -86,14 +86,12 @@ func TestCreateTracesReceiver(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tr, err := createTracesReceiver(ctx, set, tt.cfg, consumertest.NewNop())
require.NoError(t, err)
err = tr.Start(context.Background(), componenttest.NewNopHost())
if (err != nil) != tt.wantErr {
t.Errorf("factory.CreateTracesReceiver() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tr != nil {
require.NoError(t, tr.Start(context.Background(), componenttest.NewNopHost()))
require.NoError(t, tr.Shutdown(context.Background()))
}
require.NoError(t, tr.Shutdown(context.Background()))
})
}
}
Expand Down Expand Up @@ -152,14 +150,15 @@ func TestCreateMetricsReceiver(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tc, err := createMetricsReceiver(context.Background(), set, tt.cfg, consumertest.NewNop())
require.NoError(t, err)
err = tc.Start(context.Background(), componenttest.NewNopHost())
defer func() {
require.NoError(t, tc.Shutdown(context.Background()))
}()
if (err != nil) != tt.wantErr {
t.Errorf("factory.CreateMetricsReceiver() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tc != nil {
require.NoError(t, tc.Start(context.Background(), componenttest.NewNopHost()))
require.NoError(t, tc.Shutdown(context.Background()))
}
})
}
}
13 changes: 13 additions & 0 deletions receiver/opencensusreceiver/generated_component_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -357,11 +357,8 @@ func ocReceiverOnGRPCServer(t *testing.T, sr consumer.Metrics, set receiver.Crea
ln, err := net.Listen("tcp", "localhost:")
require.NoError(t, err, "Failed to find an available address to run the gRPC server: %v", err)

doneFnList := []func(){func() { ln.Close() }}
done := func() {
for _, doneFn := range doneFnList {
doneFn()
}
_ = ln.Close()
}

oci, err := New(sr, set)
Expand Down
3 changes: 1 addition & 2 deletions receiver/opencensusreceiver/metadata.yaml
Expand Up @@ -15,5 +15,4 @@ status:
codeowners:
active: [open-telemetry/collector-approvers]
tests:
config:
skip_lifecycle: true
config:

0 comments on commit 05c88d9

Please sign in to comment.