Skip to content

Instantly share code, notes, and snippets.

@asalkeld
Created October 24, 2012 06:03
Show Gist options
  • Save asalkeld/3944299 to your computer and use it in GitHub Desktop.
Save asalkeld/3944299 to your computer and use it in GitHub Desktop.
openstack common instrumentation poc
import time
import collections
TYPE_GAUGE = 'gauge'
TYPE_DELTA = 'delta'
TYPE_COUNTER = 'counter'
TYPE_GAUGE_TIME = 'gauge'
# for billing
Meter = collections.namedtuple('Meter',
' '.join(['source',
'name',
'type',
'volume',
'user_id',
'project_id',
'resource_id',
'timestamp',
'resource_metadata',
])
)
# for user level monitoring (like cloudwatch)
Monitor = collections.namedtuple('Monitor',
' '.join(['name',
'type',
'volume',
'user_id',
'project_id',
'resource_id',
])
)
# for system level monitoring (operator/admin - no user info)
# and potentially higher rate.
Stat = collections.namedtuple('Stat',
' '.join(['name',
'type',
'volume',
])
)
statsd_type = {TYPE_COUNTER: 'c', TYPE_GAUGE: 'g',
TYPE_DELTA: 'g', TYPE_GAUGE_TIME: 'ms'}
def send_stats(st):
if isinstance(st, Meter):
# send to metering, monitoring and stats
print st
elif isinstance(st, Monitor):
# send to monitoring and stats
print st
else:
# send to stats only
if st.type == TYPE_GAUGE_TIME:
print '%s:%s|%s' % (st.name, st.volume * 1000, statsd_type[st.type])
else:
print '%s:%s|%s' % (st.name, st.volume, statsd_type[st.type])
def instrument_exectime(func):
def wrapped(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
diff = time.time() - start
send_stats(Stat(name=func.__name__,
type=TYPE_GAUGE_TIME,
volume=diff))
return result
return wrapped
def instrument_calls(func):
def wrapper(*args, **kwargs):
wrapper.counter = wrapper.counter + 1
send_stats(Stat(name=func.__name__,
type=TYPE_COUNTER,
volume=wrapper.counter))
return func(*args, **kwargs)
wrapper.counter = 0
return wrapper
@instrument_exectime
@instrument_calls
def hello():
time.sleep(1)
hello()
hello()
# Stat gauge
send_stats(Stat(name='my_diag.val',
type=TYPE_GAUGE,
volume=56))
# Monitor counter
send_stats(Monitor(name='app.hits',
type=TYPE_COUNTER,
volume=45443,
user_id='fred',
project_id='my_proj',
resource_id='inst-0003'))
# Meter gauge
send_stats(Meter(source='?',
name='compute.cpu',
type=TYPE_GAUGE,
volume=43,
user_id='fred',
project_id='my_proj',
resource_id='inst-0003',
timestamp=time.time(),
resource_metadata='morestuff here'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment