Using App Insights to monitor Publishes in Sitecore

So, wouldn’t it be nice to record particular events in Sitecore – E.g. Publishes – into Application Insights? Well, yes, I thought – and it turned out that this had already been done by Ramkumar Dhinakaran several years ago…

However, using his code I found I wanted to make changes – to record the types of update (not just the total processed), and as published his code seemed to have problems with full-site publishes. Thus, I offer the following:

public class PublishEventTracker
{
	public void PublishEnd(object sender, EventArgs args)
	{
		try
		{
			Publisher publisher = Event.ExtractParameter(args, 0) as Publisher;
			if (publisher != null)
			{
				Item currentItem = publisher.Options.RootItem;
				BaseJob job = JobManager.GetJob(publisher.GetJobName());
				var messages = job.Status.Messages;

				int created = GetCount("created", messages);
				int deleted = GetCount("deleted", messages);
				int updated = GetCount("updated", messages);
				int skipped = GetCount("skipped", messages);

				//Record in Azure Telemetry
				TrackPublish(publisher.Options.SourceDatabase.Name,
					publisher.Options.TargetDatabase.Name,
					publisher.Options.Mode.ToString(),
					publisher.Options.UserName,
					publisher.Options.Language.Name,
					created,
					deleted,
					updated,
					skipped,
					publisher.Options.Deep,
					publisher.Options.PublishRelatedItems,
					currentItem?.ID.ToString(),
					currentItem?.Paths.FullPath);
			}
		}
		catch (Exception e)
		{
			Log.Error("PUBLISH TELEMETRY LOG: Could not correctly gather publishing telemetry " + e.ToString(), this);
		}
	}

	private int GetCount(string pattern, System.Collections.Specialized.StringCollection messages)
	{
		Regex rgx = new Regex(pattern + ": +(\\d+)");
		foreach (var msg in messages)
		{
			Match m = rgx.Match(msg);

			if(m.Success)
			{
				var result = m.Groups[1].Value;

				int processedNum = 0;
				if (int.TryParse(result, out processedNum))
				{
					return processedNum;
				} else
				{
					return -2;
				}
			}
		}
		return -1;
	}

	private void TrackPublish(string sourceDatabase, 
							  string targetDatabase, 
							  string publishMode, 
							  string userName, 
							  string publishedLanguage, 
							  int created,
							  int deleted, int updated, 
							  int skipped, 
							  bool publishSubitems, 
							  bool publishRelated, 
							  string itemId, 
							  string itemPath)
	{
		TelemetryClient client = new TelemetryClient();

		int total = 0;
		if (created >= 0) total += created;
		if (deleted >= 0) total += deleted;
		if (updated >= 0) total += updated;

		Dictionary<string, string> props = new Dictionary<string, string>();

		props.Add("UserName", userName);
		props.Add("SitecoreItemID", itemId);
		props.Add("SitecoreItemPath", itemPath);
		props.Add("SitecoreItemLanguage", publishedLanguage);
		props.Add("PublishMode", publishMode);
		props.Add("PublishSubItems", publishSubitems.ToString());
		props.Add("PublishRelated", publishRelated.ToString());
		props.Add("Publish", sourceDatabase + " > " + targetDatabase);

		//Sitecore Instance Name
		props.Add("InstanceName", Sitecore.Configuration.Settings.InstanceName);
		props.Add("Created", created.ToString());
		props.Add("Deleted", deleted.ToString());
		props.Add("Updated", updated.ToString());
		props.Add("Skipped", skipped.ToString());

		//Track Event
		client.TrackEvent("sitecore:publishevent", props);

		client.GetMetric(new MetricIdentifier("Sitecore Publishing", "Processed Items", "Target Database")).TrackValue(total, targetDatabase);
	}
}

Add this in via config on the Publish:End event, and publishes will be recorded into App Insights as custom events. Also note that we’re tracking the total number of created/updated/deleted items as a metric, which means that they can be graphed over time.

Using App Insights to monitor Publishes in Sitecore

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.