forked from open-telemetry/opentelemetry-collector
/
graph.go
374 lines (328 loc) · 12.3 KB
/
graph.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package graph // import "go.opentelemetry.io/collector/service/internal/graph"
import (
"context"
"errors"
"fmt"
"strings"
"go.uber.org/multierr"
"gonum.org/v1/gonum/graph"
"gonum.org/v1/gonum/graph/simple"
"gonum.org/v1/gonum/graph/topo"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/connector"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/processor"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/service/internal/capabilityconsumer"
"go.opentelemetry.io/collector/service/internal/fanoutconsumer"
)
// Settings holds configuration for building builtPipelines.
type Settings struct {
Telemetry component.TelemetrySettings
BuildInfo component.BuildInfo
ReceiverBuilder *receiver.Builder
ProcessorBuilder *processor.Builder
ExporterBuilder *exporter.Builder
ConnectorBuilder *connector.Builder
// PipelineConfigs is a map of component.ID to PipelineConfig.
PipelineConfigs map[component.ID]*PipelineConfig
}
type PipelineConfig struct {
Receivers []component.ID
Processors []component.ID
Exporters []component.ID
}
type Graph struct {
// All component instances represented as nodes, with directed edges indicating data flow.
componentGraph *simple.DirectedGraph
// Keep track of how nodes relate to pipelines, so we can declare edges in the graph.
pipelines map[component.ID]*pipelineNodes
}
func Build(ctx context.Context, set Settings) (*Graph, error) {
pipelines := &Graph{
componentGraph: simple.NewDirectedGraph(),
pipelines: make(map[component.ID]*pipelineNodes, len(set.PipelineConfigs)),
}
for pipelineID := range set.PipelineConfigs {
pipelines.pipelines[pipelineID] = &pipelineNodes{
receivers: make(map[int64]graph.Node),
exporters: make(map[int64]graph.Node),
}
}
pipelines.createNodes(set)
pipelines.createEdges()
return pipelines, pipelines.buildComponents(ctx, set)
}
// Creates a node for each instance of a component and adds it to the graph
func (g *Graph) createNodes(set Settings) {
// Keep track of connectors and where they are used. (map[connectorID][]pipelineID)
connectorsAsExporter := make(map[component.ID][]component.ID)
connectorsAsReceiver := make(map[component.ID][]component.ID)
for pipelineID, pipelineCfg := range set.PipelineConfigs {
pipe := g.pipelines[pipelineID]
for _, recvID := range pipelineCfg.Receivers {
if set.ConnectorBuilder.IsConfigured(recvID) {
connectorsAsReceiver[recvID] = append(connectorsAsReceiver[recvID], pipelineID)
continue
}
rcvrNode := g.createReceiver(pipelineID.Type(), recvID)
pipe.receivers[rcvrNode.ID()] = rcvrNode
}
pipe.capabilitiesNode = newCapabilitiesNode(pipelineID)
for _, procID := range pipelineCfg.Processors {
pipe.processors = append(pipe.processors, g.createProcessor(pipelineID, procID))
}
pipe.fanOutNode = newFanOutNode(pipelineID)
for _, exprID := range pipelineCfg.Exporters {
if set.ConnectorBuilder.IsConfigured(exprID) {
connectorsAsExporter[exprID] = append(connectorsAsExporter[exprID], pipelineID)
continue
}
expNode := g.createExporter(pipelineID.Type(), exprID)
pipe.exporters[expNode.ID()] = expNode
}
}
for connID, exprPipelineIDs := range connectorsAsExporter {
for _, eID := range exprPipelineIDs {
for _, rID := range connectorsAsReceiver[connID] {
connNode := g.createConnector(eID, rID, connID)
g.pipelines[eID].exporters[connNode.ID()] = connNode
g.pipelines[rID].receivers[connNode.ID()] = connNode
}
}
}
}
func (g *Graph) createReceiver(pipelineType component.DataType, recvID component.ID) *receiverNode {
rcvrNode := newReceiverNode(pipelineType, recvID)
if node := g.componentGraph.Node(rcvrNode.ID()); node != nil {
return node.(*receiverNode)
}
g.componentGraph.AddNode(rcvrNode)
return rcvrNode
}
func (g *Graph) createProcessor(pipelineID, procID component.ID) *processorNode {
procNode := newProcessorNode(pipelineID, procID)
g.componentGraph.AddNode(procNode)
return procNode
}
func (g *Graph) createExporter(pipelineType component.DataType, exprID component.ID) *exporterNode {
expNode := newExporterNode(pipelineType, exprID)
if node := g.componentGraph.Node(expNode.ID()); node != nil {
return node.(*exporterNode)
}
g.componentGraph.AddNode(expNode)
return expNode
}
func (g *Graph) createConnector(exprPipelineID, rcvrPipelineID, connID component.ID) *connectorNode {
connNode := newConnectorNode(exprPipelineID.Type(), rcvrPipelineID.Type(), connID)
if node := g.componentGraph.Node(connNode.ID()); node != nil {
return node.(*connectorNode)
}
g.componentGraph.AddNode(connNode)
return connNode
}
func (g *Graph) createEdges() {
for _, pg := range g.pipelines {
for _, receiver := range pg.receivers {
g.componentGraph.SetEdge(g.componentGraph.NewEdge(receiver, pg.capabilitiesNode))
}
var from, to graph.Node
from = pg.capabilitiesNode
for _, processor := range pg.processors {
to = processor
g.componentGraph.SetEdge(g.componentGraph.NewEdge(from, to))
from = processor
}
to = pg.fanOutNode
g.componentGraph.SetEdge(g.componentGraph.NewEdge(from, to))
for _, exporter := range pg.exporters {
g.componentGraph.SetEdge(g.componentGraph.NewEdge(pg.fanOutNode, exporter))
}
}
}
func (g *Graph) buildComponents(ctx context.Context, set Settings) error {
nodes, err := topo.Sort(g.componentGraph)
if err != nil {
return cycleErr(err, topo.DirectedCyclesIn(g.componentGraph))
}
for i := len(nodes) - 1; i >= 0; i-- {
node := nodes[i]
switch n := node.(type) {
case *receiverNode:
err = n.buildComponent(ctx, set.Telemetry, set.BuildInfo, set.ReceiverBuilder, g.nextConsumers(n.ID()))
case *processorNode:
err = n.buildComponent(ctx, set.Telemetry, set.BuildInfo, set.ProcessorBuilder, g.nextConsumers(n.ID())[0])
case *exporterNode:
err = n.buildComponent(ctx, set.Telemetry, set.BuildInfo, set.ExporterBuilder)
case *connectorNode:
err = n.buildComponent(ctx, set.Telemetry, set.BuildInfo, set.ConnectorBuilder, g.nextConsumers(n.ID()))
case *capabilitiesNode:
capability := consumer.Capabilities{MutatesData: false}
for _, proc := range g.pipelines[n.pipelineID].processors {
capability.MutatesData = capability.MutatesData || proc.getConsumer().Capabilities().MutatesData
}
next := g.nextConsumers(n.ID())[0]
switch n.pipelineID.Type() {
case component.DataTypeTraces:
cc := capabilityconsumer.NewTraces(next.(consumer.Traces), capability)
n.baseConsumer = cc
n.ConsumeTracesFunc = cc.ConsumeTraces
case component.DataTypeMetrics:
cc := capabilityconsumer.NewMetrics(next.(consumer.Metrics), capability)
n.baseConsumer = cc
n.ConsumeMetricsFunc = cc.ConsumeMetrics
case component.DataTypeLogs:
cc := capabilityconsumer.NewLogs(next.(consumer.Logs), capability)
n.baseConsumer = cc
n.ConsumeLogsFunc = cc.ConsumeLogs
}
case *fanOutNode:
nexts := g.nextConsumers(n.ID())
switch n.pipelineID.Type() {
case component.DataTypeTraces:
consumers := make([]consumer.Traces, 0, len(nexts))
for _, next := range nexts {
consumers = append(consumers, next.(consumer.Traces))
}
n.baseConsumer = fanoutconsumer.NewTraces(consumers)
case component.DataTypeMetrics:
consumers := make([]consumer.Metrics, 0, len(nexts))
for _, next := range nexts {
consumers = append(consumers, next.(consumer.Metrics))
}
n.baseConsumer = fanoutconsumer.NewMetrics(consumers)
case component.DataTypeLogs:
consumers := make([]consumer.Logs, 0, len(nexts))
for _, next := range nexts {
consumers = append(consumers, next.(consumer.Logs))
}
n.baseConsumer = fanoutconsumer.NewLogs(consumers)
}
}
if err != nil {
return err
}
}
return nil
}
// Find all nodes
func (g *Graph) nextConsumers(nodeID int64) []baseConsumer {
nextNodes := g.componentGraph.From(nodeID)
nexts := make([]baseConsumer, 0, nextNodes.Len())
for nextNodes.Next() {
nexts = append(nexts, nextNodes.Node().(consumerNode).getConsumer())
}
return nexts
}
// A node-based representation of a pipeline configuration.
type pipelineNodes struct {
// Use map to assist with deduplication of connector instances.
receivers map[int64]graph.Node
// The node to which receivers emit. Passes through to processors.
// Easily accessible as the first node in a pipeline.
*capabilitiesNode
// The order of processors is very important. Therefore use a slice for processors.
processors []*processorNode
// Emits to exporters.
*fanOutNode
// Use map to assist with deduplication of connector instances.
exporters map[int64]graph.Node
}
func (g *Graph) StartAll(ctx context.Context, host component.Host) error {
nodes, err := topo.Sort(g.componentGraph)
if err != nil {
return err
}
// Start in reverse topological order so that downstream components
// are started before upstream components. This ensures that each
// component's consumer is ready to consume.
for i := len(nodes) - 1; i >= 0; i-- {
comp, ok := nodes[i].(component.Component)
if !ok {
// Skip capabilities/fanout nodes
continue
}
if compErr := comp.Start(ctx, host); compErr != nil {
return compErr
}
}
return nil
}
func (g *Graph) ShutdownAll(ctx context.Context) error {
nodes, err := topo.Sort(g.componentGraph)
if err != nil {
return err
}
// Stop in topological order so that upstream components
// are stopped before downstream components. This ensures
// that each component has a chance to drain to its consumer
// before the consumer is stopped.
var errs error
for i := 0; i < len(nodes); i++ {
comp, ok := nodes[i].(component.Component)
if !ok {
// Skip capabilities/fanout nodes
continue
}
errs = multierr.Append(errs, comp.Shutdown(ctx))
}
return errs
}
// Deprecated: [0.79.0] This function will be removed in the future.
// Several components in the contrib repository use this function so it cannot be removed
// before those cases are removed. In most cases, use of this function can be replaced by a
// connector. See https://github.com/open-telemetry/opentelemetry-collector/issues/7370 and
// https://github.com/open-telemetry/opentelemetry-collector/pull/7390#issuecomment-1483710184
// for additional information.
func (g *Graph) GetExporters() map[component.DataType]map[component.ID]component.Component {
exportersMap := make(map[component.DataType]map[component.ID]component.Component)
exportersMap[component.DataTypeTraces] = make(map[component.ID]component.Component)
exportersMap[component.DataTypeMetrics] = make(map[component.ID]component.Component)
exportersMap[component.DataTypeLogs] = make(map[component.ID]component.Component)
for _, pg := range g.pipelines {
for _, expNode := range pg.exporters {
// Skip connectors, otherwise individual components can introduce cycles
if expNode, ok := g.componentGraph.Node(expNode.ID()).(*exporterNode); ok {
exportersMap[expNode.pipelineType][expNode.componentID] = expNode.Component
}
}
}
return exportersMap
}
func cycleErr(err error, cycles [][]graph.Node) error {
var topoErr topo.Unorderable
if !errors.As(err, &topoErr) || len(cycles) == 0 || len(cycles[0]) == 0 {
return err
}
// There may be multiple cycles, but report only the first one.
cycle := cycles[0]
// The last node is a duplicate of the first node.
// Remove it because we may start from a different node.
cycle = cycle[:len(cycle)-1]
// A cycle always contains a connector. For the sake of consistent
// error messages report the cycle starting from a connector.
for i := 0; i < len(cycle); i++ {
if _, ok := cycle[i].(*connectorNode); ok {
cycle = append(cycle[i:], cycle[:i]...)
break
}
}
// Repeat the first node at the end to clarify the cycle
cycle = append(cycle, cycle[0])
// Build the error message
componentDetails := make([]string, 0, len(cycle))
for _, node := range cycle {
switch n := node.(type) {
case *processorNode:
componentDetails = append(componentDetails, fmt.Sprintf("processor %q in pipeline %q", n.componentID, n.pipelineID))
case *connectorNode:
componentDetails = append(componentDetails, fmt.Sprintf("connector %q (%s to %s)", n.componentID, n.exprPipelineType, n.rcvrPipelineType))
default:
continue // skip capabilities/fanout nodes
}
}
return fmt.Errorf("cycle detected: %s", strings.Join(componentDetails, " -> "))
}