Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Dearest Monks

I built a small mod_perl handler that logs a message in a custom log file depending on the presence and the value of an HTTP header. It is named Tiscali::LogLoadBalancer and uses the Apache::LogFile module this way in httpd.conf:

PerlModule Apache::LogFile PerlModule Apache::LogFile::Config PerlLogFile "logs/loadbalancer.log" Tiscali::LoadBalancerLogFile <Location /> PerlFixuphandler Tiscali::LogLoadBalancer </Location>

The handler works almost well: header's values are correctly logged into the logs/loadbalancer.log file. E.g.:

Mon Jul  9 16:53:56 2007 [25102] X-Loadbalancer: 0 (OK)

Unfortunately, when the server is heavily stressed (like launching three parallel instances of the ab utility with three different values for that header) it happens quite often that two (or more!) different log messages are merged togehter, e.g.:

Mon Jul  9 16:53:56 2007 [25101] X-Loadbalancer: 2 (INVALID)Mon Jul  9 16:53:56 2007 [25108] X-Loadbalancer: 1 (OK)

or

Mon Jul 9 16:53:56 2007 [25101] X-Loadbalancer: 2 (INVALID)Mon Jul 9 + 16:53:56 2007 [25108] X-Loadbalancer: 1 (OK)Mon Jul 9 16:53:56 2007 + [25102] X-Loadbalancer: 0 (OK) Mon Jul 9 16:53:56 2007 [25101] X-Loadbalancer: 2 (INVALID)

Is there any way to avoid that side effect? When I opted for an Apache::LogFile-based solution I did it also because I believed that this kind of issue was already taken care of by the author, who has a well respected name in the community. I guess I am probably doing something wrong, but I can't tell what... any help?

Module's code follows:

package Tiscali::LogLoadBalancer ; use strict ; use warnings ; use Apache::Constants qw{:common} ; sub handler { my ($self,$r) = @_ ; $r ||= Apache->request() ; my $status ; my $lb = $r->header_in( q{X-Loadbalancer} ) ; if (defined $lb) { $status = q{INVALID} ; $status = q{OK} if $lb == 0 ; $status = q{OK} if $lb == 1 ; } else { $lb = q{UNDEF} ; $status = q{OK} ; } _logger($lb,$status) ; return DECLINED ; } sub _logger { my ($lb_value,$status) = @_ ; my $now = localtime ; my $pid = $$ ; my $message = qq{$now [$pid] X-Loadbalancer: $lb_value ($status)} ; print Tiscali::LoadBalancerLogFile $message ; } 1 ;

Ciao!
--bronto


In theory, there is no difference between theory and practice. In practice, there is.

In reply to Merged lines with Apache::LogFile-based log handler by bronto

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-19 21:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found