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

feat: add w3 trace context support to cmd #220

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,10 @@ The event that arrives at Honeycomb might look like:

## Attaching more traces from your build and test process

Every command running through `buildevents cmd` will receive a `HONEYCOMB_TRACE` environment variable that contains a marshalled trace propagation context. This can be used to connect more spans to this trace.
Every command running through `buildevents cmd` will receive a `HONEYCOMB_TRACE` environment variable that contains a marshalled Honeycomb Beeline trace propagation context. This can be used to connect more spans to this trace.

You may also use a W3 propagation context by passing the `--w3` flag to `buildevents cmd`. When this flag is set,
every command will instead receive the `HONEYCOMB_W3_TRACEPARENT` and `HONEYCOMB_W3_TRACESTATE` environment variables.

Ruby Beeline example:
```ruby
Expand Down
20 changes: 15 additions & 5 deletions cmd_cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"crypto/rand"
"fmt"
"os"
Expand Down Expand Up @@ -47,6 +48,7 @@ will be launched via "bash -c" using "exec". The shell can be changed with the
name := strings.TrimSpace(args[2])
quiet, _ := cmd.Flags().GetBool("quiet")
shell, _ := cmd.Flags().GetString("shell")
useW3, _ := cmd.Flags().GetBool("w3")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

totally open to a flag name that makes more sense


var quoted []string
for _, s := range args[3:] {
Expand Down Expand Up @@ -75,7 +77,7 @@ will be launched via "bash -c" using "exec". The shell can be changed with the
ParentID: spanID,
TraceContext: localFields,
}
err := runCommand(subcmd, prop, quiet, shell)
err := runCommand(subcmd, prop, quiet, shell, useW3)
dur := time.Since(start)

ev.Add(map[string]interface{}{
Expand Down Expand Up @@ -115,15 +117,23 @@ will be launched via "bash -c" using "exec". The shell can be changed with the
return execCmd
}

func runCommand(subcmd string, prop *propagation.PropagationContext, quiet bool, shell string) error {
func runCommand(subcmd string, prop *propagation.PropagationContext, quiet bool, shell string, useW3Context bool) error {
if !quiet {
fmt.Println("running", shell, "-c", subcmd)
}
cmd := exec.Command(shell, "-c", subcmd)

cmd.Env = append(os.Environ(),
"HONEYCOMB_TRACE="+propagation.MarshalHoneycombTraceContext(prop),
)
if useW3Context {
_, headers := propagation.MarshalW3CTraceContext(context.Background(), prop)
cmd.Env = append(os.Environ(),
"HONEYCOMB_W3_TRACEPARENT="+headers[propagation.TraceparentHeader],
"HONEYCOMB_W3_TRACESTATE="+headers["tracestate"],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

for some reason TraceparentHeader is exported, but TracestateHeader isn't

)
} else {
cmd.Env = append(os.Environ(),
"HONEYCOMB_TRACE="+propagation.MarshalHoneycombTraceContext(prop),
)
}

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down
Loading