New Relic Insights .NET Client

Fork me on GitHubNew Relic.NET

A lightweight C# client for New Relic Insights

  • Upload New Relic Insights events on-demand
  • Upload in batches, as a scheduled task
  • Minimal CPU usage (1 thread)
  • Proxy-aware

Upload a Single Event

Create the Event

Create a class that implements NewRelicInsightsEvent:

public class CustomNewRelicInsightsEvent : NewRelicInsightsEvent
{
	[JilDirective(Name = "name")]
	public string Name { get; set; }

	[JilDirective(Name = "count")]
	public int Count { get; set; }

	[JilDirective(Name = "unixTimeStamp")]
	public int UnixTimeStamp => 
           (int) DateTime.UtcNow
                 .Subtract(new DateTime(1970, 1, 1))
                 .TotalSeconds;

	///
	/// EventType is the New Relic Insights Event Grouping. It determines the
	/// database to which the event will persist.
	///

	///
	/// /// must be serialised
	/// in Camel case, in order to be correctly interpreted by New Relic
	/// Insights.
	/// /// /// Apply the following attribute to the
	///
	/// property in your implementation:
	/// /// /// [JilDirective(Name = "eventType")]
	/// ///
	[JilDirective(Name = "eventType")]
	public string EventType { get; set; }
}

Note: NewRelic.NET leverages Jil for serialisation. Though JilDirective attributes are not required for custom properties, the EventType property requires the specified JilDirective.

Initialise New Relic Metadata

NewRelicInsightsClient.Instance.NewRelicInsightsMetadata.AccountID = 
	"{New Relic Account ID}";
NewRelicInsightsClient.Instance.NewRelicInsightsMetadata.APIKey = 
	"{New Relic API key}";
NewRelicInsightsClient.Instance.NewRelicInsightsMetadata.URI =
	new Uri("https://insights-collector.newrelic.com/v1/accounts");

Upload the Event

New Relic Event Upload

    var customNewRelicInsightsEvent = new CustomNewRelicInsightsEvent
    {
        Name = "TEST",
        Count = id,
        EventType = "Test"
    };

Synchronously

    NewRelicInsightsClient.Instance.UploadEvents(
	new NewRelicInsightsEvent[]
	{ 
		customNewRelicInsightsEvent 
	}, 
	new HttpClientFactory());

Asynchronously

    NewRelicInsightsClient.Instance.UploadEventsAsync(
	new NewRelicInsightsEvent[]
	{ 
		customNewRelicInsightsEvent 
	}, 
	new HttpClientFactory());

Upload Events in Batches

New Relic Batch Upload

Start the Event Cache

if (!NewRelicInsightsClient.Instance.HasStarted)
{
	NewRelicInsightsClient.Instance.Initialise();
}

Add Events to the Cache

NewRelicInsightsClient.Instance
    .AddNewRelicInsightEvent(customNewRelicInsightsEvent);

Event Upload Frequency

Batch-upload occurs every minute, by default, unless a custom frequency is specified. The following example sets the upload-frequency to 10 minutes:

NewRelicInsightsClient.Instance.RecurringTaskInterval = 10;

Proxy Server

The following example indicates that a proxy should be leveraged when invoking HTTP requests to New Relic:

NewRelicInsightsClient.Instance.NewRelicInsightsMetadata
    .UseWebProxy = true;
NewRelicInsightsClient.Instance.NewRelicInsightsMetadata
    .WebProxy = new WebProxy(“127.0.0.1:8080”);

Custom HTTP Timeout

Outbound HTTP requests to NewRelic are restricted to a specific timeout (5 seconds) as follows:

NewRelicInsightsClient.Instance.NewRelicInsightsMetadata
    .UseNonDefaultTimeout = true;
NewRelicInsightsClient.Instance.NewRelicInsightsMetadata
    .NonDefaultTimeout = new TimeSpan(0,0,5);

Otherwise, the default C# HttpClient-timeout applies.

Restricting Batch Upload Size

The default batch-upload size is 1,000 events. That is, a batch consisting of no more than 1,000 events will be uploaded upon each cache upload-cycle. The following example indicates that the entire cache should be emptied upon every upload-cycle:

NewRelicInsightsClient.Instance.NewRelicInsightsMetadata
    .CacheUploadLimit = int.MaxValue;

Connect with me:

RSSGitHubTwitter
LinkedInYouTubeGoogle+

Leave a comment