def _log_task_stats(
    method_name,
    resource_instance_id,
    scheduled_execution_delay,
    task_creation_time,
    task_job_count,
    correlation_id=None,
):
    global process_task_count
    process_task_count += 1
    if task_creation_time:
        now = time.time()
        scheduled_execution_time = task_creation_time + scheduled_execution_delay
        current_job_count = cache.get(JOB_COUNT_CACHE_KEY, 0)
        wait_time = now - task_creation_time
        queue_delay = now - scheduled_execution_time
        queue_length = current_job_count - task_job_count
        delivery_info = celery.current_task.request.delivery_info
        queue = delivery_info.get("exchange") or "default"
        routing_key = delivery_info.get("routing_key") or "default"

        cache.set(QUEUE_LENGTH_CACHE_KEY, queue_length)

        if newrelic_agent:
            newrelic_agent.record_custom_event(
                "task_runner:queue_length",
                {
                    "queue_length": queue_length,
                    "queue": queue,
                    "routing_key": routing_key,
                    "wait_time_seconds": wait_time,
                    "queue_delay_seconds": queue_delay,
                    "process_task_count": process_task_count,
                    "correlation_id": correlation_id,
                },
            )

        logger.info(
            "method=%s, resource_id=%s, queue_length=%s, queue=%s, routing_key=%s, task_wait_time=%s, "
            "task_queue_delay=%s, process_task_count=%s, correlation_id=%s",
            method_name,
            resource_instance_id,
            queue_length,
            queue,
            routing_key,
            wait_time,
            queue_delay,
            process_task_count,
            correlation_id,
        )
    else:
        logger.info(
            "method=%s, resource_id=%s, correlation_id=%s",
            method_name,
            resource_instance_id,
            correlation_id,
        )
Esempio n. 2
0
def lambda_handler(event, context):
    # At this point, we're handling an invocation. Cold start is over; this code runs for each invocation.

    # This is an example of a custom event. `FROM MyPythonEvent SELECT *` in New Relic will find this event.
    agent.record_custom_event("MyPythonEvent", {"zip": "zap"})
    # This attribute gets added to the normal AwsLambdaInvocation event
    agent.add_custom_parameter("customAttribute", "customAttributeValue")

    # As normal, anything you write to stdout ends up in CloudWatch
    print("Hello, world")

    return "Success!"
def _log_retry_stats(method_name, resource_instance_id, correlation_id):
    if newrelic_agent:
        newrelic_agent.record_custom_event(
            "task_runner:retry",
            {
                "method_name": method_name,
                "resource_instance_id": resource_instance_id
            },
        )
        logger.warning(
            "will retry task: method=%s, resource_id=%s, correlation_id=%s",
            method_name,
            resource_instance_id,
            correlation_id,
        )
Esempio n. 4
0
def record_custom_event(event_type, params):
    """
    record an event
    Event doesn't share anything with request so we track the request id
    """
    if agent:
        try:
            if not params:
                params = {}
            params['kirin_request_id'] = flask.request.id
        except RuntimeError:
            pass#we are outside of a flask context :(
        try:
            agent.record_custom_event(event_type, params)
        except:
            logger = logging.getLogger(__name__)
            logger.exception('failure while reporting to newrelic')