[PLUGIN RELEASE] Metrics

Posted about 7 years back at Revolution On Rails


From Jeffrey Damick


Introduction

This gem provides a metrics collecting for controllers, database queries, and specific blocks of code or methods. It is designed to be light-weight and have minimal impact on production builds while providing performance indicators of the running application.



Disclaimer

This software is released to be used at your own risk. For feedback please drop us a line at rails-trunk [ at ] revolution DOT com.
Using this plugin should not be your first step in application optimization/scaling or even the second one.



Example

class SomeClassToTest
collect_metrics_on :my_method

def my_method(blah = nil)
true
end
end


Output:
[ERROR] [2007-06-21 23:21:19] [trunk] [Metrics]|[76716]|[MysqlAdapter.log]|0.012727|args=["root localhost trunk_test", "CREATE DATABASE `trunk_test`"]
[ERROR] [2007-06-21 23:19:56] [trunk] [Metrics]|[35158]|[Request to [Test::SomeControllerWithMetricsId]]|0.001373|action = index|path =some?
[ERROR] [2007-06-21 23:19:56] [trunk] [Metrics]|[33676]|[SomeClassToUseModuleMixin.another_method]|0.000020|args=["also"]


for more samples and test cases see test/metrics_test.rb



Usage

The metrics are written to: logs/<environment>_metrics.log

Configuration can be updated in metrics/config/metrics.yml, you may copy this file to your RAILS_ROOT/config/metrics.yml and customize for your application, the RAILS_ROOT will be checked first.



Sample metrics.yml

production:
min_real_time_threshold: 1.0
single_line_output: true
some_module/test_controller: 0.0




Installation

As plugin:
script/plugin install svn://rubyforge.org/var/svn/metrics/trunk/vendor/plugins/metrics



License

metrics is released under the MIT license.



Support

The plugin RubyForge page is http://rubyforge.org/projects/metrics

Ticketish Email Integration

Posted about 7 years back at benmyles.com - Home

Note: Also posted on the Integral Impressions blog.

One of the central features of Ticketish is email integration. Users can create new tickets by email and add comments to tickets by replying to any email that has the ticket id in the subject. I'll show how this integration is achieved, from setting up Postfix to writing the Ticket and Comment handlers.

Each project in Ticketish receives its own email address. The email address is in the format "[project.permalink]@[account.subdomain].ticketi.sh". Sending an email to the project's email address creates a new ticket. Once a new ticket is created, a notification email is sent. Replying to the notification email (or any further emails regarding the ticket) adds a comment to the ticket. Further, the user can add attachments to any emails and have them show up as attachments to a comment.

So, how does it work? Let's start with the big picture. The mail transfer agent Postfix runs on the same server as the Ticketish mongrels. Postfix receives mail for all ticketi.sh subdomains. When Postfix receives a new message, it fires up script/runner for Ticketish and passes the message in via STDIN. At this point we don't care if the project exists or not, that's for Rails to figure out. Postfix just needs to send any emails addressed to *.ticketi.sh to script/runner.

Let's dig a little deeper, and see how to accomplish this behavior with Postfix.

The first Postfix configuration file we'll look at is main.cf. I won't paste the whole file, just the interesting parts that differ from the default main.cf file Postfix comes with.


/etc/postfix/main.cf

myhostname = ticketi.sh
mydomain = ticketi.sh
mydestination = $myhostname, localhost.$mydomain,
localhost, regexp:/etc/postfix/mydestination


This is mostly straight-forward. We set the hostname and domain to "ticketi.sh". The interesting part is that we're using a regular expression lookup table with mydestination. The mydestination setting means "this is the final destination for the following domains". Obviously, we want to be the final destination for all Ticketi.sh domains, so we use a regular expression.


/etc/postfix/mydestination

/.*\.ticketi\.sh/ OK


Pretty simple. You need to make sure you generate the database file from that lookup table by running postalias:


# postalias /etc/postfix/mydestination
# ls /etc/postfix/mydestination.db
/etc/postfix/mydestination.db


Now that Postfix knows to receive mail for *.ticketi.sh, we need to tell it where to send that mail. First, we'll create a wrapper around script/runner that can receive incoming emails from Postfix.


/etc/postfix/ticketish_agent.sh

#!/bin/bash
HOME=/home/lsws /usr/bin/ruby \
/home/lsws/apps/ticketish/current/script/runner \
'data = STDIN.read; CommentHandler.receive(data) \
|| TicketHandler.receive(data)' 2>> \
/home/lsws/ticketish_agent.log


The actual Rails code in runner is pretty simple. We assign the incoming email to the data variable, and then do 'CommentHandler.receive(data) || TicketHandler.receive(data)'. This code tries to add the message as a comment first, but if the comment handler returns nil (as it will if the message isn't a comment) it'll create it as a new ticket instead.

Now we need to add a service to Postfix by adding a few lines at the bottom of master.cf.


/etc/postfix/master.cf

mailman unix - n n - - pipe
flags= user=lsws
argv=/etc/postfix/ticketish_agent.sh


We've just called the service "mailman" and it pipes the message through to the wrapper we just created. Note the user= field. As you might guess, this is the user the process runs as. Be sure to use the user your Rails application runs under.

Finally, we need to tell Postfix to deliver all local mail to this new service (all local mail, since Postfix is entirely dedicated to Ticketish). We do this by defining the local_transport in main.cf.


/etc/postfix/main.cf

local_transport = mailman


Creating the CommentHandler and TicketHandler is pretty simple. We just use the following structure:


ticketish/app/models/comment_handler.rb

class CommentHandler < ActionMailer::Base
def receive(email)
# ...
end
end


You can do whatever you like once you have the email. For example, to extract the project name and domain name from the recipient address of the email:


project_name, domain_name =
email.to.first.split("@")


So there you have it. Easy email integration with Postfix and Rails. If you haven't already heard of Ticketish, take a look. It's our new simple ticketing application, currently in beta. We're still sending out beta invites, so feel free to add your name.

Episode 48: Console Tricks

Posted about 7 years back at Railscasts

The Rails console is one of my favorite tools. This episode is packed with tips and tricks on how to get the most out of the console.

Thanks Akismet!

Posted about 7 years back at zerosum dirt(nap) - Home

I’m probably a bit behind the game on this one, but huge props are due to Akismet for making my blog life just a bit more pleasant. Before it was installed last week, I was deleting hoards of comment spam every day. Today, none.

In other blog-related news, I’m still planning on moving productions over to Mephisto, but have been hard pressed for time lately. Fortunately (when I get around to it), it has Akismet support baked right in.

UPDATE: finally moved over to a new blogging platform! About time, eh?

Episode 47: Two Many-to-Many

Posted about 7 years back at Railscasts

There are two different ways to set up a many-to-many association in Rails. In this episode you will see how to implement both ways along with some tips on choosing the right one for your project.

Hardware Accelerated Flash Video - the turning point?

Posted about 7 years back at work.rowanhick.com

I'm looking forward to the web 2.bomb era to be over. Every man and his dog is building yet another social networking, api enabled, ajaxified, beta tag wearing application and waiting for the VC money to flow in. Just follow techcrunch.com and yawn every morning at the yasu* of the day. Outside of the VC crazed crowd some interesting things are happening. Adobe's announcement on HD content deployed using the Flash Player is very interesting indeed, not so much from the feature set, but what it's actually doing technically. We now have a cross platform web delivered piece of code that is utilizing the GPU. I think this has been lost on a few people. I'm personally not interested in the HD aspect, what I am interested in is the hardware acceleration. If an appropriate DirectX or OpenGL piece of hardware is present it then the flash player will utilise it to enable full screen 'HD' video. Very very impressive. Tinic is a genius. This marks (in my mind) a turning point for web apps - the crack in the dam has started, finally we are starting to really break out of the limitations of the browser, and will pave the way for some serious development of serious web applications. Perhaps Flash will have the last laugh.. I am not a fan boy of any tech religion. I do not think Adobe are God, Microsoft know it all, or Rails will fulfill my every desire. However in unison it seems, people are looking for the (r)evolution - the race is currently on to see who can deliver this technology we're crying out for. Taking the real (engineering) enterprise to the web Back for one of the companies I worked for, we had a huge Delphi application that is a fat client, fat server model. A CAD/CAM app, it required lots of CPU cycles to render an entire window (real .. not the microsoft kind) structure, apply business rules etc, compared to your atypical order processing system (which are just fancy crud operations). Currently on the web, all you see are these crud applications. The so called web 2.0 era, for all of the applications we see, are really just crud systems. So nothing is there to support anything other than crud. Which rules out doing this particular app. The browser in it's next version is only just starting to see a Canvas object whereas this has been the backbone of many desktop applications for years/decades! Flash/Flex/Air notwithstanding of course... What gets me excited, is when the potential for an application like the previously mentioned CAD/CAM system to be delivered over the web, utilising the advantages of the web AND the advantages of the desktop. The only way of doing that is breaking out of the web browser sandbox to use a desktop's horsepower. I think this hardware acceleration development might just kick off a chain reaction. Imagine via Actionscript, suddenly being able to harness the GPU's drawing system.... or even processing for horribly complex calculations that suit for parallelism - instead of bringing your system to a crawl, pipe them off to your GPU. Think of the possibilities.... An interior design application, rendered in beautiful photo realism with all the lighting effects you could imagine, allowing you to design your dream interior and order everything for it online. Games, delivered online, using local client connections for rendering - all code, etc retained on the server and piped down on demand. Infinitely expansive as the code is never kept locally. Log back in the next day and all new baddies added into the game without you even knowing until you stumble upon one. User powered grid computing services. Imagine firing up your beast of a computer, connecting to medical research system, and donating your GPU cycles - without having to download any nasty java or activex applets to your desktop. Companies are salivating over the PS3's power for this and scrambling to figure out how to do it, imagine taking this to the desktop arena ? Is this not the Holy Grail we want ? Let's start imagining, dreaming up the possibilities... and if we're smart enough, try to realise them. My mind's already going a hundred miles an hour. What would you build if you had access to the best of both worlds ?! *yasu - Yet Another (......y) Start-Up

Roundtable: Women in Development II - Ruby on Rails Podcast

Posted about 7 years back at Ruby on Rails Podcast

Part II of a discussion with and about women in development.
See also:

Gentoo: Dependency info is missing!

Posted about 7 years back at benmyles.com - Home

After a clean Gentoo minimal install and emerge of a Ruby on Rails stack, Gentoo started throwing the following error when managing services via inet.d:


* Dependency info is missing! Please run
* # /sbin/depscan.sh
* to fix this.


Running /sbin/depscan.sh seemed to have no effect.

I tracked down a post from a mailing list that explains what's going on.

In a nutshell, my /var/lib/init.d/deptree file was empty. I'm still not sure why. The /sbin/depscan.sh command rebuilds this file, but not if it's newer than some configuration file in /etc. The answer is to rm /var/lib/init.d/deptree and then run /sbin/depscan.sh.

If you get any errors about /bin/mktemp not existing, make sure to emerge mktemp.

Easy solution, but quite obscure!

Episode 46: Catch-all Route

Posted about 7 years back at Railscasts

Sometimes you need to add complex/dynamic routes. This is often impossible to do in routes.rb, but do not worry. It can be accomplished with a catch-all route. See how in this episode.

Tumbleranting

Posted about 7 years back at zerosum dirt(nap) - Home

Seth Godin is a nicer guy than I am (he’s probably better dressed, too): I would have ballparked his quote a little higher. Explanation: there’s a “don’t waste my time” fee.

As a freelancer, this happens to me all the time and it’s muy frustrating. I like hearing ideas, I like helping you structure your approach, I love developing solutions, applications, tools for you. I don’t even mind giving estimates and free advice. But in order to do that, you need to tell me what it is you want.

And no, “just like digg but with/for xxx” isn’t what I’m talking about :-).

Code Digest #1

Posted about 7 years back at Revolution On Rails

When you program for a living, you write lots of code. There is often some code that you are fond of. We start the Code Digest series to present such code written by the RHG developers. We encourage other teams and individual developers to share similar snippets in their blogs so we all can learn from each other and become better rails developers.

Daniel Silva



<%= link_to(image_tag("/images/icon.gif", :style=>"padding-right:0px;"), {:controller => '/registration', :action => 'login', :dest => request.request_uri}, {:style => ""}) %> 

With this line, I can create an image link using Rails code, to take advantage of the power of routes in Rails while still creating this kind of structure: <a href="#"><img border="0"/></a>



Jack Dempsey



Ok, so here's something useful for using keyboard short cuts to navigate through a small supporting application:
def include_keyboard_shortcuts

hotkey = 'Ctrl+Shift'
code_string = ''
keys = {
'm'=>'/account_info/show',
's'=>'/account_info/index',
'c'=>'/menu/go?menu_action=new_customer',
'p'=>'/products',
'o'=>'/account_info/orders',
't'=>'/order_tracker',
'x'=>'/auth_sessions/destroy',
'r'=>'/current_users/reset_password'
}

current_user_needed = %w{m o r}

keys.each_pair do |k,v|
next if current_user_needed.include?(k) && current_user.nil?
code_string << "shortcut('#{hotkey}+hm#{k}',function() { document.location.href='#{v}' });\n"
end

javascript_tag(code_string)

end



Then in a layout file just:
<%= javascript_include_tag 'shortcuts' %>
<%= include_keyboard_shortcuts %>



Makes use of a nice shortcuts.js lib.


Val Aleksenko


This is a fragment of a conversation on our internal IRC with my solution for refactoring a piece of code:
May 07 14:07:56 <Anthro> There has to be a better way to do this (x and y are non-negative integers): increment = (x!=0) ? ( (y!=0) ? 0 : 1 ) : -1
May 07 14:08:22 <Anthro> I think it can be done with math instead of logic.
May 07 14:16:56 <jack> Anthro: have you thought about using sin and cos? ;-)
May 07 14:18:42 <muzzy> (x <=> 0) <=> (y <=> 0)



Granted, it is not as clear as the original, but it is hard to resist it from the aesthetic point of view.

Val Aleksenko


I love writing dynamically generated methods and classes in Ruby as the next guy. When I was writing acts_as_readonlyable, I needed to manage multiple connections. I found that the easiest solution for managing active connections was generation of an AR class for each read-only definition and borrowing the connection from it. The usage is acts_as_readonlyable :read_only_entry_in_database_config.
def acts_as_readonlyable(readonly_db)
define_readonly_class(readonly_db) unless ActiveRecord.const_defined?(readonly_class_name(readonly_db))
end

def readonly_class_name(db)
"Generated#{ db.camelize }"
end

def define_readonly_class(db)
ActiveRecord.module_eval %Q!
class #{ readonly_class_name(db) } < Base
self.abstract_class = true
establish_connection configurations[RAILS_ENV]['#{ db }']
end
!

end



Warren Konkel


The ConfigFile class is a quick and easy way to load YAML files located in your Rails application's config directory. Simply place a YAML file into your config directory and then access it like a hash. For example this will read your config/database.yml: ConfigFile['database']['production']['adapter'].
class ConfigFile
def self.[](arg)
@@cached_configs ||= {}
@@cached_config_mtimes ||= {}

base_name = "config/#{arg}.yml"
filename = File.join(RAILS_ROOT, base_name)
raise "ERROR: Config not found: #{base_name}" unless File.exists?(base_name)

if @@cached_configs[arg].nil? || @@cached_config_mtimes[arg] < File.stat(filename).mtime.to_i
@@cached_configs[arg] = YAML.load(File.open(filename))
@@cached_config_mtimes[arg] = File.stat(filename).mtime.to_i
end

@@cached_configs[arg]
end
end

Code Digest #1

Posted about 7 years back at Revolution On Rails

When you program for a living, you write lots of code. There is often some code that you are fond of. We start the Code Digest series to present such code written by the RHG developers. We encourage other teams and individual developers to share similar snippets in their blogs so we all can learn from each other and become better rails developers.

Daniel Silva



<%= link_to(image_tag("/images/icon.gif", :style=>"padding-right:0px;"), {:controller => '/registration', :action => 'login', :dest => request.request_uri}, {:style => ""}) %> 

With this line, I can create an image link using Rails code, to take advantage of the power of routes in Rails while still creating this kind of structure: <a href="#"><img border="0"/></a>



Jack Dempsey



Ok, so here's something useful for using keyboard short cuts to navigate through a small supporting application:
def include_keyboard_shortcuts

