Esempio n. 1
0
        private void WriteHistogram(string context, string name, IDictionary <string, object> data,
                                    MetricTags tags, DateTime timestamp)
        {
            // Report Wavefront Histograms using an API that is specific to Wavefront Histograms.
            if (WavefrontHistogramOptions.IsWavefrontHistogram(tags))
            {
                name = Concat(context, name);

                // Wavefront Histograms are reported as a distribution, so we must extract the
                // distribution from a HistogramValue that is carrying it in a serialized format.
                string keyFieldName   = fields.Histogram[HistogramFields.UserMaxValue];
                string valueFieldName = fields.Histogram[HistogramFields.UserMinValue];

                if (data.ContainsKey(keyFieldName) && data.ContainsKey(valueFieldName))
                {
                    string key   = (string)data[keyFieldName];
                    string value = (string)data[valueFieldName];

                    // Deserialize the distributions into the right format for reporting.
                    var distributions = WavefrontHistogramImpl.Deserialize(
                        new KeyValuePair <string, string>(key, value));

                    foreach (var distribution in distributions)
                    {
                        wavefrontSender.SendDistribution(
                            name,
                            distribution.Centroids,
                            histogramGranularities,
                            distribution.Timestamp,
                            source,
                            FilterTags(tags)
                            );
                    }
                }
            }
            else
            {
                foreach (var field in fields.Histogram)
                {
                    // Do not report non-numerical metrics
                    if (field.Key == HistogramFields.UserLastValue ||
                        field.Key == HistogramFields.UserMaxValue ||
                        field.Key == HistogramFields.UserMinValue)
                    {
                        continue;
                    }

                    if (data.ContainsKey(field.Value))
                    {
                        WriteInternal(context, name, field.Value, data[field.Value], tags,
                                      timestamp);
                    }
                }
            }
        }
Esempio n. 2
0
        public void TestFlushDistributions()
        {
            var currentTime = DateTime.UtcNow;

            long clockMillis() => DateTimeUtils.UnixTimeMilliseconds(currentTime);

            var  wavefrontHistogram = CreatePowHistogram(clockMillis);
            long minute0ClockMillis = clockMillis() / 60000 * 60000;

            // Test that nothing in the current minute bin gets flushed
            var distributions = wavefrontHistogram.FlushDistributions();

            Assert.Equal(0, distributions.Count);

            // Test that prior minute bins get flushed
            currentTime = currentTime.AddMinutes(1);
            long minute1ClockMillis = clockMillis() / 60000 * 60000;

            wavefrontHistogram.Update(0.01);

            currentTime = currentTime.AddMinutes(1);

            distributions = wavefrontHistogram.FlushDistributions();
            var actualDict = DistributionsToDictionary(distributions);

            var expectedDict = new Dictionary <long, Dictionary <double, int> > {
                { minute0ClockMillis, new Dictionary <double, int> {
                      { 0.1, 1 }, { 1, 1 }, { 10, 2 }, { 100, 1 }, { 1000, 1 }, { 10000, 2 }, { 100000, 1 }
                  } },
                { minute1ClockMillis, new Dictionary <double, int> {
                      { 0.01, 1 }
                  } }
            };

            Assert.Equal(expectedDict, actualDict);

            // Test serialization
            var serializedPair = WavefrontHistogramImpl.Serialize(distributions);

            string expectedTimestamps = distributions
                                        .Select(distribution => distribution.Timestamp.ToString())
                                        .Aggregate((result, item) => result + ';' + item);

            string expectedCentroids = distributions
                                       .Select(distribution => distribution.Centroids
                                               .Select(centroid => centroid.Key.ToString() + ' ' + centroid.Value)
                                               .Aggregate((result, item) => result + ',' + item))
                                       .Aggregate((result, item) => result + ';' + item);

            Assert.Equal(expectedTimestamps, serializedPair.Key);
            Assert.Equal(expectedCentroids, serializedPair.Value);

            // Test deserialization
            var deserializedDistributions = WavefrontHistogramImpl.Deserialize(serializedPair);

            Assert.Equal(actualDict, DistributionsToDictionary(deserializedDistributions));

            // Test that prior minute bins get cleared
            distributions = wavefrontHistogram.FlushDistributions();
            Assert.Equal(0, distributions.Count);
        }