Skip to content

Instantly share code, notes, and snippets.

@reachlin
Last active May 18, 2023 09:22
Show Gist options
  • Save reachlin/76077b6c5d982c02cc5090966d78d8ac to your computer and use it in GitHub Desktop.
Save reachlin/76077b6c5d982c02cc5090966d78d8ac to your computer and use it in GitHub Desktop.
datadog.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyNjv8oVNv6WDcDPjghc7bwD",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/reachlin/76077b6c5d982c02cc5090966d78d8ac/datadog.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"source": [
"!pip install datadog_api_client python-dateutil numpy pandas"
],
"metadata": {
"id": "NUd71Y7RwOrb"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "SGzOfEW-wEz0",
"outputId": "fd87a31e-1f1d-4db1-b3b4-40df78ce6113"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
" metric timestamp value\n",
"0 avg:aws.rds.database_connections{dbinstanceide... 1.684398e+12 75.0\n",
"1 avg:aws.rds.database_connections{dbinstanceide... 1.684398e+12 76.0\n",
"2 avg:aws.rds.database_connections{dbinstanceide... 1.684398e+12 80.0\n",
"3 avg:aws.rds.database_connections{dbinstanceide... 1.684398e+12 81.0\n",
"4 avg:aws.rds.database_connections{dbinstanceide... 1.684398e+12 72.0\n"
]
}
],
"source": [
"\"\"\"\n",
"Query Datadog timeseries points and save to csv.\n",
"\"\"\"\n",
"\n",
"import os\n",
"import pandas as pd\n",
"from datetime import datetime\n",
"from dateutil.relativedelta import relativedelta\n",
"from datadog_api_client import ApiClient, Configuration\n",
"from datadog_api_client.v1.api.metrics_api import MetricsApi\n",
"\n",
"\n",
"os.environ['DD_SITE'] =\"datadoghq.com\"\n",
"os.environ['DD_API_KEY'] =\"...\"\n",
"os.environ['DD_APP_KEY'] =\"...\"\n",
"\n",
"df = pd.DataFrame()\n",
"\n",
"configuration = Configuration()\n",
"with ApiClient(configuration) as api_client:\n",
" api_instance = MetricsApi(api_client)\n",
" response = api_instance.query_metrics(\n",
" _from=int((datetime.now() + relativedelta(minutes=-30)).timestamp()),\n",
" to=int(datetime.now().timestamp()),\n",
" query=\"avg:aws.rds.database_connections{env:production} by {dbinstanceidentifier}\",\n",
" )\n",
"\n",
" for item in response['series']:\n",
" print(f\"{item['expression']} {item['start']} - {item['end']}\")\n",
" data = item['pointlist']\n",
" df_item = pd.DataFrame(\n",
" {\n",
" 'metric': f\"{item['expression']}\",\n",
" 'timestamp': [x.value[0] for x in data], \n",
" 'value': [x.value[1] for x in data] \n",
" }\n",
" )\n",
" df = pd.concat([df, df_item], ignore_index=True)\n",
"\n",
"print(df.head())\n",
"df.to_csv('metrcs.csv')\n"
]
},
{
"cell_type": "code",
"source": [
"# last 30 mins\n",
"a = 1684397280000.0 - 1684395720000.0\n",
"b = 1684395780000.0 - 1684395720000.0\n",
"\n",
"print(a/1000/60, b/1000/60)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "9tLgFXbj6f5f",
"outputId": "a48c6ef5-539b-4d06-d8fc-2ae1b759f240"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"26.0 1.0\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# last 10 mins\n",
"b = (1684396320000.0 - 1684396260000.0)/1000/60\n",
"a = (1684396380000.0 - 1684395900000.0)/1000/60\n",
"print(a, b)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "e2iCfri93yzY",
"outputId": "37946804-809c-462b-b7c2-d0ebf5d1858e"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"8.0 1.0\n"
]
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment