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)