Try sending Metrics to datadog via python, DogStatsD

Introduction

datadog is a SaaS format server operation monitoring tool By sending each metric to datadog from various servers to be monitored Various analyzes can be performed on the browser.

datadog provides an agent that sends the resources of each server, the number of times the application is executed, and the TAT to datadog, but in this article, I made a script that keeps sending a specific value to datadog with python. I will try it.

I think there are many possible uses, such as sending the amount of data in a specific table in the database or sending the number of times a shell script is executed.

Premise

Use python3

Creating a script to send Metrics

I will try sending Metrics via the DogStats D server

datadog library installation

pip3 install datadog

Sample script

sample.py


import random
from datadog import initialize
from datadog import statsd

#First initialization
options = {
    'statsd_host':'127.0.0.1',
    'statsd_port':8125
}

initialize(**options)

#Create tag and value
tags = ['version:1', 'application:web']
value=random.randint(0, 100)

#metric set
metric="myapp.testdata.set"
statsd.set(metric, value, tags=tags)
#metric gauge
metric="myapp.testdata.gauge"
statsd.gauge(metric, value, tags=tags)

Commentary

sample first half.py


options = {
    'statsd_host':'127.0.0.1',
    'statsd_port':8125
}

initialize(**options)

First is initialization. Specify the address and port of the DogStatsD server. The default is UDP: 8125.

Second half of sample.py


#Create tag and value
tags = ['version:1', 'application:web']
value=random.randint(0, 100)

#metric set
metric="myapp.testdata.set"
statsd.set(metric, value, tags=tags)
#metric gauge
metric="myapp.testdata.gauge"
statsd.gauge(metric, value, tags=tags)

You must use one of the following methods when submitting Metrics.

Method name Commentary
set() The number of times the method is called is sent
gauge() Send the value in the method

Please choose according to your application. If you don't understand at first glance, we will explain it in the next section so that you can understand it sensuously.

Sample script result

The sample script calls both the set and gauge methods. Let's display the result of calling this script every minute with datadog.

datadog_result.png

Since the set method counts that it is called once a minute, the value 1 continues every minute, and the gauge method keeps sending random values (0 to 100). think.

Finally

--The set () method is used for the number of times the script is executed, etc. --The gauge () method is used to record changes in the amount of data, etc.

Wouldn't it be possible to visualize the data more if we could use it properly?

Recommended Posts

Try sending Metrics to datadog via python, DogStatsD
Try to understand Python self
Try to operate Facebook with Python
Try to calculate Trace in Python
Try converting cloudmonkey CLI to python3 -1
Try to get CloudWatch metrics with re: dash python data source
Try to reproduce color film with Python
Try logging in to qiita with Python
Try sending a SYN packet in Python
Try python
Try to operate Excel using Python (Xlwings)
Python amateurs try to summarize the list ①
First steps to try Google CloudVision in Python
Try to implement Oni Maitsuji Miserable in python
Try to calculate a statistical problem in Python
3.14 π day, so try to output in Python
Try auto to automatically price Enums in Python 3.6
Try to solve the Python class inheritance problem
Try to solve the man-machine chart with Python
Try to draw a life curve with python
Try to make a "cryptanalysis" cipher with Python
Try to automatically generate Python documents with Sphinx
Try to calculate RPN in Python (for beginners)
Python mock to try AWS IoT Device Shadow
Try to make a dihedral group with Python
Try to detect fish with python + OpenCV2.4 (unfinished)
Python> try: / except:
"Backport" to python 2
Try to solve the programming challenge book with python3
POST messages from python to Slack via incoming webhook
[First API] Try to get Qiita articles with Python
Python beginners try adding basic auth to Django admin
Try to make a Python module in C language
[Python] Try to read the cool answer to the FizzBuzz problem
Try to make a command standby tool with python
Try to improve your own intro quiz in Python
(Python) Try to develop a web application using Django
Try to solve the internship assignment problem with Python
[Reintroduction to python] How to import via the parent directory
Try to operate DB with Python and visualize with d3
Send data from Python to Processing via socket communication
Try to automate pdf format report creation with Python
Try to extract high frequency words using NLTK (python)