Skip to content
Closed
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
28 changes: 15 additions & 13 deletions elasticapm/traces.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ def ensure_parent_id(self):
logger.debug("Set parent id to generated %s", self.trace_parent.span_id)
return self.trace_parent.span_id

def tag(self, **tags):
for key in tags.keys():
self.tags[TAG_RE.sub("_", compat.text_type(key))] = encoding.keyword_field(compat.text_type(tags[key]))

def to_dict(self):
self.context["tags"] = self.tags
result = {
Expand Down Expand Up @@ -161,12 +165,13 @@ def __init__(self, transaction, name, span_type, context=None, leaf=False, tags=
self.duration = None
self.parent = None
self.frames = None
self.tags = tags
if self.tags:
for key in list(self.tags.keys()):
self.tags[TAG_RE.sub("_", compat.text_type(key))] = encoding.keyword_field(
compat.text_type(self.tags.pop(key))
)
self.tags = {}
if tags:
self.tag(**tags)

def tag(self, **tags):
for key in tags.keys():
self.tags[TAG_RE.sub("_", compat.text_type(key))] = encoding.keyword_field(compat.text_type(tags[key]))

def to_dict(self):
result = {
Expand Down Expand Up @@ -300,13 +305,10 @@ def tag(**tags):
Tags current transaction. Both key and value of the tag should be strings.
"""
transaction = get_transaction()
for name, value in tags.items():
if not transaction:
error_logger.warning("Ignored tag %s. No transaction currently active.", name)
return
# replace invalid characters for Elasticsearch field names with underscores
name = TAG_RE.sub("_", compat.text_type(name))
transaction.tags[compat.text_type(name)] = encoding.keyword_field(compat.text_type(value))
if not transaction:
error_logger.warning("Ignored tags %s. No transaction currently active.", ", ".join(tags.keys()))
else:
transaction.tag(**tags)


def set_transaction_name(name, override=True):
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

assert os.path.exists(ERRORS_SCHEMA) and os.path.exists(
TRANSACTIONS_SCHEMA
), 'JSON Schema files not found. Run "make update-json-schema to download'
), 'JSON Schema files not found. Run "make update-json-schema" to download'


with codecs.open(ERRORS_SCHEMA, encoding="utf8") as errors_json, codecs.open(
Expand Down
15 changes: 8 additions & 7 deletions tests/instrumentation/transactions_store_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,14 @@ def test_get_transaction_clear():

def test_tag_transaction():
requests_store = Tracer(lambda: [], lambda: [], lambda *args: None)
t = requests_store.begin_transaction("test")
transaction = requests_store.begin_transaction("test")
elasticapm.tag(foo="bar")
transaction.tag(baz="bazzinga")
requests_store.end_transaction(200, "test")

assert t.tags == {"foo": "bar"}
transaction_dict = t.to_dict()
assert transaction_dict["context"]["tags"] == {"foo": "bar"}
assert transaction.tags == {"foo": "bar", "baz": "bazzinga"}
transaction_dict = transaction.to_dict()
assert transaction_dict["context"]["tags"] == {"foo": "bar", "baz": "bazzinga"}


def test_tag_while_no_transaction(caplog):
Expand Down Expand Up @@ -328,8 +329,8 @@ def test_transaction_without_name_result(elasticapm_client):

def test_span_tagging(elasticapm_client):
elasticapm_client.begin_transaction("test")
with elasticapm.capture_span("test", tags={"foo": "bar", "ba.z": "baz.zinga"}):
pass
with elasticapm.capture_span("test", tags={"foo": "bar", "ba.z": "baz.zinga"}) as span:
span.tag(lorem="ipsum")
elasticapm_client.end_transaction("test", "OK")
span = elasticapm_client.events[SPAN][0]
assert span["context"]["tags"] == {"foo": "bar", "ba_z": "baz.zinga"}
assert span["context"]["tags"] == {"foo": "bar", "ba_z": "baz.zinga", "lorem": "ipsum"}