Ejemplo n.º 1
0
        public async Task ReportMetricAsync_GetsValidInputWithMetricLabelWithSameKeyAsScrapeResult_SuccessfullyWritesMetricWithLabelValueFromScrapeResult()
        {
            // Arrange
            var metricName             = _bogus.Name.FirstName();
            var metricDescription      = _bogus.Lorem.Sentence();
            var metricValue            = _bogus.Random.Double();
            var labelName              = _bogus.Name.FirstName();
            var scrapeResultLabelValue = _bogus.Name.FirstName();
            var metricConfigLabelValue = _bogus.Name.FirstName();
            var expectedLabelName      = labelName.ToLower();
            var configuredLabels       = new Dictionary <string, string>
            {
                { labelName.ToUpper(), metricConfigLabelValue }
            };
            var scrapeResult = ScrapeResultGenerator.Generate(metricValue);

            scrapeResult.Labels.Add(labelName, scrapeResultLabelValue);
            var metricsDeclarationProvider = CreateMetricsDeclarationProvider(metricName, configuredLabels);
            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(expectedLabelName) && EnsureAllArrayEntriesAreSpecified(specified, scrapeResult.Labels.Keys.ToArray()))), Times.Once());
            mocks.MetricFamily.Verify(mock => mock.WithLabels(It.Is <string[]>(specified => specified.Contains(scrapeResultLabelValue) && specified.Contains(metricConfigLabelValue) == false && EnsureAllArrayEntriesAreSpecified(specified, scrapeResult.Labels.Values.ToArray()))), Times.Once());
            mocks.Gauge.Verify(mock => mock.Set(metricValue), Times.Once());
        }
Ejemplo n.º 2
0
        public async Task ReportMetricAsync_WriteToNoSinks_Succeeds()
        {
            // Arrange
            var metricName        = BogusGenerator.Name.FirstName();
            var metricDescription = BogusGenerator.Lorem.Sentence();
            var metricValue       = BogusGenerator.Random.Double();
            var scrapeResult      = ScrapeResultGenerator.Generate(metricValue);
            var metricSinkWriter  = new MetricSinkWriter(new List <IMetricSink>(), NullLogger <MetricSinkWriter> .Instance);

            // Act & Assert
            await metricSinkWriter.ReportMetricAsync(metricName, metricDescription, scrapeResult);
        }
Ejemplo n.º 3
0
        public async Task ReportMetricAsync_InputDoesNotContainMetricName_ThrowsException(string metricName)
        {
            // Arrange
            var metricDescription   = BogusGenerator.Lorem.Sentence();
            var metricValue         = BogusGenerator.Random.Double();
            var scrapeResult        = ScrapeResultGenerator.Generate(metricValue);
            var statsDPublisherMock = new Mock <IStatsDPublisher>();
            var metricSink          = new StatsdMetricSink(statsDPublisherMock.Object, NullLogger <StatsdMetricSink> .Instance);

            // Act & Assert
            // ReSharper disable once ExpressionIsAlwaysNull
            await Assert.ThrowsAsync <ArgumentException>(() => metricSink.ReportMetricAsync(metricName, metricDescription, scrapeResult));
        }
Ejemplo n.º 4
0
        public async Task ReportMetricAsync_WriteToOneSinkWithoutMetricDescription_Succeeds(string metricDescription)
        {
            // Arrange
            var metricName       = BogusGenerator.Name.FirstName();
            var metricValue      = BogusGenerator.Random.Double();
            var scrapeResult     = ScrapeResultGenerator.Generate(metricValue);
            var metricSink       = new Mock <IMetricSink>();
            var metricSinkWriter = new MetricSinkWriter(new List <IMetricSink> {
                metricSink.Object
            }, NullLogger <MetricSinkWriter> .Instance);

            // Act & Assert
            await metricSinkWriter.ReportMetricAsync(metricName, metricDescription, scrapeResult);
        }
Ejemplo n.º 5
0
        public async Task ReportMetricAsync_InputDoesNotContainMetricName_ThrowsException(string metricName)
        {
            // Arrange
            var metricDescription             = BogusGenerator.Lorem.Sentence();
            var metricValue                   = BogusGenerator.Random.Double();
            var scrapeResult                  = ScrapeResultGenerator.Generate(metricValue);
            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 Assert.ThrowsAsync <ArgumentException>(() => metricSink.ReportMetricAsync(metricName, metricDescription, scrapeResult));
        }
