|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + |
| 10 | + clioutput "github.com/smart-mcp-proxy/mcpproxy-go/internal/cli/output" |
| 11 | + "github.com/smart-mcp-proxy/mcpproxy-go/internal/config" |
| 12 | +) |
| 13 | + |
| 14 | +// TelemetryStatus holds status data for display. |
| 15 | +type TelemetryStatus struct { |
| 16 | + Enabled bool `json:"enabled"` |
| 17 | + AnonymousID string `json:"anonymous_id,omitempty"` |
| 18 | + Endpoint string `json:"endpoint"` |
| 19 | + EnvOverride bool `json:"env_override,omitempty"` |
| 20 | +} |
| 21 | + |
| 22 | +// GetTelemetryCommand returns the telemetry management command. |
| 23 | +func GetTelemetryCommand() *cobra.Command { |
| 24 | + telemetryCmd := &cobra.Command{ |
| 25 | + Use: "telemetry", |
| 26 | + Short: "Manage anonymous usage telemetry", |
| 27 | + Long: `Manage anonymous usage telemetry for MCPProxy. |
| 28 | +
|
| 29 | +Telemetry sends anonymous, non-identifiable usage statistics to help |
| 30 | +improve MCPProxy. No personal data, tool names, or server details are |
| 31 | +ever transmitted. |
| 32 | +
|
| 33 | +Examples: |
| 34 | + mcpproxy telemetry status # Show telemetry status |
| 35 | + mcpproxy telemetry enable # Enable telemetry |
| 36 | + mcpproxy telemetry disable # Disable telemetry`, |
| 37 | + } |
| 38 | + |
| 39 | + telemetryCmd.AddCommand(getTelemetryStatusCommand()) |
| 40 | + telemetryCmd.AddCommand(getTelemetryEnableCommand()) |
| 41 | + telemetryCmd.AddCommand(getTelemetryDisableCommand()) |
| 42 | + |
| 43 | + return telemetryCmd |
| 44 | +} |
| 45 | + |
| 46 | +func getTelemetryStatusCommand() *cobra.Command { |
| 47 | + return &cobra.Command{ |
| 48 | + Use: "status", |
| 49 | + Short: "Show telemetry status", |
| 50 | + RunE: runTelemetryStatus, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func getTelemetryEnableCommand() *cobra.Command { |
| 55 | + return &cobra.Command{ |
| 56 | + Use: "enable", |
| 57 | + Short: "Enable anonymous telemetry", |
| 58 | + RunE: runTelemetryEnable, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func getTelemetryDisableCommand() *cobra.Command { |
| 63 | + return &cobra.Command{ |
| 64 | + Use: "disable", |
| 65 | + Short: "Disable anonymous telemetry", |
| 66 | + RunE: runTelemetryDisable, |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func runTelemetryStatus(cmd *cobra.Command, _ []string) error { |
| 71 | + cfg, err := loadTelemetryConfig() |
| 72 | + if err != nil { |
| 73 | + return fmt.Errorf("failed to load config: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + status := TelemetryStatus{ |
| 77 | + Enabled: cfg.IsTelemetryEnabled(), |
| 78 | + Endpoint: cfg.GetTelemetryEndpoint(), |
| 79 | + } |
| 80 | + |
| 81 | + if id := cfg.GetAnonymousID(); id != "" { |
| 82 | + status.AnonymousID = id |
| 83 | + } |
| 84 | + |
| 85 | + if os.Getenv("MCPPROXY_TELEMETRY") == "false" { |
| 86 | + status.EnvOverride = true |
| 87 | + } |
| 88 | + |
| 89 | + format := clioutput.ResolveFormat(globalOutputFormat, globalJSONOutput) |
| 90 | + switch format { |
| 91 | + case "json": |
| 92 | + data, err := json.MarshalIndent(status, "", " ") |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + fmt.Println(string(data)) |
| 97 | + case "yaml": |
| 98 | + formatter, err := clioutput.NewFormatter("yaml") |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + output, err := formatter.Format(status) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + fmt.Println(output) |
| 107 | + default: |
| 108 | + fmt.Println("Telemetry Status") |
| 109 | + enabledStr := "Enabled" |
| 110 | + if !status.Enabled { |
| 111 | + enabledStr = "Disabled" |
| 112 | + } |
| 113 | + fmt.Printf(" %-14s %s\n", "Status:", enabledStr) |
| 114 | + if status.EnvOverride { |
| 115 | + fmt.Printf(" %-14s %s\n", "Override:", "MCPPROXY_TELEMETRY=false") |
| 116 | + } |
| 117 | + if status.AnonymousID != "" { |
| 118 | + fmt.Printf(" %-14s %s\n", "Anonymous ID:", status.AnonymousID) |
| 119 | + } |
| 120 | + fmt.Printf(" %-14s %s\n", "Endpoint:", status.Endpoint) |
| 121 | + } |
| 122 | + |
| 123 | + return nil |
| 124 | +} |
| 125 | + |
| 126 | +func runTelemetryEnable(cmd *cobra.Command, _ []string) error { |
| 127 | + cfg, err := loadTelemetryConfig() |
| 128 | + if err != nil { |
| 129 | + return fmt.Errorf("failed to load config: %w", err) |
| 130 | + } |
| 131 | + |
| 132 | + if cfg.Telemetry == nil { |
| 133 | + cfg.Telemetry = &config.TelemetryConfig{} |
| 134 | + } |
| 135 | + enabled := true |
| 136 | + cfg.Telemetry.Enabled = &enabled |
| 137 | + |
| 138 | + configPath := config.GetConfigPath(cfg.DataDir) |
| 139 | + if err := config.SaveConfig(cfg, configPath); err != nil { |
| 140 | + return fmt.Errorf("failed to save config: %w", err) |
| 141 | + } |
| 142 | + |
| 143 | + fmt.Println("Telemetry enabled.") |
| 144 | + if os.Getenv("MCPPROXY_TELEMETRY") == "false" { |
| 145 | + fmt.Println("Warning: MCPPROXY_TELEMETRY=false environment variable is set and will override this setting.") |
| 146 | + } |
| 147 | + return nil |
| 148 | +} |
| 149 | + |
| 150 | +func runTelemetryDisable(cmd *cobra.Command, _ []string) error { |
| 151 | + cfg, err := loadTelemetryConfig() |
| 152 | + if err != nil { |
| 153 | + return fmt.Errorf("failed to load config: %w", err) |
| 154 | + } |
| 155 | + |
| 156 | + if cfg.Telemetry == nil { |
| 157 | + cfg.Telemetry = &config.TelemetryConfig{} |
| 158 | + } |
| 159 | + disabled := false |
| 160 | + cfg.Telemetry.Enabled = &disabled |
| 161 | + |
| 162 | + configPath := config.GetConfigPath(cfg.DataDir) |
| 163 | + if err := config.SaveConfig(cfg, configPath); err != nil { |
| 164 | + return fmt.Errorf("failed to save config: %w", err) |
| 165 | + } |
| 166 | + |
| 167 | + fmt.Println("Telemetry disabled.") |
| 168 | + return nil |
| 169 | +} |
| 170 | + |
| 171 | +func loadTelemetryConfig() (*config.Config, error) { |
| 172 | + if configFile != "" { |
| 173 | + cfg, err := config.LoadFromFile(configFile) |
| 174 | + if err != nil { |
| 175 | + return nil, err |
| 176 | + } |
| 177 | + if dataDir != "" { |
| 178 | + cfg.DataDir = dataDir |
| 179 | + } |
| 180 | + return cfg, nil |
| 181 | + } |
| 182 | + cfg, err := config.Load() |
| 183 | + if err != nil { |
| 184 | + return nil, err |
| 185 | + } |
| 186 | + if dataDir != "" { |
| 187 | + cfg.DataDir = dataDir |
| 188 | + } |
| 189 | + return cfg, nil |
| 190 | +} |
0 commit comments