Watch the recording! 👇
This repository contains the code reference to help create an Azure Dashboard to track the pending work on a deployed CosmosDBTrigger
. The theme is a system that processes device telemetry and produces a real time summary as telemetry is stored in an Azure Cosmos DB container.
It contains three projects:
- cosmos.trigger: A
CosmosDBTrigger
with retry policy support, that generates a summary per device and stores it on another Cosmos DB container. - cosmos.monitor: A
TimerTrigger
that will leverage the Change Feed Estimator, obtain lag estimations across leases, and report it as Metrics on Application Insights. - cosmos.writer: A console application that can simulate telemetry data for the Trigger.
The code in this repository can be used as a complete solution or referenced as individual components.
This solution requires provisioning the following resources:
- Azure Cosmos DB Account with a database and three containers: one for telemetry, another for the summary, and a last one for the leases (named "leases"). The containers must have the following partition keys:
/id
for the telemetry container./DeviceId
for the summary container./id
for the leases container.
- Two Azure Function Apps, one for the
cosmos.trigger
project and another forcosmos.monitor
project, each with it's own Application Insights instance.
The Function Apps must have the following settings:
cosmosConnection
should either contain the connection string or have theaccountEndpoint
andcredential
set accordingly to use Managed Identities. For the latter, the Managed Identity should be provisioned and granted permissions. For more details see the trigger documentation.EVENTCONTAINER
should contain the name of the container for the telemetry data.SUMMARYCONTAINER
should contain the name of the container for summary data.REGION
is optional and ideally contains the name of the Azure Region to leverage reading and writing data to a preferred Cosmos DB region.
THe highlight of this repository is the cosmos.monitor TimerTrigger. It showcases the way that you can leverage the CosmosClient
instance that is configured using the cosmosConnection
to create a Change Feed Estimator and obtain the estimated lag on each lease, including the identifier of the owner. You can use this information to produce a dashboard and visualize or even create alerts when the lag goes beyond certain thresholds.
The following code creates an Estimator using the provided CosmosClient
and referencing the Change Feed Processor configuration that the CosmosDBTrigger
is wiring:
var leaseContainer = cosmosClient.GetContainer(Environment.GetEnvironmentVariable("DATABASE"), "leases");
var monitoredContainer = cosmosClient.GetContainer(Environment.GetEnvironmentVariable("DATABASE"), Environment.GetEnvironmentVariable("EVENTCONTAINER"));
this.estimator ??= monitoredContainer.GetChangeFeedEstimator("EventProcessor", leaseContainer);
It references the monitored container and the leases container, using the Function App settings value and uses a processorName
that matches the CosmosDBTrigger.LeaseContainerPrefix
.
The estimation results are then sent as Metrics to Application Insights with extended properties (Lease identifier and Owner):
log.LogMetric($"Estimation Lease {item.LeaseToken}", item.EstimatedLag,
new Dictionary<string, object>() {
{ "Lease", item.LeaseToken },
{ "Owner", item.InstanceName } });
The project on cosmos.writer
can be used to generate a simulated load on the CosmosDBTrigger
monitored container from the command-line:
dotnet run --connectionString "<cosmos DB connection string>" --concurrency 5 --devices 100000 --db <database name> --container <telemetry container name> --time PT30M
The concurrency
parameter can be customized to increase or decrease the volume of concurrent operations based on the provisioned RU/s on the monitored container, while the time
parameter can be used to control the duration of the simulation using ISO 8601 format.
This repository contains a sample dashboard that can be imported on Application Insights to visualize the estimated lag on the monitored container. Before importing the dashboard, you must update the Resource Ids:
"/subscriptions/{your-subscription-id}/resourceGroups/{resource-group}/providers/microsoft.insights/components/{appinsights-name}"
{your-subscription-id}
: The subscription Id of the Application Insights instance.{resource-group}
: The resource group of the Application Insights instance.{appinsights-name}
: The name of the Application Insights instance.