Пример #1
0
        /// <summary>
        /// Add a metrics entry string.
        /// </summary>
        public void Entry(string name, string data)
        {
            if (name == null)
            {
                throw new ArgumentNullException();
            }
            if (name == String.Empty)
            {
                throw new ArgumentException();
            }
            if (data == null)
            {
                throw new ArgumentNullException();
            }

            // Set up the metric object
            var metric = new Metric()
            {
                Name = name,
                Data = data,
                Type = stringTypeName,
                TimeStamp = DateTimeOffset.Now
            };

            QueueMetric(metric);
        }
Пример #2
0
 /// <summary>
 /// Get a single line string to write out to our file for a given metric.
 /// </summary>
 private string MetricToString(IDictionary<string, string> properties, Metric metric)
 {
     return metric.Name + ", " +
         metric.Data + ", " +
         metric.TimeStamp.ToString() + ", " +
         metric.Type + ", " +
         "{" + String.Join("; ", properties
             .Select(property => property.Key + ": " + property.Value)
             .ToArray()) + "}";
 }
Пример #3
0
 /// <summary>
 /// Emit an array of metrics entries.
 /// </summary>
 public void Emit(IDictionary<string, string> properties, Metric[] metrics)
 {
     using (var streamWriter = File.AppendText(logFilePath))
     {
         foreach (var metric in metrics)
         {
             streamWriter.WriteLine(MetricToString(properties, metric));
         }
     }
 }
        public void Emit(IDictionary<string, string> properties, Metric[] metrics)
        {
            // Wrap up properties and metrics that we want to send.
            var objectToSend = new
            {
                Properties = properties,
                Metrics = metrics
            };

            // Convert to JSON.
            var json = JsonConvert.SerializeObject(objectToSend);

            // Send!
            httpService.Post(postUrl, json);
        }
Пример #5
0
        /// <summary>
        /// Adds a metric to the queue.
        /// </summary>
        private void QueueMetric(Metric metric)
        {
            metricQueue[metricQueueIndex++] = metric;

            if (metricQueueIndex >= batchSize)
            {
                Flush();
            }
        }
Пример #6
0
        /// <summary>
        /// Sends an increment metric to the emitter.
        /// </summary>
        public void Inc(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException();
            }
            if (name == String.Empty)
            {
                throw new ArgumentException();
            }

            // Set up the metric object
            var metric = new Metric()
            {
                Name = name,
                Type = incTypeName,
                TimeStamp = DateTimeOffset.Now
            };

            QueueMetric(metric);
        }
Пример #7
0
        /// <summary>
        /// Flushes all queued metrics. 
        /// </summary>
        public void Flush()
        {
            if (metricQueueIndex == 0)
            {
                return;
            }

            try
            {
                if (metricQueueIndex < batchSize)
                {
                    var metricsToEmit = new Metric[metricQueueIndex];

                    for (var i = 0; i < metricQueueIndex; i++)
                    {
                        metricsToEmit[i] = metricQueue[i];
                    }

                    emitter.Emit(properties, metricsToEmit);
                }
                else
                {
                    emitter.Emit(properties, metricQueue);
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Exception occurred while attempting to flush metrics", ex);
            }
            finally
            {
                metricQueueIndex = 0;
            }
        }