Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add transaction.id to errors if a transaction is ongoing #122

Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ https://github.com/elastic/apm-agent-python/compare/v1.0.0\...master[Check the H
* added settings to enable/disable source code collection and local variables collection
for errors and transactions ({pull}117[#117])
* added `service.environment` to provide an environment name (e.g. "production", "staging") ({pull}123[#123])
* added `transaction.id` to errors to better correlate errors with transactions ({pull}122[#122])
* BREAKING: Several settings and APIs have been renamed ({pull}111[#111], {pull}119[#119]):
** The decorator for custom instrumentation, `elasticapm.trace`, is now `elasticapm.capture_span`
** The setting `traces_send_frequency` has been renamed to `transaction_send_frequency`.
Expand Down
9 changes: 7 additions & 2 deletions elasticapm/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import elasticapm
from elasticapm.conf import Config, constants
from elasticapm.traces import TransactionsStore
from elasticapm.traces import TransactionsStore, get_transaction
from elasticapm.transport.base import TransportException
from elasticapm.utils import json_encoder as json
from elasticapm.utils import compat, is_master_process, stacks, varmap
Expand Down Expand Up @@ -230,12 +230,13 @@ def begin_transaction(self, transaction_type):
self.instrumentation_store.begin_transaction(transaction_type)

def end_transaction(self, name, result=''):
self.instrumentation_store.end_transaction(result, name)
transaction = self.instrumentation_store.end_transaction(result, name)
if self.instrumentation_store.should_collect():
self._collect_transactions()
if not self._send_timer:
# send first batch of data after config._wait_to_first_send
self._start_send_timer(timeout=min(self.config._wait_to_first_send, self.config.transaction_send_frequency))
return transaction

def close(self):
self._collect_transactions()
Expand Down Expand Up @@ -443,6 +444,10 @@ def _build_msg_for_logging(self, event_type, date=None, context=None, custom=Non
'timestamp': date.strftime(constants.TIMESTAMP_FORMAT),
})

transaction = get_transaction()
if transaction:
event_data['transaction'] = {'id': transaction.id}

return self._build_msg({'errors': [event_data]})

def _filter_exception_type(self, data):
Expand Down
13 changes: 13 additions & 0 deletions tests/client/client_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,3 +530,16 @@ def test_collect_source_transactions(should_collect, elasticapm_client):
assert 'context_line' not in transaction['spans'][0]['stacktrace'][0], mode
assert 'pre_context' not in transaction['spans'][0]['stacktrace'][0], mode
assert 'post_context' not in transaction['spans'][0]['stacktrace'][0], mode


def test_transaction_id_is_attached(elasticapm_client):
elasticapm_client.capture_message('noid')
elasticapm_client.begin_transaction('test')
elasticapm_client.capture_message('id')
transaction = elasticapm_client.end_transaction('test', 'test')
elasticapm_client.capture_message('noid')

errors = elasticapm_client.events
assert 'transaction' not in errors[0]['errors'][0]
assert errors[1]['errors'][0]['transaction']['id'] == transaction.id
assert 'transaction' not in errors[2]['errors'][0]