From cd1fcafed6473479d6ea3f6e102bbb968560f1af Mon Sep 17 00:00:00 2001 From: Liudmila Molkova Date: Mon, 18 Jul 2022 13:30:01 -0700 Subject: [PATCH 1/3] Continue on AppInsights setup errors to simplify local development --- .../todo/api/csharp-cosmos-sql/Program.cs | 3 +- templates/todo/api/csharp-sql/Program.cs | 3 +- templates/todo/api/csharp/Program.cs | 4 +- .../todo/api/js/src/config/observability.ts | 53 ++++++++++--------- .../src/services/telemetryService.ts | 24 +++++---- 5 files changed, 46 insertions(+), 41 deletions(-) diff --git a/templates/todo/api/csharp-cosmos-sql/Program.cs b/templates/todo/api/csharp-cosmos-sql/Program.cs index 635290afe53..3b96253c2e1 100644 --- a/templates/todo/api/csharp-cosmos-sql/Program.cs +++ b/templates/todo/api/csharp-cosmos-sql/Program.cs @@ -15,8 +15,7 @@ } })); builder.Services.AddControllers(); -var options = new ApplicationInsightsServiceOptions { ConnectionString = builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"] }; -builder.Services.AddApplicationInsightsTelemetry(options); +builder.Services.AddApplicationInsightsTelemetry(builder.Configuration); var app = builder.Build(); diff --git a/templates/todo/api/csharp-sql/Program.cs b/templates/todo/api/csharp-sql/Program.cs index ac5e8f61ea2..9ec8690c079 100644 --- a/templates/todo/api/csharp-sql/Program.cs +++ b/templates/todo/api/csharp-sql/Program.cs @@ -11,8 +11,7 @@ }); builder.Services.AddControllers(); -var options = new ApplicationInsightsServiceOptions { ConnectionString = builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"] }; -builder.Services.AddApplicationInsightsTelemetry(options); +builder.Services.AddApplicationInsightsTelemetry(builder.Configuration); var app = builder.Build(); diff --git a/templates/todo/api/csharp/Program.cs b/templates/todo/api/csharp/Program.cs index 3b93e238a44..296ca33d222 100644 --- a/templates/todo/api/csharp/Program.cs +++ b/templates/todo/api/csharp/Program.cs @@ -9,9 +9,7 @@ builder.Services.AddSingleton(); builder.Services.AddSingleton(_ => new MongoClient(builder.Configuration[builder.Configuration["AZURE_COSMOS_CONNECTION_STRING_KEY"]])); builder.Services.AddControllers(); - -var options = new ApplicationInsightsServiceOptions { ConnectionString = builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"] }; -builder.Services.AddApplicationInsightsTelemetry(options); +builder.Services.AddApplicationInsightsTelemetry(builder.Configuration); var app = builder.Build(); diff --git a/templates/todo/api/js/src/config/observability.ts b/templates/todo/api/js/src/config/observability.ts index 8c1e48e155f..f7e4fb3680f 100644 --- a/templates/todo/api/js/src/config/observability.ts +++ b/templates/todo/api/js/src/config/observability.ts @@ -23,36 +23,41 @@ export const logger = winston.createLogger({ }); export const observability = (config: ObservabilityConfig) => { - applicationInsights - .setup(config.connectionString) - .setAutoDependencyCorrelation(true) - .setAutoCollectRequests(true) - .setAutoCollectPerformance(true, true) - .setAutoCollectExceptions(true) - .setAutoCollectDependencies(true) - .setAutoCollectConsole(true) - .setUseDiskRetryCaching(true) - .setSendLiveMetrics(true) - .setDistributedTracingMode(applicationInsights.DistributedTracingModes.AI_AND_W3C); - - applicationInsights.defaultClient.context.tags[applicationInsights.defaultClient.context.keys.cloudRole] = config.roleName; - applicationInsights.defaultClient.setAutoPopulateAzureProperties(true); - applicationInsights.start(); - // Append App Insights to the winston logger logger.defaultMeta = { app: config.roleName }; - const applicationInsightsTransport = new ApplicationInsightsTransport({ - client: applicationInsights.defaultClient, - level: LogLevel.Information, - handleExceptions: true, // Handles node unhandled exceptions - handleRejections: true, // Handles node promise rejections - }); + try { + applicationInsights + .setup(config.connectionString) + .setAutoDependencyCorrelation(true) + .setAutoCollectRequests(true) + .setAutoCollectPerformance(true, true) + .setAutoCollectExceptions(true) + .setAutoCollectDependencies(true) + .setAutoCollectConsole(true) + .setUseDiskRetryCaching(true) + .setSendLiveMetrics(true) + .setDistributedTracingMode(applicationInsights.DistributedTracingModes.AI_AND_W3C); + + applicationInsights.defaultClient.context.tags[applicationInsights.defaultClient.context.keys.cloudRole] = config.roleName; + applicationInsights.defaultClient.setAutoPopulateAzureProperties(true); + applicationInsights.start(); + + + const applicationInsightsTransport = new ApplicationInsightsTransport({ + client: applicationInsights.defaultClient, + level: LogLevel.Information, + handleExceptions: true, // Handles node unhandled exceptions + handleRejections: true, // Handles node promise rejections + }); - logger.add(applicationInsightsTransport); - logger.info("Added ApplicationInsights logger transport"); + logger.add(applicationInsightsTransport); + logger.info("Added ApplicationInsights logger transport"); + } catch (err) { + logger.error(`ApplicationInsights setup failed, ensure environment variable 'APPLICATIONINSIGHTS_CONNECTION_STRING' has been set. Error: ${err}`); + } }; if (process.env.NODE_ENV !== "production") { diff --git a/templates/todo/web/react-fluent/src/services/telemetryService.ts b/templates/todo/web/react-fluent/src/services/telemetryService.ts index 0c13703b0a5..63a7afd1080 100644 --- a/templates/todo/web/react-fluent/src/services/telemetryService.ts +++ b/templates/todo/web/react-fluent/src/services/telemetryService.ts @@ -27,16 +27,20 @@ export const getApplicationInsights = (): ApplicationInsights => { } applicationInsights = new ApplicationInsights(ApplicationInsightsConfig); - applicationInsights.loadAppInsights(); - - applicationInsights.addTelemetryInitializer((telemetry: ITelemetryItem) => { - if (!telemetry) { - return; - } - if (telemetry.tags) { - telemetry.tags['ai.cloud.role'] = "webui"; - } - }); + try { + applicationInsights.loadAppInsights(); + applicationInsights.addTelemetryInitializer((telemetry: ITelemetryItem) => { + if (!telemetry) { + return; + } + if (telemetry.tags) { + telemetry.tags['ai.cloud.role'] = "webui"; + } + }); + } catch(err) { + // TODO - proper logging for web + console.error("ApplicationInsights setup failed, ensure environment variable 'APPLICATIONINSIGHTS_CONNECTION_STRING' has been set.", err); + } return applicationInsights; } From fa4bbe9fa991d5929c9ac606e07d2fe39ed808cb Mon Sep 17 00:00:00 2001 From: Liudmila Molkova Date: Mon, 18 Jul 2022 17:37:20 -0700 Subject: [PATCH 2/3] review: REACT_APP_APPLICATIONINSIGHTS_CONNECTION_STRING in web --- .../todo/web/react-fluent/src/services/telemetryService.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/todo/web/react-fluent/src/services/telemetryService.ts b/templates/todo/web/react-fluent/src/services/telemetryService.ts index 63a7afd1080..97a3b2d0578 100644 --- a/templates/todo/web/react-fluent/src/services/telemetryService.ts +++ b/templates/todo/web/react-fluent/src/services/telemetryService.ts @@ -39,7 +39,7 @@ export const getApplicationInsights = (): ApplicationInsights => { }); } catch(err) { // TODO - proper logging for web - console.error("ApplicationInsights setup failed, ensure environment variable 'APPLICATIONINSIGHTS_CONNECTION_STRING' has been set.", err); + console.error("ApplicationInsights setup failed, ensure environment variable 'REACT_APP_APPLICATIONINSIGHTS_CONNECTION_STRING' has been set.", err); } return applicationInsights; From 2872239bb646f1c8029ca83ba88e07524efc7bed Mon Sep 17 00:00:00 2001 From: Liudmila Molkova Date: Tue, 19 Jul 2022 15:16:20 -0700 Subject: [PATCH 3/3] nits --- templates/todo/api/js/src/config/observability.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/templates/todo/api/js/src/config/observability.ts b/templates/todo/api/js/src/config/observability.ts index f7e4fb3680f..336c3797432 100644 --- a/templates/todo/api/js/src/config/observability.ts +++ b/templates/todo/api/js/src/config/observability.ts @@ -45,7 +45,6 @@ export const observability = (config: ObservabilityConfig) => { applicationInsights.defaultClient.setAutoPopulateAzureProperties(true); applicationInsights.start(); - const applicationInsightsTransport = new ApplicationInsightsTransport({ client: applicationInsights.defaultClient, level: LogLevel.Information,