-
Notifications
You must be signed in to change notification settings - Fork 126
RSDK-12623: Viam cli can work with trace files from viam-server #5552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 9 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
5b49f9f
Add cli commands to import trace data
jmatth 07827f0
Redirect stderr to stop make from complaining about missing dpkg
jmatth a8cc3dc
Fix cli flags
jmatth eba223c
Rearrange cli trace commands and add support for printing to console
jmatth 3a9a80b
WIP: better organize trace related cli code
jmatth 6bb7919
Better organize cli traces code
jmatth 2680cad
Split argument structs up, fix otlp endpoint not getting passed through
jmatth 1e1db56
Split trace cli implementation into separate file
jmatth 4b81f6a
Update goutils to 0.3.6
jmatth 62de9b6
Fix typo
jmatth 5979391
Add more help text to remote trace commands, s/fetch/get to match ftd…
jmatth ad3ad5b
Add tests for trace file download
jmatth f9c6410
Fix delimited proto reader silently dropping messages that exceed the…
jmatth 1202a35
Update trace commands that deal with output location to match get-ftd…
jmatth 988190a
Merge remote-tracking branch 'upstream/main' into trace-cli
jmatth d222d8e
Attempt to fix compilation on 32-bit
jmatth 746a6fb
Fix cli tests
jmatth 048db33
Add usage text to traces commands
jmatth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
benjirewis marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,253 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| stderrors "errors" | ||
| "os" | ||
| "path" | ||
| "path/filepath" | ||
| "time" | ||
|
|
||
| "github.com/pkg/errors" | ||
| "github.com/urfave/cli/v2" | ||
| "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" | ||
| tracepb "go.opentelemetry.io/proto/otlp/trace/v1" | ||
| "go.viam.com/utils/perf" | ||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/status" | ||
|
|
||
| "go.viam.com/rdk/logging" | ||
| "go.viam.com/rdk/protoutils" | ||
| "go.viam.com/rdk/services/shell" | ||
| ) | ||
|
|
||
| var tracesPath = path.Join("~", ".viam", "trace") | ||
|
|
||
| type traceFetchRemoteArgs struct { | ||
| Organization string | ||
| Location string | ||
| Machine string | ||
| Part string | ||
| Destination string | ||
| } | ||
|
|
||
| type traceImportRemoteArgs struct { | ||
| Organization string | ||
| Location string | ||
| Machine string | ||
| Part string | ||
| Endpoint string | ||
| } | ||
|
|
||
| type traceImportLocalArgs struct { | ||
| Path string | ||
| Endpoint string | ||
| } | ||
|
|
||
| type tracePrintLocalArgs struct { | ||
| Path string | ||
| } | ||
|
|
||
| func traceImportRemoteAction(ctx *cli.Context, args traceImportRemoteArgs) error { | ||
| client, err := newViamClient(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| globalArgs, err := getGlobalArgs(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| logger := globalArgs.createLogger() | ||
| tmp, err := os.MkdirTemp("", "viamtraceimport") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| //nolint: errcheck | ||
| defer os.RemoveAll(tmp) | ||
| if err := client.tracesFetchRemoteAction( | ||
| ctx, | ||
| traceFetchRemoteArgs{ | ||
| Organization: args.Organization, | ||
| Location: args.Location, | ||
| Machine: args.Machine, | ||
| Part: args.Part, | ||
| Destination: tmp, | ||
| }, | ||
| globalArgs.Debug, | ||
| logger, | ||
| ); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return traceImportLocalAction(ctx, traceImportLocalArgs{ | ||
| Path: filepath.Join(tmp, "traces"), | ||
| Endpoint: args.Endpoint, | ||
| }) | ||
| } | ||
|
|
||
| func (c *viamClient) tracesFetchRemoteAction( | ||
| ctx *cli.Context, | ||
| flagArgs traceFetchRemoteArgs, | ||
| debug bool, | ||
| logger logging.Logger, | ||
| ) error { | ||
| part, err := c.robotPart(flagArgs.Organization, flagArgs.Location, flagArgs.Machine, flagArgs.Part) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| // Intentional use of path instead of filepath: Windows understands both / and | ||
| // \ as path separators, and we don't want a cli running on Windows to send | ||
| // a path using \ to a *NIX machine. | ||
| src := path.Join(tracesPath, part.Id, "traces") | ||
benjirewis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| gArgs, err := getGlobalArgs(ctx) | ||
| quiet := err == nil && gArgs != nil && gArgs.Quiet | ||
| var startTime time.Time | ||
| if !quiet { | ||
| startTime = time.Now() | ||
| printf(ctx.App.Writer, "Saving to %s ...", path.Join(flagArgs.Destination, part.GetId())) | ||
| } | ||
| if err := c.copyFilesFromMachine( | ||
| flagArgs.Organization, | ||
| flagArgs.Location, | ||
| flagArgs.Machine, | ||
| flagArgs.Part, | ||
| debug, | ||
| true, | ||
| false, | ||
| []string{src}, | ||
| flagArgs.Destination, | ||
| logger, | ||
| ); err != nil { | ||
| if statusErr := status.Convert(err); statusErr != nil && | ||
| statusErr.Code() == codes.InvalidArgument && | ||
| statusErr.Message() == shell.ErrMsgDirectoryCopyRequestNoRecursion { | ||
| return errDirectoryCopyRequestNoRecursion | ||
| } | ||
| return err | ||
| } | ||
| if !quiet { | ||
| printf(ctx.App.Writer, "Download finished in %s.", time.Since(startTime)) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func tracePrintRemoteAction( | ||
| ctx *cli.Context, | ||
| args machinesPartGetFTDCArgs, | ||
| ) error { | ||
| client, err := newViamClient(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| globalArgs, err := getGlobalArgs(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| logger := globalArgs.createLogger() | ||
| tmp, err := os.MkdirTemp("", "viamtraceimport") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| //nolint: errcheck | ||
| defer os.RemoveAll(tmp) | ||
| if err := client.tracesFetchRemoteAction( | ||
| ctx, | ||
| traceFetchRemoteArgs{ | ||
| Organization: args.Organization, | ||
| Location: args.Location, | ||
| Machine: args.Machine, | ||
| Part: args.Part, | ||
| Destination: tmp, | ||
| }, | ||
| globalArgs.Debug, | ||
| logger, | ||
| ); err != nil { | ||
| return err | ||
| } | ||
| return tracePrintLocalAction(ctx, tracePrintLocalArgs{Path: filepath.Join(tmp, "traces")}) | ||
| } | ||
|
|
||
| func traceFetchRemoteAction(ctx *cli.Context, args traceFetchRemoteArgs) error { | ||
| client, err := newViamClient(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| globalArgs, err := getGlobalArgs(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| logger := globalArgs.createLogger() | ||
|
|
||
| return client.tracesFetchRemoteAction(ctx, args, globalArgs.Debug, logger) | ||
| } | ||
|
|
||
| func tracePrintLocalAction( | ||
| ctx *cli.Context, | ||
| args tracePrintLocalArgs, | ||
| ) error { | ||
| traceFile, err := os.Open(args.Path) | ||
| if err != nil { | ||
| if os.IsNotExist(err) { | ||
| printf(ctx.App.Writer, "No traces found") | ||
| return nil | ||
| } | ||
| return errors.Wrap(err, "failed to open trace file") | ||
| } | ||
| traceReader := protoutils.NewDelimitedProtoReader[tracepb.ResourceSpans](traceFile) | ||
| //nolint: errcheck | ||
| defer traceReader.Close() | ||
|
|
||
| devExporter := perf.NewOtelDevelopmentExporter() | ||
| if err := devExporter.Start(); err != nil { | ||
| return err | ||
| } | ||
| defer devExporter.Stop() | ||
| var msg tracepb.ResourceSpans | ||
| err = nil | ||
| for resource := range traceReader.AllWithMemory(&msg) { | ||
| for _, scope := range resource.ScopeSpans { | ||
| err = stderrors.Join(err, devExporter.ExportOTLPSpans(ctx.Context, scope.Spans)) | ||
| } | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| func traceImportLocalAction( | ||
| ctx *cli.Context, | ||
| args traceImportLocalArgs, | ||
| ) error { | ||
| traceFile, err := os.Open(args.Path) | ||
| if err != nil { | ||
| if os.IsNotExist(err) { | ||
| printf(ctx.App.Writer, "No traces found") | ||
| return nil | ||
| } | ||
| return errors.Wrap(err, "failed to open trace file") | ||
| } | ||
| traceReader := protoutils.NewDelimitedProtoReader[tracepb.ResourceSpans](traceFile) | ||
| //nolint: errcheck | ||
| defer traceReader.Close() | ||
| endpoint := args.Endpoint | ||
| if endpoint == "" { | ||
| endpoint = "localhost:4317" | ||
| } | ||
| otlpClient := otlptracegrpc.NewClient( | ||
| otlptracegrpc.WithEndpoint(endpoint), | ||
| otlptracegrpc.WithInsecure(), | ||
| ) | ||
| if err := otlpClient.Start(ctx.Context); err != nil { | ||
| return err | ||
| } | ||
| //nolint: errcheck | ||
| defer otlpClient.Stop(ctx.Context) | ||
| var msg tracepb.ResourceSpans | ||
| for span := range traceReader.AllWithMemory(&msg) { | ||
| err := otlpClient.UploadTraces(ctx.Context, []*tracepb.ResourceSpans{span}) | ||
| if err != nil { | ||
| printf(ctx.App.Writer, "Error uploading trace: %v", err) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.