Skip to content

Commit 18376fd

Browse files
committed
[AppInsights][AaronMax] updating ikey refs to connection strings
1 parent c65fa5e commit 18376fd

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

articles/azure-monitor/app/worker-service.md

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ ms.date: 05/11/2020
99

1010
# Application Insights for Worker Service applications (non-HTTP applications)
1111

12-
[Application Insights SDK for Worker Service](https://www.nuget.org/packages/Microsoft.ApplicationInsights.WorkerService) is a new SDK which is best suited for non-HTTP workloads like messaging, background tasks, console applications etc. These types of applications don't have the notion of an incoming HTTP request like a traditional ASP.NET/ASP.NET Core Web Application, and hence using Application Insights packages for [ASP.NET](asp-net.md) or [ASP.NET Core](asp-net-core.md) applications is not supported.
12+
[Application Insights SDK for Worker Service](https://www.nuget.org/packages/Microsoft.ApplicationInsights.WorkerService) is a new SDK, which is best suited for non-HTTP workloads like messaging, background tasks, console applications etc. These types of applications don't have the notion of an incoming HTTP request like a traditional ASP.NET/ASP.NET Core Web Application, and hence using Application Insights packages for [ASP.NET](asp-net.md) or [ASP.NET Core](asp-net-core.md) applications isn't supported.
1313

14-
The new SDK does not do any telemetry collection by itself. Instead, it brings in other well known Application Insights auto collectors like [DependencyCollector](https://www.nuget.org/packages/Microsoft.ApplicationInsights.DependencyCollector/), [PerfCounterCollector](https://www.nuget.org/packages/Microsoft.ApplicationInsights.PerfCounterCollector/), [ApplicationInsightsLoggingProvider](https://www.nuget.org/packages/Microsoft.Extensions.Logging.ApplicationInsights) etc. This SDK exposes extension methods on `IServiceCollection` to enable and configure telemetry collection.
14+
The new SDK doesn't do any telemetry collection by itself. Instead, it brings in other well known Application Insights auto collectors like [DependencyCollector](https://www.nuget.org/packages/Microsoft.ApplicationInsights.DependencyCollector/), [PerfCounterCollector](https://www.nuget.org/packages/Microsoft.ApplicationInsights.PerfCounterCollector/), [ApplicationInsightsLoggingProvider](https://www.nuget.org/packages/Microsoft.Extensions.Logging.ApplicationInsights) etc. This SDK exposes extension methods on `IServiceCollection` to enable and configure telemetry collection.
1515

1616
## Supported scenarios
1717

1818
The [Application Insights SDK for Worker Service](https://www.nuget.org/packages/Microsoft.ApplicationInsights.WorkerService) is best suited for non-HTTP applications no matter where or how they run. If your application is running and has network connectivity to Azure, telemetry can be collected. Application Insights monitoring is supported everywhere .NET Core is supported. This package can be used in the newly introduced [.NET Core 3.0 Worker Service](https://devblogs.microsoft.com/aspnet/dotnet-core-workers-in-azure-container-instances), [background tasks in ASP.NET Core 2.1/2.2](/aspnet/core/fundamentals/host/hosted-services), Console apps (.NET Core/ .NET Framework), etc.
1919

2020
## Prerequisites
2121

22-
A valid Application Insights instrumentation key. This key is required to send any telemetry to Application Insights. If you need to create a new Application Insights resource to get an instrumentation key, see [Create an Application Insights resource](./create-new-resource.md).
22+
A valid Application Insights connection string. This string is required to send any telemetry to Application Insights. If you need to create a new Application Insights resource to get a connection string, see [Create an Application Insights resource](./create-new-resource.md).
2323

2424
[!INCLUDE [azure-monitor-log-analytics-rebrand](../../../includes/azure-monitor-instrumentation-key-deprecation.md)]
2525

@@ -34,11 +34,13 @@ A valid Application Insights instrumentation key. This key is required to send a
3434
</ItemGroup>
3535
```
3636

37-
1. Call `AddApplicationInsightsTelemetryWorkerService(string instrumentationKey)` extension method on `IServiceCollection`, providing the instrumentation key. This method should be called at the beginning of the application. The exact location depends on the type of application.
37+
1. Configure the connection string in the `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable or in configuration. (`appsettings.json`)
38+
39+
:::image type="content" source="media/migrate-from-instrumentation-keys-to-connection-strings/migrate-from-instrumentation-keys-to-connection-strings.png" alt-text="Screenshot displaying Application Insights overview and connection string." lightbox="media/migrate-from-instrumentation-keys-to-connection-strings/migrate-from-instrumentation-keys-to-connection-strings.png":::
3840

3941
1. Retrieve an `ILogger` instance or `TelemetryClient` instance from the Dependency Injection (DI) container by calling `serviceProvider.GetRequiredService<TelemetryClient>();` or using Constructor Injection. This step will trigger setting up of `TelemetryConfiguration` and auto collection modules.
4042

41-
Specific instructions for each type of application is described in the following sections.
43+
Specific instructions for each type of application are described in the following sections.
4244

4345
## .NET Core 3.0 worker service application
4446

@@ -99,15 +101,17 @@ Full example is shared [here](https://github.com/microsoft/ApplicationInsights-d
99101
}
100102
```
101103

102-
6. Set up the instrumentation key.
104+
6. Set up the connection string.
105+
106+
:::image type="content" source="media/migrate-from-instrumentation-keys-to-connection-strings/migrate-from-instrumentation-keys-to-connection-strings.png" alt-text="Screenshot displaying Application Insights overview and connection string." lightbox="media/migrate-from-instrumentation-keys-to-connection-strings/migrate-from-instrumentation-keys-to-connection-strings.png":::
103107

104-
Although you can provide the instrumentation key as an argument to `AddApplicationInsightsTelemetryWorkerService`, we recommend that you specify the instrumentation key in configuration. The following code sample shows how to specify an instrumentation key in `appsettings.json`. Make sure `appsettings.json` is copied to the application root folder during publishing.
108+
We recommend that you specify the connection string in configuration. The following code sample shows how to specify a connection string in `appsettings.json`. Make sure `appsettings.json` is copied to the application root folder during publishing.
105109

106110
```json
107111
{
108112
"ApplicationInsights":
109113
{
110-
"InstrumentationKey": "putinstrumentationkeyhere"
114+
"ConnectionString" : "InstrumentationKey=00000000-0000-0000-0000-000000000000;"
111115
},
112116
"Logging":
113117
{
@@ -119,17 +123,12 @@ Full example is shared [here](https://github.com/microsoft/ApplicationInsights-d
119123
}
120124
```
121125

122-
Alternatively, specify the instrumentation key in either of the following environment variables.
123-
`APPINSIGHTS_INSTRUMENTATIONKEY` or `ApplicationInsights:InstrumentationKey`
124-
125-
For example:
126-
`SET ApplicationInsights:InstrumentationKey=putinstrumentationkeyhere`
127-
OR `SET APPINSIGHTS_INSTRUMENTATIONKEY=putinstrumentationkeyhere`
126+
Alternatively, specify the connection string in the `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable.
128127

129-
Typically, `APPINSIGHTS_INSTRUMENTATIONKEY` specifies the instrumentation key for applications deployed to Web Apps as Web Jobs.
128+
Typically `APPLICATIONINSIGHTS_CONNECTION_STRING` specifies the connection string for applications deployed to Web Apps as Web Jobs.
130129

131130
> [!NOTE]
132-
> An instrumentation key specified in code wins over the environment variable `APPINSIGHTS_INSTRUMENTATIONKEY`, which wins over other options.
131+
> A connection string specified in code takes precedence over the environment variable `APPLICATIONINSIGHTS_CONNECTION_STRING`, which takes precedence over other options.
133132
134133
## ASP.NET Core background tasks with hosted services
135134

@@ -153,7 +152,7 @@ Full example is shared [here](https://github.com/microsoft/ApplicationInsights-d
153152
services.AddLogging();
154153
services.AddHostedService<TimedHostedService>();
155154

156-
// instrumentation key is read automatically from appsettings.json
155+
// connection string is read automatically from appsettings.json
157156
services.AddApplicationInsightsTelemetryWorkerService();
158157
})
159158
.UseConsoleLifetime()
@@ -215,7 +214,7 @@ Following is the code for `TimedHostedService` where the background task logic r
215214
}
216215
```
217216

218-
3. Set up the instrumentation key.
217+
3. Set up the connection string.
219218
Use the same `appsettings.json` from the .NET Core 3.0 Worker Service example above.
220219

221220
## .NET Core/.NET Framework Console application
@@ -247,9 +246,9 @@ Full example is shared [here](https://github.com/microsoft/ApplicationInsights-d
247246
IServiceCollection services = new ServiceCollection();
248247

249248
// Being a regular console app, there is no appsettings.json or configuration providers enabled by default.
250-
// Hence instrumentation key and any changes to default logging level must be specified here.
249+
// Hence connection string and any changes to default logging level must be specified here.
251250
services.AddLogging(loggingBuilder => loggingBuilder.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("Category", LogLevel.Information));
252-
services.AddApplicationInsightsTelemetryWorkerService("instrumentationkeyhere");
251+
services.AddApplicationInsightsTelemetryWorkerService("connection string here");
253252

254253
// Build ServiceProvider.
255254
IServiceProvider serviceProvider = services.BuildServiceProvider();
@@ -292,9 +291,9 @@ This console application also uses the same default `TelemetryConfiguration`, an
292291

293292
## Run your application
294293

295-
Run your application. The example workers from all of the above makes an http call every second to bing.com, and also emits few logs using `ILogger`. These lines are wrapped inside `StartOperation` call of `TelemetryClient`, which is used to create an operation (in this example `RequestTelemetry` named "operation"). Application Insights will collect these ILogger logs (warning or above by default) and dependencies, and they will be correlated to the `RequestTelemetry` with parent-child relationship. The correlation also works cross process/network boundary. For example, if the call was made to another monitored component, then it will be correlated to this parent as well.
294+
Run your application. The example workers from all of the above makes an http call every second to bing.com, and also emits few logs using `ILogger`. These lines are wrapped inside `StartOperation` call of `TelemetryClient`, which is used to create an operation (in this example `RequestTelemetry` named "operation"). Application Insights will collect these ILogger logs (warning or above by default) and dependencies, and they'll be correlated to the `RequestTelemetry` with parent-child relationship. The correlation also works cross process/network boundary. For example, if the call was made to another monitored component, then it will be correlated to this parent as well.
296295

297-
This custom operation of `RequestTelemetry` can be thought of as the equivalent of an incoming web request in a typical Web Application. While it is not necessary to use an Operation, it fits best with the [Application Insights correlation data model](./correlation.md) - with `RequestTelemetry` acting as the parent operation, and every telemetry generated inside the worker iteration being treated as logically belonging to the same operation. This approach also ensures all the telemetry generated (automatic and manual) will have the same `operation_id`. As sampling is based on `operation_id`, sampling algorithm either keeps or drops all of the telemetry from a single iteration.
296+
This custom operation of `RequestTelemetry` can be thought of as the equivalent of an incoming web request in a typical Web Application. While it isn't necessary to use an Operation, it fits best with the [Application Insights correlation data model](./correlation.md) - with `RequestTelemetry` acting as the parent operation, and every telemetry generated inside the worker iteration being treated as logically belonging to the same operation. This approach also ensures all the telemetry generated (automatic and manual) will have the same `operation_id`. As sampling is based on `operation_id`, sampling algorithm either keeps or drops all of the telemetry from a single iteration.
298297

299298
The following lists the full telemetry automatically collected by Application Insights.
300299

@@ -314,9 +313,9 @@ Dependency collection is enabled by default. [This](asp-net-dependencies.md#auto
314313

315314
`EventCounterCollectionModule` is enabled by default, and it will collect a default set of counters from .NET Core 3.0 apps. The [EventCounter](eventcounters.md) tutorial lists the default set of counters collected. It also has instructions on customizing the list.
316315

317-
### Manually tracking additional telemetry
316+
### Manually tracking other telemetry
318317

319-
While the SDK automatically collects telemetry as explained above, in most cases user will need to send additional telemetry to Application Insights service. The recommended way to track additional telemetry is by obtaining an instance of `TelemetryClient` from Dependency Injection, and then calling one of the supported `TrackXXX()` [API](api-custom-events-metrics.md) methods on it. Another typical use case is [custom tracking of operations](custom-operations-tracking.md). This approach is demonstrated in the Worker examples above.
318+
While the SDK automatically collects telemetry as explained above, in most cases user will need to send other telemetry to Application Insights service. The recommended way to track other telemetry is by obtaining an instance of `TelemetryClient` from Dependency Injection, and then calling one of the supported `TrackXXX()` [API](api-custom-events-metrics.md) methods on it. Another typical use case is [custom tracking of operations](custom-operations-tracking.md). This approach is demonstrated in the Worker examples above.
320319

321320
## Configure the Application Insights SDK
322321

@@ -346,7 +345,7 @@ public void ConfigureServices(IServiceCollection services)
346345
}
347346
```
348347

349-
Note that `ApplicationInsightsServiceOptions` in this SDK is in the namespace `Microsoft.ApplicationInsights.WorkerService` as opposed to `Microsoft.ApplicationInsights.AspNetCore.Extensions` in the ASP.NET Core SDK.
348+
The `ApplicationInsightsServiceOptions` in this SDK is in the namespace `Microsoft.ApplicationInsights.WorkerService` as opposed to `Microsoft.ApplicationInsights.AspNetCore.Extensions` in the ASP.NET Core SDK.
350349

351350
Commonly used settings in `ApplicationInsightsServiceOptions`
352351

@@ -356,15 +355,15 @@ Commonly used settings in `ApplicationInsightsServiceOptions`
356355
|EnableAdaptiveSampling | Enable/Disable Adaptive Sampling | true
357356
|EnableHeartbeat | Enable/Disable Heartbeats feature, which periodically (15-min default) sends a custom metric named 'HeartBeatState' with information about the runtime like .NET Version, Azure Environment information, if applicable, etc. | true
358357
|AddAutoCollectedMetricExtractor | Enable/Disable AutoCollectedMetrics extractor, which is a TelemetryProcessor that sends pre-aggregated metrics about Requests/Dependencies before sampling takes place. | true
359-
|EnableDiagnosticsTelemetryModule | Enable/Disable `DiagnosticsTelemetryModule`. Disabling this will cause the following settings to be ignored; `EnableHeartbeat`, `EnableAzureInstanceMetadataTelemetryModule`, `EnableAppServicesHeartbeatTelemetryModule` | true
358+
|EnableDiagnosticsTelemetryModule | Enable/Disable `DiagnosticsTelemetryModule`. Disabling this setting will cause the following settings to be ignored; `EnableHeartbeat`, `EnableAzureInstanceMetadataTelemetryModule`, `EnableAppServicesHeartbeatTelemetryModule` | true
360359

361360
See the [configurable settings in `ApplicationInsightsServiceOptions`](https://github.com/microsoft/ApplicationInsights-dotnet/blob/develop/NETCORE/src/Shared/Extensions/ApplicationInsightsServiceOptions.cs) for the most up-to-date list.
362361

363362
### Sampling
364363

365364
The Application Insights SDK for Worker Service supports both fixed-rate and adaptive sampling. Adaptive sampling is enabled by default. Sampling can be disabled by using `EnableAdaptiveSampling` option in [ApplicationInsightsServiceOptions](#using-applicationinsightsserviceoptions)
366365

367-
To configure additional sampling settings, the following example can be used.
366+
To configure other sampling settings, the following example can be used.
368367

369368
```csharp
370369
using Microsoft.ApplicationInsights.Extensibility;
@@ -412,7 +411,7 @@ Add any new `TelemetryInitializer` to the `DependencyInjection` container and SD
412411

413412
### Removing TelemetryInitializers
414413

415-
Telemetry initializers are present by default. To remove all or specific telemetry initializers, use the following sample code *after* you call `AddApplicationInsightsTelemetryWorkerService()`.
414+
Telemetry initializers are present by default. To remove all or specific telemetry initializers, use the following sample code *after* calling `AddApplicationInsightsTelemetryWorkerService()`.
416415

417416
```csharp
418417
public void ConfigureServices(IServiceCollection services)
@@ -455,7 +454,7 @@ The following automatic-collection modules are enabled by default. These modules
455454
* `DependencyTrackingTelemetryModule`
456455
* `PerformanceCollectorModule`
457456
* `QuickPulseTelemetryModule`
458-
* `AppServicesHeartbeatTelemetryModule` - (There is currently an issue involving this telemetry module. For a temporary workaround see [GitHub Issue 1689](https://github.com/microsoft/ApplicationInsights-dotnet/issues/1689
457+
* `AppServicesHeartbeatTelemetryModule` - (There's currently an issue involving this telemetry module. For a temporary workaround, see [GitHub Issue 1689](https://github.com/microsoft/ApplicationInsights-dotnet/issues/1689
459458
).)
460459
* `AzureInstanceMetadataTelemetryModule`
461460

@@ -535,11 +534,11 @@ Visual Studio IDE onboarding is currently supported only for ASP.NET/ASP.NET Cor
535534

536535
No, [Azure Monitor Application Insights Agent](./status-monitor-v2-overview.md) currently supports ASP.NET 4.x only.
537536

538-
### If I run my application in Linux, are all features supported?
537+
### Are all features supported if I run my application in Linux?
539538

540539
Yes. Feature support for this SDK is the same in all platforms, with the following exceptions:
541540

542-
* Performance counters are supported only in Windows with the exception of Process CPU/Memory shown in Live Metrics.
541+
* Performance counters are supported only in Windows except for Process CPU/Memory shown in Live Metrics.
543542
* Even though `ServerTelemetryChannel` is enabled by default, if the application is running in Linux or macOS, the channel doesn't automatically create a local storage folder to keep telemetry temporarily if there are network issues. Because of this limitation, telemetry is lost when there are temporary network or server issues. To work around this issue, configure a local folder for the channel:
544543

545544
```csharp
@@ -561,7 +560,7 @@ using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel;
561560
## Sample applications
562561

563562
[.NET Core Console Application](https://github.com/microsoft/ApplicationInsights-dotnet/tree/develop/examples/ConsoleApp)
564-
Use this sample if you are using a Console Application written in either .NET Core (2.0 or higher) or .NET Framework (4.7.2 or higher)
563+
Use this sample if you're using a Console Application written in either .NET Core (2.0 or higher) or .NET Framework (4.7.2 or higher)
565564

566565
[ASP.NET Core background tasks with HostedServices](https://github.com/microsoft/ApplicationInsights-dotnet/tree/develop/examples/BackgroundTasksWithHostedService)
567566
Use this sample if you are in ASP.NET Core 2.1/2.2, and creating background tasks as per official guidance [here](/aspnet/core/fundamentals/host/hosted-services)

0 commit comments

Comments
 (0)