Skip to content

Commit e811538

Browse files
committed
chore(cli): simplify the telemetry event data structure
1 parent 11a5165 commit e811538

File tree

3 files changed

+28
-67
lines changed

3 files changed

+28
-67
lines changed

src/client/acontext-cli/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ require (
66
github.com/AlecAivazis/survey/v2 v2.3.7
77
github.com/pelletier/go-toml/v2 v2.2.4
88
github.com/spf13/cobra v1.10.1
9-
github.com/spf13/pflag v1.0.10
109
github.com/stretchr/testify v1.9.0
1110
gopkg.in/yaml.v3 v3.0.1
1211
)
@@ -21,6 +20,7 @@ require (
2120
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
2221
github.com/pmezard/go-difflib v1.0.0 // indirect
2322
github.com/rogpeppe/go-internal v1.14.1 // indirect
23+
github.com/spf13/pflag v1.0.10 // indirect
2424
golang.org/x/sys v0.38.0 // indirect
2525
golang.org/x/term v0.37.0 // indirect
2626
golang.org/x/text v0.31.0 // indirect

src/client/acontext-cli/internal/telemetry/telemetry.go

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,14 @@ var telemetryBearerToken = ""
1919

2020
// Event represents a telemetry event
2121
type Event struct {
22-
Command string `json:"command"`
23-
Args []string `json:"args,omitempty"`
24-
Flags map[string]string `json:"flags,omitempty"`
25-
Success bool `json:"success"`
26-
Error string `json:"error,omitempty"`
27-
Duration int64 `json:"duration_ms"`
28-
Timestamp string `json:"timestamp"`
29-
Version string `json:"version"`
30-
OS string `json:"os"`
31-
Arch string `json:"arch"`
32-
CommandPath string `json:"command_path,omitempty"`
22+
Command string `json:"command"`
23+
Success bool `json:"success"`
24+
Error string `json:"error,omitempty"`
25+
Duration int64 `json:"duration_ms"`
26+
Timestamp string `json:"timestamp"`
27+
Version string `json:"version"`
28+
OS string `json:"os"`
29+
Arch string `json:"arch"`
3330
}
3431

3532
// SendEvent sends a telemetry event asynchronously
@@ -117,15 +114,12 @@ func sendEvent(event Event) error {
117114
}
118115

119116
// TrackCommand tracks a command execution
120-
func TrackCommand(command string, args []string, flags map[string]string, success bool, err error, duration time.Duration, version string) {
117+
func TrackCommand(command string, success bool, err error, duration time.Duration, version string) {
121118
event := Event{
122-
Command: command,
123-
Args: args,
124-
Flags: flags,
125-
Success: success,
126-
Duration: duration.Milliseconds(),
127-
Version: version,
128-
CommandPath: command,
119+
Command: command,
120+
Success: success,
121+
Duration: duration.Milliseconds(),
122+
Version: version,
129123
}
130124

131125
if err != nil {
@@ -136,15 +130,12 @@ func TrackCommand(command string, args []string, flags map[string]string, succes
136130
}
137131

138132
// TrackCommandAsync tracks a command execution asynchronously and returns a WaitGroup to wait for completion
139-
func TrackCommandAsync(command string, args []string, flags map[string]string, success bool, err error, duration time.Duration, version string) *sync.WaitGroup {
133+
func TrackCommandAsync(command string, success bool, err error, duration time.Duration, version string) *sync.WaitGroup {
140134
event := Event{
141-
Command: command,
142-
Args: args,
143-
Flags: flags,
144-
Success: success,
145-
Duration: duration.Milliseconds(),
146-
Version: version,
147-
CommandPath: command,
135+
Command: command,
136+
Success: success,
137+
Duration: duration.Milliseconds(),
138+
Version: version,
148139
}
149140

150141
if err != nil {
@@ -155,15 +146,12 @@ func TrackCommandAsync(command string, args []string, flags map[string]string, s
155146
}
156147

157148
// TrackCommandSync tracks a command execution synchronously and waits for completion
158-
func TrackCommandSync(command string, args []string, flags map[string]string, success bool, err error, duration time.Duration, version string) error {
149+
func TrackCommandSync(command string, success bool, err error, duration time.Duration, version string) error {
159150
event := Event{
160-
Command: command,
161-
Args: args,
162-
Flags: flags,
163-
Success: success,
164-
Duration: duration.Milliseconds(),
165-
Version: version,
166-
CommandPath: command,
151+
Command: command,
152+
Success: success,
153+
Duration: duration.Milliseconds(),
154+
Version: version,
167155
}
168156

169157
if err != nil {

src/client/acontext-cli/main.go

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/memodb-io/Acontext/acontext-cli/internal/logo"
1212
"github.com/memodb-io/Acontext/acontext-cli/internal/telemetry"
1313
"github.com/spf13/cobra"
14-
"github.com/spf13/pflag"
1514
)
1615

1716
type contextKey string
@@ -32,13 +31,13 @@ func main() {
3231
if executedCmd == nil {
3332
executedCmd = rootCmd
3433
}
35-
trackCommandAndWait(executedCmd, os.Args[1:], cmdErr, false)
34+
trackCommandAndWait(executedCmd, cmdErr, false)
3635
os.Exit(1)
3736
}
3837
}
3938

4039
// trackCommandAndWait tracks a command execution asynchronously and waits for completion
41-
func trackCommandAndWait(cmd *cobra.Command, args []string, err error, success bool) {
40+
func trackCommandAndWait(cmd *cobra.Command, err error, success bool) {
4241
// Skip telemetry for dev version
4342
if version == "dev" {
4443
return
@@ -54,17 +53,13 @@ func trackCommandAndWait(cmd *cobra.Command, args []string, err error, success b
5453
duration = time.Since(startTime)
5554
}
5655

57-
// Build command path, collect flags, and filter args
56+
// Build command path
5857
commandPath := buildCommandPath(cmd)
59-
flags := collectFlags(cmd)
60-
filteredArgs := filterArgs(args)
6158

6259
// Start async telemetry tracking and wait for completion
6360
// This ensures telemetry is sent even for blocking commands
6461
wg := telemetry.TrackCommandAsync(
6562
commandPath,
66-
filteredArgs,
67-
flags,
6863
success,
6964
err,
7065
duration,
@@ -105,28 +100,6 @@ func buildCommandPath(cmd *cobra.Command) string {
105100
return strings.Join(parts, ".")
106101
}
107102

108-
// collectFlags collects all flags that were set
109-
func collectFlags(cmd *cobra.Command) map[string]string {
110-
flags := make(map[string]string)
111-
112-
cmd.Flags().Visit(func(flag *pflag.Flag) {
113-
flags[flag.Name] = flag.Value.String()
114-
})
115-
116-
return flags
117-
}
118-
119-
// filterArgs filters out help-related arguments
120-
func filterArgs(args []string) []string {
121-
var filtered []string
122-
for _, arg := range args {
123-
if arg != "--help" && arg != "-h" && arg != "help" {
124-
filtered = append(filtered, arg)
125-
}
126-
}
127-
return filtered
128-
}
129-
130103
var rootCmd = &cobra.Command{
131104
Use: "acontext",
132105
Short: "Acontext CLI - Build context-aware AI applications",
@@ -147,7 +120,7 @@ Get started by running: acontext create
147120
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
148121
// Track successful command execution
149122
// This is called after the command's Run/RunE completes successfully
150-
trackCommandAndWait(cmd, args, nil, true)
123+
trackCommandAndWait(cmd, nil, true)
151124
return nil
152125
},
153126
Run: func(cmd *cobra.Command, args []string) {

0 commit comments

Comments
 (0)