Skip to content

Instantly share code, notes, and snippets.

@runesoerensen
Created July 7, 2015 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save runesoerensen/b6a348799cc0003e4bf0 to your computer and use it in GitHub Desktop.
Save runesoerensen/b6a348799cc0003e4bf0 to your computer and use it in GitHub Desktop.
Simple MVC app that posts Librato annotations on startup and shutdown
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Diagnostics;
using System.Net;
using System.Web;
using System.Web.Hosting;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
namespace LibratoAnnotationTest
{
public class MvcApplication : HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
SendLibratoAnnotation("Worker started",
string.Format("Commit id is {0}", ConfigurationManager.AppSettings["appharbor.commit_id"]));
}
protected void Application_End(object sender, EventArgs e)
{
var privateMemorySize = Process.GetCurrentProcess().PrivateMemorySize64;
SendLibratoAnnotation("Worker shutdown",
string.Format("Reason {0}. Private memory usage: {1} MB",
HostingEnvironment.ShutdownReason, privateMemorySize / 1024 / 1024));
}
private static void SendLibratoAnnotation(string title, string description)
{
using (var webClient = new WebClient())
{
var appSettings = ConfigurationManager.AppSettings;
webClient.Credentials = new NetworkCredential(appSettings["LIBRATO_USER"], appSettings["LIBRATO_TOKEN"]);
webClient.UploadValues(new Uri("https://metrics-api.librato.com/v1/annotations/deployments"), new NameValueCollection
{
{ "title", title },
{ "source", appSettings["appharbor.worker_name"] },
{ "description", description },
});
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment