Skip to content

Instantly share code, notes, and snippets.

@ckelner
Created July 6, 2017 14:06
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 ckelner/767343a378c84d0f2070633f3d6ff67c to your computer and use it in GitHub Desktop.
Save ckelner/767343a378c84d0f2070633f3d6ff67c to your computer and use it in GitHub Desktop.

Monitor how many more api calls your git token has

Queries a Github Api rate_limit to find out how close you are to hitting your rate limit. Great for build servers when your not sure why something broke.

Use to create graphs like: Graph or you can display multiple values on a single graph: Graph2

Install

Plugins come with a YAML file and a python file. The YAML file is the configuration, the python file is the custom check code.

  • Copy the github_api.yaml file to /etc/dd-agent/conf.d/
  • Copy the github_api.py file to /etc/dd-agent/checks.d/
  • Test your plugin sudo -u dd-agent dd-agent check github_api
  • The metrics will be available on the datadog webpage after several runs. Running several times from the command line speeds up that process.

Plugin Configuration

  • To check more than one API key, pass multiple token_names and tokens into instances. The plugin should scale to however many API tokens you need to monitor
instances:
    - git_token_name: git_token1
      git_token: 1234567890
    - git_token_name: git_token2
      git_token: 0987654321
  • The metrics created will be github_api.core_remaining.{git_token_name}, github_api.search_remaining.{git_token_name}, github_api.core_remaining.{git_token_name}.
  • {git_token_name} will be the string passed into the plugin via the Instances: section.
  • The plugin will run as often as the datadog agent.
  • By default, the agent runs every 15 seconds.
  • You can add min_collection_interval: X to the init_config: section of the YAML file to make sure to only run the plugin if X seconds has elapsed since the last run. Depending on agent execution time, a value of 45 may not happen every 3rd run, but instead flap between every 3rd and every 4th run.
import requests
import json
from checks import AgentCheck
class GitHubAPI(AgentCheck):
def check(self, instance):
if 'git_token' not in instance:
self.log.info("Skipping instance, no git token found.")
return
if 'git_token_name' not in instance:
self.log.info("Skipping instance, no git token name found.")
return
git_token = instance['git_token']
git_token_name = instance['git_token_name']
# Url to hit
url = 'https://api.github.com/rate_limit?access_token='
# get some stats (comes in a json object)
response = requests.get(url + git_token)
# Cast it to a json object
resp_json = json.loads(response.text)
# Create a few data sources with tags from the data we got back
self.gauge("github_api.core_remaining.{git_token_name}".format(git_token_name=git_token_name), resp_json['resources']['core']['remaining'], tags=['github_api'])
self.gauge("github_api.search_remaining.{git_token_name}".format(git_token_name=git_token_name), resp_json['resources']['search']['remaining'], tags=['github_api'])
self.gauge("github_api.rate_remaining.{git_token_name}".format(git_token_name=git_token_name), resp_json['rate']['remaining'], tags=['github_api'])
init_config:
# Run no more often than X seconds.
# Default interval is 15 seconds. The time is evaluated each run.
# Depending on agent executation time, it's possible 45 doesn't happen every
# 3rd run, but flaps between every 3rd and every 4th run.
min_collection_interval: 15
instances:
- git_token_name: git_token1
git_token: 1234567890
# Multple tokens to monitor?
#- git_token_name: git_token2
# git_token: 0987654321
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment