示例#1
0
        public async Task ReportMetricAsync_GetsValidInputWithOneDimensions_SuccessfullyWritesMetric()
        {
            // Arrange
            var metricName            = _bogus.Name.FirstName();
            var metricDescription     = _bogus.Lorem.Sentence();
            var metricValue           = _bogus.Random.Double();
            var dimensionName         = _bogus.Name.FirstName();
            var dimensionValue        = _bogus.Name.FirstName();
            var expectedDimensionName = dimensionName.ToLower();
            var timeSeries            = new TimeSeriesElement
            {
                Metadatavalues = new List <MetadataValue> {
                    new MetadataValue(name: new LocalizableString(dimensionName), value: dimensionValue)
                }
            };
            var measuredMetric             = MeasuredMetric.CreateForDimension(metricValue, dimensionName.ToUpper(), timeSeries);
            var scrapeResult               = ScrapeResultGenerator.GenerateFromMetric(measuredMetric);
            var metricsDeclarationProvider = CreateMetricsDeclarationProvider(metricName);
            var prometheusConfiguration    = CreatePrometheusConfiguration();
            var mocks      = CreatePrometheusMetricFactoryMock();
            var metricSink = new PrometheusScrapingEndpointMetricSink(mocks.Factory.Object, metricsDeclarationProvider, prometheusConfiguration, NullLogger <PrometheusScrapingEndpointMetricSink> .Instance);

            // Act
            await metricSink.ReportMetricAsync(metricName, metricDescription, scrapeResult);

            // Assert
            mocks.Factory.Verify(mock => mock.CreateGauge(metricName, metricDescription, It.IsAny <bool>(), It.Is <string[]>(specified => specified.Contains(expectedDimensionName) && EnsureAllArrayEntriesAreSpecified(specified, scrapeResult.Labels.Keys.ToArray()))), Times.Once());
            mocks.MetricFamily.Verify(mock => mock.WithLabels(It.Is <string[]>(specified => specified.Contains(dimensionValue) && EnsureAllArrayEntriesAreSpecified(specified, scrapeResult.Labels.Values.ToArray()))), Times.Once());
            mocks.Gauge.Verify(mock => mock.Set(metricValue), Times.Once());
        }
示例#2
0
        public async Task ReportMetricAsync_GetsValidInputWithTwoMetricValues_SuccessfullyWritesMultipleMetrics()
        {
            // Arrange
            var metricName        = _bogus.Name.FirstName();
            var metricDescription = _bogus.Lorem.Sentence();
            var firstMetricValue  = _bogus.Random.Double();
            var secondMetricValue = _bogus.Random.Double();
            var firstMetric       = MeasuredMetric.CreateWithoutDimension(firstMetricValue);
            var secondMetric      = MeasuredMetric.CreateWithoutDimension(secondMetricValue);
            var scrapeResult      = ScrapeResultGenerator.GenerateFromMetric(firstMetric);

            scrapeResult.MetricValues.Add(secondMetric);
            var metricsDeclarationProvider = CreateMetricsDeclarationProvider(metricName);
            var prometheusConfiguration    = CreatePrometheusConfiguration();
            var mocks      = CreatePrometheusMetricFactoryMock();
            var metricSink = new PrometheusScrapingEndpointMetricSink(mocks.Factory.Object, metricsDeclarationProvider, prometheusConfiguration, NullLogger <PrometheusScrapingEndpointMetricSink> .Instance);

            // Act
            await metricSink.ReportMetricAsync(metricName, metricDescription, scrapeResult);

            // Assert
            mocks.Factory.Verify(mock => mock.CreateGauge(metricName, metricDescription, It.IsAny <bool>(), It.Is <string[]>(specified => EnsureAllArrayEntriesAreSpecified(specified, scrapeResult.Labels.Keys.ToArray()))), Times.Exactly(2));
            mocks.MetricFamily.Verify(mock => mock.WithLabels(It.Is <string[]>(specified => EnsureAllArrayEntriesAreSpecified(specified, scrapeResult.Labels.Values.ToArray()))), Times.Exactly(2));
            mocks.Gauge.Verify(mock => mock.Set(firstMetricValue), Times.Once());
            mocks.Gauge.Verify(mock => mock.Set(secondMetricValue), Times.Once());
        }
        public async Task ReportMetricAsync_InputDoesNotContainMetricDescription_Succeeds(string metricDescription)
        {
            // Arrange
            var metricName          = BogusGenerator.Name.FirstName();
            var metricValue         = BogusGenerator.Random.Double();
            var measuredMetric      = MeasuredMetric.CreateWithoutDimension(metricValue);
            var scrapeResult        = ScrapeResultGenerator.GenerateFromMetric(measuredMetric);
            var statsDPublisherMock = new Mock <IStatsDPublisher>();
            var metricSink          = new StatsdMetricSink(statsDPublisherMock.Object, NullLogger <StatsdMetricSink> .Instance);

            // Act & Assert
            // ReSharper disable once ExpressionIsAlwaysNull
            await metricSink.ReportMetricAsync(metricName, metricDescription, scrapeResult);
        }
示例#4
0
        public async Task ReportMetricAsync_InputDoesNotContainMetricDescription_Succeeds(string metricDescription)
        {
            // Arrange
            var metricName                    = BogusGenerator.Name.FirstName();
            var metricValue                   = BogusGenerator.Random.Double();
            var measuredMetric                = MeasuredMetric.CreateWithoutDimension(metricValue);
            var scrapeResult                  = ScrapeResultGenerator.GenerateFromMetric(measuredMetric);
            var systemMetricConfigOptions     = BogusAtlassianStatuspageMetricSinkConfigurationGenerator.GetSinkConfiguration();
            var atlassianStatuspageClientMock = new Mock <IAtlassianStatuspageClient>();
            var metricSink                    = new AtlassianStatuspageMetricSink(atlassianStatuspageClientMock.Object, systemMetricConfigOptions, NullLogger <AtlassianStatuspageMetricSink> .Instance);

            // Act & Assert
            // ReSharper disable once ExpressionIsAlwaysNull
            await metricSink.ReportMetricAsync(metricName, metricDescription, scrapeResult);
        }
        public async Task ReportMetricAsync_GetsValidInputWithMetricValue_SuccessfullyWritesMetric()
        {
            // Arrange
            var metricName          = BogusGenerator.Name.FirstName();
            var metricDescription   = BogusGenerator.Lorem.Sentence();
            var metricValue         = BogusGenerator.Random.Double();
            var measuredMetric      = MeasuredMetric.CreateWithoutDimension(metricValue);
            var scrapeResult        = ScrapeResultGenerator.GenerateFromMetric(measuredMetric);
            var statsDPublisherMock = new Mock <IStatsDPublisher>();
            var metricSink          = new StatsdMetricSink(statsDPublisherMock.Object, NullLogger <StatsdMetricSink> .Instance);

            // Act
            await metricSink.ReportMetricAsync(metricName, metricDescription, scrapeResult);

            // Assert
            statsDPublisherMock.Verify(mock => mock.Gauge(metricValue, metricName), Times.Once());
        }
        public async Task ReportMetricAsync_GetsValidInputWithoutMetricValue_SuccessfullyWritesMetricWithDefault()
        {
            // Arrange
            const double expectedDefaultValue = 0;
            var          metricName           = BogusGenerator.Name.FirstName();
            var          metricDescription    = BogusGenerator.Lorem.Sentence();
            double?      metricValue          = null;
            // ReSharper disable once ExpressionIsAlwaysNull
            var measuredMetric      = MeasuredMetric.CreateWithoutDimension(metricValue);
            var scrapeResult        = ScrapeResultGenerator.GenerateFromMetric(measuredMetric);
            var statsDPublisherMock = new Mock <IStatsDPublisher>();
            var metricSink          = new StatsdMetricSink(statsDPublisherMock.Object, NullLogger <StatsdMetricSink> .Instance);

            // Act
            await metricSink.ReportMetricAsync(metricName, metricDescription, scrapeResult);

            // Assert
            statsDPublisherMock.Verify(mock => mock.Gauge(expectedDefaultValue, metricName), Times.Once());
        }
