You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
4
4
ms.topic: conceptual
5
-
ms.date: 03/22/2023
6
-
ms.devlang: csharp
5
+
ms.date: 04/06/2023
7
6
---
8
7
9
-
# Create and run custom availability tests by using Azure Functions
8
+
# Review TrackAvailability() test results
10
9
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
12
12
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
-
<ProjectSdk="Microsoft.NET.Sdk">
59
-
<PropertyGroup>
60
-
<TargetFramework>netstandard2.0</TargetFramework>
61
-
</PropertyGroup>
62
-
<ItemGroup>
63
-
<PackageReferenceInclude="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)
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;
> - 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
180
17
181
18
## Check availability
182
19
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.
184
21
185
22
> [!NOTE]
186
23
> 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
0 commit comments