Chilkat Online Tools

VB6 / Datadog API Collection / Create a span-based metric

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 ChilkatHttp
Dim success As Long

' 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": {
'     "id": "<string>",
'     "type": "spans_metrics",
'     "attributes": {
'       "compute": {
'         "aggregation_type": "count",
'         "include_percentiles": "<boolean>",
'         "path": "<string>"
'       },
'       "filter": {
'         "query": "*"
'       },
'       "group_by": [
'         {
'           "path": "<string>",
'           "tag_name": "<string>"
'         },
'         {
'           "path": "<string>",
'           "tag_name": "<string>"
'         }
'       ]
'     }
'   }
' }

Dim json As New ChilkatJsonObject
success = json.UpdateString("data.id","<string>")
success = json.UpdateString("data.type","spans_metrics")
success = json.UpdateString("data.attributes.compute.aggregation_type","count")
success = json.UpdateString("data.attributes.compute.include_percentiles","<boolean>")
success = json.UpdateString("data.attributes.compute.path","<string>")
success = json.UpdateString("data.attributes.filter.query","*")
success = json.UpdateString("data.attributes.group_by[0].path","<string>")
success = json.UpdateString("data.attributes.group_by[0].tag_name","<string>")
success = json.UpdateString("data.attributes.group_by[1].path","<string>")
success = json.UpdateString("data.attributes.group_by[1].tag_name","<string>")

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

Dim resp As ChilkatHttpResponse
Set resp = http.PostJson3("https://api.app.ddog-gov.com/api/v2/apm/config/metrics","application/json",json)
If (http.LastMethodSuccess = 0) Then
    Debug.Print http.LastErrorText
    Exit Sub
End If

Dim sbResponseBody As New ChilkatStringBuilder
success = resp.GetBodySb(sbResponseBody)

Dim jResp As New ChilkatJsonObject
success = jResp.LoadSb(sbResponseBody)
jResp.EmitCompact = 0

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

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

    Exit Sub
End If

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

' {
'   "data": {
'     "attributes": {
'       "compute": {
'         "aggregation_type": "distribution",
'         "include_percentiles": "<boolean>",
'         "path": "<string>"
'       },
'       "filter": {
'         "query": "<string>"
'       },
'       "group_by": [
'         {
'           "path": "<string>",
'           "tag_name": "<string>"
'         },
'         {
'           "path": "<string>",
'           "tag_name": "<string>"
'         }
'       ]
'     },
'     "id": "<string>",
'     "type": "spans_metrics"
'   }
' }

' Sample code for parsing the JSON response...
' Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

Dim path As String
Dim tag_name As String

Dim Aggregation_type As String
Aggregation_type = jResp.StringOf("data.attributes.compute.aggregation_type")
Dim Include_percentiles As String
Include_percentiles = jResp.StringOf("data.attributes.compute.include_percentiles")
Dim Path As String
Path = jResp.StringOf("data.attributes.compute.path")
Dim Query As String
Query = jResp.StringOf("data.attributes.filter.query")
Dim Id As String
Id = jResp.StringOf("data.id")
Dim v_Type As String
v_Type = jResp.StringOf("data.type")
Dim i As Long
i = 0
Dim count_i As Long
count_i = jResp.SizeOfArray("data.attributes.group_by")
Do While i < count_i
    jResp.I = i
    path = jResp.StringOf("data.attributes.group_by[i].path")
    tag_name = jResp.StringOf("data.attributes.group_by[i].tag_name")
    i = i + 1
Loop

Curl Command

curl -X POST
	-H "Content-Type: application/json"
	-H "Accept: application/json"
	-d '{
  "data": {
    "id": "<string>",
    "type": "spans_metrics",
    "attributes": {
      "compute": {
        "aggregation_type": "count",
        "include_percentiles": "<boolean>",
        "path": "<string>"
      },
      "filter": {
        "query": "*"
      },
      "group_by": [
        {
          "path": "<string>",
          "tag_name": "<string>"
        },
        {
          "path": "<string>",
          "tag_name": "<string>"
        }
      ]
    }
  }
}'
https://api.app.ddog-gov.com/api/v2/apm/config/metrics

Postman Collection Item JSON

{
  "name": "Create a span-based metric",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      },
      {
        "key": "Accept",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"data\": {\n    \"id\": \"<string>\",\n    \"type\": \"spans_metrics\",\n    \"attributes\": {\n      \"compute\": {\n        \"aggregation_type\": \"count\",\n        \"include_percentiles\": \"<boolean>\",\n        \"path\": \"<string>\"\n      },\n      \"filter\": {\n        \"query\": \"*\"\n      },\n      \"group_by\": [\n        {\n          \"path\": \"<string>\",\n          \"tag_name\": \"<string>\"\n        },\n        {\n          \"path\": \"<string>\",\n          \"tag_name\": \"<string>\"\n        }\n      ]\n    }\n  }\n}",
      "options": {
        "raw": {
          "headerFamily": "json",
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{baseUrl}}/api/v2/apm/config/metrics",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "api",
        "v2",
        "apm",
        "config",
        "metrics"
      ]
    },
    "description": "Create a metric based on your ingested spans in your organization.\nReturns the span-based metric object from the request body when the request is successful."
  },
  "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    \"id\": \"<string>\",\n    \"type\": \"spans_metrics\",\n    \"attributes\": {\n      \"compute\": {\n        \"aggregation_type\": \"count\",\n        \"include_percentiles\": \"<boolean>\",\n        \"path\": \"<string>\"\n      },\n      \"filter\": {\n        \"query\": \"*\"\n      },\n      \"group_by\": [\n        {\n          \"path\": \"<string>\",\n          \"tag_name\": \"<string>\"\n        },\n        {\n          \"path\": \"<string>\",\n          \"tag_name\": \"<string>\"\n        }\n      ]\n    }\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/apm/config/metrics",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "apm",
            "config",
            "metrics"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"data\": {\n    \"attributes\": {\n      \"compute\": {\n        \"aggregation_type\": \"distribution\",\n        \"include_percentiles\": \"<boolean>\",\n        \"path\": \"<string>\"\n      },\n      \"filter\": {\n        \"query\": \"<string>\"\n      },\n      \"group_by\": [\n        {\n          \"path\": \"<string>\",\n          \"tag_name\": \"<string>\"\n        },\n        {\n          \"path\": \"<string>\",\n          \"tag_name\": \"<string>\"\n        }\n      ]\n    },\n    \"id\": \"<string>\",\n    \"type\": \"spans_metrics\"\n  }\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    \"id\": \"<string>\",\n    \"type\": \"spans_metrics\",\n    \"attributes\": {\n      \"compute\": {\n        \"aggregation_type\": \"count\",\n        \"include_percentiles\": \"<boolean>\",\n        \"path\": \"<string>\"\n      },\n      \"filter\": {\n        \"query\": \"*\"\n      },\n      \"group_by\": [\n        {\n          \"path\": \"<string>\",\n          \"tag_name\": \"<string>\"\n        },\n        {\n          \"path\": \"<string>\",\n          \"tag_name\": \"<string>\"\n        }\n      ]\n    }\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/apm/config/metrics",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "apm",
            "config",
            "metrics"
          ]
        }
      },
      "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": "Not Authorized",
      "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    \"id\": \"<string>\",\n    \"type\": \"spans_metrics\",\n    \"attributes\": {\n      \"compute\": {\n        \"aggregation_type\": \"count\",\n        \"include_percentiles\": \"<boolean>\",\n        \"path\": \"<string>\"\n      },\n      \"filter\": {\n        \"query\": \"*\"\n      },\n      \"group_by\": [\n        {\n          \"path\": \"<string>\",\n          \"tag_name\": \"<string>\"\n        },\n        {\n          \"path\": \"<string>\",\n          \"tag_name\": \"<string>\"\n        }\n      ]\n    }\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/apm/config/metrics",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "apm",
            "config",
            "metrics"
          ]
        }
      },
      "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": "Conflict",
      "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    \"id\": \"<string>\",\n    \"type\": \"spans_metrics\",\n    \"attributes\": {\n      \"compute\": {\n        \"aggregation_type\": \"count\",\n        \"include_percentiles\": \"<boolean>\",\n        \"path\": \"<string>\"\n      },\n      \"filter\": {\n        \"query\": \"*\"\n      },\n      \"group_by\": [\n        {\n          \"path\": \"<string>\",\n          \"tag_name\": \"<string>\"\n        },\n        {\n          \"path\": \"<string>\",\n          \"tag_name\": \"<string>\"\n        }\n      ]\n    }\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/apm/config/metrics",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "apm",
            "config",
            "metrics"
          ]
        }
      },
      "status": "Conflict",
      "code": 409,
      "_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    \"id\": \"<string>\",\n    \"type\": \"spans_metrics\",\n    \"attributes\": {\n      \"compute\": {\n        \"aggregation_type\": \"count\",\n        \"include_percentiles\": \"<boolean>\",\n        \"path\": \"<string>\"\n      },\n      \"filter\": {\n        \"query\": \"*\"\n      },\n      \"group_by\": [\n        {\n          \"path\": \"<string>\",\n          \"tag_name\": \"<string>\"\n        },\n        {\n          \"path\": \"<string>\",\n          \"tag_name\": \"<string>\"\n        }\n      ]\n    }\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/apm/config/metrics",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "apm",
            "config",
            "metrics"
          ]
        }
      },
      "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}"
    }
  ]
}