Skip to content

Commit 4d05471

Browse files
Merge pull request #233708 from AaronMaxwell/aaronmax-trackavailability-update
Added rereqs, removed non-AI parts of article
2 parents c0def78 + cf7b557 commit 4d05471

File tree

2 files changed

+16
-177
lines changed

2 files changed

+16
-177
lines changed
Lines changed: 15 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -1,186 +1,23 @@
11
---
2-
title: Create and run custom availability tests by using Azure Functions
3-
description: This article explains how to create an Azure function with TrackAvailability() that will run periodically according to the configuration given in a TimerTrigger function.
2+
title: Review TrackAvailability() test results
3+
description: This article explains how to review data logged by TrackAvailability() tests
44
ms.topic: conceptual
5-
ms.date: 03/22/2023
6-
ms.devlang: csharp
5+
ms.date: 04/06/2023
76
---
87

9-
# Create and run custom availability tests by using Azure Functions
8+
# Review TrackAvailability() test results
109

11-
This article explains how to create an Azure function with `TrackAvailability()` that will run periodically according to the configuration given in the `TimerTrigger` function with your own business logic. The results of this test will be sent to your Application Insights resource, where you can query for and alert on the availability results data. Then you can create customized tests similar to what you can do via [availability monitoring](./availability-overview.md) in the Azure portal. By using customized tests, you can:
10+
This article explains how to review TrackAvailability() test results in the Azure portal and query the data using Log Analytics.
11+
## Prerequisites
1212

13-
- Write more complex availability tests than is possible by using the portal UI.
14-
- Monitor an app inside of your Azure virtual network.
15-
- Change the endpoint address.
16-
- Create an availability test even if this feature isn't available in your region.
17-
18-
> [!NOTE]
19-
> This example is designed solely to show you the mechanics of how the `TrackAvailability()` API call works within an Azure function. It doesn't show you how to write the underlying HTTP test code or business logic that's required to turn this example into a fully functional availability test. By default, if you walk through this example, you'll be creating a basic availability HTTP GET test.
20-
>
21-
> To follow these instructions, you must use the [dedicated plan](../../azure-functions/dedicated-plan.md) to allow editing code in App Service Editor.
22-
23-
## Create a timer trigger function
24-
25-
1. Create an Azure Functions resource.
26-
- If you already have an Application Insights resource:
27-
28-
- By default, Azure Functions creates an Application Insights resource. But if you want to use a resource you created previously, you must specify that during creation.
29-
- Follow the instructions on how to [create an Azure Functions resource](../../azure-functions/functions-create-scheduled-function.md#create-a-function-app) with the following modification:
30-
31-
On the **Monitoring** tab, select the **Application Insights** dropdown box and then enter or select the name of your resource.
32-
33-
:::image type="content" source="media/availability-azure-functions/app-insights-resource.png" alt-text="Screenshot that shows selecting your existing Application Insights resource on the Monitoring tab.":::
34-
35-
- If you don't have an Application Insights resource created yet for your timer-triggered function:
36-
- By default, when you're creating your Azure Functions application, it will create an Application Insights resource for you. Follow the instructions on how to [create an Azure Functions resource](../../azure-functions/functions-create-scheduled-function.md#create-a-function-app).
37-
38-
> [!NOTE]
39-
> You can host your functions on a Consumption, Premium, or App Service plan. If you're testing behind a virtual network or testing nonpublic endpoints, you'll need to use the Premium plan in place of the Consumption plan. Select your plan on the **Hosting** tab. Ensure the latest .NET version is selected when you create the function app.
40-
1. Create a timer trigger function.
41-
1. In your function app, select the **Functions** tab.
42-
1. Select **Add**. On the **Add function** pane, select the following configurations:
43-
1. **Development environment**: **Develop in portal**
44-
1. **Select a template**: **Timer trigger**
45-
1. Select **Add** to create the timer trigger function.
46-
47-
:::image type="content" source="media/availability-azure-functions/add-function.png" alt-text="Screenshot that shows how to add a timer trigger function to your function app." lightbox="media/availability-azure-functions/add-function.png":::
48-
49-
## Add and edit code in the App Service Editor
50-
51-
Go to your deployed function app, and under **Development Tools**, select the **App Service Editor** tab.
52-
53-
To create a new file, right-click under your timer trigger function (for example, **TimerTrigger1**) and select **New File**. Then enter the name of the file and select **Enter**.
54-
55-
1. Create a new file called **function.proj** and paste the following code:
56-
57-
```xml
58-
<Project Sdk="Microsoft.NET.Sdk">
59-
<PropertyGroup>
60-
<TargetFramework>netstandard2.0</TargetFramework>
61-
</PropertyGroup>
62-
<ItemGroup>
63-
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.15.0" /> <!-- Ensure you’re using the latest version -->
64-
</ItemGroup>
65-
</Project>
66-
```
67-
68-
:::image type="content" source="media/availability-azure-functions/function-proj.png" alt-text=" Screenshot that shows function.proj in the App Service Editor." lightbox="media/availability-azure-functions/function-proj.png":::
69-
70-
1. Create a new file called **runAvailabilityTest.csx** and paste the following code:
71-
72-
```csharp
73-
using System.Net.Http;
74-
75-
public async static Task RunAvailabilityTestAsync(ILogger log)
76-
{
77-
using (var httpClient = new HttpClient())
78-
{
79-
// TODO: Replace with your business logic
80-
await httpClient.GetStringAsync("https://www.bing.com/");
81-
}
82-
}
83-
```
84-
85-
1. Define the `REGION_NAME` environment variable as a valid Azure availability location.
86-
87-
Run the following command in the [Azure CLI](https://learn.microsoft.com/cli/azure/account?view=azure-cli-latest#az-account-list-locations&preserve-view=true) to list available regions.
88-
89-
```azurecli
90-
az account list-locations -o table
91-
```
92-
93-
1. Copy the following code into the **run.csx** file. (You'll replace the preexisting code.)
94-
95-
```csharp
96-
#load "runAvailabilityTest.csx"
97-
98-
using System;
99-
100-
using System.Diagnostics;
101-
102-
using Microsoft.ApplicationInsights;
103-
104-
using Microsoft.ApplicationInsights.Channel;
105-
106-
using Microsoft.ApplicationInsights.DataContracts;
107-
108-
using Microsoft.ApplicationInsights.Extensibility;
109-
110-
private static TelemetryClient telemetryClient;
111-
112-
// =============================================================
113-
114-
// ****************** DO NOT MODIFY THIS FILE ******************
115-
116-
// Business logic must be implemented in RunAvailabilityTestAsync function in runAvailabilityTest.csx
117-
118-
// If this file does not exist, please add it first
119-
120-
// =============================================================
121-
122-
public async static Task Run(TimerInfo myTimer, ILogger log, ExecutionContext executionContext)
123-
124-
{
125-
if (telemetryClient == null)
126-
{
127-
// Initializing a telemetry configuration for Application Insights based on connection string
128-
129-
var telemetryConfiguration = new TelemetryConfiguration();
130-
telemetryConfiguration.ConnectionString = Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING");
131-
telemetryConfiguration.TelemetryChannel = new InMemoryChannel();
132-
telemetryClient = new TelemetryClient(telemetryConfiguration);
133-
}
134-
135-
string testName = executionContext.FunctionName;
136-
string location = Environment.GetEnvironmentVariable("REGION_NAME");
137-
var availability = new AvailabilityTelemetry
138-
{
139-
Name = testName,
140-
141-
RunLocation = location,
142-
143-
Success = false,
144-
};
145-
146-
availability.Context.Operation.ParentId = Activity.Current.SpanId.ToString();
147-
availability.Context.Operation.Id = Activity.Current.RootId;
148-
var stopwatch = new Stopwatch();
149-
stopwatch.Start();
150-
151-
try
152-
{
153-
using (var activity = new Activity("AvailabilityContext"))
154-
{
155-
activity.Start();
156-
availability.Id = Activity.Current.SpanId.ToString();
157-
// Run business logic
158-
await RunAvailabilityTestAsync(log);
159-
}
160-
availability.Success = true;
161-
}
162-
163-
catch (Exception ex)
164-
{
165-
availability.Message = ex.Message;
166-
throw;
167-
}
168-
169-
finally
170-
{
171-
stopwatch.Stop();
172-
availability.Duration = stopwatch.Elapsed;
173-
availability.Timestamp = DateTimeOffset.UtcNow;
174-
telemetryClient.TrackAvailability(availability);
175-
telemetryClient.Flush();
176-
}
177-
}
178-
179-
```
13+
> [!div class="checklist"]
14+
> - [Azure subscription](https://azure.microsoft.com/free) and user account with the ability to create and delete resources
15+
> - [Workspace-based Application Insights resource](create-workspace-resource.md)
16+
> - Custom [Azure Functions app](../../azure-functions/functions-overview.md#introduction-to-azure-functions) running [TrackAvailability()](/dotnet/api/microsoft.applicationinsights.telemetryclient.trackavailability) with your own business logic
18017
18118
## Check availability
18219

183-
To make sure everything is working, look at the graph on the **Availability** tab of your Application Insights resource.
20+
Start by reviewing the graph on the **Availability** tab of your Application Insights resource.
18421

18522
> [!NOTE]
18623
> Tests created with `TrackAvailability()` will appear with **CUSTOM** next to the test name.
@@ -203,5 +40,7 @@ You can use Log Analytics to view your availability results, dependencies, and m
20340

20441
## Next steps
20542

206-
- [Application Map](./app-map.md)
207-
- [Transaction diagnostics](./transaction-diagnostics.md)
43+
* [Standard tests](availability-standard-tests.md)
44+
* [Availability alerts](availability-alerts.md)
45+
* [Application Map](./app-map.md)
46+
* [Transaction diagnostics](./transaction-diagnostics.md)

articles/azure-monitor/toc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ items:
762762
href: app/availability-standard-tests.md
763763
- name: Private testing
764764
href: app/availability-private-test.md
765-
- name: Availability + Azure Functions
765+
- name: TrackAvailability()
766766
href: app/availability-azure-functions.md
767767
displayName: availability,azure functions,TrackAvailability(), timer function, azure gov, azure china, custom test, customized tests, contentmatch
768768
- name: Availability alerts

0 commit comments

Comments
 (0)