def wrapped_f(*args, **kwargs): config = None result = None if current_app: config = current_app.config['ELASTIC_APM'] apm_enabled = str(current_app.config['ELASTIC_ENABLED']) == '1' elif sched.app: config = sched.app.app_context().app.config['ELASTIC_APM'] apm_enabled = str(sched.app.app_context().app. config['ELASTIC_ENABLED']) == '1' _name = name if name is not None else func.__name__ if apm_enabled: client = Client(config) if client: client.begin_transaction('registered_funcs') try: result = func(*args, **kwargs) client.end_transaction(f'{_name} - success') except Exception as e: client.capture_exception() client.end_transaction(f'{_name} - error') raise e else: print( f'could not create ElasticAPM client... running <{_name}> without APM' ) result = func(*args, **kwargs) else: result = func(*args, **kwargs) return result
def wrapped_f(*args, **kwargs): client = None if current_app: client = Client(current_app.config['ELASTIC_APM']) elif sched.app: client = Client( sched.app.app_context().app.config['ELASTIC_APM']) _name = name if name is not None else func.__name__ # ensure client was created properly if client: client.begin_transaction('registered_funcs') try: func(*args, **kwargs) client.end_transaction(f'{_name} - success') except Exception as e: client.capture_exception() client.end_transaction(f'{_name} - error') raise e else: print( f'could not create ElasticAPM client... running <{_name}> without APM' ) func(*args, **kwargs)
async def wrapper(*args, **kwargs): try: instrument() client = Client() handler = LoggingHandler() logger = logging.getLogger() logger.addHandler(handler) client.begin_transaction(tran_category) result = await func(*args, **kwargs) client.end_transaction(tran_name, ok_status) return result except Exception as e: logging.error(e, exc_info=True) client.end_transaction(tran_name, error_status) raise
def wrapper(*args, **kwargs): config = None result = None if current_app: config = current_app.config['ELASTIC_APM'] apm_enabled = current_app.config['ELASTIC_ENABLED'] elif sched.app: config = sched.app.app_context().app.config['ELASTIC_APM'] apm_enabled = sched.app.app_context().app.config['ELASTIC_ENABLED'] client = Client(config) if client and apm_enabled: client.begin_transaction('registered_funcs') try: result = func(*args, **kwargs) client.end_transaction(f'{func.__name__} - success') except Exception as e: client.capture_exception() client.end_transaction(f'{func.__name__} - error') raise e else: print(f'Running <{func.__name__}> without APM') result = func(*args, **kwargs) return result
recepient_email = "*****@*****.**" sender_email = "*****@*****.**" logger = logging.getLogger() logger.setLevel(logging.INFO) client = Client( { 'SERVER_URL': 'https://xxxxxxxxxxxxxxxxxxxxxxxx.apm.us-east-1.aws.cloud.es.io:443', 'SERVICE_NAME': 'feedback-form', 'ENVIRONMENT': 'prod', 'SECRET_TOKEN': 'xxxxxxxxxxxxxxxxxxxxxxx' } ) client.begin_transaction('request') def lambda_handler(event, context): try: validate(event=event, schema=schemas.INPUT, envelope="queryStringParameters") except SchemaValidationError: client.capture_exception() return { "statusCode": 400, "body": json.dumps({"message": "Bad Request"}), } except: client.capture_exception() return { "statusCode": 500, "body": json.dumps({"message": "Internal Request failed"}) }
'%(message)s') logging.basicConfig(format=formatter, level=logging.INFO) except Exception: client.capture_exception() try: READ_WEBSOCKET_DELAY = 1 # 1 second delay reading from firehose if slack_client.rtm_connect(): LOGGER.info("Bot connected and running!") client.capture_message('connected the bot.') try: while True: command, channel = parse_slack_output( slack_client.rtm_read()) if command and channel: client.begin_transaction(transaction_type='request') handle_command(command, channel) # elasticapm.set_user_context(command=command, channel=channel) client.capture_message('got command') client.end_transaction('processor', 200) # client.begin_transaction('processors',transaction_type='request') time.sleep(READ_WEBSOCKET_DELAY) except KeyboardInterrupt: client.capture_exception() pass else: LOGGER.error("Connection failed. Invalid Slack token or bot ID?") client.capture_exception()
pubsub.subscribe([redis_channel]) for item in pubsub.listen(): try: message = json.loads(str(item['data'].decode("utf-8"))) except Exception as e: log_message(e) continue if not zipkin_url or 'zipkinSpan' not in message: log_message(message) continue span_data = message['zipkinSpan'] try: with zipkin_span(service_name='log-message-processor', zipkin_attrs=ZipkinAttrs( trace_id=span_data['_traceId']['value'], span_id=generate_random_64bit_string(), parent_span_id=span_data['_spanId'], is_sampled=span_data['_sampled']['value'], flags=None), span_name='save_log', transport_handler=http_transport, sample_rate=100): client.begin_transaction('logger') log_message(message) client.end_transaction('logger') except Exception as e: print('did not send data to Zipkin: {}'.format(e)) log_message(message)
@elasticapm.capture_span() def log_message(message): time_delay = random.randrange(0, 2000) time.sleep(time_delay / 1000) print('message received after waiting for {}ms: {}'.format(time_delay, message)) if __name__ == '__main__': redis_host = os.environ['REDIS_HOST'] redis_port = int(os.environ['REDIS_PORT']) redis_channel = os.environ['REDIS_CHANNEL'] pubsub = redis.Redis(host=redis_host, port=redis_port, db=0).pubsub() pubsub.subscribe([redis_channel]) for item in pubsub.listen(): try: message = json.loads(str(item['data'].decode("utf-8"))) except Exception as e: log_message(e) continue spanTransaction = message['spanTransaction'] trace_parent1 = spanTransaction['context']['request']['headers']['elastic-apm-traceparent'] print('trace_parent_log: {}'.format(trace_parent1)) trace_parent = TraceParent.from_string(trace_parent1) client.begin_transaction("logger-transaction", trace_parent=trace_parent) log_message(message) client.end_transaction('logger-transaction')