Last active
August 29, 2015 14:10
-
-
Save jmvermeulen/a669364e1971c958836c to your computer and use it in GitHub Desktop.
New Relic - Plugin metrics API C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example | |
// How to call the New Relic Plugin metrics API from C# (https://platform-api.newrelic.com/platform/v1/metrics) | |
// Fill in your license key (request for free) and run the app. | |
// Data will show under the Plugins tab. | |
async void Main() | |
{ | |
var request = new RootObject(); | |
request.agent = new Agent{ host=Environment.MachineName, version = "1.0.0"}; | |
request.components = new List<Component>(); | |
var metricsData = new Dictionary<string,object> {}; | |
metricsData.Add("Component/Database[Queries/Second]",9); | |
metricsData.Add("Component/AnalyticsDatabase[Queries/Second]",new ComponentAnalytics{ min = 0, max = 100, total = 2000, count = 4}) ; | |
request.components.Add(new Component | |
{ | |
name = "Example Monitor", | |
duration = 60, | |
guid = "com.example.plugintest", | |
metrics = metricsData | |
}); | |
var data = JsonConvert.SerializeObject(request)); | |
using(var httpClient = new HttpClient()) | |
{ | |
httpClient.DefaultRequestHeaders.Add("X-License-Key", "YOURKEY"); | |
var content = new StringContent (data); | |
// Clear the content header 'Content-Type charset=utf-8', otherwise this fails as a bad request | |
content.Headers.Clear(); | |
var response = await httpClient.PostAsync("https://platform-api.newrelic.com/platform/v1/metrics", content ); | |
response.EnsureSuccessStatusCode(); | |
var text = await response.Content.ReadAsStringAsync(); | |
} | |
} | |
public class Agent | |
{ | |
public string host { get; set; } | |
public string pid { get; set; } | |
public string version { get; set; } | |
} | |
public class ComponentAnalytics | |
{ | |
public int min { get; set; } | |
public int max { get; set; } | |
public int total { get; set; } | |
public int count { get; set; } | |
public int sum_of_squares { get; set; } | |
} | |
public class Component | |
{ | |
public string name { get; set; } | |
public string guid { get; set; } | |
public int duration { get; set; } | |
public Dictionary<string,object> metrics { get; set; } | |
} | |
public class RootObject | |
{ | |
public Agent agent { get; set; } | |
public List<Component> components { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Make sure to clear the content header 'Content-Type charset=utf-8', otherwise this fails as a bad request 400.