Simulating A number of Customers in App Insights with Integration Exams
Introduction
Utility Insights is an utility efficiency administration service that lets you monitor your dwell internet utility. It supplies a real-time dashboard for monitoring your utility’s efficiency and utilization. You need to use Utility Insights to trace the efficiency of your internet utility, monitor person habits, and diagnose points. It additionally supplies highly effective analytics instruments that let you analyze person habits and determine areas for enchancment.
Utility Insights
Utility Insights identifies customers based mostly on a singular person ID that’s routinely generated and assigned to every person. This ID is saved in a cookie on the person’s browser. When a person interacts with the appliance, Utility Insights tracks these interactions utilizing this distinctive ID, permitting it to watch person habits and efficiency. In case of monitoring purposes with no person interface, you may create a customized telemetry initializer that populates the nameless person ID with a singular identifier for every request.
If you end up operating integration exams on your internet utility, you could wish to simulate a number of customers sending telemetry information to Utility Insights. This lets you take a look at how your monitoring setup corresponding to, person cohorts, funnels, and different analytics instruments behave when a number of customers work together along with your utility. Nonetheless, when operating integration exams, the person ID would possibly stay the identical for all requests since all of them originate from the identical take a look at atmosphere. This may result in inaccurate outcomes when analyzing person habits and efficiency.
On this article, you’ll learn to simulate a number of customers in Utility Insights when operating integration exams on your internet utility.
Conditions
To finish this tutorial, you will want:
- An Azure subscription. If you do not have an Azure subscription, create a free account earlier than you start.
- An Utility Insights useful resource. If you do not have an Utility Insights useful resource, create one by following the directions in Create an Utility Insights useful resource.
- Visual Studio Code or every other code editor.
Utility Setup
Create a brand new ASP.NET Core minimal API venture and configure Utility Insights. If you’re unfamiliar with the setup course of, confer with the “Utility Insights for ASP.NET Core purposes information” information that can assist you arrange Utility Insights in your venture.
Write the next code in your Program.cs
file that exposes an endpoint to ship a customized occasion to indicate a person viewing a product:
utilizing Microsoft.ApplicationInsights;
var builder = WebApplication.CreateBuilder(args);
// Add providers to the container.
builder.Providers.AddApplicationInsightsTelemetry();
// Construct the ASP.NET Core internet utility.
var app = builder.Construct();
app.MapGet("/{id}", (int id, TelemetryClient telemetry) => telemetry.TrackEvent("ViewProduct", new Dictionary<string, string> { { "id", id.ToString() }}));
// Run the appliance.
app.Run();
You’ll be able to study extra about sending customized occasions to Utility Insights within the “Sending Customized Occasions to Utility Insights” documentation information.
Writing Integration Exams
Create a new xUnit take a look at venture and add Microsoft.AspNetCore.Mvc.Testing
and FluentAssertions
NuGet packages to your take a look at venture. Create a category named CustomTelemetryInitializer
that inherits from TelemetryInitializerBase
and overrides the OnInitializeTelemetry
technique to set the person ID for telemetry information. This class can be used to set a customized person ID for every request despatched to Utility Insights.
utilizing Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers;
utilizing Microsoft.ApplicationInsights.Channel;
utilizing Microsoft.ApplicationInsights.DataContracts;
utilizing Microsoft.AspNetCore.Http;
namespace TestProject1;
public class CustomTelemetryInitializer(IHttpContextAccessor httpContextAccessor) : TelemetryInitializerBase(
httpContextAccessor)
{
non-public string _aiUserId = string.Empty;
public void SetUserId(string userId)
{
_aiUserId = userId;
}
protected override void OnInitializeTelemetry(
HttpContext platformContext,
RequestTelemetry requestTelemetry,
ITelemetry telemetry)
{
telemetry.Context.Person.Id = _aiUserId;
}
}
The CustomTelemetryInitializer
inherits from TelemetryInitializerBase
class that allows you to modify the telemetry information earlier than it’s despatched to Utility Insights. You need to use the OnInitializeTelemetry
technique to complement the telemetry information with customized properties. On this case, you’ll set a customized person identifier for every request. Utility Insights will use this person identifier as a substitute of its default person ID to trace person habits.
You’ll now use this class in your integration exams. Populate your take a look at class with the next code that units the CustomTelemetryInitializer
as a pre-processor of telemetry information and sends a request to the appliance endpoint that you just created earlier:
utilizing System.Web;
utilizing FluentAssertions;
utilizing Microsoft.ApplicationInsights;
utilizing Microsoft.ApplicationInsights.Channel;
utilizing Microsoft.ApplicationInsights.Extensibility;
utilizing Microsoft.AspNetCore.Mvc.Testing;
utilizing Microsoft.Extensions.DependencyInjection;
namespace TestProject1;
public class UnitTest1
{
[Theory]
[InlineData("test-user-1")]
[InlineData("test-user-2")]
[InlineData("test-user-3")]
public async Process SimulateUserInTests(string userId)
{
// Prepare
var manufacturing facility = new WebApplicationFactory<Program>().WithWebHostBuilder(builder =>
builder.ConfigureServices(serviceCollection =>
{
serviceCollection.AddSingleton<ITelemetryInitializer, CustomTelemetryInitializer>();
}));
var telemetryInitializer =
manufacturing facility.Providers.GetRequiredService<ITelemetryInitializer>() as CustomTelemetryInitializer;
telemetryInitializer?.SetUserId(userId);
var consumer = manufacturing facility.CreateClient();
var telemetryClient = manufacturing facility.Providers.GetRequiredService<TelemetryClient>();
// Act
var response = await consumer.GetAsync("/1");
await telemetryClient.FlushAsync(CancellationToken.None);
// Assert
response.StatusCode.Ought to().Be(HttpStatusCode.OK);
}
}
Now you can run the exams and observe the telemetry information in Utility Insights. To view the person rely recorded in Utility Insights, navigate to the Utility Insights useful resource within the Azure portal after which click on on the “Customers” possibility within the “Utilization” part. The next screenshot exhibits the person rely recorded in Utility Insights from my exams:
As proven within the screenshot, you may filter the person rely by occasions corresponding to ViewProduct
that you just configured in your API endpoint.
Conclusion
On this article, you realized find out how to simulate a number of customers in Utility Insights when operating integration exams on your internet utility. You created a customized telemetry initializer to set a customized person ID for every request despatched to Utility Insights. You then used this practice telemetry initializer in your integration exams to simulate a number of customers sending telemetry information to Utility Insights. This allowed you to check how your monitoring setups, corresponding to person cohorts, funnels, and different analytics instruments, behave when a number of customers work together along with your utility.
Know extra about our firm at Skrots. Know extra about our providers at Skrots Providers, Additionally checkout all different blogs at Weblog at Skrots