Skip to content

Commit

Permalink
Add the attrs func
Browse files Browse the repository at this point in the history
Syntactic sugar for:

    var kvs []attribute.KeyValue
    kvs = appendAttrs(kvs, ptraceAttributeMap)
  • Loading branch information
MrAlias committed Nov 6, 2024
1 parent 8a456a8 commit 658f3f4
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions internal/pkg/opentelemetry/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ func (c *Controller) Shutdown(ctx context.Context) error {
return nil
}

func attrs(m pcommon.Map) []attribute.KeyValue {
out := make([]attribute.KeyValue, 0, m.Len())
return appendAttrs(out, m)
}

func appendAttrs(dest []attribute.KeyValue, m pcommon.Map) []attribute.KeyValue {
m.Range(func(k string, v pcommon.Value) bool {
dest = append(dest, attr(k, v))
Expand Down Expand Up @@ -213,8 +218,7 @@ func appendEventOpts(dest []trace.EventOption, e ptrace.SpanEvent) []trace.Event
dest = append(dest, trace.WithTimestamp(ts))
}

var kvs []attribute.KeyValue
kvs = appendAttrs(kvs, e.Attributes())
kvs := attrs(e.Attributes())
if len(kvs) > 0 {
dest = append(dest, trace.WithAttributes(kvs...))
}
Expand Down Expand Up @@ -244,8 +248,8 @@ func (c *Controller) links(links ptrace.SpanLinkSlice) []trace.Link {
TraceFlags: trace.TraceFlags(l.Flags()),
TraceState: ts,
}),
Attributes: attrs(l.Attributes()),
}
out[i].Attributes = appendAttrs(out[i].Attributes, l.Attributes())
}
return out
}
Expand Down

0 comments on commit 658f3f4

Please sign in to comment.