public void TestDeltaCounter()
        {
            IWavefrontMetricSender testWavefrontSender = new TestWavefrontSender();
            var registry = new WavefrontSdkMetricsRegistry.Builder(testWavefrontSender)
                           .ReportingIntervalSeconds(10000)
                           .Build();
            var deltaCounter = registry.DeltaCounter("counter");

            Assert.Equal(0, deltaCounter.Count);
            deltaCounter.Inc();
            Assert.Equal(1, deltaCounter.Count);
            deltaCounter.Clear();
            Assert.Equal(0, deltaCounter.Count);
            deltaCounter.Inc(5);
            Assert.Equal(5, deltaCounter.Count);

            /// Delta counter decrements counter each time data is sent.
            deltaCounter.Dec();
            Assert.Equal(4, deltaCounter.Count);
            deltaCounter.Dec(2);
            Assert.Equal(2, deltaCounter.Count);

            /// Verify Delta Counter is reset to 0 after sending
            registry.Run(testWavefrontSender, null);
            Assert.Equal(0, deltaCounter.Count);
        }
        public void TestGauge()
        {
            var registry = new WavefrontSdkMetricsRegistry.Builder(null)
                           .ReportingIntervalSeconds(10000)
                           .Build();
            var list  = new List <int>();
            var gauge = registry.Gauge("gauge", () => list.Count);

            Assert.Equal(0, gauge.Value);
            list.Add(0);
            Assert.Equal(1, gauge.Value);
        }
        public MetricSnapshotWavefrontWriterTest()
        {
            var wfSender   = wfSenderMock.Object;
            var globalTags = new Dictionary <string, string>
            {
                { "globalKey1", "globalVal1" },
                { "globalKey2", "globalVal2" }
            };
            var histogramGranularities = new HashSet <HistogramGranularity>
            {
                HistogramGranularity.Minute
            };
            var sdkMetricsRegistry = new WavefrontSdkMetricsRegistry.Builder(wfSender).Build();

            writer = new MetricSnapshotWavefrontWriter(wfSender, "source", globalTags,
                                                       histogramGranularities, sdkMetricsRegistry, new MetricFields());
        }
        public void TestCounter()
        {
            var registry = new WavefrontSdkMetricsRegistry.Builder(null)
                           .ReportingIntervalSeconds(10000)
                           .Build();
            var counter = registry.Counter("counter");

            Assert.Equal(0, counter.Count);
            counter.Inc();
            Assert.Equal(1, counter.Count);
            counter.Inc(2);
            Assert.Equal(3, counter.Count);
            counter.Clear();
            Assert.Equal(0, counter.Count);
            counter.Inc(5);
            Assert.Equal(5, registry.Counter("counter").Count);
            Assert.Equal(0, registry.Counter("counter2").Count);
        }
        public WavefrontReporter(MetricsReportingWavefrontOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (options.WavefrontSender == null)
            {
                throw new ArgumentNullException(
                          nameof(MetricsReportingWavefrontOptions.WavefrontSender));
            }

            wavefrontSender = options.WavefrontSender;

            source = options.Source;

            if (options.ApplicationTags != null)
            {
                globalTags = new Dictionary <string, string>(options.ApplicationTags.ToPointTags());
            }
            else
            {
                globalTags = new Dictionary <string, string>();
            }

            histogramGranularities = new HashSet <HistogramGranularity>();
            if (options.WavefrontHistogram.ReportMinuteDistribution)
            {
                histogramGranularities.Add(HistogramGranularity.Minute);
            }
            if (options.WavefrontHistogram.ReportHourDistribution)
            {
                histogramGranularities.Add(HistogramGranularity.Hour);
            }
            if (options.WavefrontHistogram.ReportDayDistribution)
            {
                histogramGranularities.Add(HistogramGranularity.Day);
            }

            if (options.FlushInterval < TimeSpan.Zero)
            {
                throw new InvalidOperationException(
                          $"{nameof(MetricsReportingWavefrontOptions.FlushInterval)} " +
                          "must not be less than zero");
            }

            Filter = options.Filter;

            FlushInterval = options.FlushInterval > TimeSpan.Zero
                ? options.FlushInterval
                : AppMetricsConstants.Reporting.DefaultFlushInterval;

            // Formatting will be handled by the Wavefront sender.
            Formatter = null;

            metricFields = options.MetricFields ?? new MetricFields();

            var registryBuilder = new WavefrontSdkMetricsRegistry.Builder(wavefrontSender)
                                  .Prefix(Constants.SdkMetricPrefix + ".app_metrics")
                                  .Source(source)
                                  .Tags(globalTags);

            if (options.LoggerFactory != null)
            {
                registryBuilder.LoggerFactory(options.LoggerFactory);
            }
            sdkMetricsRegistry = registryBuilder.Build();

            reporterErrors = sdkMetricsRegistry.Counter("reporter.errors");

            double sdkVersion = Utils.GetSemVer(Assembly.GetExecutingAssembly());

            sdkMetricsRegistry.Gauge("version", () => sdkVersion);

            Logger.Info($"Using Wavefront Reporter {this}. FlushInterval: {FlushInterval}");
        }