Skip to content

Commit

Permalink
Start adding PipelineID
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerHelmuth committed Aug 22, 2024
1 parent 18d5c02 commit fdf37fe
Show file tree
Hide file tree
Showing 57 changed files with 681 additions and 598 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1345,8 +1345,8 @@ and hope to make a v1.0.0 release soon.
- `config`: Deprecate multiple types and funcs in `config` package (#6422)
- config.ComponentID => component.ID
- config.Type => component.Type
- config.DataType => component.DataType
- config.[Traces|Metrics|Logs]DataType => component.DataType[Traces|Metrics|Logs]
- config.DataType => component.Signal
- config.[Traces|Metrics|Logs]DataType => component.Signal[Traces|Metrics|Logs]
- config.Receiver => component.ReceiverConfig
- config.UnmarshalReceiver => component.UnmarshalReceiverConfig
- config.Processor => component.ProcessorConfig
Expand Down
18 changes: 9 additions & 9 deletions cmd/mdatagen/templates/component_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ func TestComponentLifecycle(t *testing.T) {
{
name: "logs_to_logs",
createFn: func(ctx context.Context, set connector.Settings, cfg component.Config) (component.Component, error) {
router := connector.NewLogsRouter(map[component.ID]consumer.Logs{component.NewID(component.DataTypeLogs): consumertest.NewNop()})
router := connector.NewLogsRouter(map[component.ID]consumer.Logs{component.NewID(component.SignalLogs): consumertest.NewNop()})
return factory.CreateLogsToLogs(ctx, set, cfg, router)
},
},
Expand All @@ -381,7 +381,7 @@ func TestComponentLifecycle(t *testing.T) {
{
name: "logs_to_metrics",
createFn: func(ctx context.Context, set connector.Settings, cfg component.Config) (component.Component, error) {
router := connector.NewMetricsRouter(map[component.ID]consumer.Metrics{component.NewID(component.DataTypeMetrics): consumertest.NewNop()})
router := connector.NewMetricsRouter(map[component.ID]consumer.Metrics{component.NewID(component.SignalMetrics): consumertest.NewNop()})
return factory.CreateLogsToMetrics(ctx, set, cfg, router)
},
},
Expand All @@ -390,7 +390,7 @@ func TestComponentLifecycle(t *testing.T) {
{
name: "logs_to_traces",
createFn: func(ctx context.Context, set connector.Settings, cfg component.Config) (component.Component, error) {
router := connector.NewTracesRouter(map[component.ID]consumer.Traces{component.NewID(component.DataTypeTraces): consumertest.NewNop()})
router := connector.NewTracesRouter(map[component.ID]consumer.Traces{component.NewID(component.SignalTraces): consumertest.NewNop()})
return factory.CreateLogsToTraces(ctx, set, cfg, router)
},
},
Expand All @@ -399,7 +399,7 @@ func TestComponentLifecycle(t *testing.T) {
{
name: "metrics_to_logs",
createFn: func(ctx context.Context, set connector.Settings, cfg component.Config) (component.Component, error) {
router := connector.NewLogsRouter(map[component.ID]consumer.Logs{component.NewID(component.DataTypeLogs): consumertest.NewNop()})
router := connector.NewLogsRouter(map[component.ID]consumer.Logs{component.NewID(component.SignalLogs): consumertest.NewNop()})
return factory.CreateMetricsToLogs(ctx, set, cfg, router)
},
},
Expand All @@ -408,7 +408,7 @@ func TestComponentLifecycle(t *testing.T) {
{
name: "metrics_to_metrics",
createFn: func(ctx context.Context, set connector.Settings, cfg component.Config) (component.Component, error) {
router := connector.NewMetricsRouter(map[component.ID]consumer.Metrics{component.NewID(component.DataTypeMetrics): consumertest.NewNop()})
router := connector.NewMetricsRouter(map[component.ID]consumer.Metrics{component.NewID(component.SignalMetrics): consumertest.NewNop()})
return factory.CreateMetricsToMetrics(ctx, set, cfg, router)
},
},
Expand All @@ -417,7 +417,7 @@ func TestComponentLifecycle(t *testing.T) {
{
name: "metrics_to_traces",
createFn: func(ctx context.Context, set connector.Settings, cfg component.Config) (component.Component, error) {
router := connector.NewTracesRouter(map[component.ID]consumer.Traces{component.NewID(component.DataTypeTraces): consumertest.NewNop()})
router := connector.NewTracesRouter(map[component.ID]consumer.Traces{component.NewID(component.SignalTraces): consumertest.NewNop()})
return factory.CreateMetricsToTraces(ctx, set, cfg, router)
},
},
Expand All @@ -426,7 +426,7 @@ func TestComponentLifecycle(t *testing.T) {
{
name: "traces_to_logs",
createFn: func(ctx context.Context, set connector.Settings, cfg component.Config) (component.Component, error) {
router := connector.NewLogsRouter(map[component.ID]consumer.Logs{component.NewID(component.DataTypeLogs): consumertest.NewNop()})
router := connector.NewLogsRouter(map[component.ID]consumer.Logs{component.NewID(component.SignalLogs): consumertest.NewNop()})
return factory.CreateTracesToLogs(ctx, set, cfg, router)
},
},
Expand All @@ -435,7 +435,7 @@ func TestComponentLifecycle(t *testing.T) {
{
name: "traces_to_metrics",
createFn: func(ctx context.Context, set connector.Settings, cfg component.Config) (component.Component, error) {
router := connector.NewMetricsRouter(map[component.ID]consumer.Metrics{component.NewID(component.DataTypeMetrics): consumertest.NewNop()})
router := connector.NewMetricsRouter(map[component.ID]consumer.Metrics{component.NewID(component.SignalMetrics): consumertest.NewNop()})
return factory.CreateTracesToMetrics(ctx, set, cfg, router)
},
},
Expand All @@ -444,7 +444,7 @@ func TestComponentLifecycle(t *testing.T) {
{
name: "traces_to_traces",
createFn: func(ctx context.Context, set connector.Settings, cfg component.Config) (component.Component, error) {
router := connector.NewTracesRouter(map[component.ID]consumer.Traces{component.NewID(component.DataTypeTraces): consumertest.NewNop()})
router := connector.NewTracesRouter(map[component.ID]consumer.Traces{component.NewID(component.SignalTraces): consumertest.NewNop()})
return factory.CreateTracesToTraces(ctx, set, cfg, router)
},
},
Expand Down
9 changes: 2 additions & 7 deletions component/componentprofiles/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ package componentprofiles // import "go.opentelemetry.io/collector/component/com

import "go.opentelemetry.io/collector/component"

func mustNewDataType(strType string) component.DataType {
return component.MustNewType(strType)
}

var (
// DataTypeProfiles is the data type tag for profiles.
DataTypeProfiles = mustNewDataType("profiles")
const (
SignalProfiles = component.Signal("profiles")
)
10 changes: 5 additions & 5 deletions component/componentstatus/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type InstanceID struct {
}

// NewInstanceID returns an ID that uniquely identifies a component.
func NewInstanceID(componentID component.ID, kind component.Kind, pipelineIDs ...component.ID) *InstanceID {
func NewInstanceID(componentID component.ID, kind component.Kind, pipelineIDs ...component.PipelineID) *InstanceID {
instanceID := &InstanceID{
componentID: componentID,
kind: kind,
Expand All @@ -47,14 +47,14 @@ func (id *InstanceID) Kind() component.Kind {

// AllPipelineIDs calls f for each pipeline this instance is associated with. If
// f returns false it will stop iteration.
func (id *InstanceID) AllPipelineIDs(f func(component.ID) bool) {
func (id *InstanceID) AllPipelineIDs(f func(component.PipelineID) bool) {
var bs []byte
for _, b := range []byte(id.pipelineIDs) {
if b != pipelineDelim {
bs = append(bs, b)
continue
}
pipelineID := component.ID{}
pipelineID := component.PipelineID{}
err := pipelineID.UnmarshalText(bs)
bs = bs[:0]
if err != nil {
Expand All @@ -68,7 +68,7 @@ func (id *InstanceID) AllPipelineIDs(f func(component.ID) bool) {

// WithPipelines returns a new InstanceID updated to include the given
// pipelineIDs.
func (id *InstanceID) WithPipelines(pipelineIDs ...component.ID) *InstanceID {
func (id *InstanceID) WithPipelines(pipelineIDs ...component.PipelineID) *InstanceID {
instanceID := &InstanceID{
componentID: id.componentID,
kind: id.kind,
Expand All @@ -78,7 +78,7 @@ func (id *InstanceID) WithPipelines(pipelineIDs ...component.ID) *InstanceID {
return instanceID
}

func (id *InstanceID) addPipelines(pipelineIDs []component.ID) {
func (id *InstanceID) addPipelines(pipelineIDs []component.PipelineID) {
delim := string(pipelineDelim)
strIDs := strings.Split(id.pipelineIDs, delim)
for _, pID := range pipelineIDs {
Expand Down
32 changes: 16 additions & 16 deletions component/componentstatus/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ import (

func TestInstanceID(t *testing.T) {
traces := component.MustNewID("traces")
tracesA := component.MustNewIDWithName("traces", "a")
tracesB := component.MustNewIDWithName("traces", "b")
tracesC := component.MustNewIDWithName("traces", "c")
tracesA := component.NewPipelineWithName("traces", "a")
tracesB := component.NewPipelineWithName("traces", "b")
tracesC := component.NewPipelineWithName("traces", "c")

idTracesA := NewInstanceID(traces, component.KindReceiver, tracesA)
idTracesAll := NewInstanceID(traces, component.KindReceiver, tracesA, tracesB, tracesC)
assert.NotEqual(t, idTracesA, idTracesAll)

assertHasPipelines := func(t *testing.T, instanceID *InstanceID, expectedPipelineIDs []component.ID) {
var pipelineIDs []component.ID
instanceID.AllPipelineIDs(func(id component.ID) bool {
assertHasPipelines := func(t *testing.T, instanceID *InstanceID, expectedPipelineIDs []component.PipelineID) {
var pipelineIDs []component.PipelineID
instanceID.AllPipelineIDs(func(id component.PipelineID) bool {
pipelineIDs = append(pipelineIDs, id)
return true
})
Expand All @@ -34,31 +34,31 @@ func TestInstanceID(t *testing.T) {
name string
id1 *InstanceID
id2 *InstanceID
pipelineIDs []component.ID
pipelineIDs []component.PipelineID
}{
{
name: "equal instances",
id1: idTracesA,
id2: NewInstanceID(traces, component.KindReceiver, tracesA),
pipelineIDs: []component.ID{tracesA},
pipelineIDs: []component.PipelineID{tracesA},
},
{
name: "equal instances - out of order",
id1: idTracesAll,
id2: NewInstanceID(traces, component.KindReceiver, tracesC, tracesB, tracesA),
pipelineIDs: []component.ID{tracesA, tracesB, tracesC},
pipelineIDs: []component.PipelineID{tracesA, tracesB, tracesC},
},
{
name: "with pipelines",
id1: idTracesAll,
id2: idTracesA.WithPipelines(tracesB, tracesC),
pipelineIDs: []component.ID{tracesA, tracesB, tracesC},
pipelineIDs: []component.PipelineID{tracesA, tracesB, tracesC},
},
{
name: "with pipelines - out of order",
id1: idTracesAll,
id2: idTracesA.WithPipelines(tracesC, tracesB),
pipelineIDs: []component.ID{tracesA, tracesB, tracesC},
pipelineIDs: []component.PipelineID{tracesA, tracesB, tracesC},
},
} {
t.Run(tc.name, func(t *testing.T) {
Expand All @@ -73,20 +73,20 @@ func TestAllPipelineIDs(t *testing.T) {
instanceID := NewInstanceID(
component.MustNewID("traces"),
component.KindReceiver,
component.MustNewIDWithName("traces", "a"),
component.MustNewIDWithName("traces", "b"),
component.MustNewIDWithName("traces", "c"),
component.NewPipelineWithName("traces", "a"),
component.NewPipelineWithName("traces", "b"),
component.NewPipelineWithName("traces", "c"),
)

count := 0
instanceID.AllPipelineIDs(func(component.ID) bool {
instanceID.AllPipelineIDs(func(component.PipelineID) bool {
count++
return true
})
assert.Equal(t, 3, count)

count = 0
instanceID.AllPipelineIDs(func(component.ID) bool {
instanceID.AllPipelineIDs(func(component.PipelineID) bool {
count++
return false
})
Expand Down
2 changes: 1 addition & 1 deletion component/componenttest/nop_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ func (nh *nopHost) GetExtensions() map[component.ID]component.Component {
return nil
}

func (nh *nopHost) GetExporters() map[component.DataType]map[component.ID]component.Component {
func (nh *nopHost) GetExporters() map[component.Signal]map[component.ID]component.Component {
return nil
}
57 changes: 37 additions & 20 deletions component/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package component // import "go.opentelemetry.io/collector/component"

import (
"errors"
"fmt"
"reflect"
"regexp"
Expand Down Expand Up @@ -145,26 +146,6 @@ func MustNewType(strType string) Type {
return ty
}

// DataType is a special Type that represents the data types supported by the collector. We currently support
// collecting metrics, traces and logs, this can expand in the future.
type DataType = Type

func mustNewDataType(strType string) DataType {
return MustNewType(strType)
}

// Currently supported data types. Add new data types here when new types are supported in the future.
var (
// DataTypeTraces is the data type tag for traces.
DataTypeTraces = mustNewDataType("traces")

// DataTypeMetrics is the data type tag for metrics.
DataTypeMetrics = mustNewDataType("metrics")

// DataTypeLogs is the data type tag for logs.
DataTypeLogs = mustNewDataType("logs")
)

// nameRegexp is used to validate the name of a component. A name can consist of
// 1 to 1024 unicode characters excluding whitespace, control characters, and
// symbols.
Expand All @@ -179,3 +160,39 @@ func validateName(nameStr string) error {
}
return nil
}

type Signal string

const (
SignalTraces Signal = "traces"
SignalMetrics Signal = "metrics"
SignalLogs Signal = "logs"
)

func (s Signal) String() string {
return string(s)
}

func (s Signal) MarshalText() (text []byte, err error) {
return []byte(s), nil
}

func (s *Signal) UnmarshalText(text []byte) error {
if len(text) == 0 {
return errors.New("id must not be empty")
}
strText := string(text)
switch strText {
case "traces":
*s = SignalTraces
return nil
case "metrics":
*s = SignalMetrics
return nil
case "logs":
*s = SignalLogs
return nil
default:
return fmt.Errorf("invalid signal %q", strText)
}
}
71 changes: 71 additions & 0 deletions component/identifiable.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,74 @@ func (id ID) String() string {

return id.typeVal.String() + typeAndNameSeparator + id.nameVal
}

type PipelineID struct {
signal Signal `mapstructure:"-"`
nameVal string `mapstructure:"-"`
}

func NewPipeline(signal Signal) PipelineID {
return PipelineID{signal: signal}
}

func NewPipelineWithName(signal Signal, nameVal string) PipelineID {
return PipelineID{signal: signal, nameVal: nameVal}
}

func (p PipelineID) Signal() Signal {
return p.signal
}

func (p PipelineID) Name() string {
return p.nameVal
}

func (p PipelineID) MarshalText() (text []byte, err error) {
return []byte(p.String()), nil
}

// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (p *PipelineID) UnmarshalText(text []byte) error {
pipelineStr := string(text)
items := strings.SplitN(pipelineStr, typeAndNameSeparator, 2)
var signalStr, nameStr string
if len(items) >= 1 {
signalStr = strings.TrimSpace(items[0])
}

if len(items) == 1 && signalStr == "" {
return errors.New("id must not be empty")
}

if signalStr == "" {
return fmt.Errorf("in %q id: the part before %s should not be empty", pipelineStr, typeAndNameSeparator)
}

err := p.signal.UnmarshalText([]byte(signalStr))
if err != nil {
return err
}

if len(items) > 1 {
// "name" part is present.
nameStr = strings.TrimSpace(items[1])
if nameStr == "" {
return fmt.Errorf("in %q id: the part after %s should not be empty", pipelineStr, typeAndNameSeparator)
}
if err := validateName(nameStr); err != nil {
return fmt.Errorf("in %q id: %w", nameStr, err)
}
}
p.nameVal = nameStr

return nil
}

// String returns the ID string representation as "type[/name]" format.
func (p PipelineID) String() string {
if p.nameVal == "" {
return p.signal.String()
}

return p.signal.String() + typeAndNameSeparator + p.nameVal
}
Loading

0 comments on commit fdf37fe

Please sign in to comment.