| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package componentstatus // import "go.opentelemetry.io/collector/component/componentstatus" |
| 5 |
|
| 6 |
import ( |
| 7 |
"slices" |
| 8 |
"sort" |
| 9 |
"strings" |
| 10 |
|
| 11 |
"go.opentelemetry.io/collector/component" |
| 12 |
"go.opentelemetry.io/collector/pipeline" |
| 13 |
) |
| 14 |
|
| 15 |
// pipelineDelim is the delimiter for internal representation of pipeline |
| 16 |
// component IDs. |
| 17 |
const pipelineDelim = byte(0x20) |
| 18 |
|
| 19 |
// InstanceID uniquely identifies a component instance |
| 20 |
// |
| 21 |
// TODO: consider moving this struct to a new package/module like `extension/statuswatcher` |
| 22 |
// https://github.com/open-telemetry/opentelemetry-collector/issues/10764 |
| 23 |
type InstanceID struct { |
| 24 |
componentID component.ID |
| 25 |
kind component.Kind |
| 26 |
pipelineIDs string // IDs encoded as a string so InstanceID is Comparable. |
| 27 |
} |
| 28 |
|
| 29 |
// NewInstanceID returns an ID that uniquely identifies a component. |
| 30 |
func NewInstanceID(componentID component.ID, kind component.Kind, pipelineIDs ...pipeline.ID) *InstanceID { |
| 31 |
instanceID := &InstanceID{ |
| 32 |
componentID: componentID, |
| 33 |
kind: kind, |
| 34 |
} |
| 35 |
instanceID.addPipelines(pipelineIDs) |
| 36 |
return instanceID |
| 37 |
} |
| 38 |
|
| 39 |
// ComponentID returns the ComponentID associated with this instance. |
| 40 |
func (id *InstanceID) ComponentID() component.ID { |
| 41 |
return id.componentID |
| 42 |
} |
| 43 |
|
| 44 |
// Kind returns the component Kind associated with this instance. |
| 45 |
func (id *InstanceID) Kind() component.Kind { |
| 46 |
return id.kind |
| 47 |
} |
| 48 |
|
| 49 |
// AllPipelineIDs calls f for each pipeline this instance is associated with. If |
| 50 |
// f returns false it will stop iteration. |
| 51 |
func (id *InstanceID) AllPipelineIDs(f func(pipeline.ID) bool) { |
| 52 |
var bs []byte |
| 53 |
for _, b := range []byte(id.pipelineIDs) { |
| 54 |
if b != pipelineDelim { |
| 55 |
bs = append(bs, b) |
| 56 |
continue |
| 57 |
} |
| 58 |
pipelineID := pipeline.ID{} |
| 59 |
err := pipelineID.UnmarshalText(bs) |
| 60 |
bs = bs[:0] |
| 61 |
if err != nil { |
| 62 |
continue |
| 63 |
} |
| 64 |
if !f(pipelineID) { |
| 65 |
break |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
// WithPipelines returns a new InstanceID updated to include the given |
| 71 |
// pipelineIDs. |
| 72 |
func (id *InstanceID) WithPipelines(pipelineIDs ...pipeline.ID) *InstanceID { |
| 73 |
instanceID := &InstanceID{ |
| 74 |
componentID: id.componentID, |
| 75 |
kind: id.kind, |
| 76 |
pipelineIDs: id.pipelineIDs, |
| 77 |
} |
| 78 |
instanceID.addPipelines(pipelineIDs) |
| 79 |
return instanceID |
| 80 |
} |
| 81 |
|
| 82 |
func (id *InstanceID) addPipelines(pipelineIDs []pipeline.ID) { |
| 83 |
delim := string(pipelineDelim) |
| 84 |
strIDs := strings.Split(id.pipelineIDs, delim) |
| 85 |
for _, pID := range pipelineIDs { |
| 86 |
strIDs = append(strIDs, pID.String()) |
| 87 |
} |
| 88 |
sort.Strings(strIDs) |
| 89 |
strIDs = slices.Compact(strIDs) |
| 90 |
id.pipelineIDs = strings.Join(strIDs, delim) + delim |
| 91 |
} |