Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi I want to push data in to datadog API, I have datadog API key but don't know how to send data from my window service in c#

What I have tried:

As I am very new to datadog, I just created user and have Datadog api key
Posted
Updated 16-Jun-20 0:19am
Comments
Richard MacCutchan 16-Jun-20 3:32am    
Ask Datadog, whoever they may be.

1 solution

You could try their API - well documented Events[^]

Assuming your event data is in JSON format and looks like
{
  "aggregation_key": "string",
  "alert_type": "info",
  "date_happened": "integer",
  "device_name": [],
  "host": "string",
  "priority": "normal",
  "related_event_id": "integer",
  "source_type_name": "string",
  "tags": [
    "environment:test"
  ],
  "text": "Oh boy!",
  "title": "Did you hear the news today?"
}


You could use RestSharp RestSharp[^] , or simpler code like
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.datadoghq.com/api/v1/events");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.Headers["DD-API-KEY"] = "the-value";          // or httpWebRequest.Headers.Add("DD-API-KEY", "the-api-key-value")
httpWebRequest.Headers["DD-APPLICATION-KEY] = "the-value";   // or httpWebRequest.Headers.Add("DD-APPLICATION-KEY", "the-application-key-value")
// HTTPS 
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };    

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    string json = "{...your event data here...}";

    streamWriter.Write(json);
}

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    var result = streamReader.ReadToEnd();
}
 
Share this answer
 
Comments
Mohammad Nawaz 16-Jun-20 7:15am    
Hi @Garth,
Thanks for your response, but it shows to post data on events. But my requirement is that I have to call a third party API and it returns some data, so how can I show that data in metrics or graphical presentation from my windows service.
Garth J Lancaster 16-Jun-20 8:22am    
I'm pretty sure I answered you question "Hi I want to push data in to datadog API, I have datadog API key but don't know how to send data from my window service in c#" ... if you want now to do a GET to receive data, you need to change some of the code

httpWebRequest.Method = "POST";
and

delete
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    string json = "{...your event data here...}";

    streamWriter.Write(json);
}


Then you'll need to read trhe DataDog API link, and see how to format the request - ie this "https://api.datadoghq.com/api/v1/events" will be different

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900