Skip to content

Instantly share code, notes, and snippets.

Matias Quaranta ealsur

View GitHub Profile
@ealsur
ealsur / Trigger.cs
Last active Feb 15, 2019
Azure Cosmos DB + Functions Cookbook - monitoring - trigger
View Trigger.cs
[FunctionName("Trigger")]
public static void Trigger([CosmosDBTrigger(
databaseName: "%MonitoredDatabase%",
collectionName: "%MonitoredCollection%",
ConnectionStringSetting = "CosmosDB",
LeaseCollectionPrefix = "%MonitoredDatabaseLeasePrefix%",
LeaseCollectionName = "leases",
CreateLeaseCollectionIfNotExists = true)] IReadOnlyList<Document> documents,
ILogger log)
{
@ealsur
ealsur / CosmosDBConnectionString.cs
Created Feb 15, 2019
Azure Cosmos DB + Functions Cookbook - monitoring - helper
View CosmosDBConnectionString.cs
private class CosmosDBConnectionString
{
public CosmosDBConnectionString(string connectionString)
{
// Use this generic builder to parse the connection string
DbConnectionStringBuilder builder = new DbConnectionStringBuilder
{
ConnectionString = connectionString
};
@ealsur
ealsur / Estimator.cs
Created Feb 15, 2019
Azure Cosmos DB + Functions Cookbook - monitoring - estimator
View Estimator.cs
private static Lazy<IRemainingWorkEstimator> remainingWorkEstimator;
public static IRemainingWorkEstimator GetRemainingWorkEstimator(
DocumentClient monitoredCollectionClient,
DocumentClient leaseCollectionClient)
{
if (remainingWorkEstimator == null)
{
// Pull the Connection string from the environment, Environment.GetEnvironmentVariable will read the local.settings.json file or the deployed Function App configuration
CosmosDBConnectionString cosmosDBConnectionString = new CosmosDBConnectionString(Environment.GetEnvironmentVariable("CosmosDB"));
@ealsur
ealsur / Function.cs
Created Feb 15, 2019
Azure Cosmos DB + Functions Cookbook - monitoring
View Function.cs
[FunctionName("Monitor")]
public static async Task Monitor(
[TimerTrigger("*/1 * * * * *", RunOnStartup = true)] TimerInfo timer, // Timer will trigger every 1 second, adjust CRON expression
[CosmosDB("%MonitoredDatabase%", "%MonitoredCollection%", ConnectionStringSetting = "CosmosDB")] DocumentClient monitoredCollectionClient,
[CosmosDB("%MonitoredDatabase%", "leases", ConnectionStringSetting = "CosmosDB")] DocumentClient leaseCollectionClient,
ILogger log)
{
var estimator = GetRemainingWorkEstimator(monitoredCollectionClient, leaseCollectionClient);
var remainingWork = await estimator.GetEstimatedRemainingWork();
// Send custom metric to App Insight
@ealsur
ealsur / hosts.json
Created Jan 15, 2019
Azure Cosmos DB + Functions - Health monitoring
View hosts.json
{
"version": "2.0",
"logging": {
"fileLoggingMode": "always",
"logLevel": {
"Host.Triggers.CosmosDB": "Trace"
}
}
}
@ealsur
ealsur / precompiled-output.cs
Created Nov 12, 2018
Azure Cosmos DB + Functions Cookbook - Multi master & preferred region
View precompiled-output.cs
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
[CosmosDB(databaseName: "%CosmosDBDatabase%",
collectionName: "%CosmosDBCollection%",
ConnectionStringSetting = "CosmosDBConnectionString",
UseMultipleWriteLocations = true,
PreferredLocations = "Central US"
)] IAsyncCollector<MyClass> documentsToSave,
ILogger log)
{
@ealsur
ealsur / precompiled-input.cs
Created Nov 12, 2018
Azure Cosmos DB + Functions Cookbook - Multi master & preferred region
View precompiled-input.cs
public static HttpResponseMessage Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "HttpInputBindingByIdInRoute/{id}")] HttpRequestMessage req,
[CosmosDB(databaseName: "%CosmosDBDatabase%",
collectionName: "%CosmosDBCollection%",
ConnectionStringSetting = "CosmosDBConnectionString",
UseMultipleWriteLocations = true,
PreferredLocations = "Central US,North Central US"
Id = "{id}")] Document document,
ILogger log)
{
@ealsur
ealsur / host.json
Created Aug 8, 2018
Custom ConnectionMode and Protocol for Functions V2
View host.json
{
"cosmosDB": {
"connectionMode": "Direct",
"protocol": "Tcp"
}
}
@ealsur
ealsur / host.json
Created Aug 8, 2018
Custom ConnectionMode and Protocol
View host.json
{
"documentDB": {
"connectionMode": "Direct",
"protocol": "Tcp"
}
}
@ealsur
ealsur / precompiled.cs
Created Apr 2, 2018
Azure Cosmos DB + Functions Cookbook - multi triggers
View precompiled.cs
[FunctionName("Trigger1")]
public static void Run(
[CosmosDBTrigger("<your-monitored-database>",
"<your-monitored-collection>",
ConnectionStringSetting = "<your-connectionstring-setting>"
LeaseCollectionPrefix = "trigger1")]
IReadOnlyList<Document> input,
TraceWriter log)
{
log.Info("Documents modified " + input.Count);
You can’t perform that action at this time.