Chilkat Online Tools

VB.NET / Datadog API Collection / Query scalar data across multiple products

Back to Collection Items

' This example assumes the Chilkat API to have been previously unlocked.
' See Global Unlock Sample for sample code.

Dim http As New Chilkat.Http
Dim success As Boolean

' 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": "scalar_request",
'     "attributes": {
'       "to": "<long>",
'       "from": "<long>",
'       "queries": [
'         {
'           "data_source": "metrics",
'           "query": "<string>",
'           "aggregator": "avg",
'           "name": "<string>"
'         },
'         {
'           "data_source": "metrics",
'           "query": "<string>",
'           "aggregator": "avg",
'           "name": "<string>"
'         }
'       ],
'       "formulas": [
'         {
'           "formula": "<string>",
'           "limit": {
'             "count": "<integer>",
'             "order": "desc"
'           }
'         },
'         {
'           "formula": "<string>",
'           "limit": {
'             "count": "<integer>",
'             "order": "desc"
'           }
'         }
'       ]
'     }
'   }
' }

Dim json As New Chilkat.JsonObject
json.UpdateString("data.type","scalar_request")
json.UpdateString("data.attributes.to","<long>")
json.UpdateString("data.attributes.from","<long>")
json.UpdateString("data.attributes.queries[0].data_source","metrics")
json.UpdateString("data.attributes.queries[0].query","<string>")
json.UpdateString("data.attributes.queries[0].aggregator","avg")
json.UpdateString("data.attributes.queries[0].name","<string>")
json.UpdateString("data.attributes.queries[1].data_source","metrics")
json.UpdateString("data.attributes.queries[1].query","<string>")
json.UpdateString("data.attributes.queries[1].aggregator","avg")
json.UpdateString("data.attributes.queries[1].name","<string>")
json.UpdateString("data.attributes.formulas[0].formula","<string>")
json.UpdateString("data.attributes.formulas[0].limit.count","<integer>")
json.UpdateString("data.attributes.formulas[0].limit.order","desc")
json.UpdateString("data.attributes.formulas[1].formula","<string>")
json.UpdateString("data.attributes.formulas[1].limit.count","<integer>")
json.UpdateString("data.attributes.formulas[1].limit.order","desc")

http.SetRequestHeader("Content-Type","application/json")
http.SetRequestHeader("Accept","application/json")

Dim resp As Chilkat.HttpResponse = http.PostJson3("https://api.app.ddog-gov.com/api/v2/query/scalar","application/json",json)
If (http.LastMethodSuccess = False) Then
    Debug.WriteLine(http.LastErrorText)
    Exit Sub
End If


Dim sbResponseBody As New Chilkat.StringBuilder
resp.GetBodySb(sbResponseBody)

Dim jResp As New Chilkat.JsonObject
jResp.LoadSb(sbResponseBody)
jResp.EmitCompact = False

Debug.WriteLine("Response Body:")
Debug.WriteLine(jResp.Emit())

Dim respStatusCode As Integer = resp.StatusCode
Debug.WriteLine("Response Status Code = " & respStatusCode)
If (respStatusCode >= 400) Then
    Debug.WriteLine("Response Header:")
    Debug.WriteLine(resp.Header)
    Debug.WriteLine("Failed.")

    Exit Sub
End If



' Sample JSON response:
' (Sample code for parsing the JSON response is shown below)

' {
'   "data": {
'     "attributes": {
'       "columns": [
'         {
'           "name": "<string>",
'           "type": "<string>",
'           "values": [
'             [
'               "<string>",
'               "<string>"
'             ],
'             [
'               "<string>",
'               "<string>"
'             ]
'           ]
'         },
'         {
'           "name": "<string>",
'           "type": "<string>",
'           "values": [
'             [
'               "<string>",
'               "<string>"
'             ],
'             [
'               "<string>",
'               "<string>"
'             ]
'           ]
'         }
'       ]
'     },
'     "type": "scalar_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



Dim name As String
Dim v_type As String
Dim j As Integer
Dim count_j As Integer
Dim k As Integer
Dim count_k As Integer
Dim strVal As String

Dim v_Type As String = jResp.StringOf("data.type")
Dim errors As String = jResp.StringOf("errors")
Dim i As Integer = 0
Dim count_i As Integer = jResp.SizeOfArray("data.attributes.columns")
While i < count_i
    jResp.I = i
    name = jResp.StringOf("data.attributes.columns[i].name")
    v_type = jResp.StringOf("data.attributes.columns[i].type")
    j = 0
    count_j = jResp.SizeOfArray("data.attributes.columns[i].values")
    While j < count_j
        jResp.J = j
        k = 0
        count_k = jResp.SizeOfArray("data.attributes.columns[i].values[j]")
        While k < count_k
            jResp.K = k
            strVal = jResp.StringOf("data.attributes.columns[i].values[j][k]")
            k = k + 1
        End While
        j = j + 1
    End While
    i = i + 1
End While

Curl Command

curl -X POST
	-H "Content-Type: application/json"
	-H "Accept: application/json"
	-d '{
  "data": {
    "type": "scalar_request",
    "attributes": {
      "to": "<long>",
      "from": "<long>",
      "queries": [
        {
          "data_source": "metrics",
          "query": "<string>",
          "aggregator": "avg",
          "name": "<string>"
        },
        {
          "data_source": "metrics",
          "query": "<string>",
          "aggregator": "avg",
          "name": "<string>"
        }
      ],
      "formulas": [
        {
          "formula": "<string>",
          "limit": {
            "count": "<integer>",
            "order": "desc"
          }
        },
        {
          "formula": "<string>",
          "limit": {
            "count": "<integer>",
            "order": "desc"
          }
        }
      ]
    }
  }
}'
https://api.app.ddog-gov.com/api/v2/query/scalar

Postman Collection Item JSON

{
  "name": "Query scalar 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\": \"scalar_request\",\n    \"attributes\": {\n      \"to\": \"<long>\",\n      \"from\": \"<long>\",\n      \"queries\": [\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"aggregator\": \"avg\",\n          \"name\": \"<string>\"\n        },\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"aggregator\": \"avg\",\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    }\n  }\n}",
      "options": {
        "raw": {
          "headerFamily": "json",
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{baseUrl}}/api/v2/query/scalar",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "api",
        "v2",
        "query",
        "scalar"
      ]
    },
    "description": "Query scalar values (as seen on Query Value, Table, and Toplist widgets).\nMultiple data sources are supported with the ability to\nprocess the data using 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\": \"scalar_request\",\n    \"attributes\": {\n      \"to\": \"<long>\",\n      \"from\": \"<long>\",\n      \"queries\": [\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"aggregator\": \"avg\",\n          \"name\": \"<string>\"\n        },\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"aggregator\": \"avg\",\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    }\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/query/scalar",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "query",
            "scalar"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"data\": {\n    \"attributes\": {\n      \"columns\": [\n        {\n          \"name\": \"<string>\",\n          \"type\": \"<string>\",\n          \"values\": [\n            [\n              \"<string>\",\n              \"<string>\"\n            ],\n            [\n              \"<string>\",\n              \"<string>\"\n            ]\n          ]\n        },\n        {\n          \"name\": \"<string>\",\n          \"type\": \"<string>\",\n          \"values\": [\n            [\n              \"<string>\",\n              \"<string>\"\n            ],\n            [\n              \"<string>\",\n              \"<string>\"\n            ]\n          ]\n        }\n      ]\n    },\n    \"type\": \"scalar_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\": \"scalar_request\",\n    \"attributes\": {\n      \"to\": \"<long>\",\n      \"from\": \"<long>\",\n      \"queries\": [\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"aggregator\": \"avg\",\n          \"name\": \"<string>\"\n        },\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"aggregator\": \"avg\",\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    }\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/query/scalar",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "query",
            "scalar"
          ]
        }
      },
      "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\": \"scalar_request\",\n    \"attributes\": {\n      \"to\": \"<long>\",\n      \"from\": \"<long>\",\n      \"queries\": [\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"aggregator\": \"avg\",\n          \"name\": \"<string>\"\n        },\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"aggregator\": \"avg\",\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    }\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/query/scalar",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "query",
            "scalar"
          ]
        }
      },
      "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\": \"scalar_request\",\n    \"attributes\": {\n      \"to\": \"<long>\",\n      \"from\": \"<long>\",\n      \"queries\": [\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"aggregator\": \"avg\",\n          \"name\": \"<string>\"\n        },\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"aggregator\": \"avg\",\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    }\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/query/scalar",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "query",
            "scalar"
          ]
        }
      },
      "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\": \"scalar_request\",\n    \"attributes\": {\n      \"to\": \"<long>\",\n      \"from\": \"<long>\",\n      \"queries\": [\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"aggregator\": \"avg\",\n          \"name\": \"<string>\"\n        },\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"aggregator\": \"avg\",\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    }\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/query/scalar",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "query",
            "scalar"
          ]
        }
      },
      "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}"
    }
  ]
}