Skip to main content

Queue time tracker

Reads time when request was processed by nginx & send time spent in queue before it was handled by wsgi.
Designed mostly for autoscaling. Instance cpu is not trustworthy enough, sometimes there can be external bottlenecks, so even if instance cpu is fine, application can hang in queue between nginx(for example) and gunicorn or another wsgi processor.

Configuration

  1. Add header with request timestamp.

    nginx:

    proxy_set_header X-RequestTime $msec;
    
  2. Read header value & send metric value

Django application

  1. Configure django settings:

aws:

  • QUEUE_TIME_TRACKER_NOTIFY_EVERY_SECONDS = 10
  • QUEUE_TIME_TRACKER_HEADER = 'HTTP_X_REQUESTTIME'
  • QUEUE_TIME_TRACKER_CACHE_NAME = 'default'
  • QUEUE_TIME_TRACKER_CACHE_KEY_PREFIX = 'foobar'
  • QUEUE_TIME_TRACKER_CLOUDWATCH_NAMESPACE = 'FooBar Web'
  • QUEUE_TIME_TRACKER_CLOUDWATCH_ACCESS_KEY = 'AK****A'
  • QUEUE_TIME_TRACKER_CLOUDWATCH_SECRET_KEY = 'so******BS'
  • QUEUE_TIME_TRACKER_CLOUDWATCH_REGION = 'us-west-1'

azure:

  • QUEUE_TIME_TRACKER_AZURE_REGION = 'eastus'
  • QUEUE_TIME_TRACKER_AZURE_SUBSCRIPTION_ID = '*****'
  • QUEUE_TIME_TRACKER_AZURE_RESOURCE_GROUP_NAME = 'example_group'
  • QUEUE_TIME_TRACKER_AZURE_PROVIDER_NAME = 'Microsoft.ContainerService'
  • QUEUE_TIME_TRACKER_AZURE_RESOURCE_TYPE = 'managedClusters'
  • QUEUE_TIME_TRACKER_AZURE_RESOURCE_ID = 'example-k8s-cluster'
  1. Wrap wsgi application with time tracker: aws:
from request_time_tracker.wsgi_django.cloudwatch import CloudWatchQueueTimeTracker

application = get_wsgi_application()

application = CloudWatchQueueTimeTracker(application)

azure:

from request_time_tracker.wsgi_django.azure import AzureMonitoringQueueTimeTracker

application = get_wsgi_application()

application = AzureMonitoringQueueTimeTracker(application)

Non-django application

  1. Wrap wsgi application with time tracker. Example:
from functools import partial
from request_time_tracker.trackers.cache.redis import RedisCacheQueueTimeTracker
from request_time_tracker.notifiers.cloudwatch import CloudWatchNotifier

tracker = partial(
    RedisCacheQueueTimeTracker, 
    queue_time_header_name='HTTP_X_REQUESTTIME',
    redis_url='redis://localhost:6379/0',
    notifier=CloudWatchNotifier(
        namespace='FooBar Web',
        aws_access_key='AK****A',
        aws_secret_key='so******BS',
        aws_region='us-west-1',
    ),
)

wsgi_application = tracker(wsgi_application)

Cloudwatch role policy:

{
    “Version”: “2012-10-17",
    “Statement”: [
        {
            “Sid”: “VisualEditor0”,
            “Effect”: “Allow”,
            “Action”: [“cloudwatch:PutMetricData”],
            “Resource”: “*”
        }
    ]
}

Azure

role definition: Monitoring Metrics Publisher https://docs.microsoft.com/en-us/azure/role-based-access-control/built-in-roles#monitoring-metrics-publisher

Metadata

Release files for request-time-tracker 0.0.20

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for request-time-tracker 0.0.20
File Size Uploaded
request-time-tracker-0.0.20.tar.gz 15.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for request-time-tracker 0.0.20
File Interpreter ABI Platform
request_time_tracker-0.0.20-py3-none-any.whl Python 3 none any Details

Total release size: 41.0 kB

Release files / request-time-tracker-0.0.20.tar.gz

Download URL request-time-tracker-0.0.20.tar.gz
Size 15.1 kB
Tags Source
SHA-256 checksum
How to use checksums
816505109580428a78eeb20581eca0d7828337b2ae307388176880956ac818e6
BLAKE2b-256 checksum
How to use checksums
ff2b28a4717a6ba697b5c5c202752d159d5f4f0a8dea4c3993d6c43ed953961c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.16

Release files / request_time_tracker-0.0.20-py3-none-any.whl

Download URL request_time_tracker-0.0.20-py3-none-any.whl
Size 25.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1367205932369e1bcec8e8519f7c3b006ae8e931473c3d4d6825e2011fe09840
BLAKE2b-256 checksum
How to use checksums
0adaa630397a06d84a9c1b13209bf155c9a638ce1cbb57b5f3fe00de4aeaa898
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.16
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page