Пример #1
0
        public void TestContextInitializationWithLicenseKey()
        {
            string license = "FakeyMcFakeKey";
            var context = new Context(license);

            Assert.AreEqual(license, context.LicenseKey);
        }
Пример #2
0
 public void TestReportBadMetricNullGuid()
 {
     try
     {
         var context = new Context();
         context.ReportMetric("", "TestComponent", "Category/TestMetric", "unit", 2);
         Assert.Fail("Reporting metric with null guid should fail");
     }
     catch (ArgumentNullException)
     {
         // Expected
     }
 }
Пример #3
0
 public void TestReportBadMetricNegativeValue()
 {
     try
     {
         var context = new Context();
         context.ReportMetric("com.newrelic.test", "TestComponent", "Category/TestMetric", "unit", -2);
         Assert.Fail("Reporting metric with null guid should fail");
     }
     catch (ArgumentException)
     {
         // Expected
     }
 }
Пример #4
0
 public void TestReportBadMetricEmptyComponent()
 {
     try
     {
         var context = new Context();
         context.ReportMetric("com.newrelic.test", "", "Category/TestMetric", "unit", 2);
         Assert.Fail("Reporting metric with empty component should fail");
     }
     catch (ArgumentNullException)
     {
         // Expected
     }
 }
Пример #5
0
        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");
            }
        }
Пример #6
0
        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");
            }
        }
Пример #7
0
        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");
        }
Пример #8
0
        /// <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;
                }
            }
        }
Пример #9
0
 public void TestReportValidMetric()
 {
     var context = new Context();
     context.ReportMetric("com.newrelic.test", "TestComponent", "Category/TestMetric", "unit", 5); // Assert no throws
 }
Пример #10
0
        public void TestContextInitialization()
        {
            var context = new Context();

            Assert.IsNotNull(context.ServiceUri, "The initialized context does not have a default service URI");
        }