public void TestSendMetricsToServiceFailsWithBadLicense() { var context = new Context("GarbageLicense") { Version = "1.0.0" }; context.ReportMetric("com.newrelic.sdkfunctest", "FunctionalTest", "TestMetric", "unit", 2); try { context.SendMetricsToService(); Assert.Fail("Exception should be thrown when no version is set"); } catch (NewRelicServiceException nrse) { Assert.AreEqual(HttpStatusCode.Forbidden, nrse.StatusCode, "Service should respond with 403 when invalid license key is sent"); } }
public void TestSendMetricsToServiceFailsWithNoVersion() { var context = new Context(); context.ReportMetric("com.newrelic.sdkfunctest", "FunctionalTest", "TestMetric", "unit", 2); try { context.SendMetricsToService(); Assert.Fail("Exception should be thrown when no version is set"); } catch (NewRelicServiceException nrse) { Assert.AreEqual(HttpStatusCode.BadRequest, nrse.StatusCode, "Service should respond with 400 when no version is set"); } }
public void TestSendMetricsToServiceSucceeds() { var context = new Context() { Version = "1.0.0" }; context.ReportMetric("com.newrelic.sdkfunctest", "FunctionalTest1", "TestMetric", "unit", 2); context.ReportMetric("com.newrelic.sdkfunctest", "FunctionalTest1", "TestMetric", "unit", 3); context.ReportMetric("com.newrelic.sdkfunctest", "FunctionalTest1", "TestMetric", "unit", 4); context.ReportMetric("com.newrelic.sdkfunctest", "FunctionalTest2", "TestMetric", "unit", 5); context.ReportMetric("com.newrelic.sdkfunctest", "FunctionalTest3", "TestMetric", "unit", 6); context.ReportMetric("com.newrelic.sdkfunctest", "FunctionalTest3", "TestMetric", "unit", 7); var requestData = context.RequestData; Assert.IsTrue(requestData.HasComponents(), "Request data should have components before send"); Assert.AreEqual(3, ((List<object>)requestData.Serialize()["components"]).Count, "There should be three components present"); // Will throw an exception for any client errors (400-499) context.SendMetricsToService(); requestData = context.RequestData; Assert.IsFalse(requestData.HasComponents(), "Request data should be cleared after a successful send"); }
/// <summary> /// This method only returns during a fatal error. It will initialize agents if necessary, and then begin polling once /// per configurable PollInterval invoking registered Agent's PollCycle() methods. Then sending the data to the New Relic service. /// </summary> public void SetupAndRun() { if (_factories.Count == 0 && _agents.Count == 0) { throw new InvalidOperationException("You must first call 'Add()' at least once with a valid factory or agent"); } // Initialize agents if they added an AgentFactory, otherwise they have explicitly added initialized agents already if (_factories.Count > 0) { InitializeFactoryAgents(); } // Initialize agents with the same Context so they aggregate to a single a request var context = new Context(); foreach (var agent in _agents) { agent.PrepareToRun(context); } var pollInterval = GetPollInterval(); // Fetch poll interval here so we can report any issues early while (true) { // Invoke each Agent's PollCycle method, logging any exceptions that occur try { foreach (var agent in _agents) { agent.PollCycle(); } } catch(Exception e) { s_log.Error("Error error occurred during PollCycle", e); } try { context.SendMetricsToService(); // Enables limited runs for tests that want to invoke the service if (_limitRun && --_limit == 0) { return; } Thread.Sleep(pollInterval); } catch (Exception e) { s_log.Fatal("Fatal error occurred. Shutting down the application", e); throw e; } } }