Skip to content

Commit

Permalink
allow setting attrs at the end of a backgrounded trace (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
dtbell91 authored Sep 13, 2023
1 parent 536cab8 commit 3bbb923
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
16 changes: 13 additions & 3 deletions otelcli/span_background_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ type BgSpanEvent struct {

// BgEnd is an empty struct that can be sent to call End().
type BgEnd struct {
StatusCode string `json:"status_code"`
StatusDesc string `json:"status_description"`
Attributes map[string]string `json:"span_attributes" env:"OTEL_CLI_ATTRIBUTES"`
StatusCode string `json:"status_code"`
StatusDesc string `json:"status_description"`
}

// AddEvent takes a BgSpanEvent from the client and attaches an event to the span.
Expand Down Expand Up @@ -70,9 +71,18 @@ func (bs BgSpan) Wait(in, reply *struct{}) error {
// End takes a BgEnd (empty) struct, replies with the usual trace info, then
// ends the span end exits the background process.
func (bs BgSpan) End(in *BgEnd, reply *BgSpan) error {
// handle --attrs arg to span end by retrieving and merging with/overwriting existing attribtues
attrs := make(map[string]string)
for k, v := range otlpclient.SpanAttributesToStringMap(bs.span) {
attrs[k] = v
}
for key, value := range in.Attributes {
attrs[key] = value
}
// handle --status-code and --status-description args to span end
c := bs.config.WithStatusCode(in.StatusCode).WithStatusDescription(in.StatusDesc)
c := bs.config.WithStatusCode(in.StatusCode).WithStatusDescription(in.StatusDesc).WithAttributes(attrs)
otlpclient.SetSpanStatus(bs.span, c.StatusCode, c.StatusDescription)
bs.span.Attributes = otlpclient.StringMapAttrsToProtobuf(c.Attributes)

// running the shutdown as a goroutine prevents the client from getting an
// error here when the server gets closed. defer didn't do the trick.
Expand Down
5 changes: 4 additions & 1 deletion otelcli/span_end.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ func spanEndCmd(config *Config) *cobra.Command {
See: otel-cli span background
otel-cli span end --sockdir $sockdir
otel-cli span end --sockdir $sockdir \
--attrs "output.length=$(wc -l < output.txt | sed -e 's/^[[:space:]]*//')
`,
Run: doSpanEnd,
}
Expand All @@ -32,6 +33,7 @@ See: otel-cli span background
cmd.Flags().StringVar(&config.SpanEndTime, "end", defaults.SpanEndTime, "an Unix epoch or RFC3339 timestamp for the end of the span")

addSpanStatusParams(&cmd, config)
addAttrParams(&cmd, config)

return &cmd
}
Expand All @@ -41,6 +43,7 @@ func doSpanEnd(cmd *cobra.Command, args []string) {
client, shutdown := createBgClient(config)

rpcArgs := BgEnd{
Attributes: config.Attributes,
StatusCode: config.StatusCode,
StatusDesc: config.StatusDescription,
}
Expand Down
1 change: 0 additions & 1 deletion otlpclient/protobuf_span.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ func StringMapAttrsToProtobuf(attributes map[string]string) []*commonpb.KeyValue
}

// SpanAttributesToStringMap converts the span's attributes to a string map.
// Only used by tests for now.
func SpanAttributesToStringMap(span *tracepb.Span) map[string]string {
out := make(map[string]string)
for _, attr := range span.Attributes {
Expand Down

0 comments on commit 3bbb923

Please sign in to comment.