public async Task LogMetric_SinksToApplicationInsights_ResultsInMetricTelemetry()
        {
            // Arrange
            string metricName  = "threshold";
            double metricValue = 0.25;

            using (ILoggerFactory loggerFactory = CreateLoggerFactory())
            {
                ILogger logger = loggerFactory.CreateLogger <ApplicationInsightsSinkTests>();

                Dictionary <string, object> telemetryContext = CreateTestTelemetryContext();

                // Act
                logger.LogMetric(metricName, metricValue, telemetryContext);
            }

            // Assert
            using (ApplicationInsightsDataClient client = CreateApplicationInsightsClient())
            {
                await RetryAssertUntilTelemetryShouldBeAvailableAsync(async() =>
                {
                    var bodySchema = new MetricsPostBodySchema(
                        id: Guid.NewGuid().ToString(),
                        parameters: new MetricsPostBodySchemaParameters("customMetrics/" + metricName));

                    IList <MetricsResultsItem> results = await client.Metrics.GetMultipleAsync(ApplicationId, new List <MetricsPostBodySchema> {
                        bodySchema
                    });
                    Assert.NotEmpty(results);
                });
            }
        }
Exemple #2
0
        public async Task MinimumAzureFunctionsDatabricksProject_WithEmbeddedTimer_ReportsAsMetricPeriodically()
        {
            ApplicationInsightsConfig applicationInsightsConfig = _config.GetApplicationInsightsConfig();
            var parameters = RunParameters.CreateNotebookParams(Enumerable.Empty <KeyValuePair <string, string> >());

            using (var project = AzureFunctionsDatabricksProject.StartNew(_config, _outputWriter))
            {
                using (var client = DatabricksClient.CreateClient(project.DatabricksConfig.BaseUrl, project.DatabricksConfig.SecurityToken))
                {
                    // Act
                    await client.Jobs.RunNow(project.DatabricksConfig.JobId, parameters);
                    await WaitUntilDatabricksJobRunIsCompleted(client, project.DatabricksConfig.JobId);
                }
            }

            // Assert
            using (ApplicationInsightsDataClient client = CreateApplicationInsightsClient(applicationInsightsConfig.ApiKey))
            {
                await RetryAssertUntilTelemetryShouldBeAvailableAsync(async() =>
                {
                    const string past10MinFilter = "PT0.1H";
                    var bodySchema = new MetricsPostBodySchema(
                        id: Guid.NewGuid().ToString(),
                        parameters: new MetricsPostBodySchemaParameters($"customMetrics/{applicationInsightsConfig.MetricName}", timespan: past10MinFilter));

                    IList <MetricsResultsItem> results =
                        await client.Metrics.GetMultipleAsync(applicationInsightsConfig.ApplicationId, new List <MetricsPostBodySchema> {
                        bodySchema
                    });

                    Assert.NotEmpty(results);
                    Assert.All(results, result => Assert.NotNull(result.Body.Value));
                }, timeout : TimeSpan.FromMinutes(2));
            }
        }
Exemple #3
0
        private MetricsPostBodySchema CreateMetricPostBodySchemaForDatabricksTracking()
        {
            const string past10MinFilter = "PT0.1H";
            var          bodySchema      = new MetricsPostBodySchema(
                id: Guid.NewGuid().ToString(),
                parameters: new MetricsPostBodySchemaParameters(
                    $"customMetrics/{ApplicationInsightsConfig.MetricName}",
                    timespan: past10MinFilter));

            return(bodySchema);
        }
        public async Task GetMetrics(MetricsPostBodySchema metric, bool hasInterval, bool isSegmented)
        {
            using (var ctx = MockContext.Start(this.GetType(), $"GetMetrics.{metric.Id}"))
            {
                var metricRequest = new List <MetricsPostBodySchema>
                {
                    metric
                };

                var client  = GetClient(ctx);
                var metrics = await client.Metrics.GetMultipleAsync(DefaultAppId, metricRequest);

                Assert.NotNull(metrics);
                Assert.Equal(1, metrics.Count);

                VerifyMetric(metric, metrics[0], hasInterval, isSegmented);
            }
        }
