-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce connectorprofiles.NewProfilesRouter (#11023)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description I missed the connector router when setting up connectorprofiles. cc @mx-psi
- Loading branch information
Showing
8 changed files
with
244 additions
and
21 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,25 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# 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: connectorprofiles | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Add ProfilesRouterAndConsumer interface, and NewProfilesRouter method. | ||
|
||
# One or more tracking issues or pull requests related to the change | ||
issues: [11023] | ||
|
||
# (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: | ||
|
||
# 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] |
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,37 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package connectorprofiles // import "go.opentelemetry.io/collector/connector/connectorprofiles" | ||
|
||
import ( | ||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/connector/internal" | ||
"go.opentelemetry.io/collector/consumer/consumerprofiles" | ||
"go.opentelemetry.io/collector/internal/fanoutconsumer" | ||
) | ||
|
||
// ProfilesRouterAndConsumer feeds the first consumerprofiles.Profiles in each of the specified pipelines. | ||
type ProfilesRouterAndConsumer interface { | ||
consumerprofiles.Profiles | ||
Consumer(...component.ID) (consumerprofiles.Profiles, error) | ||
PipelineIDs() []component.ID | ||
privateFunc() | ||
} | ||
|
||
type profilesRouter struct { | ||
consumerprofiles.Profiles | ||
internal.BaseRouter[consumerprofiles.Profiles] | ||
} | ||
|
||
func NewProfilesRouter(cm map[component.ID]consumerprofiles.Profiles) ProfilesRouterAndConsumer { | ||
consumers := make([]consumerprofiles.Profiles, 0, len(cm)) | ||
for _, cons := range cm { | ||
consumers = append(consumers, cons) | ||
} | ||
return &profilesRouter{ | ||
Profiles: fanoutconsumer.NewProfiles(consumers), | ||
BaseRouter: internal.NewBaseRouter(fanoutconsumer.NewProfiles, cm), | ||
} | ||
} | ||
|
||
func (r *profilesRouter) privateFunc() {} |
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,157 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package connectorprofiles | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/consumer" | ||
"go.opentelemetry.io/collector/consumer/consumerprofiles" | ||
"go.opentelemetry.io/collector/consumer/consumertest" | ||
"go.opentelemetry.io/collector/pdata/pprofile" | ||
"go.opentelemetry.io/collector/pdata/testdata" | ||
) | ||
|
||
type mutatingProfilesSink struct { | ||
*consumertest.ProfilesSink | ||
} | ||
|
||
func (mts *mutatingProfilesSink) Capabilities() consumer.Capabilities { | ||
return consumer.Capabilities{MutatesData: true} | ||
} | ||
|
||
func TestProfilesRouterMultiplexing(t *testing.T) { | ||
var max = 20 | ||
for numIDs := 1; numIDs < max; numIDs++ { | ||
for numCons := 1; numCons < max; numCons++ { | ||
for numProfiles := 1; numProfiles < max; numProfiles++ { | ||
t.Run( | ||
fmt.Sprintf("%d-ids/%d-cons/%d-logs", numIDs, numCons, numProfiles), | ||
fuzzProfiles(numIDs, numCons, numProfiles), | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func fuzzProfiles(numIDs, numCons, numProfiles int) func(*testing.T) { | ||
return func(t *testing.T) { | ||
allIDs := make([]component.ID, 0, numCons) | ||
allCons := make([]consumerprofiles.Profiles, 0, numCons) | ||
allConsMap := make(map[component.ID]consumerprofiles.Profiles) | ||
|
||
// If any consumer is mutating, the router must report mutating | ||
for i := 0; i < numCons; i++ { | ||
allIDs = append(allIDs, component.MustNewIDWithName("sink", strconv.Itoa(numCons))) | ||
// Random chance for each consumer to be mutating | ||
if (numCons+numProfiles+i)%4 == 0 { | ||
allCons = append(allCons, &mutatingProfilesSink{ProfilesSink: new(consumertest.ProfilesSink)}) | ||
} else { | ||
allCons = append(allCons, new(consumertest.ProfilesSink)) | ||
} | ||
allConsMap[allIDs[i]] = allCons[i] | ||
} | ||
|
||
r := NewProfilesRouter(allConsMap) | ||
td := testdata.GenerateProfiles(1) | ||
|
||
// Keep track of how many logs each consumer should receive. | ||
// This will be validated after every call to RouteProfiles. | ||
expected := make(map[component.ID]int, numCons) | ||
|
||
for i := 0; i < numProfiles; i++ { | ||
// Build a random set of ids (no duplicates) | ||
randCons := make(map[component.ID]bool, numIDs) | ||
for j := 0; j < numIDs; j++ { | ||
// This number should be pretty random and less than numCons | ||
conNum := (numCons + numIDs + i + j) % numCons | ||
randCons[allIDs[conNum]] = true | ||
} | ||
|
||
// Convert to slice, update expectations | ||
conIDs := make([]component.ID, 0, len(randCons)) | ||
for id := range randCons { | ||
conIDs = append(conIDs, id) | ||
expected[id]++ | ||
} | ||
|
||
// Route to list of consumers | ||
fanout, err := r.Consumer(conIDs...) | ||
assert.NoError(t, err) | ||
assert.NoError(t, fanout.ConsumeProfiles(context.Background(), td)) | ||
|
||
// Validate expectations for all consumers | ||
for id := range expected { | ||
profiles := []pprofile.Profiles{} | ||
switch con := allConsMap[id].(type) { | ||
case *consumertest.ProfilesSink: | ||
profiles = con.AllProfiles() | ||
case *mutatingProfilesSink: | ||
profiles = con.AllProfiles() | ||
} | ||
assert.Len(t, profiles, expected[id]) | ||
for n := 0; n < len(profiles); n++ { | ||
assert.EqualValues(t, td, profiles[n]) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
func TestProfilessRouterConsumer(t *testing.T) { | ||
ctx := context.Background() | ||
td := testdata.GenerateProfiles(1) | ||
|
||
fooID := component.MustNewID("foo") | ||
barID := component.MustNewID("bar") | ||
|
||
foo := new(consumertest.ProfilesSink) | ||
bar := new(consumertest.ProfilesSink) | ||
r := NewProfilesRouter(map[component.ID]consumerprofiles.Profiles{fooID: foo, barID: bar}) | ||
|
||
rcs := r.PipelineIDs() | ||
assert.Len(t, rcs, 2) | ||
assert.ElementsMatch(t, []component.ID{fooID, barID}, rcs) | ||
|
||
assert.Len(t, foo.AllProfiles(), 0) | ||
assert.Len(t, bar.AllProfiles(), 0) | ||
|
||
both, err := r.Consumer(fooID, barID) | ||
assert.NotNil(t, both) | ||
assert.NoError(t, err) | ||
|
||
assert.NoError(t, both.ConsumeProfiles(ctx, td)) | ||
assert.Len(t, foo.AllProfiles(), 1) | ||
assert.Len(t, bar.AllProfiles(), 1) | ||
|
||
fooOnly, err := r.Consumer(fooID) | ||
assert.NotNil(t, fooOnly) | ||
assert.NoError(t, err) | ||
|
||
assert.NoError(t, fooOnly.ConsumeProfiles(ctx, td)) | ||
assert.Len(t, foo.AllProfiles(), 2) | ||
assert.Len(t, bar.AllProfiles(), 1) | ||
|
||
barOnly, err := r.Consumer(barID) | ||
assert.NotNil(t, barOnly) | ||
assert.NoError(t, err) | ||
|
||
assert.NoError(t, barOnly.ConsumeProfiles(ctx, td)) | ||
assert.Len(t, foo.AllProfiles(), 2) | ||
assert.Len(t, bar.AllProfiles(), 2) | ||
|
||
none, err := r.Consumer() | ||
assert.Nil(t, none) | ||
assert.Error(t, err) | ||
|
||
fake, err := r.Consumer(component.MustNewID("fake")) | ||
assert.Nil(t, fake) | ||
assert.Error(t, err) | ||
} |
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