/// <summary>Update the cache and return the current cached record</summary> /// <param name="mr">the update record</param> /// <param name="includingTags">cache tag values (for later lookup by name) if true</param> /// <returns>the updated cache record</returns> public virtual MetricsCache.Record Update(MetricsRecord mr, bool includingTags) { string name = mr.Name(); MetricsCache.RecordCache recordCache = map[name]; if (recordCache == null) { recordCache = new MetricsCache.RecordCache(this); map[name] = recordCache; } ICollection <MetricsTag> tags = mr.Tags(); MetricsCache.Record record = recordCache[tags]; if (record == null) { record = new MetricsCache.Record(); recordCache[tags] = record; } foreach (AbstractMetric m in mr.Metrics()) { record.metrics[m.Name()] = m; } if (includingTags) { // mostly for some sinks that include tags as part of a dense schema foreach (MetricsTag t in mr.Tags()) { record.tags[t.Name()] = t.Value(); } } return(record); }
public virtual void PutMetrics(MetricsRecord record) { writer.Write(record.Timestamp()); writer.Write(" "); writer.Write(record.Context()); writer.Write("."); writer.Write(record.Name()); string separator = ": "; foreach (MetricsTag tag in record.Tags()) { writer.Write(separator); separator = ", "; writer.Write(tag.Name()); writer.Write("="); writer.Write(tag.Value()); } foreach (AbstractMetric metric in record.Metrics()) { writer.Write(separator); separator = ", "; writer.Write(metric.Name()); writer.Write("="); writer.Write(metric.Value()); } writer.WriteLine(); }
private MetricsRecord MakeRecord(string name, ICollection <MetricsTag> tags, ICollection <AbstractMetric> metrics) { MetricsRecord mr = Org.Mockito.Mockito.Mock <MetricsRecord>(); Org.Mockito.Mockito.When(mr.Name()).ThenReturn(name); Org.Mockito.Mockito.When(mr.Tags()).ThenReturn(tags); Org.Mockito.Mockito.When(mr.Metrics()).ThenReturn(metrics); return(mr); }
private void CheckMetricsRecords(IList <MetricsRecord> recs) { Log.Debug(recs); MetricsRecord r = recs[0]; Assert.Equal("name", "s1rec", r.Name()); MoreAsserts.AssertEquals("tags", new MetricsTag[] { Interns.Tag(MsInfo.Context, "test" ), Interns.Tag(MsInfo.Hostname, hostname) }, r.Tags()); MoreAsserts.AssertEquals("metrics", ((MetricsRecordBuilderImpl)((MetricsRecordBuilderImpl )((MetricsRecordBuilderImpl)((MetricsRecordBuilderImpl)MetricsLists.Builder(string.Empty ).AddCounter(Interns.Info("C1", "C1 desc"), 1L)).AddGauge(Interns.Info("G1", "G1 desc" ), 2L)).AddCounter(Interns.Info("S1NumOps", "Number of ops for s1"), 1L)).AddGauge (Interns.Info("S1AvgTime", "Average time for s1"), 0.0)).Metrics(), r.Metrics()); r = recs[1]; Assert.True("NumActiveSinks should be 3", Iterables.Contains(r. Metrics(), new MetricGaugeInt(MsInfo.NumActiveSinks, 3))); }
public virtual void PutMetrics(MetricsRecord record) { StringBuilder lines = new StringBuilder(); StringBuilder metricsPathPrefix = new StringBuilder(); // Configure the hierarchical place to display the graph. metricsPathPrefix.Append(metricsPrefix).Append(".").Append(record.Context()).Append (".").Append(record.Name()); foreach (MetricsTag tag in record.Tags()) { if (tag.Value() != null) { metricsPathPrefix.Append("."); metricsPathPrefix.Append(tag.Name()); metricsPathPrefix.Append("="); metricsPathPrefix.Append(tag.Value()); } } // The record timestamp is in milliseconds while Graphite expects an epoc time in seconds. long timestamp = record.Timestamp() / 1000L; // Collect datapoints. foreach (AbstractMetric metric in record.Metrics()) { lines.Append(metricsPathPrefix.ToString() + "." + metric.Name().Replace(' ', '.') ).Append(" ").Append(metric.Value()).Append(" ").Append(timestamp).Append("\n"); } try { graphite.Write(lines.ToString()); } catch (Exception e) { Log.Warn("Error sending metrics to Graphite", e); try { graphite.Close(); } catch (Exception e1) { throw new MetricsException("Error closing connection to Graphite", e1); } } }
public virtual void PutMetrics(MetricsRecord record) { string prefix = "threadSourceRec"; if (record.Name().StartsWith(prefix)) { int recordNumber = System.Convert.ToInt32(Runtime.Substring(record.Name() , prefix.Length)); AList <string> names = new AList <string>(); foreach (AbstractMetric m in record.Metrics()) { if (Runtime.EqualsIgnoreCase(m.Name(), "g1")) { collected[recordNumber].Set(m.Value()); return; } names.AddItem(m.Name()); } } }
public virtual void TestUpdate() { MetricsCache cache = new MetricsCache(); MetricsRecord mr = MakeRecord("r", Arrays.AsList(MakeTag("t", "tv")), Arrays.AsList (MakeMetric("m", 0), MakeMetric("m1", 1))); MetricsCache.Record cr = cache.Update(mr); Org.Mockito.Mockito.Verify(mr).Name(); Org.Mockito.Mockito.Verify(mr).Tags(); Org.Mockito.Mockito.Verify(mr).Metrics(); Assert.Equal("same record size", cr.Metrics().Count, ((ICollection <AbstractMetric>)mr.Metrics()).Count); Assert.Equal("same metric value", 0, cr.GetMetric("m")); MetricsRecord mr2 = MakeRecord("r", Arrays.AsList(MakeTag("t", "tv")), Arrays.AsList (MakeMetric("m", 2), MakeMetric("m2", 42))); cr = cache.Update(mr2); Assert.Equal("contains 3 metric", 3, cr.Metrics().Count); CheckMetricValue("updated metric value", cr, "m", 2); CheckMetricValue("old metric value", cr, "m1", 1); CheckMetricValue("new metric value", cr, "m2", 42); MetricsRecord mr3 = MakeRecord("r", Arrays.AsList(MakeTag("t", "tv3")), Arrays.AsList (MakeMetric("m3", 3))); // different tag value cr = cache.Update(mr3); // should get a new record Assert.Equal("contains 1 metric", 1, cr.Metrics().Count); CheckMetricValue("updated metric value", cr, "m3", 3); // tags cache should be empty so far Assert.Equal("no tags", 0, cr.Tags().Count); // until now cr = cache.Update(mr3, true); Assert.Equal("Got 1 tag", 1, cr.Tags().Count); Assert.Equal("Tag value", "tv3", cr.GetTag("t")); CheckMetricValue("Metric value", cr, "m3", 3); }
public override void PutMetrics(MetricsRecord record) { // The method handles both cases whether Ganglia support dense publish // of metrics of sparse (only on change) publish of metrics try { string recordName = record.Name(); string contextName = record.Context(); StringBuilder sb = new StringBuilder(); sb.Append(contextName); sb.Append('.'); sb.Append(recordName); AppendPrefix(record, sb); string groupName = sb.ToString(); sb.Append('.'); int sbBaseLen = sb.Length; string type = null; AbstractGangliaSink.GangliaSlope slopeFromMetric = null; AbstractGangliaSink.GangliaSlope calculatedSlope = null; MetricsCache.Record cachedMetrics = null; ResetBuffer(); // reset the buffer to the beginning if (!IsSupportSparseMetrics()) { // for sending dense metrics, update metrics cache // and get the updated data cachedMetrics = metricsCache.Update(record); if (cachedMetrics != null && cachedMetrics.MetricsEntrySet() != null) { foreach (KeyValuePair <string, AbstractMetric> entry in cachedMetrics.MetricsEntrySet ()) { AbstractMetric metric = entry.Value; sb.Append(metric.Name()); string name = sb.ToString(); // visit the metric to identify the Ganglia type and // slope metric.Visit(gangliaMetricVisitor); type = gangliaMetricVisitor.GetType(); slopeFromMetric = gangliaMetricVisitor.GetSlope(); GangliaConf gConf = GetGangliaConfForMetric(name); calculatedSlope = CalculateSlope(gConf, slopeFromMetric); // send metric to Ganglia EmitMetric(groupName, name, type, metric.Value().ToString(), gConf, calculatedSlope ); // reset the length of the buffer for next iteration sb.Length = sbBaseLen; } } } else { // we support sparse updates ICollection <AbstractMetric> metrics = (ICollection <AbstractMetric>)record.Metrics (); if (metrics.Count > 0) { // we got metrics. so send the latest foreach (AbstractMetric metric in record.Metrics()) { sb.Append(metric.Name()); string name = sb.ToString(); // visit the metric to identify the Ganglia type and // slope metric.Visit(gangliaMetricVisitor); type = gangliaMetricVisitor.GetType(); slopeFromMetric = gangliaMetricVisitor.GetSlope(); GangliaConf gConf = GetGangliaConfForMetric(name); calculatedSlope = CalculateSlope(gConf, slopeFromMetric); // send metric to Ganglia EmitMetric(groupName, name, type, metric.Value().ToString(), gConf, calculatedSlope ); // reset the length of the buffer for next iteration sb.Length = sbBaseLen; } } } } catch (IOException io) { throw new MetricsException("Failed to putMetrics", io); } }
public override bool Equals(object obj) { if (obj is MetricsRecord) { MetricsRecord other = (MetricsRecord)obj; return(Objects.Equal(Timestamp(), other.Timestamp()) && Objects.Equal(Name(), other .Name()) && Objects.Equal(Description(), other.Description()) && Objects.Equal(Tags (), other.Tags()) && Iterables.ElementsEqual(Metrics(), other.Metrics())); } return(false); }
private static AbstractMetric GetFirstMetricByName(MetricsRecord record, string name ) { return(Iterables.GetFirst(Iterables.Filter(record.Metrics(), new MetricsRecords.AbstractMetricPredicate (name)), null)); }