Skip to content

Commit 9d0435a

Browse files
authored
Merge pull request #196818 from AaronMaxwell/aaronmax-worker-service-strings
[AppInsights][AaronMax] updating ikey refs to connection strings
2 parents 8936122 + 0d571b0 commit 9d0435a

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

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

Lines changed: 32 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,18 @@ 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+
> [!NOTE]
109+
> 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.
105110
106111
```json
107112
{
108113
"ApplicationInsights":
109114
{
110-
"InstrumentationKey": "putinstrumentationkeyhere"
115+
"ConnectionString" : "InstrumentationKey=00000000-0000-0000-0000-000000000000;"
111116
},
112117
"Logging":
113118
{
@@ -119,17 +124,12 @@ Full example is shared [here](https://github.com/microsoft/ApplicationInsights-d
119124
}
120125
```
121126

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`
127+
Alternatively, specify the connection string in the `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable.
128128

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

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

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

156-
// instrumentation key is read automatically from appsettings.json
156+
// connection string is read automatically from appsettings.json
157157
services.AddApplicationInsightsTelemetryWorkerService();
158158
})
159159
.UseConsoleLifetime()
@@ -215,7 +215,7 @@ Following is the code for `TimedHostedService` where the background task logic r
215215
}
216216
```
217217

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

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

249249
// 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.
250+
// Hence connection string and any changes to default logging level must be specified here.
251251
services.AddLogging(loggingBuilder => loggingBuilder.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("Category", LogLevel.Information));
252-
services.AddApplicationInsightsTelemetryWorkerService("instrumentationkeyhere");
252+
services.AddApplicationInsightsTelemetryWorkerService("connection string here");
253253

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

293293
## Run your application
294294

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.
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'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.
296296

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.
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 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.
298298

299299
The following lists the full telemetry automatically collected by Application Insights.
300300

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

315315
`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.
316316

317-
### Manually tracking additional telemetry
317+
### Manually tracking other telemetry
318318

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.
319+
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.
320320

321321
## Configure the Application Insights SDK
322322

@@ -346,7 +346,7 @@ public void ConfigureServices(IServiceCollection services)
346346
}
347347
```
348348

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.
349+
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.
350350

351351
Commonly used settings in `ApplicationInsightsServiceOptions`
352352

@@ -356,15 +356,15 @@ Commonly used settings in `ApplicationInsightsServiceOptions`
356356
|EnableAdaptiveSampling | Enable/Disable Adaptive Sampling | true
357357
|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
358358
|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
359+
|EnableDiagnosticsTelemetryModule | Enable/Disable `DiagnosticsTelemetryModule`. Disabling this setting will cause the following settings to be ignored; `EnableHeartbeat`, `EnableAzureInstanceMetadataTelemetryModule`, `EnableAppServicesHeartbeatTelemetryModule` | true
360360

361361
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.
362362

363363
### Sampling
364364

365365
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)
366366

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

369369
```csharp
370370
using Microsoft.ApplicationInsights.Extensibility;
@@ -412,7 +412,7 @@ Add any new `TelemetryInitializer` to the `DependencyInjection` container and SD
412412

413413
### Removing TelemetryInitializers
414414

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

417417
```csharp
418418
public void ConfigureServices(IServiceCollection services)
@@ -455,7 +455,7 @@ The following automatic-collection modules are enabled by default. These modules
455455
* `DependencyTrackingTelemetryModule`
456456
* `PerformanceCollectorModule`
457457
* `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
458+
* `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
459459
).)
460460
* `AzureInstanceMetadataTelemetryModule`
461461

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

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

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

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

542-
* Performance counters are supported only in Windows with the exception of Process CPU/Memory shown in Live Metrics.
542+
* Performance counters are supported only in Windows except for Process CPU/Memory shown in Live Metrics.
543543
* 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:
544544

545545
```csharp
@@ -561,7 +561,7 @@ using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel;
561561
## Sample applications
562562

563563
[.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)
564+
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)
565565

566566
[ASP.NET Core background tasks with HostedServices](https://github.com/microsoft/ApplicationInsights-dotnet/tree/develop/examples/BackgroundTasksWithHostedService)
567567
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)