Chilkat Online Tools

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

Back to Collection Items

LOCAL loHttp
LOCAL lnSuccess
LOCAL loJson
LOCAL loResp
LOCAL loSbResponseBody
LOCAL loJResp
LOCAL lnRespStatusCode
LOCAL lcPath
LOCAL lcTag_name
LOCAL lcAggregation_type
LOCAL lcInclude_percentiles
LOCAL lcPath
LOCAL lcQuery
LOCAL lcId
LOCAL lcV_Type
LOCAL i
LOCAL lnCount_i

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

loHttp = CreateObject('Chilkat_9_5_0.Http')

* 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>"
*         }
*       ]
*     }
*   }
* }

loJson = CreateObject('Chilkat_9_5_0.JsonObject')
loJson.UpdateString("data.id","<string>")
loJson.UpdateString("data.type","spans_metrics")
loJson.UpdateString("data.attributes.compute.aggregation_type","count")
loJson.UpdateString("data.attributes.compute.include_percentiles","<boolean>")
loJson.UpdateString("data.attributes.compute.path","<string>")
loJson.UpdateString("data.attributes.filter.query","*")
loJson.UpdateString("data.attributes.group_by[0].path","<string>")
loJson.UpdateString("data.attributes.group_by[0].tag_name","<string>")
loJson.UpdateString("data.attributes.group_by[1].path","<string>")
loJson.UpdateString("data.attributes.group_by[1].tag_name","<string>")

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

loResp = loHttp.PostJson3("https://api.app.ddog-gov.com/api/v2/apm/config/metrics","application/json",loJson)
IF (loHttp.LastMethodSuccess = 0) THEN
    ? loHttp.LastErrorText
    RELEASE loHttp
    RELEASE loJson
    CANCEL
ENDIF

loSbResponseBody = CreateObject('Chilkat_9_5_0.StringBuilder')
loResp.GetBodySb(loSbResponseBody)

loJResp = CreateObject('Chilkat_9_5_0.JsonObject')
loJResp.LoadSb(loSbResponseBody)
loJResp.EmitCompact = 0

? "Response Body:"
? loJResp.Emit()

lnRespStatusCode = loResp.StatusCode
? "Response Status Code = " + STR(lnRespStatusCode)
IF (lnRespStatusCode >= 400) THEN
    ? "Response Header:"
    ? loResp.Header
    ? "Failed."
    RELEASE loResp
    RELEASE loHttp
    RELEASE loJson
    RELEASE loSbResponseBody
    RELEASE loJResp
    CANCEL
ENDIF

RELEASE loResp

* 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

lcAggregation_type = loJResp.StringOf("data.attributes.compute.aggregation_type")
lcInclude_percentiles = loJResp.StringOf("data.attributes.compute.include_percentiles")
lcPath = loJResp.StringOf("data.attributes.compute.path")
lcQuery = loJResp.StringOf("data.attributes.filter.query")
lcId = loJResp.StringOf("data.id")
lcV_Type = loJResp.StringOf("data.type")
i = 0
lnCount_i = loJResp.SizeOfArray("data.attributes.group_by")
DO WHILE i < lnCount_i
    loJResp.I = i
    lcPath = loJResp.StringOf("data.attributes.group_by[i].path")
    lcTag_name = loJResp.StringOf("data.attributes.group_by[i].tag_name")
    i = i + 1
ENDDO

RELEASE loHttp
RELEASE loJson
RELEASE loSbResponseBody
RELEASE loJResp

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}"
    }
  ]
}