Exemple #5
0
        public async Task MinimumAzureFunctionsDatabricksProject_WithEmbeddedTimer_ReportsAsMetricPeriodically()
        {
            var parameters = RunParameters.CreateNotebookParams(Enumerable.Empty <KeyValuePair <string, string> >());

            using (var project = AzureFunctionsDatabricksProject.StartNew(_config, Logger))
                using (var client = DatabricksClient.CreateClient(project.AzureFunctionDatabricksConfig.BaseUrl, project.AzureFunctionDatabricksConfig.SecurityToken))
                {
                    JobSettings settings = CreateEmptyJobSettings();
                    long        jobId    = await client.Jobs.Create(settings);

                    try
                    {
                        // Act
                        await client.Jobs.RunNow(jobId, parameters);
                        await WaitUntilDatabricksJobRunIsCompleted(client, jobId);
                    }
                    finally
                    {
                        await client.Jobs.Delete(jobId);
                    }
                }

            // Assert
            await RetryAssertUntilTelemetryShouldBeAvailableAsync(async client =>
            {
                MetricsPostBodySchema bodySchema   = CreateMetricPostBodySchemaForDatabricksTracking();
                IList <MetricsResultsItem> results =
                    await client.Metrics.GetMultipleAsync(ApplicationInsightsConfig.ApplicationId, new List <MetricsPostBodySchema> {
                    bodySchema
                });

                Assert.NotEmpty(results);
                Assert.All(results, result => Assert.NotNull(result.Body.Value));
            },
                                                                  timeout : TimeSpan.FromMinutes(2));
        }
        private void VerifyMetric(MetricsPostBodySchema expected, MetricsResultsItem actual, bool hasInterval = false, bool isSegmented = false)
        {
            Assert.Equal(expected.Id, actual.Id);
            Assert.Equal(200, actual.Status);

            string metricId = null;
            Dictionary <string, float> metricValues = null;

            var segmentInfo = new List <KeyValuePair <string, string> >();

            metricId     = actual.Body.Value.MetricId;
            metricValues = actual.Body.Value.MetricValues;

            MetricsSegmentInfo info = actual.Body.Value.Segments != null ? actual.Body.Value.Segments[0] : null;

            while (info != null)
            {
                metricId     = info.MetricId;
                metricValues = info.MetricValues;

                if (info.SegmentId != null)
                {
                    segmentInfo.Add(new KeyValuePair <string, string>(info.SegmentId, info.SegmentValue));
                }

                info = info.Segments != null ? info.Segments[0] : null;
            }

            // Check that the interval field is set appropriately
            if (hasInterval)
            {
                Assert.NotNull(actual.Body.Value.Interval);
            }
            else
            {
                Assert.Null(actual.Body.Value.Interval);
            }

            // Check that the segmentation fields are set appropriately
            if (!isSegmented)
            {
                Assert.Empty(segmentInfo);
            }
            else
            {
                for (var i = 0; i < expected.Parameters.Segment.Count; i++)
                {
                    var segmentName = expected.Parameters.Segment[i];
                    Assert.Equal(segmentName, segmentInfo[i].Key);
                    Assert.NotNull(segmentInfo[i].Value);
                }
            }

            // Check that the metric fields are set appropriately
            Assert.Equal(expected.Parameters.MetricId, metricId);
            Assert.Equal(expected.Parameters.Aggregation.Count, metricValues.Count);
            foreach (var aggregation in expected.Parameters.Aggregation)
            {
                Assert.True(metricValues.ContainsKey(aggregation));
                Assert.NotEqual(float.MinValue, metricValues[aggregation]);
            }
        }