public virtual void TestPutMetrics2() { GraphiteSink sink = new GraphiteSink(); IList <MetricsTag> tags = new AList <MetricsTag>(); tags.AddItem(new MetricsTag(MsInfo.Context, "all")); tags.AddItem(new MetricsTag(MsInfo.Hostname, null)); ICollection <AbstractMetric> metrics = new HashSet <AbstractMetric>(); metrics.AddItem(MakeMetric("foo1", 1)); metrics.AddItem(MakeMetric("foo2", 2)); MetricsRecord record = new MetricsRecordImpl(MsInfo.Context, (long)10000, tags, metrics ); ArgumentCaptor <string> argument = ArgumentCaptor.ForClass <string>(); GraphiteSink.Graphite mockGraphite = MakeGraphite(); Whitebox.SetInternalState(sink, "graphite", mockGraphite); sink.PutMetrics(record); try { Org.Mockito.Mockito.Verify(mockGraphite).Write(argument.Capture()); } catch (IOException e) { Runtime.PrintStackTrace(e); } string result = argument.GetValue(); Assert.Equal(true, result.Equals("null.all.Context.Context=all.foo1 1 10\n" + "null.all.Context.Context=all.foo2 2 10\n") || result.Equals("null.all.Context.Context=all.foo2 2 10\n" + "null.all.Context.Context=all.foo1 1 10\n")); }
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")); }
public virtual IList <MetricsRecordImpl> GetRecords() { IList <MetricsRecordImpl> recs = Lists.NewArrayListWithCapacity(rbs.Count); foreach (MetricsRecordBuilderImpl rb in rbs) { MetricsRecordImpl mr = rb.GetRecord(); if (mr != null) { recs.AddItem(mr); } } return(recs); }
public virtual void TestPutMetrics3() { // setup GraphiteSink GraphiteSink sink = new GraphiteSink(); GraphiteSink.Graphite mockGraphite = MakeGraphite(); Whitebox.SetInternalState(sink, "graphite", mockGraphite); // given two metrics records with timestamps 1000 milliseconds apart. IList <MetricsTag> tags = Collections.EmptyList(); ICollection <AbstractMetric> metrics = new HashSet <AbstractMetric>(); metrics.AddItem(MakeMetric("foo1", 1)); MetricsRecord record1 = new MetricsRecordImpl(MsInfo.Context, 1000000000000L, tags , metrics); MetricsRecord record2 = new MetricsRecordImpl(MsInfo.Context, 1000000001000L, tags , metrics); sink.PutMetrics(record1); sink.PutMetrics(record2); sink.Flush(); try { sink.Close(); } catch (IOException e) { Runtime.PrintStackTrace(e); } // then the timestamps in the graphite stream should differ by one second. try { Org.Mockito.Mockito.Verify(mockGraphite).Write(Matchers.Eq("null.default.Context.foo1 1 1000000000\n" )); Org.Mockito.Mockito.Verify(mockGraphite).Write(Matchers.Eq("null.default.Context.foo1 1 1000000001\n" )); } catch (IOException e) { Runtime.PrintStackTrace(e); } }
public virtual void TestTagsForPrefix() { ConfigBuilder cb = new ConfigBuilder().Add("test.sink.ganglia.tagsForPrefix.all", "*").Add("test.sink.ganglia.tagsForPrefix.some", "NumActiveSinks, " + "NumActiveSources" ).Add("test.sink.ganglia.tagsForPrefix.none", string.Empty); GangliaSink30 sink = new GangliaSink30(); sink.Init(cb.Subset("test.sink.ganglia")); IList <MetricsTag> tags = new AList <MetricsTag>(); tags.AddItem(new MetricsTag(MsInfo.Context, "all")); tags.AddItem(new MetricsTag(MsInfo.NumActiveSources, "foo")); tags.AddItem(new MetricsTag(MsInfo.NumActiveSinks, "bar")); tags.AddItem(new MetricsTag(MsInfo.NumAllSinks, "haa")); tags.AddItem(new MetricsTag(MsInfo.Hostname, "host")); ICollection <AbstractMetric> metrics = new HashSet <AbstractMetric>(); MetricsRecord record = new MetricsRecordImpl(MsInfo.Context, (long)1, tags, metrics ); StringBuilder sb = new StringBuilder(); sink.AppendPrefix(record, sb); Assert.Equal(".NumActiveSources=foo.NumActiveSinks=bar.NumAllSinks=haa" , sb.ToString()); tags.Set(0, new MetricsTag(MsInfo.Context, "some")); sb = new StringBuilder(); sink.AppendPrefix(record, sb); Assert.Equal(".NumActiveSources=foo.NumActiveSinks=bar", sb.ToString ()); tags.Set(0, new MetricsTag(MsInfo.Context, "none")); sb = new StringBuilder(); sink.AppendPrefix(record, sb); Assert.Equal(string.Empty, sb.ToString()); tags.Set(0, new MetricsTag(MsInfo.Context, "nada")); sb = new StringBuilder(); sink.AppendPrefix(record, sb); Assert.Equal(string.Empty, sb.ToString()); }
public virtual void TestFailureAndPutMetrics() { GraphiteSink sink = new GraphiteSink(); IList <MetricsTag> tags = new AList <MetricsTag>(); tags.AddItem(new MetricsTag(MsInfo.Context, "all")); tags.AddItem(new MetricsTag(MsInfo.Hostname, "host")); ICollection <AbstractMetric> metrics = new HashSet <AbstractMetric>(); metrics.AddItem(MakeMetric("foo1", 1.25)); metrics.AddItem(MakeMetric("foo2", 2.25)); MetricsRecord record = new MetricsRecordImpl(MsInfo.Context, (long)10000, tags, metrics ); GraphiteSink.Graphite mockGraphite = MakeGraphite(); Whitebox.SetInternalState(sink, "graphite", mockGraphite); // throw exception when first try Org.Mockito.Mockito.DoThrow(new IOException("IO exception")).When(mockGraphite).Write (Matchers.AnyString()); sink.PutMetrics(record); Org.Mockito.Mockito.Verify(mockGraphite).Write(Matchers.AnyString()); Org.Mockito.Mockito.Verify(mockGraphite).Close(); // reset mock and try again Org.Mockito.Mockito.Reset(mockGraphite); Org.Mockito.Mockito.When(mockGraphite.IsConnected()).ThenReturn(false); ArgumentCaptor <string> argument = ArgumentCaptor.ForClass <string>(); sink.PutMetrics(record); Org.Mockito.Mockito.Verify(mockGraphite).Write(argument.Capture()); string result = argument.GetValue(); Assert.Equal(true, result.Equals("null.all.Context.Context=all.Hostname=host.foo1 1.25 10\n" + "null.all.Context.Context=all.Hostname=host.foo2 2.25 10\n") || result.Equals ("null.all.Context.Context=all.Hostname=host.foo2 2.25 10\n" + "null.all.Context.Context=all.Hostname=host.foo1 1.25 10\n" )); }