Skip to content

Commit ce13f92

Browse files
committed
added API functions for setting transaction outcome
1 parent f23f37f commit ce13f92

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

docs/api.asciidoc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,44 @@ elasticapm.set_transaction_result('SUCCESS')
234234
* `override`: if `True` (the default), overrides any previously set result.
235235
If `False`, only sets the result if the result hasn't already been set.
236236

237+
[float]
238+
[[api-set-transaction-success]]
239+
==== `elasticapm.set_transaction_success()`
240+
241+
Marks the current transaction as successful.
242+
This should only be called at the end of a transaction after the outcome is determined.
243+
For supported frameworks, the transaction outcome is set automatically if it has not been set yet.
244+
245+
This value is used for error rate calculations.
246+
247+
Example:
248+
249+
[source,python]
250+
----
251+
import elasticapm
252+
253+
elasticapm.set_transaction_success()
254+
----
255+
256+
[float]
257+
[[api-set-transaction-failure]]
258+
==== `elasticapm.set_transaction_failure()`
259+
260+
Marks the current transaction as failed.
261+
This should only be called at the end of a transaction after the outcome is determined.
262+
For supported frameworks, the transaction outcome is set automatically if it has not been set yet.
263+
264+
This value is used for error rate calculations.
265+
266+
Example:
267+
268+
[source,python]
269+
----
270+
import elasticapm
271+
272+
elasticapm.set_transaction_success()
273+
----
274+
237275

238276
[float]
239277
[[api-get-transaction-id]]

elasticapm/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@
4040
label,
4141
set_context,
4242
set_custom_context,
43+
set_transaction_failure,
4344
set_transaction_name,
4445
set_transaction_result,
46+
set_transaction_success,
4547
set_user_context,
4648
tag,
4749
)

elasticapm/traces.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,13 @@ def tag(**tags):
729729

730730

731731
def set_transaction_name(name, override=True):
732+
"""
733+
Sets the name of the transaction
734+
735+
:param name: the name of the transaction
736+
:param override: if set to False, the name is only set if no name has been set before
737+
:return: None
738+
"""
732739
transaction = execution_context.get_transaction()
733740
if not transaction:
734741
return
@@ -737,13 +744,52 @@ def set_transaction_name(name, override=True):
737744

738745

739746
def set_transaction_result(result, override=True):
747+
"""
748+
Sets the result of the transaction. The result could be e.g. the HTTP status class (e.g "HTTP 5xx") for
749+
HTTP requests, or "success"/"fail" for background tasks.
750+
751+
:param name: the name of the transaction
752+
:param override: if set to False, the name is only set if no name has been set before
753+
:return: None
754+
"""
755+
740756
transaction = execution_context.get_transaction()
741757
if not transaction:
742758
return
743759
if transaction.result is None or override:
744760
transaction.result = result
745761

746762

763+
def set_transaction_success():
764+
"""
765+
Marks this transaction as successful. This should only be done at the end of a transaction
766+
when the outcome is determined.
767+
768+
This value is used for error rate calculations.
769+
770+
:return: None
771+
"""
772+
transaction = execution_context.get_transaction()
773+
if not transaction:
774+
return
775+
transaction.set_success()
776+
777+
778+
def set_transaction_failure():
779+
"""
780+
Marks this transaction as failed. This should only be done at the end of a transaction
781+
when the outcome is determined.
782+
783+
This value is used for error rate calculations.
784+
785+
:return: None
786+
"""
787+
transaction = execution_context.get_transaction()
788+
if not transaction:
789+
return
790+
transaction.set_failure()
791+
792+
747793
def get_transaction_id():
748794
"""
749795
Returns the current transaction ID

0 commit comments

Comments
 (0)