示例#7
0
        public async Task ReportMetricAsync_GetsValidInputWithMetricValueAndPromitorToSystemMetricMapping_SuccessfullyWritesMetric()
        {
            // Arrange
            var promitorMetricName            = BogusGenerator.Name.FirstName();
            var systemMetricId                = BogusGenerator.Name.FirstName();
            var metricDescription             = BogusGenerator.Lorem.Sentence();
            var metricValue                   = BogusGenerator.Random.Double();
            var measuredMetric                = MeasuredMetric.CreateWithoutDimension(metricValue);
            var scrapeResult                  = ScrapeResultGenerator.GenerateFromMetric(measuredMetric);
            var systemMetricConfigOptions     = BogusAtlassianStatuspageMetricSinkConfigurationGenerator.GetSinkConfiguration(systemMetricId: systemMetricId, promitorMetricName: promitorMetricName);
            var atlassianStatuspageClientMock = new Mock <IAtlassianStatuspageClient>();
            var metricSink = new AtlassianStatuspageMetricSink(atlassianStatuspageClientMock.Object, systemMetricConfigOptions, NullLogger <AtlassianStatuspageMetricSink> .Instance);

            // Act
            await metricSink.ReportMetricAsync(promitorMetricName, metricDescription, scrapeResult);

            // Assert
            atlassianStatuspageClientMock.Verify(mock => mock.ReportMetricAsync(systemMetricId, metricValue), Times.Once());
        }
示例#8
0
        public async Task ReportMetricAsync_GetsValidInputWithPromitorMetricThatIsNotMappedToSystemMetricId_DoesNotWriteMetric()
        {
            // Arrange
            const double expectedDefaultValue = 0;
            var          promitorMetricName   = BogusGenerator.Name.FirstName();
            var          systemMetricId       = BogusGenerator.Name.FirstName();
            var          metricDescription    = BogusGenerator.Lorem.Sentence();
            double?      metricValue          = null;
            // ReSharper disable once ExpressionIsAlwaysNull
            var measuredMetric                = MeasuredMetric.CreateWithoutDimension(metricValue);
            var scrapeResult                  = ScrapeResultGenerator.GenerateFromMetric(measuredMetric);
            var systemMetricConfigOptions     = BogusAtlassianStatuspageMetricSinkConfigurationGenerator.GetSinkConfiguration(promitorMetricName: promitorMetricName);
            var atlassianStatuspageClientMock = new Mock <IAtlassianStatuspageClient>();
            var metricSink = new AtlassianStatuspageMetricSink(atlassianStatuspageClientMock.Object, systemMetricConfigOptions, NullLogger <AtlassianStatuspageMetricSink> .Instance);

            // Act
            await metricSink.ReportMetricAsync(promitorMetricName, metricDescription, scrapeResult);

            // Assert
            atlassianStatuspageClientMock.Verify(mock => mock.ReportMetricAsync(systemMetricId, expectedDefaultValue), Times.Never);
        }
示例#9
0
        public async Task ReportMetricAsync_GetsValidInputWithoutMetricValue_SuccessfullyWritesMetricWithDefault(double?expectedDefaultValue)
        {
            // Arrange
            double expectedMeasuredMetric = expectedDefaultValue ?? double.NaN; // If nothing is configured, NaN is used.
            var    metricName             = _bogus.Name.FirstName();
            var    metricDescription      = _bogus.Lorem.Sentence();
            double?metricValue            = null;
            // ReSharper disable once ExpressionIsAlwaysNull
            var measuredMetric             = MeasuredMetric.CreateWithoutDimension(metricValue);
            var scrapeResult               = ScrapeResultGenerator.GenerateFromMetric(measuredMetric);
            var metricsDeclarationProvider = CreateMetricsDeclarationProvider(metricName);
            var prometheusConfiguration    = CreatePrometheusConfiguration(metricUnavailableValue: expectedDefaultValue);
            var mocks      = CreatePrometheusMetricFactoryMock();
            var metricSink = new PrometheusScrapingEndpointMetricSink(mocks.Factory.Object, metricsDeclarationProvider, prometheusConfiguration, NullLogger <PrometheusScrapingEndpointMetricSink> .Instance);

            // Act
            await metricSink.ReportMetricAsync(metricName, metricDescription, scrapeResult);

            // Assert
            mocks.Factory.Verify(mock => mock.CreateGauge(metricName, metricDescription, It.IsAny <bool>(), It.Is <string[]>(specified => EnsureAllArrayEntriesAreSpecified(specified, scrapeResult.Labels.Keys.ToArray()))), Times.Once());
            mocks.MetricFamily.Verify(mock => mock.WithLabels(It.Is <string[]>(specified => EnsureAllArrayEntriesAreSpecified(specified, scrapeResult.Labels.Values.ToArray()))), Times.Once());
            mocks.Gauge.Verify(mock => mock.Set(expectedMeasuredMetric), Times.Once());
        }