Instrumenting PHP: A Minimalist Approach

I needed a quick way to measure performance and log errors in my Sift Science for WooCommerce plugin. I didn’t want to go back through all my code and embed logging and timing measurement statements, so I considered a more generic and lazy approach.

I decided to create a class that wraps the class I want to measure/monitor. Its constructor takes a class instance, it saves that instance. Then, for every function call to the wrapper class, the function in the underlying class is called and information is logged as needed. Here’s the class from the project:

https://github.com/Fermiac/woocommerce-siftscience/blob/master/includes/class-wc-siftscience-instrumentation.php

How it Works

The most interesting piece of code in this class is here:

public function __call( $name, $args ) {
  $metric = "{$this->prefix}_{$name}";
  $timer = $this->stats->create_timer( $metric );
  $error_timer = $this->stats->create_timer( "error_$metric" );
  try {
    $result = call_user_func_array( array( $this->subject, $name ), $args );
    $this->stats->save_timer( $timer );
    return $result;
  } catch ( Exception $exception ) {
    $this->logger->log_exception( $exception );
    throw $exception;
  }
}

It’s pretty straight forward; I use the PHP magic function __call for all method calls to the class. I initialize timers and then call the function of the class I’m wrapping. If it succeeds or fails I log the information I want and then I pass on the result or exception.

Drawbacks

The main drawback to this approach is that this wrapper class can’t be passed around the same way as the original class. That is, if you’re using PHP type hints, the wrapper class will not implement the same interface as the class it wraps. This also means that IDEs will have trouble auto-completing your code.

To compensate for this, I generally pass the original class to the constructors that need them. I then use the instrumentation class instance to plug into WordPress hooks. You lose a bit of detail in that you only get measurements on the outer surface of your plugin, but overall I’ve found this approach to be satisfactory. You can still calculate the total run time of your plugin and it’s nearly impossible for an exception to be thrown without it getting caught by the wrapper.

Conclusion

I found this approach easy and simple for collecting metrics and useful logs throughout my plugin. I may carve it out and share it as re-usable code one day, but for now the class has dependencies on other classes specific to my project. However, it wouldn’t be hard to copy the class and modify to your specific need.

Host your own podcast with PHPodcast

I’ve been using a little PHP script for the past few months to host my own private podcast. So I decided to clean it up a little and share it on GitHub.

https://github.com/nabsul/phpodcast

A little background: I had some audio files that I wanted to listen through with the ability to increase the speed and pause at any time to continue later. This is everything that most podcast apps do. So I decided to host my own private podcast channel containing the audio files.

PHPodcast is a simple script that creates a podcast RSS feed based on a directory of audio files. You’ll need to modify a config file to your specific setup, but everything you need to edit is clearly marked.

It basically saves you the time of learning how to correctly build and format a podcast RSS feed. Nothing super hard, but doing it from scratch can take a few hours of research, trial and error.

Here’s a live podcast served up using PHPodcast:

https://phpodcast.nabeel.us/

Enjoy!

I have a fantastic startup idea!

“I have a fantastic startup idea.”

You’d be surprised how often I hear this. When I do, it’s often followed by “All I need is a developer. Let’s partner up!”.

Now don’t get me wrong, I think entrepreneurship is a great thing, and I often hear ideas that are genuinely interesting. However, a great idea isn’t enough to justify taking on such an endeavor.

Over the years, through trial and many, many errors, I’ve developed a few rules to help filter these pitches. Here are some tips:

Are you the right person for the job?

I’ve had someone pitch me their startup idea and tell me “Most of my friends are married and have kids. You’re still single, so I figure you have plenty of free time and could even quit your job to work on this.” Yeah, thanks for choosing me over all the qualified candidates.

Seriously though, even when it’s not that obvious, you should ask yourself if you’re really the right person for the job. Do you have the experience needed? Some things you can learn on the job, but if you don’t have what it takes to deliver, things will not be fun.

Are they willing to put in the time and effort?

This is my favorite because it weeds out 80% of the pitches. A lot of people think they have great ideas, but are not really willing to put in the time and effort needed to succeed. Before committing to any project, try to determine how serious the potential partner is.

Have they done any serious research about their business idea? Ask them to write up a short summary of their idea or put together an excel sheet with some concrete numbers. Thinking up a storm of ideas and dreaming of wild success is fun. You’ll be surprised how many people give up when even the smallest amount of real work is required of them.

Will they be honest and fair partners?

This one’s important because failing to catch it early can result in heartache after lots of wasted time and effort. If everything else looks good and you’re considering working with this person, you’re eventually going to discuss partnership details. Often this boils down to who owns what percentage of this theoretical non-existent venture.

If they try to low-ball or otherwise offer you much less than you deserve, just walk away and never look back. Here’s why: If they’re already trying to haggle with you over something that doesn’t yet exist, just imagine what will happen if any level of success is achieved.

Seriously, avoid this type of partner at all cost.

Conclusion

I hope these tips were helpful. This wasn’t meant to be a discouraging post. Entrepreneurship (or simply building things outside of your 9-5 job) is really fun and a great experience, even if you make a bunch of mistakes and fail along the way.