Creating perf counters using IronRuby

Custom performance counters are an extremely useful tool for monitoring production applications.

But before you can use them, you need to create them!

Some of our deployment scripts are written in PowerShell, which means you can use all the power of the BCL. But others are written in Rake/Ruby, where you can’t.

Thankfully the IronRuby project gives you the best of both worlds:

include System::Diagnostics

def delete_counters(category_name)
	if PerformanceCounterCategory.Exists(category_name)
		puts "Deleting counter category: #{category_name}"
		PerformanceCounterCategory.Delete category_name

def create_counter(counter_name)
	puts "Creating counter for #{counter_name}"
	counter =
	counter.CounterName = counter_name
	counter.CounterType = PerformanceCounterType.NumberOfItems32

def create_counter_category(category_name, counters)
	puts "Creating counter category: #{category_name}"
	PerformanceCounterCategory.Create(category_name, "", PerformanceCounterCategoryType.SingleInstance, counters)

category_name = "My Service"


puts "Creating counter creation data"
counters =
counters.Add create_counter("A Counter")
counters.Add create_counter("Another Counter")
create_counter_category(category_name, counters)

Leave a Reply

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

You are commenting using your 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