Пример #1
0
        /// <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);
        }
Пример #2
0
        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();
        }
Пример #3
0
        /// <summary>Creates a mock MetricsRecord with the given name and tags.</summary>
        /// <param name="name">String name</param>
        /// <param name="tags">List<MetricsTag> tags</param>
        /// <returns>MetricsRecord newly created mock</returns>
        private static MetricsRecord MockMetricsRecord(string name, IList <MetricsTag> tags
                                                       )
        {
            MetricsRecord record = Org.Mockito.Mockito.Mock <MetricsRecord>();

            Org.Mockito.Mockito.When(record.Name()).ThenReturn(name);
            Org.Mockito.Mockito.When(record.Tags()).ThenReturn(tags);
            return(record);
        }
Пример #4
0
        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);
        }
Пример #5
0
            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());
                    }
                }
            }
Пример #6
0
        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)));
        }
Пример #7
0
        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);
                }
            }
        }
Пример #8
0
 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);
     }
 }
Пример #9
0
 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);
 }
Пример #10
0
 /// <summary>Whether to accept the record</summary>
 /// <param name="record">to filter on</param>
 /// <returns>true to accept; false otherwise.</returns>
 public virtual bool Accepts(MetricsRecord record)
 {
     return(Accepts(record.Name()) && Accepts(record.Tags()));
 }