| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Package component outlines the abstraction of components within the OpenTelemetry Collector. It provides details on the component |
| 5 |
// lifecycle as well as defining the interface that components must fulfill. |
| 6 |
package component // import "go.opentelemetry.io/collector/component" |
| 7 |
|
| 8 |
import ( |
| 9 |
"context" |
| 10 |
"fmt" |
| 11 |
"strings" |
| 12 |
) |
| 13 |
|
| 14 |
// Component is either a receiver, exporter, processor, connector, or an extension. |
| 15 |
// |
| 16 |
// A component's lifecycle has the following phases: |
| 17 |
// |
| 18 |
// 1. Creation: The component is created using its respective factory, via a Create* call. |
| 19 |
// 2. Start: The component's Start method is called. |
| 20 |
// 3. Running: The component is up and running. |
| 21 |
// 4. Shutdown: The component's Shutdown method is called and the lifecycle is complete. |
| 22 |
// |
| 23 |
// Once the lifecycle is complete it may be repeated, in which case a new component |
| 24 |
// is created, starts, runs and is shutdown again. |
| 25 |
type Component interface { |
| 26 |
// Start tells the component to start. Host parameter can be used for communicating |
| 27 |
// with the host after Start() has already returned. If an error is returned by |
| 28 |
// Start() then the collector startup will be aborted. |
| 29 |
// If this is an exporter component it may prepare for exporting |
| 30 |
// by connecting to the endpoint. |
| 31 |
// |
| 32 |
// If the component needs to perform a long-running starting operation then it is recommended |
| 33 |
// that Start() returns quickly and the long-running operation is performed in background. |
| 34 |
// In that case make sure that the long-running operation does not use the context passed |
| 35 |
// to Start() function since that context will be cancelled soon and can abort the long-running |
| 36 |
// operation. Create a new context from the context.Background() for long-running operations. |
| 37 |
Start(ctx context.Context, host Host) error |
| 38 |
|
| 39 |
// Shutdown is invoked during service shutdown. After Shutdown() is called, if the component |
| 40 |
// accepted data in any way, it should not accept it anymore. |
| 41 |
// |
| 42 |
// This method must be safe to call: |
| 43 |
// - without Start() having been called |
| 44 |
// - if the component is in a shutdown state already |
| 45 |
// |
| 46 |
// If there are any background operations running by the component they must be aborted before |
| 47 |
// this function returns. Remember that if you started any long-running background operations from |
| 48 |
// the Start() method, those operations must be also cancelled. If there are any buffers in the |
| 49 |
// component, they should be cleared and the data sent immediately to the next component. |
| 50 |
// |
| 51 |
// The component's lifecycle is completed once the Shutdown() method returns. No other |
| 52 |
// methods of the component are called after that. If necessary a new component with |
| 53 |
// the same or different configuration may be created and started (this may happen |
| 54 |
// for example if we want to restart the component). |
| 55 |
Shutdown(ctx context.Context) error |
| 56 |
} |
| 57 |
|
| 58 |
// StartFunc specifies the function invoked when the component.Component is being started. |
| 59 |
type StartFunc func(context.Context, Host) error |
| 60 |
|
| 61 |
// Start starts the component. |
| 62 |
func (f StartFunc) Start(ctx context.Context, host Host) error { |
| 63 |
if f == nil { |
| 64 |
return nil |
| 65 |
} |
| 66 |
return f(ctx, host) |
| 67 |
} |
| 68 |
|
| 69 |
// ShutdownFunc specifies the function invoked when the component.Component is being shutdown. |
| 70 |
type ShutdownFunc func(context.Context) error |
| 71 |
|
| 72 |
// Shutdown shuts down the component. |
| 73 |
func (f ShutdownFunc) Shutdown(ctx context.Context) error { |
| 74 |
if f == nil { |
| 75 |
return nil |
| 76 |
} |
| 77 |
return f(ctx) |
| 78 |
} |
| 79 |
|
| 80 |
// Kind represents component kinds. |
| 81 |
type Kind struct { |
| 82 |
name string |
| 83 |
} |
| 84 |
|
| 85 |
var ( |
| 86 |
KindReceiver = Kind{name: "Receiver"} |
| 87 |
KindProcessor = Kind{name: "Processor"} |
| 88 |
KindExporter = Kind{name: "Exporter"} |
| 89 |
KindExtension = Kind{name: "Extension"} |
| 90 |
KindConnector = Kind{name: "Connector"} |
| 91 |
) |
| 92 |
|
| 93 |
func (k Kind) String() string { |
| 94 |
return k.name |
| 95 |
} |
| 96 |
|
| 97 |
// StabilityLevel represents the stability level of the component created by the factory. |
| 98 |
// The stability level is used to determine if the component should be used in production |
| 99 |
// or not. For more details see: |
| 100 |
// https://github.com/open-telemetry/opentelemetry-collector/blob/main/docs/component-stability.md#stability-levels |
| 101 |
type StabilityLevel int |
| 102 |
|
| 103 |
const ( |
| 104 |
StabilityLevelUndefined StabilityLevel = iota // skip 0, start types from 1. |
| 105 |
StabilityLevelUnmaintained |
| 106 |
StabilityLevelDeprecated |
| 107 |
StabilityLevelDevelopment |
| 108 |
StabilityLevelAlpha |
| 109 |
StabilityLevelBeta |
| 110 |
StabilityLevelStable |
| 111 |
) |
| 112 |
|
| 113 |
func (sl *StabilityLevel) UnmarshalText(in []byte) error { |
| 114 |
str := strings.ToLower(string(in)) |
| 115 |
switch str { |
| 116 |
case "undefined": |
| 117 |
*sl = StabilityLevelUndefined |
| 118 |
case "unmaintained": |
| 119 |
*sl = StabilityLevelUnmaintained |
| 120 |
case "deprecated": |
| 121 |
*sl = StabilityLevelDeprecated |
| 122 |
case "development": |
| 123 |
*sl = StabilityLevelDevelopment |
| 124 |
case "alpha": |
| 125 |
*sl = StabilityLevelAlpha |
| 126 |
case "beta": |
| 127 |
*sl = StabilityLevelBeta |
| 128 |
case "stable": |
| 129 |
*sl = StabilityLevelStable |
| 130 |
default: |
| 131 |
return fmt.Errorf("unsupported stability level: %q", string(in)) |
| 132 |
} |
| 133 |
return nil |
| 134 |
} |
| 135 |
|
| 136 |
func (sl StabilityLevel) String() string { |
| 137 |
switch sl { |
| 138 |
case StabilityLevelUndefined: |
| 139 |
return "Undefined" |
| 140 |
case StabilityLevelUnmaintained: |
| 141 |
return "Unmaintained" |
| 142 |
case StabilityLevelDeprecated: |
| 143 |
return "Deprecated" |
| 144 |
case StabilityLevelDevelopment: |
| 145 |
return "Development" |
| 146 |
case StabilityLevelAlpha: |
| 147 |
return "Alpha" |
| 148 |
case StabilityLevelBeta: |
| 149 |
return "Beta" |
| 150 |
case StabilityLevelStable: |
| 151 |
return "Stable" |
| 152 |
} |
| 153 |
return "" |
| 154 |
} |
| 155 |
|
| 156 |
func (sl StabilityLevel) LogMessage() string { |
| 157 |
switch sl { |
| 158 |
case StabilityLevelUnmaintained: |
| 159 |
return "Unmaintained component. Actively looking for contributors. Component will become deprecated after 3 months of remaining unmaintained." |
| 160 |
case StabilityLevelDeprecated: |
| 161 |
return "Deprecated component. Will be removed in future releases." |
| 162 |
case StabilityLevelDevelopment: |
| 163 |
return "Development component. May change in the future." |
| 164 |
case StabilityLevelAlpha: |
| 165 |
return "Alpha component. May change in the future." |
| 166 |
case StabilityLevelBeta: |
| 167 |
return "Beta component. May change in the future." |
| 168 |
case StabilityLevelStable: |
| 169 |
return "Stable component." |
| 170 |
default: |
| 171 |
return "Stability level of component is undefined" |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
// Factory is implemented by all Component factories. |
| 176 |
type Factory interface { |
| 177 |
// Type gets the type of the component created by this factory. |
| 178 |
Type() Type |
| 179 |
|
| 180 |
// CreateDefaultConfig creates the default configuration for the Component. |
| 181 |
// This method can be called multiple times depending on the pipeline |
| 182 |
// configuration and should not cause side effects that prevent the creation |
| 183 |
// of multiple instances of the Component. |
| 184 |
// The object returned by this method needs to pass the checks implemented by |
| 185 |
// 'componenttest.CheckConfigStruct'. It is recommended to have these checks in the |
| 186 |
// tests of any implementation of the Factory interface. |
| 187 |
CreateDefaultConfig() Config |
| 188 |
} |
| 189 |
|
| 190 |
// CreateDefaultConfigFunc is the equivalent of Factory.CreateDefaultConfig(). |
| 191 |
type CreateDefaultConfigFunc func() Config |
| 192 |
|
| 193 |
// CreateDefaultConfig implements Factory.CreateDefaultConfig(). |
| 194 |
func (f CreateDefaultConfigFunc) CreateDefaultConfig() Config { |
| 195 |
return f() |
| 196 |
} |