| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package graph // import "go.opentelemetry.io/collector/service/internal/graph" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
|
| 9 |
otelattr "go.opentelemetry.io/otel/attribute" |
| 10 |
|
| 11 |
"go.opentelemetry.io/collector/component" |
| 12 |
"go.opentelemetry.io/collector/connector" |
| 13 |
"go.opentelemetry.io/collector/connector/xconnector" |
| 14 |
"go.opentelemetry.io/collector/consumer" |
| 15 |
"go.opentelemetry.io/collector/consumer/xconsumer" |
| 16 |
"go.opentelemetry.io/collector/pipeline" |
| 17 |
"go.opentelemetry.io/collector/pipeline/xpipeline" |
| 18 |
"go.opentelemetry.io/collector/service/internal/attribute" |
| 19 |
"go.opentelemetry.io/collector/service/internal/builders" |
| 20 |
"go.opentelemetry.io/collector/service/internal/capabilityconsumer" |
| 21 |
"go.opentelemetry.io/collector/service/internal/componentattribute" |
| 22 |
"go.opentelemetry.io/collector/service/internal/metadata" |
| 23 |
"go.opentelemetry.io/collector/service/internal/obsconsumer" |
| 24 |
"go.opentelemetry.io/collector/service/internal/refconsumer" |
| 25 |
) |
| 26 |
|
| 27 |
const pipelineIDAttrKey = "otelcol.pipeline.id" |
| 28 |
|
| 29 |
var _ consumerNode = (*connectorNode)(nil) |
| 30 |
|
| 31 |
type connectorNode struct { |
| 32 |
attribute.Attributes |
| 33 |
componentID component.ID |
| 34 |
exprPipelineType pipeline.Signal |
| 35 |
rcvrPipelineType pipeline.Signal |
| 36 |
component.Component |
| 37 |
consumer baseConsumer |
| 38 |
} |
| 39 |
|
| 40 |
func newConnectorNode(exprPipelineType, rcvrPipelineType pipeline.Signal, connID component.ID) *connectorNode { |
| 41 |
return &connectorNode{ |
| 42 |
Attributes: attribute.Connector(exprPipelineType, rcvrPipelineType, connID), |
| 43 |
componentID: connID, |
| 44 |
exprPipelineType: exprPipelineType, |
| 45 |
rcvrPipelineType: rcvrPipelineType, |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
func (n *connectorNode) getConsumer() baseConsumer { |
| 50 |
return n.consumer |
| 51 |
} |
| 52 |
|
| 53 |
func (n *connectorNode) buildComponent( |
| 54 |
ctx context.Context, |
| 55 |
tel component.TelemetrySettings, |
| 56 |
info component.BuildInfo, |
| 57 |
builder *builders.ConnectorBuilder, |
| 58 |
nexts []baseConsumer, |
| 59 |
) error { |
| 60 |
set := connector.Settings{ |
| 61 |
ID: n.componentID, |
| 62 |
TelemetrySettings: componentattribute.TelemetrySettingsWithAttributes(tel, *n.Set()), |
| 63 |
BuildInfo: info, |
| 64 |
} |
| 65 |
|
| 66 |
switch n.rcvrPipelineType { |
| 67 |
case pipeline.SignalTraces: |
| 68 |
return n.buildTraces(ctx, set, builder, nexts) |
| 69 |
case pipeline.SignalMetrics: |
| 70 |
return n.buildMetrics(ctx, set, builder, nexts) |
| 71 |
case pipeline.SignalLogs: |
| 72 |
return n.buildLogs(ctx, set, builder, nexts) |
| 73 |
case xpipeline.SignalProfiles: |
| 74 |
return n.buildProfiles(ctx, set, builder, nexts) |
| 75 |
} |
| 76 |
return nil |
| 77 |
} |
| 78 |
|
| 79 |
func (n *connectorNode) buildTraces( |
| 80 |
ctx context.Context, |
| 81 |
set connector.Settings, |
| 82 |
builder *builders.ConnectorBuilder, |
| 83 |
nexts []baseConsumer, |
| 84 |
) error { |
| 85 |
tb, err := metadata.NewTelemetryBuilder(set.TelemetrySettings) |
| 86 |
if err != nil { |
| 87 |
return err |
| 88 |
} |
| 89 |
|
| 90 |
producedSettings := obsconsumer.Settings{ |
| 91 |
ItemCounter: tb.ConnectorProducedItems, |
| 92 |
SizeCounter: tb.ConnectorProducedSize, |
| 93 |
Logger: set.Logger, |
| 94 |
} |
| 95 |
consumedSettings := obsconsumer.Settings{ |
| 96 |
ItemCounter: tb.ConnectorConsumedItems, |
| 97 |
SizeCounter: tb.ConnectorConsumedSize, |
| 98 |
Logger: set.Logger, |
| 99 |
} |
| 100 |
|
| 101 |
consumers := make(map[pipeline.ID]consumer.Traces, len(nexts)) |
| 102 |
for _, next := range nexts { |
| 103 |
consumers[next.(*capabilitiesNode).pipelineID] = obsconsumer.NewTraces( |
| 104 |
next.(consumer.Traces), |
| 105 |
producedSettings, |
| 106 |
obsconsumer.WithStaticDataPointAttribute( |
| 107 |
otelattr.String( |
| 108 |
pipelineIDAttrKey, |
| 109 |
next.(*capabilitiesNode).pipelineID.String(), |
| 110 |
), |
| 111 |
), |
| 112 |
) |
| 113 |
} |
| 114 |
next := connector.NewTracesRouter(consumers) |
| 115 |
|
| 116 |
switch n.exprPipelineType { |
| 117 |
case pipeline.SignalTraces: |
| 118 |
n.Component, err = builder.CreateTracesToTraces(ctx, set, next) |
| 119 |
if err != nil { |
| 120 |
return err |
| 121 |
} |
| 122 |
|
| 123 |
// Connectors which might pass along data must inherit capabilities of all nexts |
| 124 |
n.consumer = obsconsumer.NewTraces( |
| 125 |
capabilityconsumer.NewTraces( |
| 126 |
n.Component.(consumer.Traces), |
| 127 |
aggregateCap(n.Component.(consumer.Traces), nexts), |
| 128 |
), |
| 129 |
consumedSettings, |
| 130 |
) |
| 131 |
n.consumer = refconsumer.NewTraces(n.consumer.(consumer.Traces)) |
| 132 |
case pipeline.SignalMetrics: |
| 133 |
n.Component, err = builder.CreateMetricsToTraces(ctx, set, next) |
| 134 |
if err != nil { |
| 135 |
return err |
| 136 |
} |
| 137 |
n.consumer = obsconsumer.NewMetrics(n.Component.(consumer.Metrics), consumedSettings) |
| 138 |
n.consumer = refconsumer.NewMetrics(n.consumer.(consumer.Metrics)) |
| 139 |
case pipeline.SignalLogs: |
| 140 |
n.Component, err = builder.CreateLogsToTraces(ctx, set, next) |
| 141 |
if err != nil { |
| 142 |
return err |
| 143 |
} |
| 144 |
n.consumer = obsconsumer.NewLogs(n.Component.(consumer.Logs), consumedSettings) |
| 145 |
n.consumer = refconsumer.NewLogs(n.consumer.(consumer.Logs)) |
| 146 |
case xpipeline.SignalProfiles: |
| 147 |
n.Component, err = builder.CreateProfilesToTraces(ctx, set, next) |
| 148 |
if err != nil { |
| 149 |
return err |
| 150 |
} |
| 151 |
n.consumer = obsconsumer.NewProfiles(n.Component.(xconsumer.Profiles), consumedSettings) |
| 152 |
n.consumer = refconsumer.NewProfiles(n.consumer.(xconsumer.Profiles)) |
| 153 |
} |
| 154 |
return nil |
| 155 |
} |
| 156 |
|
| 157 |
func (n *connectorNode) buildMetrics( |
| 158 |
ctx context.Context, |
| 159 |
set connector.Settings, |
| 160 |
builder *builders.ConnectorBuilder, |
| 161 |
nexts []baseConsumer, |
| 162 |
) error { |
| 163 |
tb, err := metadata.NewTelemetryBuilder(set.TelemetrySettings) |
| 164 |
if err != nil { |
| 165 |
return err |
| 166 |
} |
| 167 |
|
| 168 |
producedSettings := obsconsumer.Settings{ |
| 169 |
ItemCounter: tb.ConnectorProducedItems, |
| 170 |
SizeCounter: tb.ConnectorProducedSize, |
| 171 |
Logger: set.Logger, |
| 172 |
} |
| 173 |
consumedSettings := obsconsumer.Settings{ |
| 174 |
ItemCounter: tb.ConnectorConsumedItems, |
| 175 |
SizeCounter: tb.ConnectorConsumedSize, |
| 176 |
Logger: set.Logger, |
| 177 |
} |
| 178 |
|
| 179 |
consumers := make(map[pipeline.ID]consumer.Metrics, len(nexts)) |
| 180 |
for _, next := range nexts { |
| 181 |
consumers[next.(*capabilitiesNode).pipelineID] = obsconsumer.NewMetrics( |
| 182 |
next.(consumer.Metrics), |
| 183 |
producedSettings, |
| 184 |
obsconsumer.WithStaticDataPointAttribute( |
| 185 |
otelattr.String( |
| 186 |
pipelineIDAttrKey, |
| 187 |
next.(*capabilitiesNode).pipelineID.String(), |
| 188 |
), |
| 189 |
), |
| 190 |
) |
| 191 |
} |
| 192 |
next := connector.NewMetricsRouter(consumers) |
| 193 |
|
| 194 |
switch n.exprPipelineType { |
| 195 |
case pipeline.SignalMetrics: |
| 196 |
n.Component, err = builder.CreateMetricsToMetrics(ctx, set, next) |
| 197 |
if err != nil { |
| 198 |
return err |
| 199 |
} |
| 200 |
|
| 201 |
// Connectors which might pass along data must inherit capabilities of all nexts |
| 202 |
n.consumer = obsconsumer.NewMetrics( |
| 203 |
capabilityconsumer.NewMetrics( |
| 204 |
n.Component.(consumer.Metrics), |
| 205 |
aggregateCap(n.Component.(consumer.Metrics), nexts), |
| 206 |
), |
| 207 |
consumedSettings, |
| 208 |
) |
| 209 |
n.consumer = refconsumer.NewMetrics(n.consumer.(consumer.Metrics)) |
| 210 |
case pipeline.SignalTraces: |
| 211 |
n.Component, err = builder.CreateTracesToMetrics(ctx, set, next) |
| 212 |
if err != nil { |
| 213 |
return err |
| 214 |
} |
| 215 |
n.consumer = obsconsumer.NewTraces(n.Component.(consumer.Traces), consumedSettings) |
| 216 |
n.consumer = refconsumer.NewTraces(n.consumer.(consumer.Traces)) |
| 217 |
case pipeline.SignalLogs: |
| 218 |
n.Component, err = builder.CreateLogsToMetrics(ctx, set, next) |
| 219 |
if err != nil { |
| 220 |
return err |
| 221 |
} |
| 222 |
n.consumer = obsconsumer.NewLogs(n.Component.(consumer.Logs), consumedSettings) |
| 223 |
n.consumer = refconsumer.NewLogs(n.consumer.(consumer.Logs)) |
| 224 |
case xpipeline.SignalProfiles: |
| 225 |
n.Component, err = builder.CreateProfilesToMetrics(ctx, set, next) |
| 226 |
if err != nil { |
| 227 |
return err |
| 228 |
} |
| 229 |
n.consumer = obsconsumer.NewProfiles(n.Component.(xconsumer.Profiles), consumedSettings) |
| 230 |
n.consumer = refconsumer.NewProfiles(n.consumer.(xconsumer.Profiles)) |
| 231 |
} |
| 232 |
return nil |
| 233 |
} |
| 234 |
|
| 235 |
func (n *connectorNode) buildLogs( |
| 236 |
ctx context.Context, |
| 237 |
set connector.Settings, |
| 238 |
builder *builders.ConnectorBuilder, |
| 239 |
nexts []baseConsumer, |
| 240 |
) error { |
| 241 |
tb, err := metadata.NewTelemetryBuilder(set.TelemetrySettings) |
| 242 |
if err != nil { |
| 243 |
return err |
| 244 |
} |
| 245 |
|
| 246 |
producedSettings := obsconsumer.Settings{ |
| 247 |
ItemCounter: tb.ConnectorProducedItems, |
| 248 |
SizeCounter: tb.ConnectorProducedSize, |
| 249 |
Logger: set.Logger, |
| 250 |
} |
| 251 |
consumedSettings := obsconsumer.Settings{ |
| 252 |
ItemCounter: tb.ConnectorConsumedItems, |
| 253 |
SizeCounter: tb.ConnectorConsumedSize, |
| 254 |
Logger: set.Logger, |
| 255 |
} |
| 256 |
|
| 257 |
consumers := make(map[pipeline.ID]consumer.Logs, len(nexts)) |
| 258 |
for _, next := range nexts { |
| 259 |
consumers[next.(*capabilitiesNode).pipelineID] = obsconsumer.NewLogs( |
| 260 |
next.(consumer.Logs), |
| 261 |
producedSettings, |
| 262 |
obsconsumer.WithStaticDataPointAttribute( |
| 263 |
otelattr.String( |
| 264 |
pipelineIDAttrKey, |
| 265 |
next.(*capabilitiesNode).pipelineID.String(), |
| 266 |
), |
| 267 |
), |
| 268 |
) |
| 269 |
} |
| 270 |
next := connector.NewLogsRouter(consumers) |
| 271 |
|
| 272 |
switch n.exprPipelineType { |
| 273 |
case pipeline.SignalLogs: |
| 274 |
n.Component, err = builder.CreateLogsToLogs(ctx, set, next) |
| 275 |
if err != nil { |
| 276 |
return err |
| 277 |
} |
| 278 |
|
| 279 |
// Connectors which might pass along data must inherit capabilities of all nexts |
| 280 |
n.consumer = obsconsumer.NewLogs( |
| 281 |
capabilityconsumer.NewLogs( |
| 282 |
n.Component.(consumer.Logs), |
| 283 |
aggregateCap(n.Component.(consumer.Logs), nexts), |
| 284 |
), |
| 285 |
consumedSettings, |
| 286 |
) |
| 287 |
n.consumer = refconsumer.NewLogs(n.consumer.(consumer.Logs)) |
| 288 |
case pipeline.SignalTraces: |
| 289 |
n.Component, err = builder.CreateTracesToLogs(ctx, set, next) |
| 290 |
if err != nil { |
| 291 |
return err |
| 292 |
} |
| 293 |
n.consumer = obsconsumer.NewTraces(n.Component.(consumer.Traces), consumedSettings) |
| 294 |
n.consumer = refconsumer.NewTraces(n.consumer.(consumer.Traces)) |
| 295 |
case pipeline.SignalMetrics: |
| 296 |
n.Component, err = builder.CreateMetricsToLogs(ctx, set, next) |
| 297 |
if err != nil { |
| 298 |
return err |
| 299 |
} |
| 300 |
n.consumer = obsconsumer.NewMetrics(n.Component.(consumer.Metrics), consumedSettings) |
| 301 |
n.consumer = refconsumer.NewMetrics(n.consumer.(consumer.Metrics)) |
| 302 |
case xpipeline.SignalProfiles: |
| 303 |
n.Component, err = builder.CreateProfilesToLogs(ctx, set, next) |
| 304 |
if err != nil { |
| 305 |
return err |
| 306 |
} |
| 307 |
n.consumer = obsconsumer.NewProfiles(n.Component.(xconsumer.Profiles), consumedSettings) |
| 308 |
n.consumer = refconsumer.NewProfiles(n.consumer.(xconsumer.Profiles)) |
| 309 |
} |
| 310 |
return nil |
| 311 |
} |
| 312 |
|
| 313 |
func (n *connectorNode) buildProfiles( |
| 314 |
ctx context.Context, |
| 315 |
set connector.Settings, |
| 316 |
builder *builders.ConnectorBuilder, |
| 317 |
nexts []baseConsumer, |
| 318 |
) error { |
| 319 |
tb, err := metadata.NewTelemetryBuilder(set.TelemetrySettings) |
| 320 |
if err != nil { |
| 321 |
return err |
| 322 |
} |
| 323 |
|
| 324 |
producedSettings := obsconsumer.Settings{ |
| 325 |
ItemCounter: tb.ConnectorProducedItems, |
| 326 |
SizeCounter: tb.ConnectorProducedSize, |
| 327 |
Logger: set.Logger, |
| 328 |
} |
| 329 |
consumedSettings := obsconsumer.Settings{ |
| 330 |
ItemCounter: tb.ConnectorConsumedItems, |
| 331 |
SizeCounter: tb.ConnectorConsumedSize, |
| 332 |
Logger: set.Logger, |
| 333 |
} |
| 334 |
|
| 335 |
consumers := make(map[pipeline.ID]xconsumer.Profiles, len(nexts)) |
| 336 |
for _, next := range nexts { |
| 337 |
consumers[next.(*capabilitiesNode).pipelineID] = obsconsumer.NewProfiles( |
| 338 |
next.(xconsumer.Profiles), |
| 339 |
producedSettings, |
| 340 |
obsconsumer.WithStaticDataPointAttribute( |
| 341 |
otelattr.String( |
| 342 |
pipelineIDAttrKey, |
| 343 |
next.(*capabilitiesNode).pipelineID.String(), |
| 344 |
), |
| 345 |
), |
| 346 |
) |
| 347 |
} |
| 348 |
next := xconnector.NewProfilesRouter(consumers) |
| 349 |
|
| 350 |
switch n.exprPipelineType { |
| 351 |
case xpipeline.SignalProfiles: |
| 352 |
n.Component, err = builder.CreateProfilesToProfiles(ctx, set, next) |
| 353 |
if err != nil { |
| 354 |
return err |
| 355 |
} |
| 356 |
|
| 357 |
// Connectors which might pass along data must inherit capabilities of all nexts |
| 358 |
n.consumer = obsconsumer.NewProfiles( |
| 359 |
capabilityconsumer.NewProfiles( |
| 360 |
n.Component.(xconsumer.Profiles), |
| 361 |
aggregateCap(n.Component.(xconsumer.Profiles), nexts), |
| 362 |
), |
| 363 |
consumedSettings, |
| 364 |
) |
| 365 |
n.consumer = refconsumer.NewProfiles(n.consumer.(xconsumer.Profiles)) |
| 366 |
case pipeline.SignalTraces: |
| 367 |
n.Component, err = builder.CreateTracesToProfiles(ctx, set, next) |
| 368 |
if err != nil { |
| 369 |
return err |
| 370 |
} |
| 371 |
n.consumer = obsconsumer.NewTraces(n.Component.(consumer.Traces), consumedSettings) |
| 372 |
n.consumer = refconsumer.NewTraces(n.consumer.(consumer.Traces)) |
| 373 |
case pipeline.SignalMetrics: |
| 374 |
n.Component, err = builder.CreateMetricsToProfiles(ctx, set, next) |
| 375 |
if err != nil { |
| 376 |
return err |
| 377 |
} |
| 378 |
n.consumer = obsconsumer.NewMetrics(n.Component.(consumer.Metrics), consumedSettings) |
| 379 |
n.consumer = refconsumer.NewMetrics(n.consumer.(consumer.Metrics)) |
| 380 |
case pipeline.SignalLogs: |
| 381 |
n.Component, err = builder.CreateLogsToProfiles(ctx, set, next) |
| 382 |
if err != nil { |
| 383 |
return err |
| 384 |
} |
| 385 |
n.consumer = obsconsumer.NewLogs(n.Component.(consumer.Logs), consumedSettings) |
| 386 |
n.consumer = refconsumer.NewLogs(n.consumer.(consumer.Logs)) |
| 387 |
} |
| 388 |
return nil |
| 389 |
} |
| 390 |
|
| 391 |
// When connecting pipelines of the same data type, the connector must |
| 392 |
// inherit the capabilities of pipelines in which it is acting as a receiver. |
| 393 |
// Since the incoming and outgoing data types are the same, we must also consider |
| 394 |
// that the connector itself may mutate the data and pass it along. |
| 395 |
func aggregateCap(base baseConsumer, nexts []baseConsumer) consumer.Capabilities { |
| 396 |
capabilities := base.Capabilities() |
| 397 |
for _, next := range nexts { |
| 398 |
capabilities.MutatesData = capabilities.MutatesData || next.Capabilities().MutatesData |
| 399 |
} |
| 400 |
return capabilities |
| 401 |
} |