forked from open-telemetry/opentelemetry-lambda
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NH-76996 Otelcol: collecting Lambda function logs (code refactoring) #21
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3734782
Initial
jerrytfleung 27c4595
nits
jerrytfleung bd84eff
updated starttimestamp
jerrytfleung f124ae1
Changed to use subscribe
jerrytfleung 7063c3c
backup
jerrytfleung fe5cca9
indexing
jerrytfleung 65aa3cb
Removed metrics
jerrytfleung 4b64bb5
Polish
jerrytfleung 7c86ac6
nits
jerrytfleung b7e4fee
Added critical
jerrytfleung 33d8ce2
Added ALL cases
jerrytfleung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
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
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
76 changes: 76 additions & 0 deletions
76
collector/receiver/telemetryapireceiver/internal/sharedcomponent/sharedcomponent.go
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,76 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Package sharedcomponent exposes util functionality for receivers and exporters | ||
// that need to share state between different signal types instances such as net.Listener or os.File. | ||
package sharedcomponent // import "github.com/open-telemetry/opentelemetry-lambda/collector/receiver/telemetryapireceiver/internal/sharedcomponent" | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
) | ||
|
||
// SharedComponents a map that keeps reference of all created instances for a given configuration, | ||
// and ensures that the shared state is started and stopped only once. | ||
type SharedComponents struct { | ||
comps map[any]*SharedComponent | ||
} | ||
|
||
// NewSharedComponents returns a new empty SharedComponents. | ||
func NewSharedComponents() *SharedComponents { | ||
return &SharedComponents{ | ||
comps: make(map[any]*SharedComponent), | ||
} | ||
} | ||
|
||
// GetOrAdd returns the already created instance if exists, otherwise creates a new instance | ||
// and adds it to the map of references. | ||
func (scs *SharedComponents) GetOrAdd(key any, create func() component.Component) *SharedComponent { | ||
if c, ok := scs.comps[key]; ok { | ||
return c | ||
} | ||
newComp := &SharedComponent{ | ||
Component: create(), | ||
removeFunc: func() { | ||
delete(scs.comps, key) | ||
}, | ||
} | ||
scs.comps[key] = newComp | ||
return newComp | ||
} | ||
|
||
// SharedComponent ensures that the wrapped component is started and stopped only once. | ||
// When stopped it is removed from the SharedComponents map. | ||
type SharedComponent struct { | ||
component.Component | ||
|
||
startOnce sync.Once | ||
stopOnce sync.Once | ||
removeFunc func() | ||
} | ||
|
||
// Unwrap returns the original component. | ||
func (r *SharedComponent) Unwrap() component.Component { | ||
return r.Component | ||
} | ||
|
||
// Start implements component.Component. | ||
func (r *SharedComponent) Start(ctx context.Context, host component.Host) error { | ||
var err error | ||
r.startOnce.Do(func() { | ||
err = r.Component.Start(ctx, host) | ||
}) | ||
return err | ||
} | ||
|
||
// Shutdown implements component.Component. | ||
func (r *SharedComponent) Shutdown(ctx context.Context) error { | ||
var err error | ||
r.stopOnce.Do(func() { | ||
err = r.Component.Shutdown(ctx) | ||
r.removeFunc() | ||
}) | ||
return err | ||
} |
72 changes: 72 additions & 0 deletions
72
collector/receiver/telemetryapireceiver/internal/sharedcomponent/sharedcomponent_test.go
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,72 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package sharedcomponent | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/component/componenttest" | ||
) | ||
|
||
var id = component.MustNewID("test") | ||
|
||
func TestNewSharedComponents(t *testing.T) { | ||
comps := NewSharedComponents() | ||
assert.Len(t, comps.comps, 0) | ||
} | ||
|
||
type mockComponent struct { | ||
component.StartFunc | ||
component.ShutdownFunc | ||
} | ||
|
||
func TestSharedComponents_GetOrAdd(t *testing.T) { | ||
nop := &mockComponent{} | ||
createNop := func() component.Component { return nop } | ||
|
||
comps := NewSharedComponents() | ||
got := comps.GetOrAdd(id, createNop) | ||
assert.Len(t, comps.comps, 1) | ||
assert.Same(t, nop, got.Unwrap()) | ||
assert.Same(t, got, comps.GetOrAdd(id, createNop)) | ||
|
||
// Shutdown nop will remove | ||
assert.NoError(t, got.Shutdown(context.Background())) | ||
assert.Len(t, comps.comps, 0) | ||
assert.NotSame(t, got, comps.GetOrAdd(id, createNop)) | ||
} | ||
|
||
func TestSharedComponent(t *testing.T) { | ||
wantErr := errors.New("my error") | ||
calledStart := 0 | ||
calledStop := 0 | ||
comp := &mockComponent{ | ||
StartFunc: func(_ context.Context, _ component.Host) error { | ||
calledStart++ | ||
return wantErr | ||
}, | ||
ShutdownFunc: func(_ context.Context) error { | ||
calledStop++ | ||
return wantErr | ||
}, | ||
} | ||
createComp := func() component.Component { return comp } | ||
|
||
comps := NewSharedComponents() | ||
got := comps.GetOrAdd(id, createComp) | ||
assert.Equal(t, wantErr, got.Start(context.Background(), componenttest.NewNopHost())) | ||
assert.Equal(t, 1, calledStart) | ||
// Second time is not called anymore. | ||
assert.NoError(t, got.Start(context.Background(), componenttest.NewNopHost())) | ||
assert.Equal(t, 1, calledStart) | ||
assert.Equal(t, wantErr, got.Shutdown(context.Background())) | ||
assert.Equal(t, 1, calledStop) | ||
// Second time is not called anymore. | ||
assert.NoError(t, got.Shutdown(context.Background())) | ||
assert.Equal(t, 1, calledStop) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like we change the type because it can be something other than a map?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.