Created
June 13, 2012 17:59
-
-
Save alq666/2925528 to your computer and use it in GitHub Desktop.
Measure datadog latency using datadog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Measures redirect latency from http://app.datadoghq.com to login. | |
Use like this: curl -L -v http://app.datadoghq.com --trace-time 2>&1 | python redirect-timing.py | |
""" | |
import sys | |
from datetime import datetime, time | |
import time as t2 | |
import re | |
from dogapi import dog_http_api | |
from ConfigParser import ConfigParser | |
import os | |
# import logging; logging.basicConfig(level=logging.DEBUG) | |
# Assumes dog is configured | |
cfg = ConfigParser() | |
cfg.read(os.path.join(os.environ["HOME"], ".dogrc")) | |
dog_http_api.api_key = cfg.get("Connection", "apikey") | |
dog_http_api.application_key = cfg.get("Connection", "appkey") | |
TS_RE = re.compile(r"^(\d{2}:\d{2}:\d{2}.\d{6})") | |
REQ_RE = re.compile(r"request to this URL") | |
first_line = True | |
convert = lambda s: datetime.strptime(s, "%H:%M:%S.%f") | |
to_ms = lambda td: td.seconds * 1000.0 + td.microseconds / 1000.0 | |
reqs = [] | |
start = t2.time() | |
while True: | |
line = sys.stdin.readline() | |
if line is None or len(line) == 0: | |
# Done, print results | |
break | |
line = line.strip() | |
# Get start of experiment | |
if first_line: | |
m = TS_RE.match(line) | |
assert m, line | |
reqs.append((convert(m.group(1)), None)) | |
first_line = False | |
continue | |
m = REQ_RE.search(line) | |
if m: | |
elements = line.split() | |
ts = convert(elements[0]) | |
url = elements[-1][1:-1] | |
reqs.append((ts, url)) | |
# Finally substract reqs[1:] from its predecessor | |
results = [(to_ms(reqs[i][0] - reqs[i-1][0]), reqs[i][1]) for i in range(1, len(reqs))] | |
for duration, url in results: | |
dog_http_api.metric("datadog.net.redirect", (start, duration), tags=["url:{0}".format(url)]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment