Debian Codegraph @

otelcol/command.go

raw
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 "errors"
8 "flag"
9 "fmt"
10 "os"
11 "text/tabwriter"
12
13 "github.com/spf13/cobra"
14
15 "go.opentelemetry.io/collector/featuregate"
16 )
17
18 // NewCommand constructs a new cobra.Command using the given CollectorSettings.
19 // Any URIs specified in CollectorSettings.ConfigProviderSettings.ResolverSettings.URIs
20 // are considered defaults and will be overwritten by config flags passed as
21 // command-line arguments to the executable.
22 // At least one Provider must be set.
23 func NewCommand(set CollectorSettings) *cobra.Command {
24 flagSet := flags(featuregate.GlobalRegistry())
25 rootCmd := &cobra.Command{
26 Use: set.BuildInfo.Command,
27 Version: set.BuildInfo.Version,
28 SilenceUsage: true,
29 RunE: func(cmd *cobra.Command, _ []string) error {
30 err := updateSettingsUsingFlags(&set, flagSet)
31 if err != nil {
32 return err
33 }
34
35 col, err := NewCollector(set)
36 if err != nil {
37 return err
38 }
39 return col.Run(cmd.Context())
40 },
41 }
42 rootCmd.AddCommand(newFeatureGateCommand())
43 rootCmd.AddCommand(newComponentsCommand(set))
44 rootCmd.AddCommand(newValidateSubCommand(set, flagSet))
45 rootCmd.AddCommand(newConfigPrintSubCommand(set, flagSet))
46 rootCmd.Flags().AddGoFlagSet(flagSet)
47 return rootCmd
48 }
49
50 // Puts command line flags from flags into the CollectorSettings, to be used during config resolution.
51 func updateSettingsUsingFlags(set *CollectorSettings, flags *flag.FlagSet) error {
52 resolverSet := &set.ConfigProviderSettings.ResolverSettings
53 configFlags := getConfigFlag(flags)
54
55 if len(configFlags) > 0 {
56 resolverSet.URIs = configFlags
57 }
58 if len(resolverSet.URIs) == 0 {
59 return errors.New("at least one config flag must be provided")
60 }
61
62 if set.ConfigProviderSettings.ResolverSettings.DefaultScheme == "" {
63 set.ConfigProviderSettings.ResolverSettings.DefaultScheme = "env"
64 }
65
66 if len(resolverSet.ProviderFactories) == 0 {
67 return errors.New("at least one Provider must be supplied")
68 }
69
70 return nil
71 }
72
73 func newFeatureGateCommand() *cobra.Command {
74 return &cobra.Command{
75 Use: "featuregate [feature-id]",
76 Short: "Display feature gates information",
77 Long: "Display information about available feature gates and their status",
78 RunE: func(_ *cobra.Command, args []string) error {
79 if len(args) > 0 {
80 found := false
81 featuregate.GlobalRegistry().VisitAll(func(g *featuregate.Gate) {
82 if g.ID() == args[0] {
83 found = true
84 fmt.Printf("Feature: %s\n", g.ID())
85 fmt.Printf("Enabled: %v\n", g.IsEnabled())
86 fmt.Printf("Stage: %s\n", g.Stage())
87 fmt.Printf("Description: %s\n", g.Description())
88 fmt.Printf("From Version: %s\n", g.FromVersion())
89 if g.ToVersion() != "" {
90 fmt.Printf("To Version: %s\n", g.ToVersion())
91 }
92 }
93 })
94 if !found {
95 return fmt.Errorf("feature %q not found", args[0])
96 }
97 return nil
98 }
99
100 w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
101 fmt.Fprintf(w, "ID\tEnabled\tStage\tDescription\n")
102 featuregate.GlobalRegistry().VisitAll(func(g *featuregate.Gate) {
103 fmt.Fprintf(w, "%s\t%v\t%s\t%s\n",
104 g.ID(),
105 g.IsEnabled(),
106 g.Stage(),
107 g.Description())
108 })
109 return w.Flush()
110 },
111 }
112 }