From 92d0340d9475b36bbc23dca8b45553ddae643e3b Mon Sep 17 00:00:00 2001 From: Varshi Bachu Date: Mon, 13 Jan 2025 15:52:52 -0800 Subject: [PATCH] initial commit --- .../Correlation/TelemetryActivator.cs | 80 +------------- ...rableTaskJobHostConfigurationExtensions.cs | 3 +- test/FunctionsV2/CorrelationEndToEndTests.cs | 103 ------------------ .../PlatformSpecificHelpers.FunctionsV2.cs | 3 +- 4 files changed, 8 insertions(+), 181 deletions(-) diff --git a/src/WebJobs.Extensions.DurableTask/Correlation/TelemetryActivator.cs b/src/WebJobs.Extensions.DurableTask/Correlation/TelemetryActivator.cs index 261c19c41..2a597fee8 100644 --- a/src/WebJobs.Extensions.DurableTask/Correlation/TelemetryActivator.cs +++ b/src/WebJobs.Extensions.DurableTask/Correlation/TelemetryActivator.cs @@ -17,11 +17,10 @@ namespace Microsoft.Azure.WebJobs.Extensions.DurableTask.Correlation /// /// TelemetryActivator initializes Distributed Tracing. This class only works for netstandard2.0. /// - public class TelemetryActivator : ITelemetryActivator, IAsyncDisposable, IDisposable + public class TelemetryActivator : ITelemetryModule, IAsyncDisposable, IDisposable { private readonly DurableTaskOptions options; private readonly INameResolver nameResolver; - private EndToEndTraceHelper endToEndTraceHelper; private TelemetryClient telemetryClient; private IAsyncDisposable telemetryModule; @@ -57,7 +56,7 @@ public void Dispose() /// /// Initialize is initialize the telemetry client. /// - public void Initialize(ILogger logger) + public void Initialize(TelemetryConfiguration configuration) { if (this.options.Tracing.DistributedTracingEnabled) { @@ -66,13 +65,10 @@ public void Initialize(ILogger logger) return; } - this.endToEndTraceHelper = new EndToEndTraceHelper(logger, this.options.Tracing.TraceReplayEvents); - TelemetryConfiguration telemetryConfiguration = this.SetupTelemetryConfiguration(); - if (this.options.Tracing.Version == Options.DurableDistributedTracingVersion.V2) { DurableTelemetryModule module = new DurableTelemetryModule(); - module.Initialize(telemetryConfiguration); + module.Initialize(configuration); this.telemetryModule = module; } else @@ -80,7 +76,7 @@ public void Initialize(ILogger logger) this.SetUpV1DistributedTracing(); if (CorrelationSettings.Current.EnableDistributedTracing) { - this.SetUpTelemetryClient(telemetryConfiguration); + this.SetUpTelemetryClient(configuration); if (CorrelationSettings.Current.EnableDistributedTracing) { @@ -128,75 +124,7 @@ private void SetUpTelemetryCallbacks() private void SetUpTelemetryClient(TelemetryConfiguration telemetryConfiguration) { - this.endToEndTraceHelper.ExtensionInformationalEvent( - hubName: this.options.HubName, - functionName: string.Empty, - instanceId: string.Empty, - message: "Setting up the telemetry client...", - writeToUserLogs: true); - this.telemetryClient = new TelemetryClient(telemetryConfiguration); } - - private TelemetryConfiguration SetupTelemetryConfiguration() - { - TelemetryConfiguration config = TelemetryConfiguration.CreateDefault(); - if (this.OnSend != null) - { - config.TelemetryChannel = new NoOpTelemetryChannel { OnSend = this.OnSend }; - } - - string resolvedInstrumentationKey = this.nameResolver.Resolve("APPINSIGHTS_INSTRUMENTATIONKEY"); - string resolvedConnectionString = this.nameResolver.Resolve("APPLICATIONINSIGHTS_CONNECTION_STRING"); - - bool instrumentationKeyProvided = !string.IsNullOrEmpty(resolvedInstrumentationKey); - bool connectionStringProvided = !string.IsNullOrEmpty(resolvedConnectionString); - - if (instrumentationKeyProvided && connectionStringProvided) - { - this.endToEndTraceHelper.ExtensionWarningEvent( - hubName: this.options.HubName, - functionName: string.Empty, - instanceId: string.Empty, - message: "Both 'APPINSIGHTS_INSTRUMENTATIONKEY' and 'APPLICATIONINSIGHTS_CONNECTION_STRING' are defined in the current environment variables. Please specify one. We recommend specifying 'APPLICATIONINSIGHTS_CONNECTION_STRING'."); - } - - if (!instrumentationKeyProvided && !connectionStringProvided) - { - this.endToEndTraceHelper.ExtensionWarningEvent( - hubName: this.options.HubName, - functionName: string.Empty, - instanceId: string.Empty, - message: "'APPINSIGHTS_INSTRUMENTATIONKEY' or 'APPLICATIONINSIGHTS_CONNECTION_STRING' were not defined in the current environment variables, but distributed tracing is enabled. Please specify one. We recommend specifying 'APPLICATIONINSIGHTS_CONNECTION_STRING'."); - } - - if (instrumentationKeyProvided) - { - this.endToEndTraceHelper.ExtensionInformationalEvent( - hubName: this.options.HubName, - functionName: string.Empty, - instanceId: string.Empty, - message: "Reading APPINSIGHTS_INSTRUMENTATIONKEY...", - writeToUserLogs: true); - -#pragma warning disable CS0618 // Type or member is obsolete - config.InstrumentationKey = resolvedInstrumentationKey; -#pragma warning restore CS0618 // Type or member is obsolete - } - - if (connectionStringProvided) - { - this.endToEndTraceHelper.ExtensionInformationalEvent( - hubName: this.options.HubName, - functionName: string.Empty, - instanceId: string.Empty, - message: "Reading APPLICATIONINSIGHTS_CONNECTION_STRING...", - writeToUserLogs: true); - - config.ConnectionString = resolvedConnectionString; - } - - return config; - } } } \ No newline at end of file diff --git a/src/WebJobs.Extensions.DurableTask/DurableTaskJobHostConfigurationExtensions.cs b/src/WebJobs.Extensions.DurableTask/DurableTaskJobHostConfigurationExtensions.cs index 84d0f78ad..5a8e0a1b6 100644 --- a/src/WebJobs.Extensions.DurableTask/DurableTaskJobHostConfigurationExtensions.cs +++ b/src/WebJobs.Extensions.DurableTask/DurableTaskJobHostConfigurationExtensions.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Microsoft.ApplicationInsights.Extensibility; using Microsoft.Azure.WebJobs.Extensions.DurableTask.ContextImplementations; using Microsoft.Azure.WebJobs.Extensions.DurableTask.Correlation; using Microsoft.Azure.WebJobs.Extensions.DurableTask.Options; @@ -88,7 +89,7 @@ public static IWebJobsBuilder AddDurableTask(this IWebJobsBuilder builder) serviceCollection.TryAddSingleton(); serviceCollection.TryAddSingleton(); serviceCollection.TryAddSingleton(); - serviceCollection.AddSingleton(); + serviceCollection.AddSingleton(); serviceCollection.TryAddSingleton(); #pragma warning disable CS0612, CS0618 // Type or member is obsolete serviceCollection.TryAddSingleton(); diff --git a/test/FunctionsV2/CorrelationEndToEndTests.cs b/test/FunctionsV2/CorrelationEndToEndTests.cs index 6eb2e9090..a5404ba31 100644 --- a/test/FunctionsV2/CorrelationEndToEndTests.cs +++ b/test/FunctionsV2/CorrelationEndToEndTests.cs @@ -219,109 +219,6 @@ internal async Task, List>> return new Tuple, List>(result.CorrelationSort(), exceptionTelemetryList); } - /* - * End to end test that checks if a warning is logged when distributed tracing is - * enabled, but APPINSIGHTS_INSTRUMENTATIONKEY isn't set. The test also checks - * that the warning isn't logged when the environment variable is set. - */ - [Theory] - [Trait("Category", PlatformSpecificHelpers.TestCategory)] - [InlineData(false, false, false)] - [InlineData(false, false, true)] - [InlineData(true, false, false)] - [InlineData(true, false, true)] - [InlineData(false, true, false)] - [InlineData(false, true, true)] - [InlineData(true, true, false)] - [InlineData(true, true, true)] - public void TelemetryClientSetup_AppInsights_Warnings(bool instrumentationKeyIsSet, bool connStringIsSet, bool extendedSessions) - { - TraceOptions traceOptions = new TraceOptions() - { - DistributedTracingEnabled = true, - DistributedTracingProtocol = "W3CTraceContext", - }; - - DurableTaskOptions options = new DurableTaskOptions(); - options.Tracing = traceOptions; - - string instKeyEnvVarName = "APPINSIGHTS_INSTRUMENTATIONKEY"; - string connStringEnvVarName = "APPLICATIONINSIGHTS_CONNECTION_STRING"; - string environmentVariableValue = "test value"; - string connStringValue = "InstrumentationKey=xxxx;IngestionEndpoint =https://xxxx.applicationinsights.azure.com/;LiveEndpoint=https://xxxx.livediagnostics.monitor.azure.com/"; - - var mockNameResolver = GetNameResolverMock(new[] { (instKeyEnvVarName, string.Empty), (connStringEnvVarName, string.Empty) }); - - if (instrumentationKeyIsSet && connStringIsSet) - { - mockNameResolver = GetNameResolverMock(new[] { (instKeyEnvVarName, environmentVariableValue), (connStringEnvVarName, connStringValue) }); - } - else if (instrumentationKeyIsSet) - { - mockNameResolver = GetNameResolverMock(new[] { (instKeyEnvVarName, environmentVariableValue), (connStringEnvVarName, string.Empty) }); - } - else if (connStringIsSet) - { - mockNameResolver = GetNameResolverMock(new[] { (instKeyEnvVarName, string.Empty), (connStringEnvVarName, connStringValue) }); - } - - using (var host = TestHelpers.GetJobHost( - this.loggerProvider, - "SingleOrchestration", - extendedSessions, - nameResolver: mockNameResolver.Object, - options: options)) - { - string bothSettingsSetWarningMessage = "Both 'APPINSIGHTS_INSTRUMENTATIONKEY' and 'APPLICATIONINSIGHTS_CONNECTION_STRING' are defined in the current environment variables. Please specify one. We recommend specifying 'APPLICATIONINSIGHTS_CONNECTION_STRING'."; - var bothSettingsSetWarningLogMessage = this.loggerProvider.GetAllLogMessages().Where(l => l.FormattedMessage.StartsWith(bothSettingsSetWarningMessage)); - - string neitherSettingsSetWarningMessage = "'APPINSIGHTS_INSTRUMENTATIONKEY' or 'APPLICATIONINSIGHTS_CONNECTION_STRING' were not defined in the current environment variables, but distributed tracing is enabled. Please specify one. We recommend specifying 'APPLICATIONINSIGHTS_CONNECTION_STRING'."; - var neitherSettingsSetWarningLogMessage = this.loggerProvider.GetAllLogMessages().Where(l => l.FormattedMessage.StartsWith(neitherSettingsSetWarningMessage)); - - string settingUpTelemetryClientMessage = "Setting up the telemetry client..."; - var settingUpTelemetryClientLogMessage = this.loggerProvider.GetAllLogMessages().Where(l => l.FormattedMessage.StartsWith(settingUpTelemetryClientMessage)); - - string readingInstrumentationKeyMessage = "Reading APPINSIGHTS_INSTRUMENTATIONKEY..."; - var readingInstrumentationKeyLogMessage = this.loggerProvider.GetAllLogMessages().Where(l => l.FormattedMessage.StartsWith(readingInstrumentationKeyMessage)); - - string readingConnStringMessage = "Reading APPLICATIONINSIGHTS_CONNECTION_STRING..."; - var readingConnStringLogMessage = this.loggerProvider.GetAllLogMessages().Where(l => l.FormattedMessage.StartsWith(readingConnStringMessage)); - - if (instrumentationKeyIsSet && connStringIsSet) - { - Assert.Single(bothSettingsSetWarningLogMessage); - Assert.Empty(neitherSettingsSetWarningLogMessage); - Assert.Single(settingUpTelemetryClientLogMessage); - Assert.Single(readingInstrumentationKeyLogMessage); - Assert.Single(readingConnStringLogMessage); - } - else if (instrumentationKeyIsSet && !connStringIsSet) - { - Assert.Empty(bothSettingsSetWarningLogMessage); - Assert.Empty(neitherSettingsSetWarningLogMessage); - Assert.Single(settingUpTelemetryClientLogMessage); - Assert.Single(readingInstrumentationKeyLogMessage); - Assert.Empty(readingConnStringLogMessage); - } - else if (!instrumentationKeyIsSet && connStringIsSet) - { - Assert.Empty(bothSettingsSetWarningLogMessage); - Assert.Empty(neitherSettingsSetWarningLogMessage); - Assert.Single(settingUpTelemetryClientLogMessage); - Assert.Empty(readingInstrumentationKeyLogMessage); - Assert.Single(readingConnStringLogMessage); - } - else - { - Assert.Empty(bothSettingsSetWarningLogMessage); - Assert.Single(neitherSettingsSetWarningLogMessage); - Assert.Single(settingUpTelemetryClientLogMessage); - Assert.Empty(readingInstrumentationKeyLogMessage); - Assert.Empty(readingConnStringLogMessage); - } - } - } - private static Mock GetNameResolverMock((string Key, string Value)[] settings) { var mock = new Mock(); diff --git a/test/FunctionsV2/PlatformSpecificHelpers.FunctionsV2.cs b/test/FunctionsV2/PlatformSpecificHelpers.FunctionsV2.cs index 7436f47e5..48b6c4167 100644 --- a/test/FunctionsV2/PlatformSpecificHelpers.FunctionsV2.cs +++ b/test/FunctionsV2/PlatformSpecificHelpers.FunctionsV2.cs @@ -7,6 +7,7 @@ using System.Reflection; using System.Threading.Tasks; using Microsoft.ApplicationInsights.Channel; +using Microsoft.ApplicationInsights.Extensibility; using Microsoft.Azure.WebJobs.Extensions.DurableTask.Correlation; using Microsoft.Azure.WebJobs.Extensions.DurableTask.Options; using Microsoft.Azure.WebJobs.Extensions.DurableTask.Storage; @@ -89,7 +90,7 @@ public static ITestHost CreateJobHost( if (onSend != null) { - serviceCollection.AddSingleton(serviceProvider => + serviceCollection.AddSingleton(serviceProvider => { var durableTaskOptions = serviceProvider.GetService>(); var nameResolver = serviceProvider.GetService();