Skip to content

Instantly share code, notes, and snippets.

@garnaat
Created April 24, 2014 20:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garnaat/11268936 to your computer and use it in GitHub Desktop.
Save garnaat/11268936 to your computer and use it in GitHub Desktop.
A click-based script to upload a custom metric to StackDriver
#!/usr/bin/env python
import requests
import time
import json
import click
@click.command()
@click.option('--name', prompt='metric name',
help='the name of the custom metric', required=True)
@click.option('--value', default=1, help='The value to store',
prompt='metric value', required=True)
@click.option('--api-key', help='Your Stackdriver APIKEY (or SDCUSTOM_API_KEY)',
required=True)
def sdcustom(name, value, api_key):
ts = int(time.time())
data_point = {'name': name,
'value': value,
'collected_at': ts}
gateway = {'timestamp': ts,
'proto_version': 1,
'data': data_point}
headers = {'content-type': 'application/json',
'x-stackdriver-apikey': api_key}
r = requests.post('https://custom-gateway.stackdriver.com/v1/custom',
data=json.dumps(gateway),
headers=headers)
if r.status_code != 201:
print('Error writing to Stackdriver')
print(r.status_code)
if __name__ == '__main__':
sdcustom(auto_envvar_prefix='SDCUSTOM')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment