Skip to content
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

make exec arg handling precise (#191) #202

Merged
merged 1 commit into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions data_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,8 @@ var suites = []FixtureSuite{
{
Name: "otel-cli span exec echo",
Config: FixtureConfig{
CliArgs: []string{"exec", "--service", "main_test.go", "--name", "test-span-123", "--kind", "server", "echo hello world"},
// intentionally calling a command with no args bc it's a special case in exec.go
CliArgs: []string{"exec", "--service", "main_test.go", "--name", "test-span-123", "--kind", "server", "echo"},
Env: map[string]string{
"OTEL_EXPORTER_OTLP_ENDPOINT": "{{endpoint}}",
"TRACEPARENT": "00-edededededededededededededed9000-edededededededed-01",
Expand All @@ -626,7 +627,7 @@ var suites = []FixtureSuite{
"span_id": "*",
"trace_id": "edededededededededededededed9000",
},
CliOutput: "hello world\n",
CliOutput: "\n",
SpanCount: 1,
},
},
Expand All @@ -638,7 +639,7 @@ var suites = []FixtureSuite{
Config: FixtureConfig{
CliArgs: []string{
"exec", "--name", "outer", "--endpoint", "{{endpoint}}", "--fail", "--verbose", "--",
"./otel-cli", "exec", "--name", "inner", "--endpoint", "{{endpoint}}", "--tp-required", "--fail", "--verbose", "echo hello world"},
"./otel-cli", "exec", "--name", "inner", "--endpoint", "{{endpoint}}", "--tp-required", "--fail", "--verbose", "echo", "hello world"},
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hate changing the tests here but "echo hello world" was a horrible choice I made, and don't remember why.

},
Expect: Results{
Config: otelcli.DefaultConfig(),
Expand Down
29 changes: 14 additions & 15 deletions otelcli/exec.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package otelcli

import (
"bytes"
"context"
"encoding/csv"
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -42,26 +43,22 @@ func init() {
}

func doExec(cmd *cobra.Command, args []string) {
// joining the string here is kinda gross... but should be fine
// there might be a better way in Cobra, maybe require passing it after a '--'?
commandString := strings.Join(args, " ")

// put the command in the attributes, before creating the span so it gets picked up
config.Attributes["command"] = commandString
config.Attributes["command"] = args[0]
config.Attributes["arguments"] = ""

span := NewProtobufSpanWithConfig(config)
var child *exec.Cmd
if len(args) > 1 {
// CSV-join the arguments to send as an attribute
buf := bytes.NewBuffer([]byte{})
csv.NewWriter(buf).WriteAll([][]string{args[1:]})
config.Attributes["arguments"] = buf.String()

// use cmd.exe to launch PowerShell on Windows
var shell string
if runtime.GOOS == "windows" {
shell = "cmd.exe"
commandString = "/C powershell " + commandString
child = exec.Command(args[0], args[1:]...)
} else {
shell = "/bin/sh"
child = exec.Command(args[0])
}

child := exec.Command(shell, "-c", commandString)

// attach all stdio to the parent's handles
child.Stdin = os.Stdin
child.Stdout = os.Stdout
Expand All @@ -78,6 +75,8 @@ func doExec(cmd *cobra.Command, args []string) {
}
}

span := NewProtobufSpanWithConfig(config)

// set the traceparent to the current span to be available to the child process
if config.IsRecording() {
tp := traceparentFromSpan(span)
Expand Down