Skip to content

Commit 2564835

Browse files
authored
CLOUDP-105503: Telemetry Improvements (#1270)
1 parent d66990f commit 2564835

File tree

38 files changed

+697
-208
lines changed

38 files changed

+697
-208
lines changed

cmd/atlas/atlas.go

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,44 +23,30 @@ import (
2323

2424
"github.com/mongodb/mongodb-atlas-cli/internal/cli/root/atlas"
2525
"github.com/mongodb/mongodb-atlas-cli/internal/config"
26-
"github.com/mongodb/mongodb-atlas-cli/internal/flag"
2726
"github.com/mongodb/mongodb-atlas-cli/internal/telemetry"
2827
"github.com/spf13/cobra"
2928
)
3029

31-
var (
32-
profile string
33-
)
34-
3530
// Execute adds all child commands to the root command and sets flags appropriately.
3631
// This is called by main.main(). It only needs to happen once to the rootCmd.
3732
func Execute() {
3833
ctx := telemetry.NewContext()
39-
rootCmd := atlas.Builder(&profile)
40-
if cmd, err := rootCmd.ExecuteContextC(ctx); err != nil {
41-
telemetry.TrackCommand(telemetry.TrackOptions{
42-
Cmd: cmd,
34+
rootCmd := atlas.Builder()
35+
if err := rootCmd.ExecuteContext(ctx); err != nil {
36+
telemetry.FinishTrackingCommand(telemetry.TrackOptions{
4337
Err: err,
4438
})
4539
os.Exit(1)
4640
}
4741
}
4842

4943
// loadConfig reads in config file and ENV variables if set.
50-
func loadConfig() {
44+
func loadConfig() error {
5145
if err := config.LoadAtlasCLIConfig(); err != nil {
52-
log.Fatalf("Error loading config: %v", err)
46+
return fmt.Errorf("error loading config: %w", err)
5347
}
54-
}
5548

56-
func initProfile() {
57-
if profile != "" {
58-
config.SetName(profile)
59-
} else if profile = config.GetString(flag.Profile); profile != "" {
60-
config.SetName(profile)
61-
} else if availableProfiles := config.List(); len(availableProfiles) == 1 {
62-
config.SetName(availableProfiles[0])
63-
}
49+
return nil
6450
}
6551

6652
// createConfigFromMongoCLIConfig creates the atlasCLI config file from the mongocli config file.
@@ -134,13 +120,24 @@ func mongoCLIConfigFilePath() (configPath string, err error) {
134120
return configPath, nil
135121
}
136122

123+
func trackInitError(e error) {
124+
if e == nil {
125+
return
126+
}
127+
if cmd, args, err := atlas.Builder().Find(os.Args[1:]); err == nil {
128+
telemetry.StartTrackingCommand(cmd, args)
129+
telemetry.FinishTrackingCommand(telemetry.TrackOptions{
130+
Err: e,
131+
})
132+
}
133+
log.Fatal(e)
134+
}
135+
137136
func main() {
138137
cobra.EnableCommandSorting = false
139138

140139
createConfigFromMongoCLIConfig()
141-
loadConfig()
142-
143-
cobra.OnInitialize(initProfile)
140+
trackInitError(loadConfig())
144141

145142
Execute()
146143
}

e2e/atlas/logs_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package atlas_test
1717

1818
import (
19-
"log"
2019
"os"
2120
"strings"
2221
"testing"
@@ -58,7 +57,7 @@ func downloadLogTmpPath(t *testing.T, cliPath, hostname, logFile, projectID stri
5857
t.Helper()
5958
dir, err := os.Getwd()
6059
if err != nil {
61-
log.Fatal(err)
60+
t.Fatal(err)
6261
}
6362
filepath := dir + logFile
6463

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ require (
3131
github.com/stretchr/testify v1.7.2
3232
github.com/tangzero/inflector v1.0.0
3333
github.com/withfig/autocomplete-tools/packages/cobra v1.2.0
34-
go.mongodb.org/atlas v0.15.1-0.20220506113516-0a876936c455
34+
go.mongodb.org/atlas v0.16.1-0.20220531163122-551edbfb2f27
3535
go.mongodb.org/mongo-driver v1.9.1
36-
go.mongodb.org/ops-manager v0.38.0
36+
go.mongodb.org/ops-manager v0.38.1-0.20220531172257-e2e9ea43cc71
3737
golang.org/x/crypto v0.0.0-20220511200225-c6db032c6c88
3838
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a
3939
google.golang.org/api v0.82.0

go.sum

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -358,13 +358,12 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
358358
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
359359
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
360360
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
361-
go.mongodb.org/atlas v0.15.0/go.mod h1:lQhRHIxc6jQHEK3/q9WLu/SdBkPj2fQYhjLGUF6Z3U8=
362-
go.mongodb.org/atlas v0.15.1-0.20220506113516-0a876936c455 h1:ai/IUkgQHGpsphE1+Tk1rSt8SrlNfBRIqVwobkLTQaY=
363-
go.mongodb.org/atlas v0.15.1-0.20220506113516-0a876936c455/go.mod h1:lQhRHIxc6jQHEK3/q9WLu/SdBkPj2fQYhjLGUF6Z3U8=
361+
go.mongodb.org/atlas v0.16.1-0.20220531163122-551edbfb2f27 h1:rGTb8CaE9ZKNjmdUJ58jFcHopLg6o6Kzfm9AIayq1Hw=
362+
go.mongodb.org/atlas v0.16.1-0.20220531163122-551edbfb2f27/go.mod h1:lQhRHIxc6jQHEK3/q9WLu/SdBkPj2fQYhjLGUF6Z3U8=
364363
go.mongodb.org/mongo-driver v1.9.1 h1:m078y9v7sBItkt1aaoe2YlvWEXcD263e1a4E1fBrJ1c=
365364
go.mongodb.org/mongo-driver v1.9.1/go.mod h1:0sQWfOeY63QTntERDJJ/0SuKK0T1uVSgKCuAROlKEPY=
366-
go.mongodb.org/ops-manager v0.38.0 h1:2LQYrrTdIUsMvdYOGCejaxWjn8JF9RYNe3nExcIyJfA=
367-
go.mongodb.org/ops-manager v0.38.0/go.mod h1:rit/vJzJTlZ3TppvZSb6hbGk6BzG14daGEuDF1XAd5k=
365+
go.mongodb.org/ops-manager v0.38.1-0.20220531172257-e2e9ea43cc71 h1:hbpH9FXHn4RXYSI2Exd1nUgLyj9Ht8y1OddxYtDH0Mc=
366+
go.mongodb.org/ops-manager v0.38.1-0.20220531172257-e2e9ea43cc71/go.mod h1:NVwauDvGZ28yuddHSQKaLLQKVzKejU8YxOXKxTSZyZY=
368367
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
369368
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
370369
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=

internal/cli/atlas/atlas.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package atlas
1616

1717
import (
1818
"fmt"
19-
"os"
2019

2120
"github.com/mongodb/mongodb-atlas-cli/internal/cli"
2221
"github.com/mongodb/mongodb-atlas-cli/internal/cli/alerts"
@@ -43,6 +42,7 @@ import (
4342
"github.com/mongodb/mongodb-atlas-cli/internal/cli/events"
4443
"github.com/mongodb/mongodb-atlas-cli/internal/cli/performanceadvisor"
4544
"github.com/mongodb/mongodb-atlas-cli/internal/config"
45+
"github.com/mongodb/mongodb-atlas-cli/internal/log"
4646
"github.com/mongodb/mongodb-atlas-cli/internal/validate"
4747
"github.com/spf13/cobra"
4848
)
@@ -58,7 +58,9 @@ func Builder() *cobra.Command {
5858
Short: "MongoDB Atlas operations.",
5959
Long: deprecatedMessage,
6060
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
61-
_, _ = fmt.Fprint(os.Stderr, deprecatedMessage)
61+
log.SetOutput(cmd.ErrOrStderr())
62+
63+
_, _ = log.Warning(deprecatedMessage)
6264
if err := opts.InitFlow(); err != nil {
6365
return err
6466
}

internal/cli/atlas/dbusers/certs/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (opts *ListOpts) initStore(ctx context.Context) func() error {
4343
}
4444

4545
func (opts *ListOpts) Run() error {
46-
r, err := opts.store.DBUserCertificates(opts.ConfigProjectID(), opts.username)
46+
r, err := opts.store.DBUserCertificates(opts.ConfigProjectID(), opts.username, nil)
4747
if err != nil {
4848
return err
4949
}

internal/cli/atlas/dbusers/certs/list_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestListBuilder(t *testing.T) {
4242

4343
mockStore.
4444
EXPECT().
45-
DBUserCertificates(listOpts.ProjectID, username).
45+
DBUserCertificates(listOpts.ProjectID, username, nil).
4646
Return(expected, nil).
4747
Times(1)
4848

internal/cli/atlas/quickstart/cluster_setup.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
atlas "go.mongodb.org/atlas/mongodbatlas"
2828
)
2929

30+
var ErrNoRegions = errors.New("no regions found for cloud provider")
31+
3032
func (opts *Opts) createCluster() error {
3133
if _, err := opts.store.CreateCluster(opts.newCluster()); err != nil {
3234
return err
@@ -69,6 +71,11 @@ func (opts *Opts) askClusterRegion() error {
6971
if err != nil {
7072
return err
7173
}
74+
75+
if len(regions) == 0 {
76+
return fmt.Errorf("%w: %v", ErrNoRegions, opts.Provider)
77+
}
78+
7279
regionQ := newRegionQuestions(regions)
7380
return telemetry.TrackAskOne(regionQ, &opts.Region, survey.WithValidator(survey.Required))
7481
}

internal/cli/atlas/quickstart/mongoshell_setup.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ package quickstart
1616

1717
import (
1818
"fmt"
19-
"os"
2019

2120
"github.com/mongodb/mongodb-atlas-cli/internal/config"
21+
"github.com/mongodb/mongodb-atlas-cli/internal/log"
2222
"github.com/mongodb/mongodb-atlas-cli/internal/telemetry"
2323
"github.com/pkg/browser"
2424
)
@@ -40,7 +40,7 @@ func (opts *Opts) askMongoShellQuestion() error {
4040
if config.MongoShellPath() != "" {
4141
return nil
4242
}
43-
_, _ = fmt.Fprint(os.Stderr, "No MongoDB shell version configured.\n")
43+
_, _ = log.Warningln("No MongoDB shell version configured.")
4444
if err := telemetry.TrackAskOne(newIsMongoShellInstalledQuestion(), &opts.mongoShellInstalled); err != nil {
4545
return err
4646
}

internal/cli/atlas/quickstart/quick_start.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"fmt"
2121
"io"
2222
"os"
23-
"os/signal"
2423
"strconv"
2524
"strings"
2625
"syscall"
@@ -30,7 +29,9 @@ import (
3029
"github.com/mongodb/mongodb-atlas-cli/internal/cli"
3130
"github.com/mongodb/mongodb-atlas-cli/internal/config"
3231
"github.com/mongodb/mongodb-atlas-cli/internal/flag"
32+
"github.com/mongodb/mongodb-atlas-cli/internal/log"
3333
"github.com/mongodb/mongodb-atlas-cli/internal/mongosh"
34+
"github.com/mongodb/mongodb-atlas-cli/internal/sighandle"
3435
"github.com/mongodb/mongodb-atlas-cli/internal/store"
3536
"github.com/mongodb/mongodb-atlas-cli/internal/telemetry"
3637
"github.com/mongodb/mongodb-atlas-cli/internal/usage"
@@ -158,7 +159,7 @@ func (opts *Opts) Run() error {
158159
if publicIP := store.IPAddress(); publicIP != "" {
159160
opts.IPAddresses = []string{publicIP}
160161
} else {
161-
_, _ = fmt.Fprintf(os.Stderr, quickstartTemplateIPNotFound, cli.ExampleAtlasEntryPoint())
162+
_, _ = log.Warningf(quickstartTemplateIPNotFound, cli.ExampleAtlasEntryPoint())
162163
}
163164
}
164165

@@ -312,13 +313,13 @@ func askMongoShellAndSetConfig() error {
312313
// program if it receives an interrupt from the OS. We then handle this by printing
313314
// the dbUsername and dbPassword.
314315
func (opts *Opts) setupCloseHandler() {
315-
c := make(chan os.Signal, 1)
316-
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
317-
go func() {
318-
<-c
316+
sighandle.Notify(func(sig os.Signal) {
319317
fmt.Printf(quickstartTemplateCloseHandler, opts.ClusterName)
318+
telemetry.FinishTrackingCommand(telemetry.TrackOptions{
319+
Signal: sig.String(),
320+
})
320321
os.Exit(0)
321-
}()
322+
}, os.Interrupt, syscall.SIGTERM)
322323
}
323324

324325
func (opts *Opts) providerAndRegionToConstant() {
@@ -374,7 +375,7 @@ func (opts *Opts) newDefaultValues() (*quickstart, error) {
374375
if publicIP := store.IPAddress(); publicIP != "" {
375376
values.IPAddresses = []string{publicIP}
376377
} else {
377-
_, _ = fmt.Fprintf(os.Stderr, quickstartTemplateIPNotFound, cli.ExampleAtlasEntryPoint())
378+
_, _ = log.Warningf(quickstartTemplateIPNotFound, cli.ExampleAtlasEntryPoint())
378379
}
379380
}
380381

0 commit comments

Comments
 (0)