hotkey = 'Ctrl+Shift'
code_string = ''
keys = {
'm'=>'/account_info/show',
's'=>'/account_info/index',
'c'=>'/menu/go?menu_action=new_customer',
'p'=>'/products',
'o'=>'/account_info/orders',
't'=>'/order_tracker',
'x'=>'/auth_sessions/destroy',
'r'=>'/current_users/reset_password'
}

current_user_needed = %w{m o r}

keys.each_pair do |k,v|
next if current_user_needed.include?(k) && current_user.nil?
code_string << "shortcut('#{hotkey}+hm#{k}',function() { document.location.href='#{v}' });\n"
end

javascript_tag(code_string)

end



Then in a layout file just:
<%= javascript_include_tag 'shortcuts' %>
<%= include_keyboard_shortcuts %>



Makes use of a nice shortcuts.js lib.


Val Aleksenko


This is a fragment of a conversation on our internal IRC with my solution for refactoring a piece of code:
May 07 14:07:56 <Anthro> There has to be a better way to do this (x and y are non-negative integers): increment = (x!=0) ? ( (y!=0) ? 0 : 1 ) : -1
May 07 14:08:22 <Anthro> I think it can be done with math instead of logic.
May 07 14:16:56 <jack> Anthro: have you thought about using sin and cos? ;-)
May 07 14:18:42 <muzzy> (x <=> 0) <=> (y <=> 0)



Granted, it is not as clear as the original, but it is hard to resist it from the aesthetic point of view.

Val Aleksenko


I love writing dynamically generated methods and classes in Ruby as the next guy. When I was writing acts_as_readonlyable, I needed to manage multiple connections. I found that the easiest solution for managing active connections was generation of an AR class for each read-only definition and borrowing the connection from it. The usage is acts_as_readonlyable :read_only_entry_in_database_config.
def acts_as_readonlyable(readonly_db)
define_readonly_class(readonly_db) unless ActiveRecord.const_defined?(readonly_class_name(readonly_db))
end

def readonly_class_name(db)
"Generated#{ db.camelize }"
end

def define_readonly_class(db)
ActiveRecord.module_eval %Q!
class #{ readonly_class_name(db) } < Base
self.abstract_class = true
establish_connection configurations[RAILS_ENV]['#{ db }']
end
!

end



Warren Konkel


The ConfigFile class is a quick and easy way to load YAML files located in your Rails application's config directory. Simply place a YAML file into your config directory and then access it like a hash. For example this will read your config/database.yml: ConfigFile['database']['production']['adapter'].
class ConfigFile
def self.[](arg)
@@cached_configs ||= {}
@@cached_config_mtimes ||= {}

base_name = "config/#{arg}.yml"
filename = File.join(RAILS_ROOT, base_name)
raise "ERROR: Config not found: #{base_name}" unless File.exists?(base_name)

if @@cached_configs[arg].nil? || @@cached_config_mtimes[arg] < File.stat(filename).mtime.to_i
@@cached_configs[arg] = YAML.load(File.open(filename))
@@cached_config_mtimes[arg] = File.stat(filename).mtime.to_i
end

@@cached_configs[arg]
end
end

Ticketish

Posted about 7 years back at benmyles.com - Home

It has been a while since I've posted on my personal blog. In fact, I've decided to start fresh. Lately I've been very busy at Integral Impressions working on some great Ruby on Rails projects.

One such project is Ticketish. Just a few days ago we started sending out invitations for a private beta program. Ticketish is a simple issue/bug/ticket management web app, designed to do what it needs to do and then get out of the way.

We built Ticketish for internal use, and have been using it for some months now. We've come to really appreciate it, and decided to add some polish and release it to the world. If you're interested, go sign up for the beta and I'll get an invitation to you within a day or two (probably quicker).

The State (And Future) Of The UJS Plugin

Posted about 7 years back at danwebb.net - Home

Over the past few weeks loads of people have been asking me about what’s going on the UJS plugin. It’s obviously fallen in to disrepair so people, understandably, have been concerned. There has been a reason for this aside from the fact that I am a lazy barsteward (which of course I am, but that’s beside the point). Here is a letter I’ve just written to the UJS mailing list that I’d thought I’d post here to try to get a little more feedback. It’s a little bit long but bear with me…