Ejemplo n.º 6
0
        public async Task ReportMetricAsync_WriteToOneSinkWithoutMetricName_ThrowsException(string metricName)
        {
            // Arrange
            var metricDescription = BogusGenerator.Lorem.Sentence();
            var metricValue       = BogusGenerator.Random.Double();
            var scrapeResult      = ScrapeResultGenerator.Generate(metricValue);
            var metricSink        = new Mock <IMetricSink>();
            var metricSinkWriter  = new MetricSinkWriter(new List <IMetricSink> {
                metricSink.Object
            }, NullLogger <MetricSinkWriter> .Instance);

            // Act & Assert
            // ReSharper disable once ExpressionIsAlwaysNull
            await Assert.ThrowsAsync <ArgumentException>(() => metricSinkWriter.ReportMetricAsync(metricName, metricDescription, scrapeResult));
        }
Ejemplo n.º 7
0
        public async Task ReportMetricAsync_InputDoesNotContainMetricDescription_Succeeds(string metricDescription)
        {
            // Arrange
            var metricName   = _bogus.Name.FirstName();
            var metricValue  = _bogus.Random.Double();
            var scrapeResult = ScrapeResultGenerator.Generate(metricValue);
            var metricsDeclarationProvider = CreateMetricsDeclarationProvider(metricName);
            var prometheusConfiguration    = CreatePrometheusConfiguration();
            var metricFactoryMock          = CreatePrometheusMetricFactoryMock();
            var metricSink = new PrometheusScrapingEndpointMetricSink(metricFactoryMock.Factory.Object, metricsDeclarationProvider, prometheusConfiguration, NullLogger <PrometheusScrapingEndpointMetricSink> .Instance);

            // Act & Assert
            // ReSharper disable once ExpressionIsAlwaysNull
            await metricSink.ReportMetricAsync(metricName, metricDescription, scrapeResult);
        }
Ejemplo n.º 8
0
        public async Task ReportMetricAsync_InputDoesNotContainMetricName_ThrowsException(string metricName)
        {
            // Arrange
            var metricDescription          = BogusGenerator.Lorem.Sentence();
            var metricValue                = BogusGenerator.Random.Double();
            var scrapeResult               = ScrapeResultGenerator.Generate(metricValue);
            var metricsDeclarationProvider = CreateMetricsDeclarationProvider(metricName);
            var prometheusConfiguration    = CreatePrometheusConfiguration();
            var metricFactoryMock          = CreatePrometheusMetricFactoryMock();
            var metricSink = new PrometheusScrapingEndpointMetricSink(metricFactoryMock.Factory.Object, metricsDeclarationProvider, prometheusConfiguration, NullLogger <PrometheusScrapingEndpointMetricSink> .Instance);

            // Act & Assert
            // ReSharper disable once ExpressionIsAlwaysNull
            await Assert.ThrowsAsync <ArgumentException>(() => metricSink.ReportMetricAsync(metricName, metricDescription, scrapeResult));
        }
Ejemplo n.º 9
0
        public async Task ReportMetricAsync_WriteToOneSink_Succeeds()
        {
            // Arrange
            var metricName        = BogusGenerator.Name.FirstName();
            var metricDescription = BogusGenerator.Lorem.Sentence();
            var metricValue       = BogusGenerator.Random.Double();
            var scrapeResult      = ScrapeResultGenerator.Generate(metricValue);
            var metricSink        = new Mock <IMetricSink>();
            var metricSinkWriter  = new MetricSinkWriter(new List <IMetricSink> {
                metricSink.Object
            }, NullLogger <MetricSinkWriter> .Instance);

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

            // Assert
            metricSink.Verify(mock => mock.ReportMetricAsync(metricName, metricDescription, scrapeResult), Times.Once());
        }
Ejemplo n.º 10
0
        public async Task ReportMetricAsync_WriteToStatsDSink_Succeeds()
        {
            // Arrange
            var metricName          = _bogus.Name.FirstName();
            var metricDescription   = _bogus.Lorem.Sentence();
            var metricValue         = _bogus.Random.Double();
            var scrapeResult        = ScrapeResultGenerator.Generate(metricValue);
            var statsDPublisherMock = new Mock <IStatsDPublisher>();
            var statsdMetricSink    = new StatsdMetricSink(statsDPublisherMock.Object, NullLogger <StatsdMetricSink> .Instance);
            var metricSinkWriter    = new MetricSinkWriter(new List <IMetricSink> {
                statsdMetricSink
            }, NullLogger <MetricSinkWriter> .Instance);

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

            // Assert
            statsDPublisherMock.Verify(mock => mock.Gauge(metricValue, metricName), Times.Once());
        }
Ejemplo n.º 11
0
        public async Task ReportMetricAsync_GetsValidInputWithMetricValue_SuccessfullyWritesMetric()
        {
            // Arrange
            var metricName                 = _bogus.Name.FirstName();
            var metricDescription          = _bogus.Lorem.Sentence();
            var metricValue                = _bogus.Random.Double();
            var scrapeResult               = ScrapeResultGenerator.Generate(metricValue);
            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.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(metricValue), Times.Once());
        }