Created
June 1, 2015 09:31
-
-
Save tomhawkin/474d91763681f040bced to your computer and use it in GitHub Desktop.
Sitecore New Relic Monitoring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // -------------------------------------------------------------------------------------------------------------------- | |
| // <summary> | |
| // New Relic Http Module. | |
| // Fixes the naming of (web) transactions in the New Relic dashboard. | |
| // And fixes IIS request logging. | |
| // original code taken from https://marketplace.sitecore.net/Modules/N/New_Relic.aspx?sc_lang=en | |
| // but fixed to use the template name to identify items due to the new relic transaction limit being | |
| // hit when using this with urls as the transaction name in large solutions | |
| // </summary> | |
| // -------------------------------------------------------------------------------------------------------------------- | |
| namespace Site.Website.Processors | |
| { | |
| using System; | |
| using System.Web; | |
| using Sitecore.Diagnostics; | |
| /// <summary> | |
| /// New Relic Http Module. | |
| /// Fixes the naming of (web) transactions in the New Relic dashboard. | |
| /// And fixes IIS request logging. | |
| /// </summary> | |
| public class NewRelicHttpModule : IHttpModule | |
| { | |
| /// <summary> | |
| /// The init. | |
| /// </summary> | |
| /// <param name="context"> | |
| /// The context. | |
| /// </param> | |
| public void Init(HttpApplication context) | |
| { | |
| context.AuthenticateRequest += this.OnAuthenticateRequest; | |
| context.BeginRequest += OnBeginRequest; | |
| context.EndRequest += OnEndRequest; | |
| } | |
| /// <summary> | |
| /// The dispose. | |
| /// </summary> | |
| public void Dispose() | |
| { | |
| } | |
| /// <summary> | |
| /// Catch the begin request event. This event can be used to store the original request information for later use. | |
| /// </summary> | |
| /// <param name="sender"> | |
| /// The sender. | |
| /// </param> | |
| /// <param name="e"> | |
| /// The e. | |
| /// </param> | |
| private static void OnBeginRequest(object sender, EventArgs e) | |
| { | |
| try | |
| { | |
| var app = (HttpApplication)sender; | |
| app.Context.Items["APM.Path"] = app.Context.Request.Path; | |
| app.Context.Items["APM.Url.Host"] = app.Context.Request.Url.Host; | |
| app.Context.Items["APM.Url.AbsoluteUri"] = app.Context.Request.Url.AbsoluteUri; | |
| app.Context.Items["APM.Url.Query"] = app.Context.Request.Url.Query; | |
| } | |
| catch (Exception exception) | |
| { | |
| Log.Error("NewRelic logging error: " + exception.Message, exception); | |
| } | |
| } | |
| /// <summary> | |
| /// Catch the end request event. This event can be used to do a RewritePath() to the original request. This results in proper URL logging by IIS. | |
| /// </summary> | |
| /// <param name="sender"> | |
| /// The sender. | |
| /// </param> | |
| /// <param name="e"> | |
| /// The e. | |
| /// </param> | |
| private static void OnEndRequest(object sender, EventArgs e) | |
| { | |
| try | |
| { | |
| var app = (HttpApplication)sender; | |
| if (app.Context.Items["APM.Path"] != null) | |
| { | |
| var path = app.Context.Items["APM.Path"] as string; | |
| if (path != null) | |
| { | |
| app.Context.RewritePath(path); | |
| } | |
| } | |
| // Add custom parameters to New Relic transaction | |
| // global::NewRelic.Api.Agent.NewRelic.AddCustomParameter("Portal", portal); | |
| // global::NewRelic.Api.Agent.NewRelic.AddCustomParameter("UserName", userName); | |
| // global::NewRelic.Api.Agent.NewRelic.AddCustomParameter("UserAgent", userAgent); | |
| // global::NewRelic.Api.Agent.NewRelic.AddCustomParameter("IpAddress", ipAddress); | |
| // Add user parameters to the New Relic transaction | |
| // global::NewRelic.Api.Agent.NewRelic.SetUserParameters(userName, "", portal); | |
| } | |
| catch (Exception exception) | |
| { | |
| Log.Error("NewRelic logging error: " + exception.Message, exception); | |
| } | |
| } | |
| /// <summary> | |
| /// Catch the authenticate request. This is the only event in which the new transaction name can be set. | |
| /// </summary> | |
| /// <param name="sender"> | |
| /// The sender. | |
| /// </param> | |
| /// <param name="eventArgs"> | |
| /// The event Args. | |
| /// </param> | |
| private void OnAuthenticateRequest(object sender, EventArgs eventArgs) | |
| { | |
| try | |
| { | |
| if (Sitecore.Context.Site != null && !string.IsNullOrEmpty(Sitecore.Context.Site.Name)) | |
| { | |
| var site = Sitecore.Context.Site.Name; | |
| const string Category = "Uri"; | |
| if (Sitecore.Context.Item != null) | |
| { | |
| try | |
| { | |
| var transaction = string.Format("[{0}].{1}", site, Sitecore.Context.Item.TemplateName); | |
| NewRelic.Api.Agent.NewRelic.SetTransactionName(Category, transaction); | |
| } | |
| catch (Exception exception) | |
| { | |
| Log.Error("NewRelic logging error: " + exception.Message, exception); | |
| } | |
| } | |
| if (HttpContext.Current.Request.Path.StartsWith("/~/media/")) | |
| { | |
| try | |
| { | |
| var transaction = string.Format("[{0}]{1}", site, "SitecoreImage"); | |
| NewRelic.Api.Agent.NewRelic.SetTransactionName(Category, transaction); | |
| } | |
| catch (Exception exception) | |
| { | |
| Log.Error("NewRelic logging error: " + exception.Message, exception); | |
| } | |
| } | |
| } | |
| } | |
| catch (Exception exception) | |
| { | |
| Log.Error("NewRelic logging error: " + exception.Message, exception); | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // -------------------------------------------------------------------------------------------------------------------- | |
| // <summary> | |
| // The global asax. | |
| // inherits from Sitecore.Web.Application | |
| // </summary> | |
| // -------------------------------------------------------------------------------------------------------------------- | |
| namespace Site.Website | |
| { | |
| using System; | |
| using System.Web.Http; | |
| using System.Web.Mvc; | |
| using System.Web.Routing; | |
| using NewRelic.Api.Agent; | |
| using Sitecore.Diagnostics; | |
| using Sitecore.Web; | |
| /// <summary> | |
| /// The global. | |
| /// </summary> | |
| public class Global : Application | |
| { | |
| /// <summary> | |
| /// The application_ start. | |
| /// </summary> | |
| /// <param name="sender"> | |
| /// The sender. | |
| /// </param> | |
| /// <param name="e"> | |
| /// The e. | |
| /// </param> | |
| protected void Application_Start(object sender, EventArgs e) | |
| { | |
| AreaRegistration.RegisterAllAreas(); | |
| WebApiConfig.Register(GlobalConfiguration.Configuration); | |
| RouteConfig.RegisterRoutes(RouteTable.Routes); | |
| MvcHandler.DisableMvcResponseHeader = true; | |
| } | |
| /// <summary> | |
| /// The application_ error. | |
| /// </summary> | |
| /// <param name="sender"> | |
| /// The sender. | |
| /// </param> | |
| /// <param name="e"> | |
| /// The e. | |
| /// </param> | |
| protected void Application_Error(object sender, EventArgs e) | |
| { | |
| var ex = this.Server.GetLastError(); | |
| NewRelic.NoticeError(ex); | |
| Log.Error("Uncaught Error:", ex, this); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!-- | |
| the New Relic processor goes in between the Rewrite module and the http module in system.webserver/modules in the web.config | |
| --> | |
| <system.webServer> | |
| <modules runAllManagedModulesForAllRequests="true"> | |
| <remove name="WebDAVModule" /> | |
| <add type="Sitecore.Web.RewriteModule, Sitecore.Kernel" name="SitecoreRewriteModule" /> | |
| <add name="NewRelic" type="Site.Website.Processors.NewRelicHttpModule, Site.Website" /> | |
| <add type="Sitecore.Nexus.Web.HttpModule,Sitecore.Nexus" name="SitecoreHttpModule" /> | |
| </modules> | |
| </system.webServer> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment