Azure

Creating Cohorts Utilizing Customized Properties in Software Insights

In Software Insights, a cohort is a gaggle of customers, classes, occasions, or operations that share widespread properties. Cohorts are created utilizing an analytics question. If you have to analyze a selected set of customers or occasions repeatedly, cohorts can present extra flexibility to precise the precise set of customers, classes, occasions, or operations, you have an interest in. Cohorts are much like filters, however since they’re outlined by customized analytics queries, they’re extra adaptable and complicated. In contrast to filters, it can save you cohorts for later use by different members of your crew. The Microsoft documentation gives additional info on the variations between cohorts and filters.

On this article, you’ll be taught so as to add customized properties to your utility’s telemetry after which use these properties to create cohorts.

Creating Software Insights Telemetry Initializer

To start with, you have to create a brand new ASP.NET Core minimal API venture and configure Software Insights. If you’re unfamiliar with the setup course of, you possibly can check with the “Software Insights for ASP.NET Core functions information” information to arrange Software Insights in your venture.

Subsequent, you’ll create a telemetry initializer so as to add customized properties to your utility’s telemetry earlier than it’s despatched to Software Insights. To create a telemetry initializer, outline a category named CustomTelemetryInitializer that inherits from the TelemetryInitializerBase class in your venture. Subsequent, override the Initialize methodology so as to add a customized property named CustomerType to the telemetry, as proven beneath.

utilizing Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers;
utilizing Microsoft.ApplicationInsights.Channel;
utilizing Microsoft.ApplicationInsights.DataContracts;

public class CustomTelemetryInitializer(IHttpContextAccessor httpContextAccessor) : TelemetryInitializerBase(httpContextAccessor)
{
    protected override void OnInitializeTelemetry(
        HttpContext platformContext,
        RequestTelemetry requestTelemetry,
        ITelemetry telemetry)
    {
        if (platformContext.Request.Question.TryGetValue("userType", out var worth))
        {
            telemetry.Context.GlobalProperties.TryAdd("CustomerType", worth);
        }
    }
}

Within the above code, the CustomerType property is about to the worth of the userType question parameter within the request URL. This property will likely be added to all telemetry gadgets despatched to Software Insights.

To register the CustomTelemetry Initializer class with the Software Insights SDK, you have to add it to the companies assortment within the Program.cs file as proven beneath.

utilizing Microsoft.ApplicationInsights.Extensibility;

var builder = WebApplication.CreateBuilder(args);

// Add companies to the container.
builder.Companies.AddApplicationInsightsTelemetry();

// Add the customized telemetry initializer.
builder.Companies.AddSingleton<ITelemetryInitializer, CustomTelemetryInitializer>();

// Construct the ASP.NET Core internet utility.
var app = builder.Construct();

app.MapGet("/", (HttpRequest request) => $"Buyer sort: {request.Question["userType"]}");

// Run the applying.
app.Run();

Save the adjustments and run the applying. Now, while you navigate to the applying URL with the userType question parameter, the CustomerType property will likely be added to the telemetry. Ship the next HTTP requests to the applying to create telemetry gadgets with the CustomerType property set to completely different values.

http://<URL of the service>/?userType=Gold
http://<URL of the service>/?userType=Silver

Creating Cohorts

You’ll now outline a cohort of operations which have been carried out by customers with the chosen CustomerType property. To create a cohort, open the Software Insights useful resource within the Azure portal and navigate to the “Cohorts” part.

Subsequent, outline the title of the cohort, then click on on the “Edit” button to outline the cohort’s parameters. Click on on the “Add Parameter” button so as to add a brand new parameter to the cohort as proven beneath.

Add Parameter

Within the “Add Parameter” dialog, set the main points of the parameter as proven beneath.

New parameter

The question textual content is as follows.

requests
    | distinct tostring(customDimensions.CustomerType)

Click on on the “Run Question” button to view the distinct values that the CustomerType property within the telemetry has. Subsequent, click on on the “Save” button to avoid wasting the cohort and click on on the “Executed Modifying” button to shut the cohort parameter editor.

Subsequent, launch the consequence editor by clicking on the “Edit” button and outline the question to filter the “Operation Id” primarily based on the CustomerType property as proven beneath.

Customer Type property

The question textual content is as follows.

requests
| the place tostring(customDimensions.CustomerType) in ({CustomerType})

The question filters the operations primarily based on the chosen values of the CustomerType property proven within the cohort parameter dropdown. Click on on the “Executed Modifying” button to avoid wasting the cohort and shut the consequence editor.

Set the “CustomerType” parameter to “Gold” to restrict the outcomes.

Lastly, click on on the “Save” button to avoid wasting the cohort. You could have now created a cohort that filters operations primarily based on the CustomerType property within the telemetry.

Now you can use the cohort to filter the telemetry within the Software Insights useful resource. For instance, to view the classes of customers that belong to the “Gold” cohort vs. others, click on on the “Classes” tab and set the x-axis to the cohort you created as proven beneath.

Sessions tab

Conclusion

On this article, you discovered so as to add customized properties to your utility’s telemetry after which use these properties to create cohorts in Software Insights. You should use cohorts to filter telemetry primarily based on customized properties and create customized reviews to research the information. This may also help you to grasp the conduct of particular teams of customers and make knowledgeable selections about your utility.

Know extra about our firm at Skrots. Know extra about our companies at Skrots Companies, Additionally checkout all different blogs at Weblog at Skrots

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button