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
	end
end

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

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

category_name = "My Service"

delete_counters(category_name)

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

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 )

Google+ photo

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

Connecting to %s