Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions submit_metric_with_multible_hosts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# <img src="https://cdn.pixabay.com/photo/2016/10/15/15/17/digital-1742679_1280.jpg" alt="code generation Icon" width="" height="">

## What does this script do

When using the V2 metric submission API endpoint (https://docs.datadoghq.com/api/latest/metrics/#submit-metrics) users cannot specify multiple hots tags on the same `series` parameter.

Instead, we need to define a seperate `series` for each hostname.

This could be a tedious process especially if we have a large number of hosts with different metric value and timestamps.

`generate.sh` will automatically create a ready to use script that contains the proper API call to submit multiple data points for multiple hosts in different timestamps.

## Instructions

Before launching the `generate.sh` script, make sure to fill up the `data.csv` file with the list of host names, their metric values and their timestamp accordingly.

Once done you can launch the `generate.sh` script which will automatically generate another script `send.sh` which could be executed to submit the metrics.

> **_NOTE:_** Please make sure to replace the API key and the endpoint URL accordingly.
5 changes: 5 additions & 0 deletions submit_metric_with_multible_hosts/data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
hostname,value,timestamp
A_host,6,1689773651
B_host,4,1689773651
C_host,2,1689773651
D_host,2.5,1689773651
45 changes: 45 additions & 0 deletions submit_metric_with_multible_hosts/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
cat > send.sh <<EOF
curl -X POST "https://api.datadoghq.com/api/v2/series" \\
-H "Accept: application/json" \\
-H "Content-Type: application/json" \\
-H "DD-API-KEY: YOUR_API_KEY_HERE" \\
-d @- << EOF
{
"series": [
EOF

i=0
total=$(cat data.csv | wc -l)
total=$(($total - 2))

while IFS="," read -r hostname value timestamp
do
cat >> send.sh <<EOF
{
"metric": "example.metric.1",
"type": 0,
"points": [
{
"timestamp": "$timestamp",
"value": "$value"
}
],
"resources": [
{
"name": "$hostname",
"type": "host"
}
]
}
EOF
if [ $i -eq $total ]
then
echo " ]" >> send.sh
else
echo "," >> send.sh
fi
i=$((i+1))
done < <(tail -n +2 data.csv)
echo "}" >> send.sh
echo "EOF" >> send.sh
bash send.sh