Chilkat Online Tools

SQL Server / Datadog API Collection / Submit a Service Check

Back to Collection Items

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    -- Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    -- The following JSON is sent in the request body.

    -- [
    --   {
    --     "check": "app.ok",
    --     "status": 0,
    --     "tags": [
    --       "environment:test"
    --     ],
    --     "host_name": "app.host1",
    --     "message": "app is running",
    --     "timestamp": 12636800
    --   },
    --   {
    --     "check": "app.ok",
    --     "status": 0,
    --     "tags": [
    --       "environment:test"
    --     ],
    --     "host_name": "app.host1",
    --     "message": "app is running",
    --     "timestamp": -99255440
    --   }
    -- ]

    DECLARE @jarr int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonArray', @jarr OUT

    EXEC sp_OAMethod @jarr, 'AddObjectAt', @success OUT, -1
    DECLARE @jsonObj_1 int
    EXEC sp_OAGetProperty @jarr, 'Size', @iTmp0 OUT
    EXEC sp_OAMethod @jarr, 'ObjectAt', @jsonObj_1 OUT, @iTmp0 - 1
    EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'check', 'app.ok'
    EXEC sp_OAMethod @jsonObj_1, 'UpdateInt', @success OUT, 'status', 0
    EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'tags[0]', 'environment:test'
    EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'host_name', 'app.host1'
    EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'message', 'app is running'
    EXEC sp_OAMethod @jsonObj_1, 'UpdateInt', @success OUT, 'timestamp', 12636800
    EXEC @hr = sp_OADestroy @jsonObj_1

    EXEC sp_OAMethod @jarr, 'AddObjectAt', @success OUT, -1
    EXEC sp_OAGetProperty @jarr, 'Size', @iTmp0 OUT
    EXEC sp_OAMethod @jarr, 'ObjectAt', @jsonObj_1 OUT, @iTmp0 - 1
    EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'check', 'app.ok'
    EXEC sp_OAMethod @jsonObj_1, 'UpdateInt', @success OUT, 'status', 0
    EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'tags[0]', 'environment:test'
    EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'host_name', 'app.host1'
    EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'message', 'app is running'
    EXEC sp_OAMethod @jsonObj_1, 'UpdateInt', @success OUT, 'timestamp', -99255440
    EXEC @hr = sp_OADestroy @jsonObj_1

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Accept', 'text/json'

    DECLARE @sbRequestBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbRequestBody OUT

    EXEC sp_OAMethod @jarr, 'EmitSb', @success OUT, @sbRequestBody

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PTextSb', @resp OUT, 'POST', 'https://api.app.ddog-gov.com/api/v1/check_run', @sbRequestBody, 'utf-8', 'application/json', 0, 0
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @jarr
        EXEC @hr = sp_OADestroy @sbRequestBody
        RETURN
      END

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody

    DECLARE @jResp int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @resp

        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @jarr
        EXEC @hr = sp_OADestroy @sbRequestBody
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END
    EXEC @hr = sp_OADestroy @resp

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

    -- {
    --   "status": "ok"
    -- }

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

    DECLARE @status nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @status OUT, 'status'

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @jarr
    EXEC @hr = sp_OADestroy @sbRequestBody
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO

Curl Command

curl -X POST
	-H "Content-Type: application/json"
	-H "Accept: text/json"
	-d '[
  {
    "check": "app.ok",
    "status": 0,
    "tags": [
      "environment:test"
    ],
    "host_name": "app.host1",
    "message": "app is running",
    "timestamp": 12636800
  },
  {
    "check": "app.ok",
    "status": 0,
    "tags": [
      "environment:test"
    ],
    "host_name": "app.host1",
    "message": "app is running",
    "timestamp": -99255440
  }
]'
https://api.app.ddog-gov.com/api/v1/check_run

Postman Collection Item JSON

