Support multiple tenants in a single Application Insights instance

As Application Insights continues to evolve, the ability to slice and dice your data also continues to increase. With an eye toward microservices, Microsoft recently made added support for multi-component applications to Application Insights that allow you to aggregate all of your components’ telemetry, but still filter by individual services via the cloud service role name. But what if we have multiple tenants using our application and have architected our application so each tenant gets their own dedicated set of services? How can we track telemetry for each of those clients individually if desired, but still be able to get an overall view of application health and minimize administration?

There are several options, each with their own pros and cons:

  • We could isolate each tenant into their own Application Insights resource and use OMS to get an aggregated dashboard view of all our tenants
  • We could hijack the cloud service role name to indicate the tenant name using the latest Application Insights changes mentioned previously
  • We could filter by request host names if they’re deterministic of the tenant

Instead, let’s look at a simpler solution that requires minimal effort and has minimal drawbacks.

Add Custom Properties to Telemetry

Beyond just logging custom events and custom metrics, Application Insights also offers the ability to add custom properties to all outbound telemetry, even the default system-generated telemetry. Our goal is to add a Tenant property to all outbound telemetry that will allow us to filter for and aggregate by individual tenants in Application Insights . We’ll start by creating a custom server-side Telemetry Processor in our web application.

This processor will read the TenantName property from app settings and add it to all telemetry as a custom property named Tenant before moving on to the next processor in the chain. Next, we’ll tell Application Insights to start using our processor by adding it to our ApplicationInsights.config file.

After we publish our changes, all server-side telemetry from this application will contain a Tenant property filled in with whatever value is in the config.

Seeing Tenant Data in Application Insights

Now that the data’s stored, we will be able to filter on Tenants as if they were any other property on the telemetry.

We can also group by the Tenant in the Metrics Explorer to see our metrics broken out by each Tenant.

Note that there does seem to be a delay between the new custom property appearing in your telemetry and the new custom property being available as an option to group by or filter on. If you don’t see your custom property available in the drop-down lists, even after refreshing, come back a little bit later and try again.

While we only added server-side telemetry processing in the example above, the the same basic concept also applies to client-side telemetry. We can easily create a new Telemetry Initializer in our Application Insights JavaScript snippet. As long as the tenant data is populated on the same custom property (TenantName in this example), it will seamlessly integrate with our per-tenant server-side telemetry.

One thought on “Support multiple tenants in a single Application Insights instance

Leave a Reply

Your email address will not be published.