unicodeC / Datadog API Collection / Query timeseries data across multiple products
Back to Collection Items
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkStringBuilderW.h>
void ChilkatSample(void)
{
HCkHttpW http;
BOOL success;
HCkJsonObjectW json;
HCkHttpResponseW resp;
HCkStringBuilderW sbResponseBody;
HCkJsonObjectW jResp;
int respStatusCode;
const wchar_t *query_index;
int j;
int count_j;
const wchar_t *strVal;
const wchar_t *family;
const wchar_t *name;
const wchar_t *plural;
const wchar_t *scale_factor;
const wchar_t *short_name;
const wchar_t *v_Type;
const wchar_t *errors;
int i;
int count_i;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http = CkHttpW_Create();
// Use this online tool to generate code from sample JSON: Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "data": {
// "type": "timeseries_request",
// "attributes": {
// "to": "<long>",
// "from": "<long>",
// "queries": [
// {
// "data_source": "metrics",
// "query": "<string>",
// "name": "<string>"
// },
// {
// "data_source": "metrics",
// "query": "<string>",
// "name": "<string>"
// }
// ],
// "formulas": [
// {
// "formula": "<string>",
// "limit": {
// "count": "<integer>",
// "order": "desc"
// }
// },
// {
// "formula": "<string>",
// "limit": {
// "count": "<integer>",
// "order": "desc"
// }
// }
// ],
// "interval": "<long>"
// }
// }
// }
json = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(json,L"data.type",L"timeseries_request");
CkJsonObjectW_UpdateString(json,L"data.attributes.to",L"<long>");
CkJsonObjectW_UpdateString(json,L"data.attributes.from",L"<long>");
CkJsonObjectW_UpdateString(json,L"data.attributes.queries[0].data_source",L"metrics");
CkJsonObjectW_UpdateString(json,L"data.attributes.queries[0].query",L"<string>");
CkJsonObjectW_UpdateString(json,L"data.attributes.queries[0].name",L"<string>");
CkJsonObjectW_UpdateString(json,L"data.attributes.queries[1].data_source",L"metrics");
CkJsonObjectW_UpdateString(json,L"data.attributes.queries[1].query",L"<string>");
CkJsonObjectW_UpdateString(json,L"data.attributes.queries[1].name",L"<string>");
CkJsonObjectW_UpdateString(json,L"data.attributes.formulas[0].formula",L"<string>");
CkJsonObjectW_UpdateString(json,L"data.attributes.formulas[0].limit.count",L"<integer>");
CkJsonObjectW_UpdateString(json,L"data.attributes.formulas[0].limit.order",L"desc");
CkJsonObjectW_UpdateString(json,L"data.attributes.formulas[1].formula",L"<string>");
CkJsonObjectW_UpdateString(json,L"data.attributes.formulas[1].limit.count",L"<integer>");
CkJsonObjectW_UpdateString(json,L"data.attributes.formulas[1].limit.order",L"desc");
CkJsonObjectW_UpdateString(json,L"data.attributes.interval",L"<long>");
CkHttpW_SetRequestHeader(http,L"Content-Type",L"application/json");
CkHttpW_SetRequestHeader(http,L"Accept",L"application/json");
resp = CkHttpW_PostJson3(http,L"https://api.app.ddog-gov.com/api/v2/query/timeseries",L"application/json",json);
if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(json);
return;
}
sbResponseBody = CkStringBuilderW_Create();
CkHttpResponseW_GetBodySb(resp,sbResponseBody);
jResp = CkJsonObjectW_Create();
CkJsonObjectW_LoadSb(jResp,sbResponseBody);
CkJsonObjectW_putEmitCompact(jResp,FALSE);
wprintf(L"Response Body:\n");
wprintf(L"%s\n",CkJsonObjectW_emit(jResp));
respStatusCode = CkHttpResponseW_getStatusCode(resp);
wprintf(L"Response Status Code = %d\n",respStatusCode);
if (respStatusCode >= 400) {
wprintf(L"Response Header:\n");
wprintf(L"%s\n",CkHttpResponseW_header(resp));
wprintf(L"Failed.\n");
CkHttpResponseW_Dispose(resp);
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(json);
CkStringBuilderW_Dispose(sbResponseBody);
CkJsonObjectW_Dispose(jResp);
return;
}
CkHttpResponseW_Dispose(resp);
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "data": {
// "attributes": {
// "series": [
// {
// "group_tags": [
// "<string>",
// "<string>"
// ],
// "query_index": "<integer>",
// "unit": [
// {
// "family": "<string>",
// "name": "<string>",
// "plural": "<string>",
// "scale_factor": "<double>",
// "short_name": "<string>"
// },
// {
// "family": "<string>",
// "name": "<string>",
// "plural": "<string>",
// "scale_factor": "<double>",
// "short_name": "<string>"
// }
// ]
// },
// {
// "group_tags": [
// "<string>",
// "<string>"
// ],
// "query_index": "<integer>",
// "unit": [
// {
// "family": "<string>",
// "name": "<string>",
// "plural": "<string>",
// "scale_factor": "<double>",
// "short_name": "<string>"
// },
// {
// "family": "<string>",
// "name": "<string>",
// "plural": "<string>",
// "scale_factor": "<double>",
// "short_name": "<string>"
// }
// ]
// }
// ],
// "times": [
// "<long>",
// "<long>"
// ],
// "values": [
// [
// "<double>",
// "<double>"
// ],
// [
// "<double>",
// "<double>"
// ]
// ]
// },
// "type": "timeseries_response"
// },
// "errors": "<string>"
// }
// Sample code for parsing the JSON response...
// Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code
// Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
v_Type = CkJsonObjectW_stringOf(jResp,L"data.type");
errors = CkJsonObjectW_stringOf(jResp,L"errors");
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jResp,L"data.attributes.series");
while (i < count_i) {
CkJsonObjectW_putI(jResp,i);
query_index = CkJsonObjectW_stringOf(jResp,L"data.attributes.series[i].query_index");
j = 0;
count_j = CkJsonObjectW_SizeOfArray(jResp,L"data.attributes.series[i].group_tags");
while (j < count_j) {
CkJsonObjectW_putJ(jResp,j);
strVal = CkJsonObjectW_stringOf(jResp,L"data.attributes.series[i].group_tags[j]");
j = j + 1;
}
j = 0;
count_j = CkJsonObjectW_SizeOfArray(jResp,L"data.attributes.series[i].unit");
while (j < count_j) {
CkJsonObjectW_putJ(jResp,j);
family = CkJsonObjectW_stringOf(jResp,L"data.attributes.series[i].unit[j].family");
name = CkJsonObjectW_stringOf(jResp,L"data.attributes.series[i].unit[j].name");
plural = CkJsonObjectW_stringOf(jResp,L"data.attributes.series[i].unit[j].plural");
scale_factor = CkJsonObjectW_stringOf(jResp,L"data.attributes.series[i].unit[j].scale_factor");
short_name = CkJsonObjectW_stringOf(jResp,L"data.attributes.series[i].unit[j].short_name");
j = j + 1;
}
i = i + 1;
}
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jResp,L"data.attributes.times");
while (i < count_i) {
CkJsonObjectW_putI(jResp,i);
strVal = CkJsonObjectW_stringOf(jResp,L"data.attributes.times[i]");
i = i + 1;
}
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jResp,L"data.attributes.values");
while (i < count_i) {
CkJsonObjectW_putI(jResp,i);
j = 0;
count_j = CkJsonObjectW_SizeOfArray(jResp,L"data.attributes.values[i]");
while (j < count_j) {
CkJsonObjectW_putJ(jResp,j);
strVal = CkJsonObjectW_stringOf(jResp,L"data.attributes.values[i][j]");
j = j + 1;
}
i = i + 1;
}
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(json);
CkStringBuilderW_Dispose(sbResponseBody);
CkJsonObjectW_Dispose(jResp);
}
Curl Command
curl -X POST
-H "Content-Type: application/json"
-H "Accept: application/json"
-d '{
"data": {
"type": "timeseries_request",
"attributes": {
"to": "<long>",
"from": "<long>",
"queries": [
{
"data_source": "metrics",
"query": "<string>",
"name": "<string>"
},
{
"data_source": "metrics",
"query": "<string>",
"name": "<string>"
}
],
"formulas": [
{
"formula": "<string>",
"limit": {
"count": "<integer>",
"order": "desc"
}
},
{
"formula": "<string>",
"limit": {
"count": "<integer>",
"order": "desc"
}
}
],
"interval": "<long>"
}
}
}'
https://api.app.ddog-gov.com/api/v2/query/timeseries
Postman Collection Item JSON
{
"name": "Query timeseries data across multiple products",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
},
{
"key": "Accept",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"data\": {\n \"type\": \"timeseries_request\",\n \"attributes\": {\n \"to\": \"<long>\",\n \"from\": \"<long>\",\n \"queries\": [\n {\n \"data_source\": \"metrics\",\n \"query\": \"<string>\",\n \"name\": \"<string>\"\n },\n {\n \"data_source\": \"metrics\",\n \"query\": \"<string>\",\n \"name\": \"<string>\"\n }\n ],\n \"formulas\": [\n {\n \"formula\": \"<string>\",\n \"limit\": {\n \"count\": \"<integer>\",\n \"order\": \"desc\"\n }\n },\n {\n \"formula\": \"<string>\",\n \"limit\": {\n \"count\": \"<integer>\",\n \"order\": \"desc\"\n }\n }\n ],\n \"interval\": \"<long>\"\n }\n }\n}",
"options": {
"raw": {
"headerFamily": "json",
"language": "json"
}
}
},
"url": {
"raw": "{{baseUrl}}/api/v2/query/timeseries",
"host": [
"{{baseUrl}}"
],
"path": [
"api",
"v2",
"query",
"timeseries"
]
},
"description": "Query timeseries data across various data sources and\nprocess the data by applying formulas and functions."
},
"response": [
{
"name": "OK",
"originalRequest": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
},
{
"key": "Accept",
"value": "application/json"
},
{
"description": "Added as a part of security scheme: apikey",
"key": "DD-API-KEY",
"value": "<API Key>"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"data\": {\n \"type\": \"timeseries_request\",\n \"attributes\": {\n \"to\": \"<long>\",\n \"from\": \"<long>\",\n \"queries\": [\n {\n \"data_source\": \"metrics\",\n \"query\": \"<string>\",\n \"name\": \"<string>\"\n },\n {\n \"data_source\": \"metrics\",\n \"query\": \"<string>\",\n \"name\": \"<string>\"\n }\n ],\n \"formulas\": [\n {\n \"formula\": \"<string>\",\n \"limit\": {\n \"count\": \"<integer>\",\n \"order\": \"desc\"\n }\n },\n {\n \"formula\": \"<string>\",\n \"limit\": {\n \"count\": \"<integer>\",\n \"order\": \"desc\"\n }\n }\n ],\n \"interval\": \"<long>\"\n }\n }\n}",
"options": {
"raw": {
"headerFamily": "json",
"language": "json"
}
}
},
"url": {
"raw": "{{baseUrl}}/api/v2/query/timeseries",
"host": [
"{{baseUrl}}"
],
"path": [
"api",
"v2",
"query",
"timeseries"
]
}
},
"status": "OK",
"code": 200,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"cookie": [
],
"body": "{\n \"data\": {\n \"attributes\": {\n \"series\": [\n {\n \"group_tags\": [\n \"<string>\",\n \"<string>\"\n ],\n \"query_index\": \"<integer>\",\n \"unit\": [\n {\n \"family\": \"<string>\",\n \"name\": \"<string>\",\n \"plural\": \"<string>\",\n \"scale_factor\": \"<double>\",\n \"short_name\": \"<string>\"\n },\n {\n \"family\": \"<string>\",\n \"name\": \"<string>\",\n \"plural\": \"<string>\",\n \"scale_factor\": \"<double>\",\n \"short_name\": \"<string>\"\n }\n ]\n },\n {\n \"group_tags\": [\n \"<string>\",\n \"<string>\"\n ],\n \"query_index\": \"<integer>\",\n \"unit\": [\n {\n \"family\": \"<string>\",\n \"name\": \"<string>\",\n \"plural\": \"<string>\",\n \"scale_factor\": \"<double>\",\n \"short_name\": \"<string>\"\n },\n {\n \"family\": \"<string>\",\n \"name\": \"<string>\",\n \"plural\": \"<string>\",\n \"scale_factor\": \"<double>\",\n \"short_name\": \"<string>\"\n }\n ]\n }\n ],\n \"times\": [\n \"<long>\",\n \"<long>\"\n ],\n \"values\": [\n [\n \"<double>\",\n \"<double>\"\n ],\n [\n \"<double>\",\n \"<double>\"\n ]\n ]\n },\n \"type\": \"timeseries_response\"\n },\n \"errors\": \"<string>\"\n}"
},
{
"name": "Bad Request",
"originalRequest": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
},
{
"key": "Accept",
"value": "application/json"
},
{
"description": "Added as a part of security scheme: apikey",
"key": "DD-API-KEY",
"value": "<API Key>"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"data\": {\n \"type\": \"timeseries_request\",\n \"attributes\": {\n \"to\": \"<long>\",\n \"from\": \"<long>\",\n \"queries\": [\n {\n \"data_source\": \"metrics\",\n \"query\": \"<string>\",\n \"name\": \"<string>\"\n },\n {\n \"data_source\": \"metrics\",\n \"query\": \"<string>\",\n \"name\": \"<string>\"\n }\n ],\n \"formulas\": [\n {\n \"formula\": \"<string>\",\n \"limit\": {\n \"count\": \"<integer>\",\n \"order\": \"desc\"\n }\n },\n {\n \"formula\": \"<string>\",\n \"limit\": {\n \"count\": \"<integer>\",\n \"order\": \"desc\"\n }\n }\n ],\n \"interval\": \"<long>\"\n }\n }\n}",
"options": {
"raw": {
"headerFamily": "json",
"language": "json"
}
}
},
"url": {
"raw": "{{baseUrl}}/api/v2/query/timeseries",
"host": [
"{{baseUrl}}"
],
"path": [
"api",
"v2",
"query",
"timeseries"
]
}
},
"status": "Bad Request",
"code": 400,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"cookie": [
],
"body": "{\n \"errors\": [\n \"<string>\",\n \"<string>\"\n ]\n}"
},
{
"name": "Unauthorized",
"originalRequest": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
},
{
"key": "Accept",
"value": "application/json"
},
{
"description": "Added as a part of security scheme: apikey",
"key": "DD-API-KEY",
"value": "<API Key>"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"data\": {\n \"type\": \"timeseries_request\",\n \"attributes\": {\n \"to\": \"<long>\",\n \"from\": \"<long>\",\n \"queries\": [\n {\n \"data_source\": \"metrics\",\n \"query\": \"<string>\",\n \"name\": \"<string>\"\n },\n {\n \"data_source\": \"metrics\",\n \"query\": \"<string>\",\n \"name\": \"<string>\"\n }\n ],\n \"formulas\": [\n {\n \"formula\": \"<string>\",\n \"limit\": {\n \"count\": \"<integer>\",\n \"order\": \"desc\"\n }\n },\n {\n \"formula\": \"<string>\",\n \"limit\": {\n \"count\": \"<integer>\",\n \"order\": \"desc\"\n }\n }\n ],\n \"interval\": \"<long>\"\n }\n }\n}",
"options": {
"raw": {
"headerFamily": "json",
"language": "json"
}
}
},
"url": {
"raw": "{{baseUrl}}/api/v2/query/timeseries",
"host": [
"{{baseUrl}}"
],
"path": [
"api",
"v2",
"query",
"timeseries"
]
}
},
"status": "Unauthorized",
"code": 401,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"cookie": [
],
"body": "{\n \"errors\": [\n \"<string>\",\n \"<string>\"\n ]\n}"
},
{
"name": "Forbidden",
"originalRequest": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
},
{
"key": "Accept",
"value": "application/json"
},
{
"description": "Added as a part of security scheme: apikey",
"key": "DD-API-KEY",
"value": "<API Key>"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"data\": {\n \"type\": \"timeseries_request\",\n \"attributes\": {\n \"to\": \"<long>\",\n \"from\": \"<long>\",\n \"queries\": [\n {\n \"data_source\": \"metrics\",\n \"query\": \"<string>\",\n \"name\": \"<string>\"\n },\n {\n \"data_source\": \"metrics\",\n \"query\": \"<string>\",\n \"name\": \"<string>\"\n }\n ],\n \"formulas\": [\n {\n \"formula\": \"<string>\",\n \"limit\": {\n \"count\": \"<integer>\",\n \"order\": \"desc\"\n }\n },\n {\n \"formula\": \"<string>\",\n \"limit\": {\n \"count\": \"<integer>\",\n \"order\": \"desc\"\n }\n }\n ],\n \"interval\": \"<long>\"\n }\n }\n}",
"options": {
"raw": {
"headerFamily": "json",
"language": "json"
}
}
},
"url": {
"raw": "{{baseUrl}}/api/v2/query/timeseries",
"host": [
"{{baseUrl}}"
],
"path": [
"api",
"v2",
"query",
"timeseries"
]
}
},
"status": "Forbidden",
"code": 403,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"cookie": [
],
"body": "{\n \"errors\": [\n \"<string>\",\n \"<string>\"\n ]\n}"
},
{
"name": "Too many requests",
"originalRequest": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
},
{
"key": "Accept",
"value": "application/json"
},
{
"description": "Added as a part of security scheme: apikey",
"key": "DD-API-KEY",
"value": "<API Key>"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"data\": {\n \"type\": \"timeseries_request\",\n \"attributes\": {\n \"to\": \"<long>\",\n \"from\": \"<long>\",\n \"queries\": [\n {\n \"data_source\": \"metrics\",\n \"query\": \"<string>\",\n \"name\": \"<string>\"\n },\n {\n \"data_source\": \"metrics\",\n \"query\": \"<string>\",\n \"name\": \"<string>\"\n }\n ],\n \"formulas\": [\n {\n \"formula\": \"<string>\",\n \"limit\": {\n \"count\": \"<integer>\",\n \"order\": \"desc\"\n }\n },\n {\n \"formula\": \"<string>\",\n \"limit\": {\n \"count\": \"<integer>\",\n \"order\": \"desc\"\n }\n }\n ],\n \"interval\": \"<long>\"\n }\n }\n}",
"options": {
"raw": {
"headerFamily": "json",
"language": "json"
}
}
},
"url": {
"raw": "{{baseUrl}}/api/v2/query/timeseries",
"host": [
"{{baseUrl}}"
],
"path": [
"api",
"v2",
"query",
"timeseries"
]
}
},
"status": "Too Many Requests",
"code": 429,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"cookie": [
],
"body": "{\n \"errors\": [\n \"<string>\",\n \"<string>\"\n ]\n}"
}
]
}