I’ve been chatting to Luke and users of UJS about what to do with it and still haven’t quiet decided hence the lack of news but below is a rundown of where we are at on the whole thing. However, this is definitely personal opinion and doesn’t necessarily represent Luke’s opinion on the matter.

Essentially, the status is that, of late, I personally have not used UJS at all and have found a much better process by using Low Pro on its own without all the Ruby scaffolding of the UJS plugin. Secondarily, after talking to lots of developers at RailsConf it seems that the UJS plugin has failed to truly achieve it’s main goal which is to get Rails developers to write JavaScript using progressive enhancement. Many people seem to mainly use the plugin to get their JavaScript in to a separate file which is actually not even essential to progressive enhancement and I think this is a failing in the design of UJS itself. To achieve progressive enhancement you really need to think of JavaScript as a separate layer on top of a working HTML application but UJS lets you get away with keeping behavior in your views and hence leads many developers to think in the same way as they did before but think they are unobtrusive because they don’t see any JavaScript in their HTML – which is obviously not what we wanted to achieve. While many people can and do successfully use UJS for progressive enhancement even more seem not to – UJS has not been the ‘angel on your shoulder’ that I originally wanted it to be.

On top of this, the method by which the generated JavaScript is kept in the session has many limitations which myself and Luke have been aware of from the start. It’s not generally a good idea to keep this much information in the session (in fact, normally I never store more than a user ID if I can help it) and while acceptable for light to medium use it does have an upper limit depending on the type of session storage you are using. Rails edge now uses cookies to store session info by default which have a very very low limit which will cripple UJS completely. We have considered other alternatives such as some kind of file based storage but every time it just strikes me as too much scaffolding just to allow developers to put behavior in their view files which, as a said above, I’ve come to believe is a really bad idea anyway.

One of the things that I do personally like and something that has received the most positive feedback are the behavior helpers (the make_*) stuff which essentially encapsulate common tasks with sensible defaults in a very Rails like way. It’s a real time saver and the conventions provided mean that the best path (such as using this.href for the Ajax url) is the easiest. Recently, I’ve come to do this in my own projects via Low Pro and it’s behavior ‘classes’ (although they need a better name!). Now via Low Pro I can write stuff like:

Event.addBehavior({
  '.product a.description' : Remote.Link({ update : 'product_description' }),
  '.product' : Draggable({ revert : true }),
  '#basket' : Droppable
});

I really like this and am slowly building up a library (which you can see if you look at the Low Pro trunk) of common behaviors. There’s a date picker, a drag/drop implementation and the remote stuff I’ve illustrated above. I’m planning on writing autocompleters and in-place editors as behaviors as well. But behaviors have proven really easy to write and I love them as a tool for building site specific components. In fact, as I’ve worked with Low Pro it’s become apparent to me that behaviors are by far the killer feature which is interesting as they were just an experiment I hacked together one day without much thought.

So what to do? Well, there’s two ways to go as far as I can see. The first is to shut down development on UJS completely (or hand it over to another party if anyone is interested) and go on to promote the techniques of implementing progressive enhancement using Low Pro that I’ve found to be so successful recently. This could possibly be via the UJS4Rails site or through my own site – I’m not sure which would be a better platform right now.

The other would be to re-think the UJS plugin totally and go for some kind of 2.0 release that would take a completely different tack. However, all of the ideas for this I’ve thought of or heard so far don’t really compel me to write them. I think to work on this myself I’d need to be sure that I’d want to use it and so far this is not the case but any ideas and feedback are very welcome so please do drop me a mail or feedback on this list.

Either way, we need to make fixes to the current plugin to make it work with Rails 1.2.3 which I’ve been working on recently but I’d love patches if you’ve already solved these issues yourself (which it appears many of you have).

So yes, that’s it. Let me know what you think, I’d appreciate any feedback you have.

Large Mephisto Deployment

Posted about 7 years back at Mephisto - Home

I’m not sure how many other large sites use Mephisto, but I managed to deploy it to the-leaky-cauldron.org the other day (after lots of heavy modding to get things like polls and article ratings working). Leaky gets about 3 million unique visitors a month – and with 9,000 articles and over 300,000 comments (not all of which have converted yet) I thought I’d let you know. —Mephisto group message by Nick Poulden.

I’m not sure, but I think that’s one of the largest Mephisto installations around. Great job, Nick Poulden!