def begin_transaction(self): """ Begins a new transaction. You can provide a parent transaction to enable distributed tracing. :param apm_client: APM client. :param args: options loaded from command line or environment variables. :return: None """ if self.args.apm_parent_id_file_load: ts_id = self.args.apm_parent_id_file_load.read() self.args.apm_parent_id_file_load.close() self.parent = trace_parent_from_string(ts_id) self.transaction = self.apm_client.begin_transaction( self.args.transaction_name, trace_parent=self.parent) self.logger.debug('Parent transaction : ' + ts_id) elif self.args.apm_parent_id: self.parent = trace_parent_from_string(self.args.apm_parent_id) self.transaction = self.apm_client.begin_transaction( self.args.transaction_name, trace_parent=self.parent) self.logger.debug('Parent transaction : ' + self.args.apm_parent_id) else: self.transaction = self.apm_client.begin_transaction( self.args.transaction_name) self.logger.debug('The transaction begins.') if self.args.apm_parent_id_file_save: with capture_span('parent'): self.args.apm_parent_id_file_save.write(self.get_parent_id()) self.args.apm_parent_id_file_save.close() return self.transaction
def _run_message_process( self, method_frame: pika.spec.Basic.Deliver, header_frame: pika.spec.BasicProperties, message_body: bytes, handler_function: Callable, _queue_name: str, ) -> CallbackResult: if self.apm_client: if header_frame.headers and "traceparent" in header_frame.headers: parent = elasticapm.trace_parent_from_string( header_frame.headers["traceparent"]) self.apm_client.begin_transaction("RabbitMQ", trace_parent=parent) callback_result: CallbackResult = handler_function( method_frame=method_frame, header_frame=header_frame, message_body=message_body, ) trace_id = elasticapm.get_trace_id() logger.debug( f"{callback_result.action.value.title()} message on Queue<{_queue_name}> with " f"delivery_tag: {method_frame.delivery_tag} trace_id: {trace_id}") if trace_id: tran_result = ("success" if callback_result.action == MQAction.ack else "failure") self.apm_client.end_transaction(name=f"MQ QUEUE {_queue_name}", result=tran_result) return callback_result
def begin_transaction(transaction_name): global apm_cli, apm_parent_id if apm_parent_id: parent = elasticapm.trace_parent_from_string(apm_parent_id) transaction = apm_cli.begin_transaction(transaction_name, trace_parent=parent) LOGGER.debug('Parent transaction : {}'.format(apm_parent_id)) else: transaction = apm_cli.begin_transaction(transaction_name) LOGGER.debug('The {} transaction begins.'.format(transaction_name)) return transaction
def _start_trace(self, event_name: str) -> Optional[str]: if self.apm_client: if event_name is None: raise ValueError("Must set event name when using with apm client") # Get trace parent from the context if exists # This will correlate this MQ with the trigger request traceparent_string = elasticapm.get_trace_parent_header() if traceparent_string: parent = elasticapm.trace_parent_from_string(traceparent_string) else: parent = None self.apm_client.begin_transaction( transaction_type="RabbitMQ", trace_parent=parent ) traceparent_string = elasticapm.get_trace_parent_header() else: traceparent_string = None return traceparent_string
def generate_distributed_traces(self, tasks_data, status, end_time, traceparent, apm_service_name, apm_server_url, apm_verify_server_cert, apm_secret_token, apm_api_key): """ generate distributed traces from the collected TaskData and HostData """ tasks = [] parent_start_time = None for task_uuid, task in tasks_data.items(): if parent_start_time is None: parent_start_time = task.start tasks.append(task) apm_cli = self.init_apm_client(apm_server_url, apm_service_name, apm_verify_server_cert, apm_secret_token, apm_api_key) if apm_cli: instrument() # Only call this once, as early as possible. if traceparent: parent = trace_parent_from_string(traceparent) apm_cli.begin_transaction("Session", trace_parent=parent, start=parent_start_time) else: apm_cli.begin_transaction("Session", start=parent_start_time) # Populate trace metadata attributes if self.ansible_version is not None: label(ansible_version=self.ansible_version) label(ansible_session=self.session, ansible_host_name=self.host, ansible_host_user=self.user) if self.ip_address is not None: label(ansible_host_ip=self.ip_address) for task_data in tasks: for host_uuid, host_data in task_data.host_data.items(): self.create_span_data(apm_cli, task_data, host_data) apm_cli.end_transaction(name=__name__, result=status, duration=end_time - parent_start_time)