| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otelcol // import "go.opentelemetry.io/collector/otelcol" |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"sort" |
| 9 |
|
| 10 |
"github.com/spf13/cobra" |
| 11 |
yaml "go.yaml.in/yaml/v3" |
| 12 |
|
| 13 |
"go.opentelemetry.io/collector/component" |
| 14 |
"go.opentelemetry.io/collector/connector" |
| 15 |
"go.opentelemetry.io/collector/exporter" |
| 16 |
"go.opentelemetry.io/collector/extension" |
| 17 |
"go.opentelemetry.io/collector/processor" |
| 18 |
"go.opentelemetry.io/collector/receiver" |
| 19 |
) |
| 20 |
|
| 21 |
type componentWithStability struct { |
| 22 |
Name component.Type |
| 23 |
Module string |
| 24 |
Stability map[string]string |
| 25 |
} |
| 26 |
|
| 27 |
type componentWithoutStability struct { |
| 28 |
Scheme string `yaml:",omitempty"` |
| 29 |
Module string |
| 30 |
} |
| 31 |
|
| 32 |
type componentsOutput struct { |
| 33 |
BuildInfo component.BuildInfo |
| 34 |
Receivers []componentWithStability |
| 35 |
Processors []componentWithStability |
| 36 |
Exporters []componentWithStability |
| 37 |
Connectors []componentWithStability |
| 38 |
Extensions []componentWithStability |
| 39 |
Providers []componentWithoutStability |
| 40 |
Converters []componentWithoutStability `yaml:",omitempty"` |
| 41 |
} |
| 42 |
|
| 43 |
// newComponentsCommand constructs a new components command using the given CollectorSettings. |
| 44 |
func newComponentsCommand(set CollectorSettings) *cobra.Command { |
| 45 |
return &cobra.Command{ |
| 46 |
Use: "components", |
| 47 |
Short: "Outputs available components in this collector distribution", |
| 48 |
Long: "Outputs available components in this collector distribution including their stability levels. The output format is not stable and can change between releases.", |
| 49 |
Args: cobra.ExactArgs(0), |
| 50 |
RunE: func(cmd *cobra.Command, _ []string) error { |
| 51 |
factories, err := set.Factories() |
| 52 |
if err != nil { |
| 53 |
return fmt.Errorf("failed to initialize factories: %w", err) |
| 54 |
} |
| 55 |
|
| 56 |
components := componentsOutput{} |
| 57 |
for _, con := range sortFactoriesByType[connector.Factory](factories.Connectors) { |
| 58 |
components.Connectors = append(components.Connectors, componentWithStability{ |
| 59 |
Name: con.Type(), |
| 60 |
Module: factories.ConnectorModules[con.Type()], |
| 61 |
Stability: map[string]string{ |
| 62 |
"logs-to-logs": con.LogsToLogsStability().String(), |
| 63 |
"logs-to-metrics": con.LogsToMetricsStability().String(), |
| 64 |
"logs-to-traces": con.LogsToTracesStability().String(), |
| 65 |
|
| 66 |
"metrics-to-logs": con.MetricsToLogsStability().String(), |
| 67 |
"metrics-to-metrics": con.MetricsToMetricsStability().String(), |
| 68 |
"metrics-to-traces": con.MetricsToTracesStability().String(), |
| 69 |
|
| 70 |
"traces-to-logs": con.TracesToLogsStability().String(), |
| 71 |
"traces-to-metrics": con.TracesToMetricsStability().String(), |
| 72 |
"traces-to-traces": con.TracesToTracesStability().String(), |
| 73 |
}, |
| 74 |
}) |
| 75 |
} |
| 76 |
for _, ext := range sortFactoriesByType[extension.Factory](factories.Extensions) { |
| 77 |
components.Extensions = append(components.Extensions, componentWithStability{ |
| 78 |
Name: ext.Type(), |
| 79 |
Module: factories.ExtensionModules[ext.Type()], |
| 80 |
Stability: map[string]string{ |
| 81 |
"extension": ext.Stability().String(), |
| 82 |
}, |
| 83 |
}) |
| 84 |
} |
| 85 |
for _, prs := range sortFactoriesByType[processor.Factory](factories.Processors) { |
| 86 |
components.Processors = append(components.Processors, componentWithStability{ |
| 87 |
Name: prs.Type(), |
| 88 |
Module: factories.ProcessorModules[prs.Type()], |
| 89 |
Stability: map[string]string{ |
| 90 |
"logs": prs.LogsStability().String(), |
| 91 |
"metrics": prs.MetricsStability().String(), |
| 92 |
"traces": prs.TracesStability().String(), |
| 93 |
}, |
| 94 |
}) |
| 95 |
} |
| 96 |
for _, rcv := range sortFactoriesByType[receiver.Factory](factories.Receivers) { |
| 97 |
components.Receivers = append(components.Receivers, componentWithStability{ |
| 98 |
Name: rcv.Type(), |
| 99 |
Module: factories.ReceiverModules[rcv.Type()], |
| 100 |
Stability: map[string]string{ |
| 101 |
"logs": rcv.LogsStability().String(), |
| 102 |
"metrics": rcv.MetricsStability().String(), |
| 103 |
"traces": rcv.TracesStability().String(), |
| 104 |
}, |
| 105 |
}) |
| 106 |
} |
| 107 |
for _, exp := range sortFactoriesByType[exporter.Factory](factories.Exporters) { |
| 108 |
components.Exporters = append(components.Exporters, componentWithStability{ |
| 109 |
Name: exp.Type(), |
| 110 |
Module: factories.ExporterModules[exp.Type()], |
| 111 |
Stability: map[string]string{ |
| 112 |
"logs": exp.LogsStability().String(), |
| 113 |
"metrics": exp.MetricsStability().String(), |
| 114 |
"traces": exp.TracesStability().String(), |
| 115 |
}, |
| 116 |
}) |
| 117 |
} |
| 118 |
components.BuildInfo = set.BuildInfo |
| 119 |
|
| 120 |
for providerScheme, providerModuleModule := range set.ProviderModules { |
| 121 |
components.Providers = append(components.Providers, componentWithoutStability{ |
| 122 |
Scheme: providerScheme, |
| 123 |
Module: providerModuleModule, |
| 124 |
}) |
| 125 |
} |
| 126 |
|
| 127 |
for _, converterModule := range set.ConverterModules { |
| 128 |
components.Converters = append(components.Converters, componentWithoutStability{ |
| 129 |
Module: converterModule, |
| 130 |
}) |
| 131 |
} |
| 132 |
|
| 133 |
yamlData, err := yaml.Marshal(components) |
| 134 |
if err != nil { |
| 135 |
return err |
| 136 |
} |
| 137 |
fmt.Fprint(cmd.OutOrStdout(), string(yamlData)) |
| 138 |
return nil |
| 139 |
}, |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
func sortFactoriesByType[T component.Factory](factories map[component.Type]T) []T { |
| 144 |
// Gather component types (factories map keys) |
| 145 |
componentTypes := make([]component.Type, 0, len(factories)) |
| 146 |
for componentType := range factories { |
| 147 |
componentTypes = append(componentTypes, componentType) |
| 148 |
} |
| 149 |
|
| 150 |
// Sort component types as strings |
| 151 |
sort.Slice(componentTypes, func(i, j int) bool { |
| 152 |
return componentTypes[i].String() < componentTypes[j].String() |
| 153 |
}) |
| 154 |
|
| 155 |
// Build and return list of factories, sorted by component types |
| 156 |
sortedFactories := make([]T, 0, len(factories)) |
| 157 |
for _, componentType := range componentTypes { |
| 158 |
sortedFactories = append(sortedFactories, factories[componentType]) |
| 159 |
} |
| 160 |
|
| 161 |
return sortedFactories |
| 162 |
} |