Skip to content

Commit

Permalink
cmd/uplink: add mon.Task and allow writing stats to a file
Browse files Browse the repository at this point in the history
adds mon.Task calls to all command Execute methods as well
as some other functions that had contexts available already
and adds a --debug-monkit-stats flag to write the stats
to when the command finishes.

Change-Id: I06581f278403054893ac83403c18410c121a96f8
  • Loading branch information
zeebo authored and Storj Robot committed Mar 19, 2024
1 parent 6ec8420 commit 9dd6c51
Show file tree
Hide file tree
Showing 24 changed files with 111 additions and 27 deletions.
2 changes: 2 additions & 0 deletions cmd/uplink/access_maker.go
Expand Up @@ -38,6 +38,8 @@ func (am *accessMaker) Setup(params clingy.Parameters, ex ulext.External) {
}

func (am *accessMaker) Execute(ctx context.Context, name string, access *uplink.Access) (_ *uplink.Access, err error) {
defer mon.Task()(&ctx)(&err)

defaultName, accesses, err := am.ex.GetAccessInfo(false)
if err != nil {
return nil, err
Expand Down
2 changes: 2 additions & 0 deletions cmd/uplink/cmd_access_create.go
Expand Up @@ -53,6 +53,8 @@ func (c *cmdAccessCreate) Setup(params clingy.Parameters) {
}

func (c *cmdAccessCreate) Execute(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)

if c.satelliteAddr == "" {
if c.passphraseStdin {
return errs.New("Must specify the satellite address as a flag when passphrase-stdin is set.")
Expand Down
4 changes: 3 additions & 1 deletion cmd/uplink/cmd_access_export.go
Expand Up @@ -28,7 +28,9 @@ func (c *cmdAccessExport) Setup(params clingy.Parameters) {
c.filename = params.Arg("filename", "Name of the file to save to").(string)
}

func (c *cmdAccessExport) Execute(ctx context.Context) error {
func (c *cmdAccessExport) Execute(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)

if c.filename == "" {
return errs.New("Must specify a filename to write to.")
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/uplink/cmd_access_import.go
Expand Up @@ -32,6 +32,8 @@ func (c *cmdAccessImport) Setup(params clingy.Parameters) {
}

func (c *cmdAccessImport) Execute(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)

if c.name == "" {
return errs.New("Must specify a name to import the access as.")
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/uplink/cmd_access_inspect.go
Expand Up @@ -38,7 +38,9 @@ func (c *cmdAccessInspect) Setup(params clingy.Parameters) {
}

// Execute runs the command.
func (c *cmdAccessInspect) Execute(ctx context.Context) error {
func (c *cmdAccessInspect) Execute(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)

toOpen := ""
if c.access != nil {
toOpen = *c.access
Expand Down
4 changes: 3 additions & 1 deletion cmd/uplink/cmd_access_list.go
Expand Up @@ -32,7 +32,9 @@ func (c *cmdAccessList) Setup(params clingy.Parameters) {
).(bool)
}

func (c *cmdAccessList) Execute(ctx context.Context) error {
func (c *cmdAccessList) Execute(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)

defaultName, accesses, err := c.ex.GetAccessInfo(true)
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions cmd/uplink/cmd_access_register.go
Expand Up @@ -38,6 +38,8 @@ func (c *cmdAccessRegister) Setup(params clingy.Parameters) {
}

func (c *cmdAccessRegister) Execute(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)

accessNameOrValue := ""
if c.accessNameOrValue != nil && len(*c.accessNameOrValue) > 0 {
accessNameOrValue = *c.accessNameOrValue
Expand Down
4 changes: 3 additions & 1 deletion cmd/uplink/cmd_access_remove.go
Expand Up @@ -27,7 +27,9 @@ func (c *cmdAccessRemove) Setup(params clingy.Parameters) {
c.access = params.Arg("name", "Access name to delete").(string)
}

func (c *cmdAccessRemove) Execute(ctx context.Context) error {
func (c *cmdAccessRemove) Execute(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)

defaultName, accesses, err := c.ex.GetAccessInfo(true)
if err != nil {
return err
Expand Down
4 changes: 3 additions & 1 deletion cmd/uplink/cmd_access_restrict.go
Expand Up @@ -37,7 +37,9 @@ func (c *cmdAccessRestrict) Setup(params clingy.Parameters) {
c.am.perms.Setup(params, true)
}

func (c *cmdAccessRestrict) Execute(ctx context.Context) error {
func (c *cmdAccessRestrict) Execute(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)

access, err := c.ex.OpenAccess(c.access)
if err != nil {
return err
Expand Down
4 changes: 3 additions & 1 deletion cmd/uplink/cmd_access_revoke.go
Expand Up @@ -28,7 +28,9 @@ func (c *cmdAccessRevoke) Setup(params clingy.Parameters) {
c.revokee = params.Arg("revokee", "Access name or value revoke").(string)
}

func (c *cmdAccessRevoke) Execute(ctx context.Context) error {
func (c *cmdAccessRevoke) Execute(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)

project, err := c.ex.OpenProject(ctx, c.access)
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions cmd/uplink/cmd_access_setup.go
Expand Up @@ -39,6 +39,8 @@ func (c *cmdAccessSetup) Setup(params clingy.Parameters) {
}

func (c *cmdAccessSetup) Execute(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)

name, err := c.ex.PromptInput(ctx, "Enter name to import as [default: main]:")
if err != nil {
return errs.Wrap(err)
Expand Down
4 changes: 3 additions & 1 deletion cmd/uplink/cmd_access_use.go
Expand Up @@ -26,7 +26,9 @@ func (c *cmdAccessUse) Setup(params clingy.Parameters) {
c.access = params.Arg("access", "Access name to use").(string)
}

func (c *cmdAccessUse) Execute(ctx context.Context) error {
func (c *cmdAccessUse) Execute(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)

_, accesses, err := c.ex.GetAccessInfo(true)
if err != nil {
return err
Expand Down
17 changes: 13 additions & 4 deletions cmd/uplink/cmd_cp.go
Expand Up @@ -150,7 +150,9 @@ func (c *cmdCp) Setup(params clingy.Parameters) {
).([]ulloc.Location)
}

func (c *cmdCp) Execute(ctx context.Context) error {
func (c *cmdCp) Execute(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)

if len(c.locs) < 2 {
return errs.New("must have at least one source and destination path")
}
Expand Down Expand Up @@ -191,7 +193,9 @@ func (c *cmdCp) Execute(ctx context.Context) error {
return combineErrs(eg)
}

func (c *cmdCp) dispatchCopy(ctx context.Context, fs ulfs.Filesystem, source, dest ulloc.Location) error {
func (c *cmdCp) dispatchCopy(ctx context.Context, fs ulfs.Filesystem, source, dest ulloc.Location) (err error) {
defer mon.Task()(&ctx)(&err)

if !source.Remote() && !dest.Remote() {
return errs.New("at least one location must be a remote sj:// location")
}
Expand Down Expand Up @@ -247,7 +251,9 @@ func (c *cmdCp) dispatchCopy(ctx context.Context, fs ulfs.Filesystem, source, de
return c.copyFile(ctx, fs, source, dest, bar)
}

func (c *cmdCp) copyRecursive(ctx context.Context, fs ulfs.Filesystem, source, dest ulloc.Location) error {
func (c *cmdCp) copyRecursive(ctx context.Context, fs ulfs.Filesystem, source, dest ulloc.Location) (err error) {
defer mon.Task()(&ctx)(&err)

if source.Std() || dest.Std() {
return errs.New("cannot recursively copy to stdin/stdout")
}
Expand Down Expand Up @@ -367,6 +373,8 @@ func (c *cmdCp) copyRecursive(ctx context.Context, fs ulfs.Filesystem, source, d
}

func (c *cmdCp) copyFile(ctx context.Context, fs ulfs.Filesystem, source, dest ulloc.Location, bar *mpb.Bar) (err error) {
defer mon.Task()(&ctx)(&err)

if c.dryrun {
return nil
}
Expand Down Expand Up @@ -525,7 +533,8 @@ func (c *cmdCp) parallelCopy(
dst ulfs.MultiWriteHandle,
p int, chunkSize int64,
offset, length int64,
bar *mpb.Bar) error {
bar *mpb.Bar) (err error) {
defer mon.Task()(&ctx)(&err)

if offset != 0 {
if err := src.SetOffset(offset); err != nil {
Expand Down
12 changes: 9 additions & 3 deletions cmd/uplink/cmd_ls.go
Expand Up @@ -64,14 +64,18 @@ func (c *cmdLs) Setup(params clingy.Parameters) {
).(*ulloc.Location)
}

func (c *cmdLs) Execute(ctx context.Context) error {
func (c *cmdLs) Execute(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)

if c.prefix == nil {
return c.listBuckets(ctx)
}
return c.listLocation(ctx, *c.prefix)
}

func (c *cmdLs) listBuckets(ctx context.Context) error {
func (c *cmdLs) listBuckets(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)

project, err := c.ex.OpenProject(ctx, c.access)
if err != nil {
return err
Expand All @@ -90,7 +94,9 @@ func (c *cmdLs) listBuckets(ctx context.Context) error {
}
}

func (c *cmdLs) listLocation(ctx context.Context, prefix ulloc.Location) error {
func (c *cmdLs) listLocation(ctx context.Context, prefix ulloc.Location) (err error) {
defer mon.Task()(&ctx)(&err)

fs, err := c.ex.OpenFilesystem(ctx, c.access, ulext.BypassEncryption(c.encrypted))
if err != nil {
return err
Expand Down
4 changes: 3 additions & 1 deletion cmd/uplink/cmd_mb.go
Expand Up @@ -38,7 +38,9 @@ func (c *cmdMb) Setup(params clingy.Parameters) {
).(string)
}

func (c *cmdMb) Execute(ctx context.Context) error {
func (c *cmdMb) Execute(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)

project, err := c.ex.OpenProject(ctx, c.access)
if err != nil {
return errs.Wrap(err)
Expand Down
4 changes: 3 additions & 1 deletion cmd/uplink/cmd_meta_get.go
Expand Up @@ -42,7 +42,9 @@ func (c *cmdMetaGet) Setup(params clingy.Parameters) {
c.entry = params.Arg("entry", "Metadata entry to get", clingy.Optional).(*string)
}

func (c *cmdMetaGet) Execute(ctx context.Context) error {
func (c *cmdMetaGet) Execute(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)

project, err := c.ex.OpenProject(ctx, c.access, ulext.BypassEncryption(c.encrypted))
if err != nil {
return err
Expand Down
12 changes: 9 additions & 3 deletions cmd/uplink/cmd_mv.go
Expand Up @@ -63,7 +63,9 @@ func (c *cmdMv) Setup(params clingy.Parameters) {
c.dest = params.Arg("dest", "Destination to move", clingy.Transform(ulloc.Parse)).(ulloc.Location)
}

func (c *cmdMv) Execute(ctx context.Context) error {
func (c *cmdMv) Execute(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)

fs, err := c.ex.OpenFilesystem(ctx, c.access)
if err != nil {
return err
Expand Down Expand Up @@ -114,7 +116,9 @@ func (c *cmdMv) Execute(ctx context.Context) error {
return c.moveFile(ctx, fs, c.source, c.dest)
}

func (c *cmdMv) moveRecursive(ctx context.Context, fs ulfs.Filesystem) error {
func (c *cmdMv) moveRecursive(ctx context.Context, fs ulfs.Filesystem) (err error) {
defer mon.Task()(&ctx)(&err)

iter, err := fs.List(ctx, c.source, &ulfs.ListOptions{
Recursive: true,
})
Expand Down Expand Up @@ -188,7 +192,9 @@ func (c *cmdMv) moveRecursive(ctx context.Context, fs ulfs.Filesystem) error {
return nil
}

func (c *cmdMv) moveFile(ctx context.Context, fs ulfs.Filesystem, source, dest ulloc.Location) error {
func (c *cmdMv) moveFile(ctx context.Context, fs ulfs.Filesystem, source, dest ulloc.Location) (err error) {
defer mon.Task()(&ctx)(&err)

if c.dryrun {
return nil
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/uplink/cmd_rb.go
Expand Up @@ -39,7 +39,9 @@ func (c *cmdRb) Setup(params clingy.Parameters) {
).(ulloc.Location)
}

func (c *cmdRb) Execute(ctx context.Context) error {
func (c *cmdRb) Execute(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)

project, err := c.ex.OpenProject(ctx, c.access)
if err != nil {
return err
Expand Down
4 changes: 3 additions & 1 deletion cmd/uplink/cmd_rm.go
Expand Up @@ -63,7 +63,9 @@ func (c *cmdRm) Setup(params clingy.Parameters) {
).(ulloc.Location)
}

func (c *cmdRm) Execute(ctx context.Context) error {
func (c *cmdRm) Execute(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)

if c.location.Local() {
return errs.New("remove %v skipped: local delete", c.location)
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/uplink/cmd_share.go
Expand Up @@ -66,7 +66,9 @@ func (c *cmdShare) Setup(params clingy.Parameters) {
c.ap.Setup(params, false)
}

func (c *cmdShare) Execute(ctx context.Context) error {
func (c *cmdShare) Execute(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)

if len(c.ap.prefixes) == 0 {
return errs.New("you must specify at least one prefix to share. Use the access restrict command to restrict with no prefixes")
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/uplink/cmd_version.go
Expand Up @@ -33,7 +33,9 @@ func (c *cmdVersion) Setup(params clingy.Parameters) {
).(bool)
}

func (c *cmdVersion) Execute(ctx context.Context) error {
func (c *cmdVersion) Execute(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)

if version.Build.Release {
fmt.Fprintln(clingy.Stdout(ctx), "Release build")
} else {
Expand Down
19 changes: 19 additions & 0 deletions cmd/uplink/external.go
Expand Up @@ -74,6 +74,7 @@ type external struct {
pprofFile string
traceFile string
monkitTraceFile string
monkitStatsFile string
}

events struct {
Expand Down Expand Up @@ -160,6 +161,11 @@ func (ex *external) Setup(f clingy.Flags) {
clingy.Advanced,
).(string)

ex.debug.monkitStatsFile = f.Flag(
"debug-monkit-stats", "File to collect Monkit stats", "",
clingy.Advanced,
).(string)

ex.analytics = f.Flag(
"analytics", "Whether to send usage information to Storj", nil,
clingy.Transform(strconv.ParseBool), clingy.Optional, clingy.Boolean,
Expand Down Expand Up @@ -249,6 +255,8 @@ func (ex *external) analyticsEnabled() bool {

// Wrap is called by clingy with the command to be executed.
func (ex *external) Wrap(ctx context.Context, cmd clingy.Command) (err error) {
defer mon.Task()(&ctx)(&err)

if err = ex.migrate(); err != nil {
return err
}
Expand Down Expand Up @@ -300,6 +308,17 @@ func (ex *external) Wrap(ctx context.Context, cmd clingy.Command) (err error) {
defer trace.Stop()
}

if ex.debug.monkitStatsFile != "" {
fh, err := os.Create(ex.debug.monkitStatsFile)
if err != nil {
return errs.Wrap(err)
}
defer func() { _ = fh.Close() }()
defer monkit.Default.Stats(func(key monkit.SeriesKey, field string, val float64) {
fmt.Fprintf(fh, "%v\t%v\t%v\n", key, field, val)
})
}

// N.B.: Tracing is currently disabled by default (sample == 0, traceID == 0) and is
// something a user can only opt into. as a result, we don't check ex.analyticsEnabled()
// in this if statement. If we do ever start turning on trace samples by default, we
Expand Down
8 changes: 6 additions & 2 deletions cmd/uplink/external_access.go
Expand Up @@ -156,7 +156,9 @@ func (ex *external) SaveAccessInfo(defaultName string, accesses map[string]strin
return nil
}

func (ex *external) RequestAccess(ctx context.Context, satelliteAddr, apiKey, passphrase string, unencryptedObjectKeys bool) (*uplink.Access, error) {
func (ex *external) RequestAccess(ctx context.Context, satelliteAddr, apiKey, passphrase string, unencryptedObjectKeys bool) (_ *uplink.Access, err error) {
defer mon.Task()(&ctx)(&err)

config := uplink.Config{}
if unencryptedObjectKeys {
access.DisableObjectKeyEncryption(&config)
Expand All @@ -168,7 +170,9 @@ func (ex *external) RequestAccess(ctx context.Context, satelliteAddr, apiKey, pa
return access, nil
}

func (ex *external) ExportAccess(ctx context.Context, access *uplink.Access, filename string) error {
func (ex *external) ExportAccess(ctx context.Context, access *uplink.Access, filename string) (err error) {
defer mon.Task()(&ctx)(&err)

serialized, err := access.Serialize()
if err != nil {
return errs.Wrap(err)
Expand Down
8 changes: 6 additions & 2 deletions cmd/uplink/external_project.go
Expand Up @@ -17,15 +17,19 @@ import (

const uplinkCLIUserAgent = "uplink-cli"

func (ex *external) OpenFilesystem(ctx context.Context, accessName string, options ...ulext.Option) (ulfs.Filesystem, error) {
func (ex *external) OpenFilesystem(ctx context.Context, accessName string, options ...ulext.Option) (_ ulfs.Filesystem, err error) {
defer mon.Task()(&ctx)(&err)

project, err := ex.OpenProject(ctx, accessName, options...)
if err != nil {
return nil, err
}
return ulfs.NewMixed(ulfs.NewLocal(ulfs.NewLocalBackendOS()), ulfs.NewRemote(project)), nil
}

func (ex *external) OpenProject(ctx context.Context, accessName string, options ...ulext.Option) (*uplink.Project, error) {
func (ex *external) OpenProject(ctx context.Context, accessName string, options ...ulext.Option) (_ *uplink.Project, err error) {
defer mon.Task()(&ctx)(&err)

opts := ulext.LoadOptions(options...)

access, err := ex.OpenAccess(accessName)
Expand Down

0 comments on commit 9dd6c51

Please sign in to comment.