{
  "name": "Submit a Service Check",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      },
      {
        "key": "Accept",
        "value": "text/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "[\n  {\n    \"check\": \"app.ok\",\n    \"status\": 0,\n    \"tags\": [\n      \"environment:test\"\n    ],\n    \"host_name\": \"app.host1\",\n    \"message\": \"app is running\",\n    \"timestamp\": 12636800\n  },\n  {\n    \"check\": \"app.ok\",\n    \"status\": 0,\n    \"tags\": [\n      \"environment:test\"\n    ],\n    \"host_name\": \"app.host1\",\n    \"message\": \"app is running\",\n    \"timestamp\": -99255440\n  }\n]",
      "options": {
        "raw": {
          "headerFamily": "json",
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{baseUrl}}/api/v1/check_run",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "api",
        "v1",
        "check_run"
      ]
    },
    "description": "Submit a list of Service Checks.\n\n**Notes**:\n- A valid API key is required.\n- Service checks can be submitted up to 10 minutes in the past."
  },
  "response": [
    {
      "name": "Payload accepted",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Accept",
            "value": "text/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "[\n  {\n    \"check\": \"app.ok\",\n    \"status\": 0,\n    \"tags\": [\n      \"environment:test\"\n    ],\n    \"host_name\": \"app.host1\",\n    \"message\": \"app is running\",\n    \"timestamp\": 12636800\n  },\n  {\n    \"check\": \"app.ok\",\n    \"status\": 0,\n    \"tags\": [\n      \"environment:test\"\n    ],\n    \"host_name\": \"app.host1\",\n    \"message\": \"app is running\",\n    \"timestamp\": -99255440\n  }\n]",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v1/check_run",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "check_run"
          ]
        }
      },
      "status": "Accepted",
      "code": 202,
      "_postman_previewlanguage": "text",
      "header": [
        {
          "key": "Content-Type",
          "value": "text/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"status\": \"ok\"\n}"
    },
    {
      "name": "Bad Request",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Accept",
            "value": "text/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "[\n  {\n    \"check\": \"app.ok\",\n    \"status\": 0,\n    \"tags\": [\n      \"environment:test\"\n    ],\n    \"host_name\": \"app.host1\",\n    \"message\": \"app is running\",\n    \"timestamp\": 12636800\n  },\n  {\n    \"check\": \"app.ok\",\n    \"status\": 0,\n    \"tags\": [\n      \"environment:test\"\n    ],\n    \"host_name\": \"app.host1\",\n    \"message\": \"app is running\",\n    \"timestamp\": -99255440\n  }\n]",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v1/check_run",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "check_run"
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "text",
      "header": [
        {
          "key": "Content-Type",
          "value": "text/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"Bad Request\",\n    \"Bad Request\"\n  ]\n}"
    },
    {
      "name": "Authentication Error",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Accept",
            "value": "text/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "[\n  {\n    \"check\": \"app.ok\",\n    \"status\": 0,\n    \"tags\": [\n      \"environment:test\"\n    ],\n    \"host_name\": \"app.host1\",\n    \"message\": \"app is running\",\n    \"timestamp\": 12636800\n  },\n  {\n    \"check\": \"app.ok\",\n    \"status\": 0,\n    \"tags\": [\n      \"environment:test\"\n    ],\n    \"host_name\": \"app.host1\",\n    \"message\": \"app is running\",\n    \"timestamp\": -99255440\n  }\n]",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v1/check_run",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "check_run"
          ]
        }
      },
      "status": "Forbidden",
      "code": 403,
      "_postman_previewlanguage": "text",
      "header": [
        {
          "key": "Content-Type",
          "value": "text/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"Bad Request\",\n    \"Bad Request\"\n  ]\n}"
    },
    {
      "name": "Request timeout",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Accept",
            "value": "text/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "[\n  {\n    \"check\": \"app.ok\",\n    \"status\": 0,\n    \"tags\": [\n      \"environment:test\"\n    ],\n    \"host_name\": \"app.host1\",\n    \"message\": \"app is running\",\n    \"timestamp\": 12636800\n  },\n  {\n    \"check\": \"app.ok\",\n    \"status\": 0,\n    \"tags\": [\n      \"environment:test\"\n    ],\n    \"host_name\": \"app.host1\",\n    \"message\": \"app is running\",\n    \"timestamp\": -99255440\n  }\n]",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v1/check_run",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "check_run"
          ]
        }
      },
      "status": "Request Timeout",
      "code": 408,
      "_postman_previewlanguage": "text",
      "header": [
        {
          "key": "Content-Type",
          "value": "text/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"Bad Request\",\n    \"Bad Request\"\n  ]\n}"
    },
    {
      "name": "Payload too large",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Accept",
            "value": "text/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "[\n  {\n    \"check\": \"app.ok\",\n    \"status\": 0,\n    \"tags\": [\n      \"environment:test\"\n    ],\n    \"host_name\": \"app.host1\",\n    \"message\": \"app is running\",\n    \"timestamp\": 12636800\n  },\n  {\n    \"check\": \"app.ok\",\n    \"status\": 0,\n    \"tags\": [\n      \"environment:test\"\n    ],\n    \"host_name\": \"app.host1\",\n    \"message\": \"app is running\",\n    \"timestamp\": -99255440\n  }\n]",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v1/check_run",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "check_run"
          ]
        }
      },
      "status": "Request Entity Too Large",
      "code": 413,
      "_postman_previewlanguage": "text",
      "header": [
        {
          "key": "Content-Type",
          "value": "text/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"Bad Request\",\n    \"Bad Request\"\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  {\n    \"check\": \"app.ok\",\n    \"status\": 0,\n    \"tags\": [\n      \"environment:test\"\n    ],\n    \"host_name\": \"app.host1\",\n    \"message\": \"app is running\",\n    \"timestamp\": 12636800\n  },\n  {\n    \"check\": \"app.ok\",\n    \"status\": 0,\n    \"tags\": [\n      \"environment:test\"\n    ],\n    \"host_name\": \"app.host1\",\n    \"message\": \"app is running\",\n    \"timestamp\": -99255440\n  }\n]",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v1/check_run",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "check_run"
          ]
        }
      },
      "status": "Too Many Requests",
      "code": 429,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"Bad Request\",\n    \"Bad Request\"\n  ]\n}"
    }
  ]
}