-
Notifications
You must be signed in to change notification settings - Fork 6
/
CosmosMonitor.cs
49 lines (45 loc) · 1.88 KB
/
CosmosMonitor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
namespace cosmos.monitor
{
public class CosmosMonitor
{
private ChangeFeedEstimator estimator;
/// <summary>
/// Creates an Estimator to estimate the Processor progress
/// </summary>
private ChangeFeedEstimator GetOrCreateEstimator(CosmosClient cosmosClient)
{
var leaseContainer = cosmosClient.GetContainer(Environment.GetEnvironmentVariable("DATABASE"), "leases");
var monitoredContainer = cosmosClient.GetContainer(Environment.GetEnvironmentVariable("DATABASE"), Environment.GetEnvironmentVariable("EVENTCONTAINER"));
this.estimator ??= monitoredContainer.GetChangeFeedEstimator("EventProcessor", leaseContainer);
return this.estimator;
}
[FunctionName("CosmosMonitor")]
public async Task Run([TimerTrigger("*/30 * * * * *")]TimerInfo timerInfo,
[CosmosDB(
databaseName: "%DATABASE%",
containerName: "%EVENTCONTAINER%",
PreferredLocations = "%REGION%",
Connection = "cosmosConnection")]CosmosClient cosmosClient,
ILogger log)
{
var estimator = GetOrCreateEstimator(cosmosClient).GetCurrentStateIterator();
while (estimator.HasMoreResults)
{
var response = await estimator.ReadNextAsync();
foreach (var item in response)
{
log.LogMetric($"Estimation Lease {item.LeaseToken}", item.EstimatedLag,
new Dictionary<string, object>() {
{ "Lease", item.LeaseToken },
{ "Owner", item.InstanceName } });
}
}
}
}
}