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

allow setting attrs at the end of a backgrounded trace #255

Merged
merged 1 commit into from
Sep 13, 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
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