Debian Codegraph @

pipeline/pipeline.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package pipeline // import "go.opentelemetry.io/collector/pipeline"
5 import (
6 "errors"
7 "fmt"
8 "regexp"
9 "strings"
10 )
11
12 // typeAndNameSeparator is the separator that is used between type and name in type/name composite keys.
13 const typeAndNameSeparator = "/"
14
15 // ID represents the identity for a pipeline. It combines two values:
16 // * signal - the Signal of the pipeline.
17 // * name - the name of that pipeline.
18 type ID struct {
19 signal Signal `mapstructure:"-"`
20 name string `mapstructure:"-"`
21 }
22
23 // NewID returns a new ID with the given Signal and empty name.
24 func NewID(signal Signal) ID {
25 return NewIDWithName(signal, "")
26 }
27
28 // NewIDWithName returns a new ID with the given Signal and name.
29 func NewIDWithName(signal Signal, name string) ID {
30 return ID{signal: signal, name: name}
31 }
32
33 // Signal returns the Signal of the ID.
34 func (i ID) Signal() Signal {
35 return i.signal
36 }
37
38 // Name returns the name of the ID.
39 func (i ID) Name() string {
40 return i.name
41 }
42
43 // MarshalText implements the encoding.TextMarshaler interface.
44 // This marshals the Signal and name as one string in the config.
45 func (i ID) MarshalText() (text []byte, err error) {
46 return []byte(i.String()), nil
47 }
48
49 // UnmarshalText implements the encoding.TextUnmarshaler interface.
50 func (i *ID) UnmarshalText(text []byte) error {
51 idStr := string(text)
52 signalStr, nameStr, hasName := strings.Cut(idStr, typeAndNameSeparator)
53 signalStr = strings.TrimSpace(signalStr)
54
55 if signalStr == "" {
56 if hasName {
57 return fmt.Errorf("in %q id: the part before %s should not be empty", idStr, typeAndNameSeparator)
58 }
59 return errors.New("id must not be empty")
60 }
61
62 if hasName {
63 // "name" part is present.
64 nameStr = strings.TrimSpace(nameStr)
65 if nameStr == "" {
66 return fmt.Errorf("in %q id: the part after %s should not be empty", idStr, typeAndNameSeparator)
67 }
68 if err := validateName(nameStr); err != nil {
69 return fmt.Errorf("in %q id: %w", nameStr, err)
70 }
71 }
72
73 if err := i.signal.UnmarshalText([]byte(signalStr)); err != nil {
74 return fmt.Errorf("in %q id: %w", idStr, err)
75 }
76 i.name = nameStr
77
78 return nil
79 }
80
81 // String returns the ID string representation as "signal[/name]" format.
82 func (i ID) String() string {
83 if i.name == "" {
84 return i.signal.String()
85 }
86
87 return i.signal.String() + typeAndNameSeparator + i.name
88 }
89
90 // nameRegexp is used to validate the name of an ID. A name can consist of
91 // 1 to 1024 unicode characters excluding whitespace, control characters, and
92 // symbols.
93 var nameRegexp = regexp.MustCompile(`^[^\pZ\pC\pS]+$`)
94
95 func validateName(nameStr string) error {
96 if len(nameStr) > 1024 {
97 return fmt.Errorf("name %q is longer than 1024 characters (%d characters)", nameStr, len(nameStr))
98 }
99 if !nameRegexp.MatchString(nameStr) {
100 return fmt.Errorf("invalid character(s) in name %q", nameStr)
101 }
102 return nil
103 }