github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/util/report/report.go (about) 1 // Package report contains CLI error reporting and handling and cleanup methods. 2 // This is mostly required due to the impossibility to catch `os.Exit`s 3 // (https://stackoverflow.com/questions/39509447/trap-os-exit-in-golang) 4 // and other funkiness around error paths. 5 // 6 // This package currently only tracks the m3o platform calls. 7 // Please use `1` event values for failure and `0` for success to be consistent 8 // in our Google Analytics alerts. 9 package report 10 11 import ( 12 "context" 13 "fmt" 14 "strings" 15 16 pb "github.com/tickoalcantara12/micro/v3/proto/alert" 17 "github.com/tickoalcantara12/micro/v3/service/client" 18 "github.com/tickoalcantara12/micro/v3/util/helper" 19 "github.com/urfave/cli/v2" 20 ) 21 22 // Error is a helper function to record error events 23 func Error(ctx *cli.Context, a ...interface{}) { 24 val := uint64(1) 25 err := TrackEvent(ctx, TrackingData{ 26 Category: getTrackingCategory(ctx), 27 Action: "error", 28 Label: fmt.Sprint(a...), 29 Value: &val, 30 }) 31 if err != nil { 32 fmt.Println(err) 33 } 34 } 35 36 // Errorf is a helper function to record error events 37 func Errorf(ctx *cli.Context, format string, a ...interface{}) { 38 val := uint64(1) 39 err := TrackEvent(ctx, TrackingData{ 40 Category: getTrackingCategory(ctx), 41 Action: "error", 42 Label: fmt.Sprintf(format, a...), 43 Value: &val, 44 }) 45 if err != nil { 46 fmt.Println(err) 47 } 48 } 49 50 // Success is a helper function to record success events 51 func Success(ctx *cli.Context, a ...interface{}) { 52 val := uint64(0) 53 err := TrackEvent(ctx, TrackingData{ 54 Category: getTrackingCategory(ctx), 55 Action: "success", 56 Label: fmt.Sprint(a...), 57 Value: &val, 58 }) 59 if err != nil { 60 fmt.Println(err) 61 } 62 } 63 64 // Successf is a helper function to record success events 65 func Successf(ctx *cli.Context, format string, a ...interface{}) { 66 val := uint64(0) 67 err := TrackEvent(ctx, TrackingData{ 68 Category: getTrackingCategory(ctx), 69 Action: "success", 70 Label: fmt.Sprintf(format, a...), 71 Value: &val, 72 }) 73 if err != nil { 74 fmt.Println(err) 75 } 76 } 77 78 type TrackingData struct { 79 Category string 80 Action string 81 Label string 82 UserID string 83 Value *uint64 84 } 85 86 func getTrackingCategory(ctx *cli.Context) string { 87 if ctx == nil { 88 return "cli" 89 } 90 command := ctx.Command.Name 91 subcommand := helper.Subcommand(ctx) 92 if len(strings.TrimSpace(subcommand)) == 0 { 93 return command 94 } 95 return strings.Join([]string{command, subcommand}, "/") 96 } 97 98 // TrackEvent records an event on google analytics 99 // For details consult https://support.google.com/analytics/answer/1033068?hl=en 100 // 101 // Example: 102 // Category: "Videos" 103 // Action: "Downloaded" 104 // Label: "Gone With the Wind" 105 func TrackEvent(ctx *cli.Context, td TrackingData) error { 106 sendEvent(ctx, td) 107 return nil 108 } 109 110 // send event to alert service 111 func sendEvent(ctx *cli.Context, td TrackingData) error { 112 alertService := pb.NewAlertService("alert", client.DefaultClient) 113 val := uint64(0) 114 if td.Value != nil { 115 val = *td.Value 116 } 117 _, err := alertService.ReportEvent(context.TODO(), &pb.ReportEventRequest{ 118 Event: &pb.Event{ 119 Category: td.Category, 120 Action: td.Action, 121 Label: td.Label, 122 Value: val, 123 }, 124 }) 125 return err 126 }