-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathtelemetry_cmd.go
More file actions
230 lines (199 loc) · 6.08 KB
/
Copy pathtelemetry_cmd.go
File metadata and controls
230 lines (199 loc) · 6.08 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
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/spf13/cobra"
"go.uber.org/zap"
clioutput "github.com/smart-mcp-proxy/mcpproxy-go/internal/cli/output"
"github.com/smart-mcp-proxy/mcpproxy-go/internal/config"
"github.com/smart-mcp-proxy/mcpproxy-go/internal/httpapi"
"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 (no network call)",
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. Spec 042.
Use this command to audit what telemetry mcpproxy collects on your install.`,
RunE: runTelemetryShowPayload,
}
}
func runTelemetryShowPayload(_ *cobra.Command, _ []string) error {
cfg, err := loadTelemetryConfig()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
configPath := config.GetConfigPath(cfg.DataDir)
// Build a non-running telemetry service. We never call Start, so no
// goroutine is launched and no network call is made.
svc := telemetry.New(cfg, configPath, httpapi.GetBuildVersion(), Edition, zap.NewNop())
payload := svc.BuildPayload()
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 := 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
}