Created
April 24, 2014 20:46
-
-
Save garnaat/11268936 to your computer and use it in GitHub Desktop.
A click-based script to upload a custom metric to StackDriver
This file contains hidden or 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
#!/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