@@ -96,6 +96,17 @@ def ensure_parent_id(self):
9696 logger .debug ("Set parent id to generated %s" , self .trace_parent .span_id )
9797 return self .trace_parent .span_id
9898
99+ def tag (self , ** tags ):
100+ """
101+ Tag this transaction with one or multiple key/value tags. Both the values should be strings
102+
103+ transaction_obj.tag(key1="value1", key2="value2")
104+
105+ Note that keys will be dedotted, replacing dot (.), star (*) and double quote (") with an underscore (_)
106+ """
107+ for key in tags .keys ():
108+ self .tags [TAG_RE .sub ("_" , compat .text_type (key ))] = encoding .keyword_field (compat .text_type (tags [key ]))
109+
99110 def to_dict (self ):
100111 self .context ["tags" ] = self .tags
101112 result = {
@@ -161,12 +172,20 @@ def __init__(self, transaction, name, span_type, context=None, leaf=False, tags=
161172 self .duration = None
162173 self .parent = None
163174 self .frames = None
164- self .tags = tags
165- if self .tags :
166- for key in list (self .tags .keys ()):
167- self .tags [TAG_RE .sub ("_" , compat .text_type (key ))] = encoding .keyword_field (
168- compat .text_type (self .tags .pop (key ))
169- )
175+ self .tags = {}
176+ if tags :
177+ self .tag (** tags )
178+
179+ def tag (self , ** tags ):
180+ """
181+ Tag this span with one or multiple key/value tags. Both the values should be strings
182+
183+ span_obj.tag(key1="value1", key2="value2")
184+
185+ Note that keys will be dedotted, replacing dot (.), star (*) and double quote (") with an underscore (_)
186+ """
187+ for key in tags .keys ():
188+ self .tags [TAG_RE .sub ("_" , compat .text_type (key ))] = encoding .keyword_field (compat .text_type (tags [key ]))
170189
171190 def to_dict (self ):
172191 result = {
@@ -300,13 +319,10 @@ def tag(**tags):
300319 Tags current transaction. Both key and value of the tag should be strings.
301320 """
302321 transaction = get_transaction ()
303- for name , value in tags .items ():
304- if not transaction :
305- error_logger .warning ("Ignored tag %s. No transaction currently active." , name )
306- return
307- # replace invalid characters for Elasticsearch field names with underscores
308- name = TAG_RE .sub ("_" , compat .text_type (name ))
309- transaction .tags [compat .text_type (name )] = encoding .keyword_field (compat .text_type (value ))
322+ if not transaction :
323+ error_logger .warning ("Ignored tags %s. No transaction currently active." , ", " .join (tags .keys ()))
324+ else :
325+ transaction .tag (** tags )
310326
311327
312328def set_transaction_name (name , override = True ):
0 commit comments