Skip to content

Commit ae35180

Browse files
authored
Merge pull request #278585 from kainawroth/kainawroth-livemetrics
Moving code samples for live metrics to language-specific docs
2 parents b85e1c7 + c321e8c commit ae35180

File tree

4 files changed

+175
-317
lines changed

4 files changed

+175
-317
lines changed

articles/azure-monitor/app/asp-net-core.md

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,66 @@ If `IConfiguration` has loaded configuration from multiple providers, then `serv
166166

167167
Run your application and make requests to it. Telemetry should now flow to Application Insights. The Application Insights SDK automatically collects incoming web requests to your application, along with the following telemetry.
168168

169-
### Live Metrics
169+
### Live metrics
170170

171-
[Live Metrics](./live-stream.md) can be used to quickly verify if Application Insights monitoring is configured correctly. It might take a few minutes for telemetry to appear in the portal and analytics, but Live Metrics shows CPU usage of the running process in near real time. It can also show other telemetry like requests, dependencies, and traces.
171+
[Live metrics](./live-stream.md) can be used to quickly verify if application monitoring with Application Insights is configured correctly. Telemetry can take a few minutes to appear in the Azure portal, but the live metrics pane shows CPU usage of the running process in near real time. It can also show other telemetry like requests, dependencies, and traces.
172+
173+
#### Enable live metrics by using code for any .NET application
174+
175+
> [!NOTE]
176+
> Live metrics are enabled by default when you onboard it by using the recommended instructions for .NET applications.
177+
178+
To manually configure live metrics:
179+
180+
1. Install the NuGet package [Microsoft.ApplicationInsights.PerfCounterCollector](https://www.nuget.org/packages/Microsoft.ApplicationInsights.PerfCounterCollector).
181+
1. The following sample console app code shows setting up live metrics:
182+
183+
```csharp
184+
using Microsoft.ApplicationInsights;
185+
using Microsoft.ApplicationInsights.Extensibility;
186+
using Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse;
187+
188+
// Create a TelemetryConfiguration instance.
189+
TelemetryConfiguration config = TelemetryConfiguration.CreateDefault();
190+
config.InstrumentationKey = "INSTRUMENTATION-KEY-HERE";
191+
QuickPulseTelemetryProcessor quickPulseProcessor = null;
192+
config.DefaultTelemetrySink.TelemetryProcessorChainBuilder
193+
.Use((next) =>
194+
{
195+
quickPulseProcessor = new QuickPulseTelemetryProcessor(next);
196+
return quickPulseProcessor;
197+
})
198+
.Build();
199+
200+
var quickPulseModule = new QuickPulseTelemetryModule();
201+
202+
// Secure the control channel.
203+
// This is optional, but recommended.
204+
quickPulseModule.AuthenticationApiKey = "YOUR-API-KEY-HERE";
205+
quickPulseModule.Initialize(config);
206+
quickPulseModule.RegisterTelemetryProcessor(quickPulseProcessor);
207+
208+
// Create a TelemetryClient instance. It is important
209+
// to use the same TelemetryConfiguration here as the one
210+
// used to set up live metrics.
211+
TelemetryClient client = new TelemetryClient(config);
212+
213+
// This sample runs indefinitely. Replace with actual application logic.
214+
while (true)
215+
{
216+
// Send dependency and request telemetry.
217+
// These will be shown in live metrics.
218+
// CPU/Memory Performance counter is also shown
219+
// automatically without any additional steps.
220+
client.TrackDependency("My dependency", "target", "http://sample",
221+
DateTimeOffset.Now, TimeSpan.FromMilliseconds(300), true);
222+
client.TrackRequest("My Request", DateTimeOffset.Now,
223+
TimeSpan.FromMilliseconds(230), "200", true);
224+
Task.Delay(1000).Wait();
225+
}
226+
```
227+
228+
The preceding sample is for a console app, but the same code can be used in any .NET applications. If any other telemetry modules are enabled to autocollect telemetry, it's important to ensure that the same configuration used for initializing those modules is used for the live metrics module.
172229

173230
### ILogger logs
174231

@@ -250,7 +307,7 @@ var aiOptions = new Microsoft.ApplicationInsights.AspNetCore.Extensions.Applicat
250307
// Disables adaptive sampling.
251308
aiOptions.EnableAdaptiveSampling = false;
252309

253-
// Disables QuickPulse (Live Metrics stream).
310+
// Disables live metrics (also known as QuickPulse).
254311
aiOptions.EnableQuickPulseMetricStream = false;
255312

256313
builder.Services.AddApplicationInsightsTelemetry(aiOptions);
@@ -267,7 +324,7 @@ public void ConfigureServices(IServiceCollection services)
267324
// Disables adaptive sampling.
268325
aiOptions.EnableAdaptiveSampling = false;
269326

270-
// Disables QuickPulse (Live Metrics stream).
327+
// Disables live metrics (also known as QuickPulse).
271328
aiOptions.EnableQuickPulseMetricStream = false;
272329
services.AddApplicationInsightsTelemetry(aiOptions);
273330
}
@@ -452,7 +509,7 @@ By default, the following automatic-collection modules are enabled. These module
452509
* `RequestTrackingTelemetryModule`: Collects RequestTelemetry from incoming web requests.
453510
* `DependencyTrackingTelemetryModule`: Collects [DependencyTelemetry](./asp-net-dependencies.md) from outgoing HTTP calls and SQL calls.
454511
* `PerformanceCollectorModule`: Collects Windows PerformanceCounters.
455-
* `QuickPulseTelemetryModule`: Collects telemetry to show in the Live Metrics portal.
512+
* `QuickPulseTelemetryModule`: Collects telemetry to show in the live metrics pane.
456513
* `AppServicesHeartbeatTelemetryModule`: Collects heartbeats (which are sent as custom metrics), about the App Service environment where the application is hosted.
457514
* `AzureInstanceMetadataTelemetryModule`: Collects heartbeats (which are sent as custom metrics), about the Azure VM environment where the application is hosted.
458515
* `EventCounterCollectionModule`: Collects [EventCounters](eventcounters.md). This module is a new feature and is available in SDK version 2.8.0 and later.

articles/azure-monitor/app/asp-net.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,78 @@ Although it's possible to manually add the JavaScript (Web) SDK Loader Script to
361361

362362
For the template-based ASP.NET MVC app from this article, the file that you need to edit is *_Layout.cshtml*. You can find it under **Views** > **Shared**. To add client-side monitoring, open *_Layout.cshtml* and follow the [JavaScript (Web) SDK Loader Script-based setup instructions](./javascript-sdk.md?tabs=javascriptwebsdkloaderscript#get-started) from the article about client-side JavaScript SDK configuration.
363363

364+
## Live metrics
365+
366+
[Live metrics](./live-stream.md) can be used to quickly verify if application monitoring with Application Insights is configured correctly. Telemetry can take a few minutes to appear in the Azure portal, but the live metrics pane shows CPU usage of the running process in near real time. It can also show other telemetry like requests, dependencies, and traces.
367+
368+
### Enable live metrics by using code for any .NET application
369+
370+
> [!NOTE]
371+
> Live metrics are enabled by default when you onboard it by using the recommended instructions for .NET applications.
372+
373+
To manually configure live metrics:
374+
375+
1. Install the NuGet package [Microsoft.ApplicationInsights.PerfCounterCollector](https://www.nuget.org/packages/Microsoft.ApplicationInsights.PerfCounterCollector).
376+
1. The following sample console app code shows setting up live metrics:
377+
378+
```csharp
379+
using Microsoft.ApplicationInsights;
380+
using Microsoft.ApplicationInsights.Extensibility;
381+
using Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse;
382+
using System;
383+
using System.Threading.Tasks;
384+
385+
namespace LiveMetricsDemo
386+
{
387+
class Program
388+
{
389+
static void Main(string[] args)
390+
{
391+
// Create a TelemetryConfiguration instance.
392+
TelemetryConfiguration config = TelemetryConfiguration.CreateDefault();
393+
config.InstrumentationKey = "INSTRUMENTATION-KEY-HERE";
394+
QuickPulseTelemetryProcessor quickPulseProcessor = null;
395+
config.DefaultTelemetrySink.TelemetryProcessorChainBuilder
396+
.Use((next) =>
397+
{
398+
quickPulseProcessor = new QuickPulseTelemetryProcessor(next);
399+
return quickPulseProcessor;
400+
})
401+
.Build();
402+
403+
var quickPulseModule = new QuickPulseTelemetryModule();
404+
405+
// Secure the control channel.
406+
// This is optional, but recommended.
407+
quickPulseModule.AuthenticationApiKey = "YOUR-API-KEY-HERE";
408+
quickPulseModule.Initialize(config);
409+
quickPulseModule.RegisterTelemetryProcessor(quickPulseProcessor);
410+
411+
// Create a TelemetryClient instance. It is important
412+
// to use the same TelemetryConfiguration here as the one
413+
// used to set up live metrics.
414+
TelemetryClient client = new TelemetryClient(config);
415+
416+
// This sample runs indefinitely. Replace with actual application logic.
417+
while (true)
418+
{
419+
// Send dependency and request telemetry.
420+
// These will be shown in live metrics.
421+
// CPU/Memory Performance counter is also shown
422+
// automatically without any additional steps.
423+
client.TrackDependency("My dependency", "target", "http://sample",
424+
DateTimeOffset.Now, TimeSpan.FromMilliseconds(300), true);
425+
client.TrackRequest("My Request", DateTimeOffset.Now,
426+
TimeSpan.FromMilliseconds(230), "200", true);
427+
Task.Delay(1000).Wait();
428+
}
429+
}
430+
}
431+
}
432+
```
433+
434+
The preceding sample is for a console app, but the same code can be used in any .NET applications. If any other telemetry modules are enabled to autocollect telemetry, it's important to ensure that the same configuration used for initializing those modules is used for the live metrics module.
435+
364436
## Frequently asked questions
365437

366438
This section provides answers to common questions.

0 commit comments

Comments
 (0)