Beispiel #1
0
        public void SerializeTo(ref StatsMetric metricStats, SerializedMetric serializedMetric)
        {
            serializedMetric.Reset();

            var builder = serializedMetric.Builder;
            var unit    = _commandToUnit[metricStats.MetricType];

            builder.Append(_prefix);
            builder.Append(metricStats.StatName);
            builder.Append(':');
            switch (metricStats.MetricType)
            {
            case MetricType.Set: builder.Append(metricStats.StringValue); break;

            default: builder.AppendFormat(CultureInfo.InvariantCulture, "{0}", metricStats.NumericValue); break;
            }

            builder.Append('|');
            builder.Append(unit);

            if (metricStats.SampleRate != 1.0)
            {
                builder.AppendFormat(CultureInfo.InvariantCulture, "|@{0}", metricStats.SampleRate);
            }

            _serializerHelper.AppendTags(builder, metricStats.Tags);
        }
Beispiel #2
0
        public void OnNewValue(ref StatsMetric metric)
        {
            var key = _aggregator.CreateKey(metric);

            if (_aggregator.TryGetValue(ref key, out var _))
            {
                _aggregator.Update(ref key, metric);
            }
        public void OnNewValue(ref StatsMetric metric)
        {
            var key = _aggregator.CreateKey(metric);

            if (_aggregator.TryGetValue(ref key, out var v))
            {
                v.NumericValue += metric.NumericValue;
                _aggregator.Update(ref key, v);
            }
        public MetricStatsKey CreateKey(StatsMetric metric)
        {
            if (metric.MetricType != _expectedMetricType)
            {
                throw new ArgumentException($"Metric type is {metric.MetricType} instead of {_expectedMetricType}.");
            }

            return(new MetricStatsKey(metric.StatName, metric.Tags));
        }
        public void OnNewValue(ref StatsMetric metric)
        {
            string value = metric.StringValue;
            var    key   = _aggregator.CreateKey(metric);

            if (_aggregator.TryGetValue(ref key, out var v))
            {
                v.Values.Add(value);
                _aggregator.Update(ref key, v);
            }
Beispiel #6
0
        private static void AddStatsMetric(GaugeAggregator aggregator, string statName, double value)
        {
            var statsMetric = new StatsMetric
            {
                MetricType   = MetricType.Gauge,
                StatName     = statName,
                NumericValue = value,
            };

            aggregator.OnNewValue(ref statsMetric);
        }
Beispiel #7
0
        private static void AddStatsMetric(SetAggregator aggregator, string statName, string value)
        {
            var statsMetric = new StatsMetric
            {
                MetricType  = MetricType.Set,
                StatName    = statName,
                StringValue = value,
            };

            aggregator.OnNewValue(ref statsMetric);
        }
        private static void AssertSerialize(
            string expectValue,
            ref StatsMetric statsMetric,
            string prefix)
        {
            var serializerHelper = new SerializerHelper(null);
            var serializer       = new MetricSerializer(serializerHelper, prefix);
            var serializedMetric = new SerializedMetric();

            serializer.SerializeTo(ref statsMetric, serializedMetric);
            Assert.AreEqual(expectValue, serializedMetric.ToString());
        }
        private static void AddStatsMetric(CountAggregator aggregator, string statName, double value)
        {
            var statsMetric = new StatsMetric
            {
                MetricType = MetricType.
                             Count,
                StatName     = statName,
                NumericValue = value,
                SampleRate   = 1,
            };

            aggregator.OnNewValue(ref statsMetric);
        }
Beispiel #10
0
        public void OnNewValue(ref StatsMetric metric)
        {
            var key = _aggregator.CreateKey(metric);

            // In order to aggregate count metrics, the sample rate must be the same.
            // On average, calling 1000 times `service.Increment(metricName, 1, sampleRate: 0.5);` generates arround
            // 500 `StatsMetric` objects with `NumericValue = 1` and `SampleRate = 0.5`. From the DogStatsd server perspective,
            // it is equivalent to generate 500 `StatsMetric` objects with `NumericValue = 2` and `SampleRate = 1`.
            // This code normalizes `StatsMetric.NumericValue` to always have `SampleRate = 1`.
            metric.NumericValue = metric.NumericValue / metric.SampleRate;

            if (_aggregator.TryGetValue(ref key, out var v))
            {
                v.NumericValue += metric.NumericValue;
                _aggregator.Update(ref key, v);
            }
Beispiel #11
0
 private void SendMetric(string metricName, int value)
 {
     if (_optionalTransport != null && _optionalMetricSerializer != null)
     {
         var serializedMetric = new SerializedMetric();
         var metricStats      = new StatsMetric
         {
             MetricType   = MetricType.Count,
             StatName     = metricName,
             NumericValue = value,
             SampleRate   = 1.0,
             Tags         = _optionalTags,
         };
         _optionalMetricSerializer.SerializeTo(ref metricStats, serializedMetric);
         var bytes = BufferBuilder.GetBytes(serializedMetric.ToString());
         _optionalTransport.Send(bytes, bytes.Length);
     }
 }
        private static void AssertSetSerialize(
            string expectValue,
            string name,
            object value,
            double sampleRate = 1.0,
            string[] tags     = null,
            string prefix     = null)
        {
            var statsMetric = new StatsMetric
            {
                MetricType  = MetricType.Set,
                StatName    = name,
                SampleRate  = sampleRate,
                StringValue = value.ToString(),
                Tags        = tags,
            };

            AssertSerialize(expectValue, ref statsMetric, prefix);
        }
        private static void AssertSerialize(
            string expectValue,
            MetricType metricType,
            string name,
            double value,
            double sampleRate = 1.0,
            string[] tags     = null,
            string prefix     = null)
        {
            var statsMetric = new StatsMetric
            {
                MetricType   = metricType,
                StatName     = name,
                SampleRate   = sampleRate,
                NumericValue = value,
                Tags         = tags,
            };

            AssertSerialize(expectValue, ref statsMetric, prefix);
        }
Beispiel #14
0
        private bool RouteMetric(ref StatsMetric metric)
        {
            switch (metric.MetricType)
            {
            case MetricType.Count:
                if (_optionalCountAggregator != null)
                {
                    _optionalCountAggregator.OnNewValue(ref metric);
                    return(false);
                }

                break;

            case MetricType.Gauge:
                if (_optionalGaugeAggregator != null)
                {
                    _optionalGaugeAggregator.OnNewValue(ref metric);
                    return(false);
                }

                break;

            case MetricType.Set:
                if (_optionalSetAggregator != null)
                {
                    _optionalSetAggregator.OnNewValue(ref metric);
                    return(false);
                }

                break;

            default:
                break;
            }

            this._serializers.MetricSerializer.SerializeTo(ref metric, _serializedMetric);
            return(true);
        }
 public void FlushStatsMetric(StatsMetric metric)
 {
     _serializer.SerializeTo(ref metric, _serializedMetric);
     _bufferBuilder.Add(_serializedMetric);
 }
Beispiel #16
0
        private static void Add(AggregatorFlusher <StatsMetric> aggregator, string key, StatsMetric value)
        {
            var statsKey = new MetricStatsKey(key, null);

            aggregator.Add(ref statsKey, value);
        }