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