From 095fae1a0ff071b247150ea0e5f75b425b0d1004 Mon Sep 17 00:00:00 2001 From: Rohit Ranjan <90008725+RohitRanjanMS@users.noreply.github.com> Date: Mon, 4 Dec 2023 12:46:39 -0800 Subject: [PATCH] Adding support for pre-aggregated (standard) metrics. (#3047) Adding support for pre-aggregated (standard) metrics. --- .../ApplicationInsightsLoggerOptions.cs | 9 ++++++++- ...ApplicationInsightsServiceCollectionExtensions.cs | 7 +++++++ .../Loggers/ApplicationInsightsConfigurationTests.cs | 12 +++++++----- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.Azure.WebJobs.Logging.ApplicationInsights/ApplicationInsightsLoggerOptions.cs b/src/Microsoft.Azure.WebJobs.Logging.ApplicationInsights/ApplicationInsightsLoggerOptions.cs index 49903c32f..a23bb2337 100644 --- a/src/Microsoft.Azure.WebJobs.Logging.ApplicationInsights/ApplicationInsightsLoggerOptions.cs +++ b/src/Microsoft.Azure.WebJobs.Logging.ApplicationInsights/ApplicationInsightsLoggerOptions.cs @@ -142,7 +142,13 @@ public TimeSpan QuickPulseInitializationDelay /// Disabled by default. /// public EventLevel? DiagnosticsEventListenerLogLevel { get; set; } - + + /// + /// Gets or sets the flag that enables standard metrics collection in ApplicationInsights. + /// Disabled by default. + /// + public bool EnableAutocollectedMetricsExtractor { get; set; } = false; + public string Format() { JObject sampling = null; @@ -232,6 +238,7 @@ public string Format() { nameof(DependencyTrackingOptions), dependencyTrackingOptions }, { nameof(TokenCredentialOptions), tokenCredentialOptions }, { nameof(DiagnosticsEventListenerLogLevel), DiagnosticsEventListenerLogLevel?.ToString() }, + { nameof(EnableAutocollectedMetricsExtractor), EnableAutocollectedMetricsExtractor }, }; return options.ToString(Formatting.Indented); diff --git a/src/Microsoft.Azure.WebJobs.Logging.ApplicationInsights/Extensions/ApplicationInsightsServiceCollectionExtensions.cs b/src/Microsoft.Azure.WebJobs.Logging.ApplicationInsights/Extensions/ApplicationInsightsServiceCollectionExtensions.cs index 8888e29d0..832d04503 100644 --- a/src/Microsoft.Azure.WebJobs.Logging.ApplicationInsights/Extensions/ApplicationInsightsServiceCollectionExtensions.cs +++ b/src/Microsoft.Azure.WebJobs.Logging.ApplicationInsights/Extensions/ApplicationInsightsServiceCollectionExtensions.cs @@ -358,6 +358,13 @@ private static void SetupTelemetryConfiguration( } } + // Metrics extractor must be added before filtering and adaptive sampling telemetry processor to account for all the data. + if (options.EnableAutocollectedMetricsExtractor) + { + configuration.TelemetryProcessorChainBuilder + .Use((next) => new AutocollectedMetricsExtractor(next)); + } + QuickPulseTelemetryProcessor quickPulseProcessor = null; configuration.TelemetryProcessorChainBuilder .Use((next) => new OperationFilteringTelemetryProcessor(next)); diff --git a/test/Microsoft.Azure.WebJobs.Host.UnitTests/Loggers/ApplicationInsightsConfigurationTests.cs b/test/Microsoft.Azure.WebJobs.Host.UnitTests/Loggers/ApplicationInsightsConfigurationTests.cs index 6cc971c19..db9993a2f 100644 --- a/test/Microsoft.Azure.WebJobs.Host.UnitTests/Loggers/ApplicationInsightsConfigurationTests.cs +++ b/test/Microsoft.Azure.WebJobs.Host.UnitTests/Loggers/ApplicationInsightsConfigurationTests.cs @@ -92,7 +92,7 @@ public void DependencyInjectionConfiguration_Configures_With_InstrumentationKey( Assert.Single(modules.OfType()); Assert.Single(modules.OfType()); Assert.Single(modules.OfType()); - // SelfDiagnosticsTelemetryModule is dabled by default and instead NullTelemetryModule is added + // SelfDiagnosticsTelemetryModule is disabled by default and instead NullTelemetryModule is added Assert.Single(modules.OfType()); var dependencyModule = modules.OfType().Single(); @@ -135,6 +135,7 @@ public void DependencyInjectionConfiguration_Configures_With_ConnectionString() { o.ConnectionString = "InstrumentationKey=somekey;EndpointSuffix=applicationinsights.us"; o.DiagnosticsEventListenerLogLevel = EventLevel.Verbose; + o.EnableAutocollectedMetricsExtractor = true; }); }); @@ -195,10 +196,11 @@ public void DependencyInjectionConfiguration_Configures_With_ConnectionString() Assert.NotNull(providers[0]); // Verify Processors - Assert.Equal(4, config.TelemetryProcessors.Count); - Assert.IsType(config.TelemetryProcessors[0]); - Assert.IsType(config.TelemetryProcessors[1]); - Assert.IsType(config.TelemetryProcessors[2]); + Assert.Equal(5, config.TelemetryProcessors.Count); + Assert.IsType(config.TelemetryProcessors[0]); + Assert.IsType(config.TelemetryProcessors[1]); + Assert.IsType(config.TelemetryProcessors[2]); + Assert.IsType(config.TelemetryProcessors[3]); Assert.Empty(config.TelemetryProcessors.OfType()); // Verify ApplicationIdProvider