Skip to content

Instantly share code, notes, and snippets.

@doublerr
Created April 20, 2015 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doublerr/541c91634120967424d2 to your computer and use it in GitHub Desktop.
Save doublerr/541c91634120967424d2 to your computer and use it in GitHub Desktop.
Simple function for submitting metrics to singalfx
# Copyright 2015 Alex Brandt <alex.brandt@rackspace.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import logging
import tornado.gen
import tornado.httpclient
logger = logging.getLogger(__name__)
@tornado.gen.coroutine
def submit(metric_name, metric_value, metric_type = 'gauge', dimensions = None):
'''submit a metric value to signalfuse.
Parameters
----------
:``metric_name``: name of the metric being submitted
:``metric_value``: value of the metric being submitted
:``metric_type``: type of metric­one of gauge, counter, cumulative_counter
:``dimensions``: dictionary mapping dimension names to dimension values
'''
logger.info('STARTING: submit %s', metric_name)
if PARAMETERS['signalfuse.organization_id'] is None or PARAMETERS['signalfuse.token'] is None:
logger.info('STOPPING: submit %s­noop', metric_name)
return
if dimensions is None:
dimensions = {}
request = tornado.httpclient.HTTPRequest(
'https://ingest.signalfx.com/v2/datapoint?orgid={0}'.format(PARAMETERS['signalfuse.organization_id']),
method = 'POST',
headers = {
'Content-Type': 'application/json',
'X-SF-TOKEN': PARAMETERS['signalfuse.token'],
},
body = json.dumps({
metric_type: [
{
'metric': metric_name,
'value': metric_value,
'dimensions': dimensions,
},
],
})
)
logger.debug('request: %s', request)
logger.debug('request.headers: %s', request.headers)
logger.debug('request.body: %s', request.body)
response = yield tornado.httpclient.AsyncHTTPClient().fetch(request, raise_error = False)
if response.code not in range(200,300):
logger.error('error response received')
logger.error('%s %s', response.code, response.reason)
logger.debug('%s', response.body)
if response.error is not None:
logger.error(response.error)
logger.info('STOPPING: submit %s', metric_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment