-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathtelemetry_cmd.go
More file actions
259 lines (223 loc) · 7.16 KB
/
Copy pathtelemetry_cmd.go
File metadata and controls
259 lines (223 loc) · 7.16 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"time"
"github.com/spf13/cobra"
clioutput "github.com/smart-mcp-proxy/mcpproxy-go/internal/cli/output"
"github.com/smart-mcp-proxy/mcpproxy-go/internal/cliclient"
"github.com/smart-mcp-proxy/mcpproxy-go/internal/config"
"github.com/smart-mcp-proxy/mcpproxy-go/internal/socket"
"github.com/smart-mcp-proxy/mcpproxy-go/internal/telemetry"
)
// 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"`
EnvOverrideName string `json:"env_override_name,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())
telemetryCmd.AddCommand(getTelemetryShowPayloadCommand())
return telemetryCmd
}
func getTelemetryShowPayloadCommand() *cobra.Command {
return &cobra.Command{
Use: "show-payload",
Short: "Print the next telemetry payload as JSON (requires running daemon)",
Long: `Print the exact JSON heartbeat payload that mcpproxy would next
send to the telemetry endpoint, without making any network call. Counters in
the payload reflect the current in-memory state of the running daemon. Spec 042.
Use this command to audit what telemetry mcpproxy collects on your install.
Requires the daemon to be running so runtime stats (server_count,
connected_server_count, tool_count, surface_requests, etc.) are populated.
Start the daemon with: mcpproxy serve`,
RunE: runTelemetryShowPayload,
}
}
func runTelemetryShowPayload(_ *cobra.Command, _ []string) error {
cfg, err := loadTelemetryConfig()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
// Require running daemon so runtime stats are populated. Offline mode
// would emit zero-valued runtime fields and mislead users.
socketPath := socket.DetectSocketPath(cfg.DataDir)
if !socket.IsSocketAvailable(socketPath) {
return fmt.Errorf("telemetry show-payload requires running daemon. Start with: mcpproxy serve")
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client := cliclient.NewClient(socketPath, nil)
payload, err := client.GetTelemetryPayload(ctx)
if err != nil {
return fmt.Errorf("failed to get telemetry payload from daemon: %w", err)
}
data, err := json.MarshalIndent(payload, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal payload: %w", err)
}
fmt.Println(string(data))
return nil
}
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
}
// Spec 042: env vars override config (DO_NOT_TRACK > CI > MCPPROXY_TELEMETRY).
if disabled, reason := telemetry.IsDisabledByEnv(); disabled {
status.EnvOverride = true
status.EnvOverrideName = string(reason)
status.Enabled = false
}
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:", status.EnvOverrideName)
}
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 := telemetryConfigSavePath(cfg)
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 := telemetryConfigSavePath(cfg)
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
}
// telemetryConfigSavePath returns the config path that telemetry subcommands
// should write to. It mirrors loadTelemetryConfig: when the user passed
// --config, that exact file is used; otherwise the default derived from
// DataDir. This fixes a bug where enable/disable always wrote to the default
// location regardless of --config (pre-existing from PR #345 / Spec 036).
func telemetryConfigSavePath(cfg *config.Config) string {
if configFile != "" {
return configFile
}
return config.GetConfigPath(cfg.DataDir)
}