swift-distributed-tracing icon indicating copy to clipboard operation
swift-distributed-tracing copied to clipboard

Add guidance in README on how to set span attributes

Open adam-fowler opened this issue 2 years ago • 4 comments

Accessing span attributes is most likely accessed behind a lock. To avoid locking and unlocking continuously it is preferable span attributes are built up separate from the span and then set once on the span.

Eg replace

span.attributes["attr1"] = value1
span.attributes["attr2"] = value2
...

with

var attributes = span.attributes
attributes["attr1"] = value1
attributes["attr2"] = value2
...
span.attributes = attributes

adam-fowler avatar May 11 '23 09:05 adam-fowler

The alternative would be to replace the API such that you can't set single attributes.

adam-fowler avatar May 11 '23 09:05 adam-fowler

var attributes = span.attributes
attributes["attr1"] = value1
attributes["attr2"] = value2
...
span.attributes = attributes

I suspect if this is the best practice, since it’s risk breaking data integrity by overwriting attributes set by other actors. For most users, setting one by one should be the easiest and safest. A possibility addition for bulk access to span attributes might be

span.withAttributes { attributes in
    attributes["attr1"] = value1
    attributes["attr2"] = value2
}

stevapple avatar May 11 '23 10:05 stevapple

I like the bulk change idea, that's viable -- thank you @stevapple.

I think we can add these after the 1.0 which we're about to cut

ktoso avatar May 25 '23 22:05 ktoso

This change was added via #133, but we might want to highlight it in the documentation as well.

slashmo avatar Sep 06 '23 22:09 slashmo