Example #1
0
        public virtual void TestGetMetricsAndJmx()
        {
            // create test source with a single metric counter of value 0
            TestMetricsSourceAdapter.TestSource source = new TestMetricsSourceAdapter.TestSource
                                                             ("test");
            MetricsSourceBuilder sb           = MetricsAnnotations.NewSourceBuilder(source);
            MetricsSource        s            = sb.Build();
            IList <MetricsTag>   injectedTags = new AList <MetricsTag>();
            MetricsSourceAdapter sa           = new MetricsSourceAdapter("test", "test", "test desc", s
                                                                         , injectedTags, null, null, 1, false);
            // all metrics are initially assumed to have changed
            MetricsCollectorImpl            builder        = new MetricsCollectorImpl();
            IEnumerable <MetricsRecordImpl> metricsRecords = sa.GetMetrics(builder, true);
            // Validate getMetrics and JMX initial values
            MetricsRecordImpl metricsRecord = metricsRecords.GetEnumerator().Next();

            Assert.Equal(0L, metricsRecord.Metrics().GetEnumerator().Next(
                             ).Value());
            Thread.Sleep(100);
            // skip JMX cache TTL
            Assert.Equal(0L, (Number)sa.GetAttribute("C1"));
            // change metric value
            source.IncrementCnt();
            // validate getMetrics and JMX
            builder        = new MetricsCollectorImpl();
            metricsRecords = sa.GetMetrics(builder, true);
            metricsRecord  = metricsRecords.GetEnumerator().Next();
            Assert.True(metricsRecord.Metrics().GetEnumerator().HasNext());
            Thread.Sleep(100);
            // skip JMX cache TTL
            Assert.Equal(1L, (Number)sa.GetAttribute("C1"));
        }
Example #2
0
        public virtual void TestPurgeOldMetrics()
        {
            // create test source with a single metric counter of value 1
            TestMetricsSourceAdapter.PurgableSource source = new TestMetricsSourceAdapter.PurgableSource
                                                                 (this);
            MetricsSourceBuilder sb           = MetricsAnnotations.NewSourceBuilder(source);
            MetricsSource        s            = sb.Build();
            IList <MetricsTag>   injectedTags = new AList <MetricsTag>();
            MetricsSourceAdapter sa           = new MetricsSourceAdapter("tst", "tst", "testdesc", s, injectedTags
                                                                         , null, null, 1, false);
            MBeanInfo info  = sa.GetMBeanInfo();
            bool      sawIt = false;

            foreach (MBeanAttributeInfo mBeanAttributeInfo in info.GetAttributes())
            {
                sawIt |= mBeanAttributeInfo.GetName().Equals(source.lastKeyName);
            }
            Assert.True("The last generated metric is not exported to jmx",
                        sawIt);
            Thread.Sleep(1000);
            // skip JMX cache TTL
            info  = sa.GetMBeanInfo();
            sawIt = false;
            foreach (MBeanAttributeInfo mBeanAttributeInfo_1 in info.GetAttributes())
            {
                sawIt |= mBeanAttributeInfo_1.GetName().Equals(source.lastKeyName);
            }
            Assert.True("The last generated metric is not exported to jmx",
                        sawIt);
        }
Example #3
0
        private void RegisterSystemSource()
        {
            MetricsConfig sysConf = sourceConfigs[MsName];

            sysSource = new MetricsSourceAdapter(prefix, MsStatsName, MsStatsDesc, MetricsAnnotations
                                                 .MakeSource(this), injectedTags, period, sysConf == null ? ((MetricsConfig)config
                                                                                                             .Subset(SourceKey)) : sysConf);
            sysSource.Start();
        }
Example #4
0
        private void SnapshotMetrics(MetricsSourceAdapter sa, MetricsBufferBuilder bufferBuilder
                                     )
        {
            long startTime = Time.Now();

            bufferBuilder.Add(sa.Name(), sa.GetMetrics(collector, true));
            collector.Clear();
            snapshotStat.Add(Time.Now() - startTime);
            Log.Debug("Snapshotted source " + sa.Name());
        }
Example #5
0
 private void StopSources()
 {
     lock (this)
     {
         foreach (KeyValuePair <string, MetricsSourceAdapter> entry in sources)
         {
             MetricsSourceAdapter sa = entry.Value;
             Log.Debug("Stopping metrics source " + entry.Key + ": class=" + sa.Source().GetType
                           ());
             sa.Stop();
         }
         sysSource.Stop();
         sources.Clear();
     }
 }
Example #6
0
 internal virtual void RegisterSource(string name, string desc, MetricsSource source
                                      )
 {
     lock (this)
     {
         Preconditions.CheckNotNull(config, "config");
         MetricsConfig        conf = sourceConfigs[name];
         MetricsSourceAdapter sa   = conf != null ? new MetricsSourceAdapter(prefix, name, desc
                                                                             , source, injectedTags, period, conf) : new MetricsSourceAdapter(prefix, name, desc
                                                                                                                                              , source, injectedTags, period, ((MetricsConfig)config.Subset(SourceKey)));
         sources[name] = sa;
         sa.Start();
         Log.Debug("Registered source " + name);
     }
 }
Example #7
0
        public virtual void TestRegisterSourceWithoutName()
        {
            MetricsSystem ms = new MetricsSystemImpl();

            TestMetricsSystemImpl.TestSource  ts  = new TestMetricsSystemImpl.TestSource("ts");
            TestMetricsSystemImpl.TestSource2 ts2 = new TestMetricsSystemImpl.TestSource2("ts2"
                                                                                          );
            ms.Register(ts);
            ms.Register(ts2);
            ms.Init("TestMetricsSystem");
            // if metrics source is registered without name,
            // the class name will be used as the name
            MetricsSourceAdapter sa = ((MetricsSystemImpl)ms).GetSourceAdapter("TestSource");

            NUnit.Framework.Assert.IsNotNull(sa);
            MetricsSourceAdapter sa2 = ((MetricsSystemImpl)ms).GetSourceAdapter("TestSource2"
                                                                                );

            NUnit.Framework.Assert.IsNotNull(sa2);
            ms.Shutdown();
        }
Example #8
0
        public virtual void TestStartStopStart()
        {
            DefaultMetricsSystem.Shutdown();
            // Clear pre-existing source names.
            MetricsSystemImpl ms = new MetricsSystemImpl("test");

            TestMetricsSystemImpl.TestSource ts = new TestMetricsSystemImpl.TestSource("ts");
            ms.Start();
            ms.Register("ts", string.Empty, ts);
            MetricsSourceAdapter sa = ms.GetSourceAdapter("ts");

            NUnit.Framework.Assert.IsNotNull(sa);
            NUnit.Framework.Assert.IsNotNull(sa.GetMBeanName());
            ms.Stop();
            ms.Shutdown();
            ms.Start();
            sa = ms.GetSourceAdapter("ts");
            NUnit.Framework.Assert.IsNotNull(sa);
            NUnit.Framework.Assert.IsNotNull(sa.GetMBeanName());
            ms.Stop();
            ms.Shutdown();
        }