Ejemplo n.º 1
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();
        }
Ejemplo n.º 2
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);
                }
            }
        }
Ejemplo n.º 3
0
        public virtual void AppendPrefix(MetricsRecord record, StringBuilder sb)
        {
            string contextName            = record.Context();
            ICollection <MetricsTag> tags = record.Tags();

            if (useTagsMap.Contains(contextName))
            {
                ICollection <string> useTags = useTagsMap[contextName];
                foreach (MetricsTag t in tags)
                {
                    if (useTags == null || useTags.Contains(t.Name()))
                    {
                        // the context is always skipped here because it is always added
                        // the hostname is always skipped to avoid case-mismatches
                        // from different DNSes.
                        if (t.Info() != MsInfo.Context && t.Info() != MsInfo.Hostname && t.Value() != null)
                        {
                            sb.Append('.').Append(t.Name()).Append('=').Append(t.Value());
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
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);
     }
 }