Skip to content

Commit

Permalink
add w3 trace context support to cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
cewkrupa committed Sep 18, 2024
1 parent 8a9106e commit 67b6c06
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
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")

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"],
)
} else {
cmd.Env = append(os.Environ(),
"HONEYCOMB_TRACE="+propagation.MarshalHoneycombTraceContext(prop),
)
}

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

0 comments on commit 67b6c06

Please sign in to comment.