From 864fe0df49f7dea765cc8529cc3b01652b8d1be5 Mon Sep 17 00:00:00 2001 From: danish9039 Date: Sun, 5 Jan 2025 03:16:41 +0530 Subject: [PATCH 1/4] add feature command Signed-off-by: danish9039 --- otelcol/command.go | 19 +++++++++++++++ service/display_feature.go | 44 ++++++++++++++++++++++++++++++++++ service/internal/graph/host.go | 4 ++-- 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 service/display_feature.go diff --git a/otelcol/command.go b/otelcol/command.go index 6efd16df562..6ef0809a15c 100644 --- a/otelcol/command.go +++ b/otelcol/command.go @@ -4,12 +4,16 @@ package otelcol // import "go.opentelemetry.io/collector/otelcol" import ( + // Standard library "errors" "flag" + // Third party "github.com/spf13/cobra" + // Project internal "go.opentelemetry.io/collector/featuregate" + "go.opentelemetry.io/collector/service" ) // NewCommand constructs a new cobra.Command using the given CollectorSettings. @@ -36,6 +40,7 @@ func NewCommand(set CollectorSettings) *cobra.Command { return col.Run(cmd.Context()) }, } + rootCmd.AddCommand(newFeaturesCommand()) rootCmd.AddCommand(newComponentsCommand(set)) rootCmd.AddCommand(newValidateSubCommand(set, flagSet)) rootCmd.Flags().AddGoFlagSet(flagSet) @@ -63,3 +68,17 @@ func updateSettingsUsingFlags(set *CollectorSettings, flags *flag.FlagSet) error } return nil } + +func newFeaturesCommand() *cobra.Command { + return &cobra.Command{ + Use: "features [feature-id]", + Short: "Display feature gates information", + Long: "Display information about available feature gates and their status", + RunE: func(cmd *cobra.Command, args []string) error { + if len(args) > 0 { + return service.DisplayFeature(args[0]) + } + return service.DisplayFeatures() + }, + } +} diff --git a/service/display_feature.go b/service/display_feature.go new file mode 100644 index 00000000000..efb7def7f6b --- /dev/null +++ b/service/display_feature.go @@ -0,0 +1,44 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package service // import "go.opentelemetry.io/collector/service" + +import ( + "fmt" + "os" + "text/tabwriter" + + "go.opentelemetry.io/collector/service/internal/graph" +) + +func DisplayFeatures() error { + w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) + fmt.Fprintf(w, "ID\tEnabled\tStage\tDescription\n") + data := graph.GetFeaturesTableData() + for _, row := range data.Rows { + fmt.Fprintf(w, "%s\t%v\t%s\t%s\n", + row.ID, + row.Enabled, + row.Stage, + row.Description) + } + return w.Flush() +} + +func DisplayFeature(id string) error { + data := graph.GetFeaturesTableData() + for _, row := range data.Rows { + if row.ID == id { + fmt.Printf("Feature: %s\n", row.ID) + fmt.Printf("Enabled: %v\n", row.Enabled) + fmt.Printf("Stage: %s\n", row.Stage) + fmt.Printf("Description: %s\n", row.Description) + fmt.Printf("From Version: %s\n", row.FromVersion) + if row.ToVersion != "" { + fmt.Printf("To Version: %s\n", row.ToVersion) + } + return nil + } + } + return fmt.Errorf("feature %q not found", id) +} diff --git a/service/internal/graph/host.go b/service/internal/graph/host.go index 3aaef5e055f..4d72a8b5bb5 100644 --- a/service/internal/graph/host.go +++ b/service/internal/graph/host.go @@ -138,11 +138,11 @@ func (host *Host) zPagesRequest(w http.ResponseWriter, _ *http.Request) { func handleFeaturezRequest(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf-8") zpages.WriteHTMLPageHeader(w, zpages.HeaderData{Title: "Feature Gates"}) - zpages.WriteHTMLFeaturesTable(w, getFeaturesTableData()) + zpages.WriteHTMLFeaturesTable(w, GetFeaturesTableData()) zpages.WriteHTMLPageFooter(w) } -func getFeaturesTableData() zpages.FeatureGateTableData { +func GetFeaturesTableData() zpages.FeatureGateTableData { data := zpages.FeatureGateTableData{} featuregate.GlobalRegistry().VisitAll(func(gate *featuregate.Gate) { data.Rows = append(data.Rows, zpages.FeatureGateTableRowData{ From dde94a2dd51af5de513b71b202a7c09168456eed Mon Sep 17 00:00:00 2001 From: danish9039 Date: Sun, 5 Jan 2025 03:30:55 +0530 Subject: [PATCH 2/4] .. Signed-off-by: danish9039 --- otelcol/command.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/otelcol/command.go b/otelcol/command.go index 6ef0809a15c..4558fef42e8 100644 --- a/otelcol/command.go +++ b/otelcol/command.go @@ -4,14 +4,11 @@ package otelcol // import "go.opentelemetry.io/collector/otelcol" import ( - // Standard library "errors" "flag" - // Third party "github.com/spf13/cobra" - // Project internal "go.opentelemetry.io/collector/featuregate" "go.opentelemetry.io/collector/service" ) From 3afa8e3e60259e571fee6a700c17a3e5bf1c8629 Mon Sep 17 00:00:00 2001 From: danish9039 Date: Tue, 7 Jan 2025 01:34:46 +0530 Subject: [PATCH 3/4] fix Signed-off-by: danish9039 --- otelcol/command.go | 56 ++++++++++++++++++++++++++-------- service/display_feature.go | 44 -------------------------- service/internal/graph/host.go | 4 +-- 3 files changed, 45 insertions(+), 59 deletions(-) delete mode 100644 service/display_feature.go diff --git a/otelcol/command.go b/otelcol/command.go index 4558fef42e8..c22f2ffbe77 100644 --- a/otelcol/command.go +++ b/otelcol/command.go @@ -6,11 +6,13 @@ package otelcol // import "go.opentelemetry.io/collector/otelcol" import ( "errors" "flag" - + "fmt" + "text/tabwriter" + "os" "github.com/spf13/cobra" "go.opentelemetry.io/collector/featuregate" - "go.opentelemetry.io/collector/service" + //"go.opentelemetry.io/collector/service" ) // NewCommand constructs a new cobra.Command using the given CollectorSettings. @@ -67,15 +69,43 @@ func updateSettingsUsingFlags(set *CollectorSettings, flags *flag.FlagSet) error } func newFeaturesCommand() *cobra.Command { - return &cobra.Command{ - Use: "features [feature-id]", - Short: "Display feature gates information", - Long: "Display information about available feature gates and their status", - RunE: func(cmd *cobra.Command, args []string) error { - if len(args) > 0 { - return service.DisplayFeature(args[0]) - } - return service.DisplayFeatures() - }, - } + return &cobra.Command{ + Use: "features [feature-id]", + Short: "Display feature gates information", + Long: "Display information about available feature gates and their status", + RunE: func(cmd *cobra.Command, args []string) error { + if len(args) > 0 { + found := false + featuregate.GlobalRegistry().VisitAll(func(g *featuregate.Gate) { + if g.ID() == args[0] { + found = true + fmt.Printf("Feature: %s\n", g.ID()) + fmt.Printf("Enabled: %v\n", g.IsEnabled()) + fmt.Printf("Stage: %s\n", g.Stage()) + fmt.Printf("Description: %s\n", g.Description()) + fmt.Printf("From Version: %s\n", g.FromVersion()) + if g.ToVersion() != "" { + fmt.Printf("To Version: %s\n", g.ToVersion()) + } + } + }) + if !found { + return fmt.Errorf("feature %q not found", args[0]) + } + return nil + } + + w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) + fmt.Fprintf(w, "ID\tEnabled\tStage\tDescription\n") + featuregate.GlobalRegistry().VisitAll(func(g *featuregate.Gate) { + fmt.Fprintf(w, "%s\t%v\t%s\t%s\n", + g.ID(), + g.IsEnabled(), + g.Stage(), + g.Description()) + }) + return w.Flush() + }, + } } + diff --git a/service/display_feature.go b/service/display_feature.go deleted file mode 100644 index efb7def7f6b..00000000000 --- a/service/display_feature.go +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -package service // import "go.opentelemetry.io/collector/service" - -import ( - "fmt" - "os" - "text/tabwriter" - - "go.opentelemetry.io/collector/service/internal/graph" -) - -func DisplayFeatures() error { - w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) - fmt.Fprintf(w, "ID\tEnabled\tStage\tDescription\n") - data := graph.GetFeaturesTableData() - for _, row := range data.Rows { - fmt.Fprintf(w, "%s\t%v\t%s\t%s\n", - row.ID, - row.Enabled, - row.Stage, - row.Description) - } - return w.Flush() -} - -func DisplayFeature(id string) error { - data := graph.GetFeaturesTableData() - for _, row := range data.Rows { - if row.ID == id { - fmt.Printf("Feature: %s\n", row.ID) - fmt.Printf("Enabled: %v\n", row.Enabled) - fmt.Printf("Stage: %s\n", row.Stage) - fmt.Printf("Description: %s\n", row.Description) - fmt.Printf("From Version: %s\n", row.FromVersion) - if row.ToVersion != "" { - fmt.Printf("To Version: %s\n", row.ToVersion) - } - return nil - } - } - return fmt.Errorf("feature %q not found", id) -} diff --git a/service/internal/graph/host.go b/service/internal/graph/host.go index 4d72a8b5bb5..3aaef5e055f 100644 --- a/service/internal/graph/host.go +++ b/service/internal/graph/host.go @@ -138,11 +138,11 @@ func (host *Host) zPagesRequest(w http.ResponseWriter, _ *http.Request) { func handleFeaturezRequest(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf-8") zpages.WriteHTMLPageHeader(w, zpages.HeaderData{Title: "Feature Gates"}) - zpages.WriteHTMLFeaturesTable(w, GetFeaturesTableData()) + zpages.WriteHTMLFeaturesTable(w, getFeaturesTableData()) zpages.WriteHTMLPageFooter(w) } -func GetFeaturesTableData() zpages.FeatureGateTableData { +func getFeaturesTableData() zpages.FeatureGateTableData { data := zpages.FeatureGateTableData{} featuregate.GlobalRegistry().VisitAll(func(gate *featuregate.Gate) { data.Rows = append(data.Rows, zpages.FeatureGateTableRowData{ From 52c1c61ad1682aa47956828d0221fd5f32cad1ad Mon Sep 17 00:00:00 2001 From: danish9039 Date: Tue, 7 Jan 2025 02:35:13 +0530 Subject: [PATCH 4/4] .. Signed-off-by: danish9039 --- otelcol/command.go | 79 +++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 40 deletions(-) diff --git a/otelcol/command.go b/otelcol/command.go index c22f2ffbe77..632603106f5 100644 --- a/otelcol/command.go +++ b/otelcol/command.go @@ -7,12 +7,12 @@ import ( "errors" "flag" "fmt" + "os" "text/tabwriter" - "os" + "github.com/spf13/cobra" "go.opentelemetry.io/collector/featuregate" - //"go.opentelemetry.io/collector/service" ) // NewCommand constructs a new cobra.Command using the given CollectorSettings. @@ -69,43 +69,42 @@ func updateSettingsUsingFlags(set *CollectorSettings, flags *flag.FlagSet) error } func newFeaturesCommand() *cobra.Command { - return &cobra.Command{ - Use: "features [feature-id]", - Short: "Display feature gates information", - Long: "Display information about available feature gates and their status", - RunE: func(cmd *cobra.Command, args []string) error { - if len(args) > 0 { - found := false - featuregate.GlobalRegistry().VisitAll(func(g *featuregate.Gate) { - if g.ID() == args[0] { - found = true - fmt.Printf("Feature: %s\n", g.ID()) - fmt.Printf("Enabled: %v\n", g.IsEnabled()) - fmt.Printf("Stage: %s\n", g.Stage()) - fmt.Printf("Description: %s\n", g.Description()) - fmt.Printf("From Version: %s\n", g.FromVersion()) - if g.ToVersion() != "" { - fmt.Printf("To Version: %s\n", g.ToVersion()) - } - } - }) - if !found { - return fmt.Errorf("feature %q not found", args[0]) - } - return nil - } + return &cobra.Command{ + Use: "features [feature-id]", + Short: "Display feature gates information", + Long: "Display information about available feature gates and their status", + RunE: func(_ *cobra.Command, args []string) error { + if len(args) > 0 { + found := false + featuregate.GlobalRegistry().VisitAll(func(g *featuregate.Gate) { + if g.ID() == args[0] { + found = true + fmt.Printf("Feature: %s\n", g.ID()) + fmt.Printf("Enabled: %v\n", g.IsEnabled()) + fmt.Printf("Stage: %s\n", g.Stage()) + fmt.Printf("Description: %s\n", g.Description()) + fmt.Printf("From Version: %s\n", g.FromVersion()) + if g.ToVersion() != "" { + fmt.Printf("To Version: %s\n", g.ToVersion()) + } + } + }) + if !found { + return fmt.Errorf("feature %q not found", args[0]) + } + return nil + } - w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) - fmt.Fprintf(w, "ID\tEnabled\tStage\tDescription\n") - featuregate.GlobalRegistry().VisitAll(func(g *featuregate.Gate) { - fmt.Fprintf(w, "%s\t%v\t%s\t%s\n", - g.ID(), - g.IsEnabled(), - g.Stage(), - g.Description()) - }) - return w.Flush() - }, - } + w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) + fmt.Fprintf(w, "ID\tEnabled\tStage\tDescription\n") + featuregate.GlobalRegistry().VisitAll(func(g *featuregate.Gate) { + fmt.Fprintf(w, "%s\t%v\t%s\t%s\n", + g.ID(), + g.IsEnabled(), + g.Stage(), + g.Description()) + }) + return w.Flush() + }, + } } -