Too Long; Did Not Read
If you want to save a whole bunch of data for logs but don’t want to spend any money, you can dump large amounts of strings into DynamoDB (without likely exceeding Amazon’s free tier).
I outlined the context of my problem in a previous post (which also has sample code for basic DynamoDB usage with Boto), but basically, in order to troubleshoot or fine tune behavior that was happening in production for a side project, I needed to log what was happening in an understandable (plain English) way.
At work, we use Splunk which is an awesome service, but I ended up not using it. I explain in greater detail below, but at the end of the day, it all boiled down to the fact that it would cost several thousand dollars per month.
Cost Analysis for Paid Logging Services
After some quick googling coupled with an elementary knowledge of existing services, I found a few solutions to my problem at hand. In order to understand the costs of all these services, I wanted to convey the costs in terms of something that everyone can understand: Chipotle Burritos.
To keep things simple, we’re going to assume that this is a steak burrito. Now if we go to Chipotle, there’s going to be about ten people making a single burrito in their production line. When you order double meat from the get go, it wouldn’t matter if Paul Revere was working that day, the double meat message isn’t going to make it to the register. So this is going to be a double meat burrito, but it’ll still be the same price as a regular burrito. Locally, that burrito is $7.40.
At work, we use Splunk. I’m not sure if their free tier has gone away, but the entry level service costs 243 Chipotle Steak Burritos per month, or about 7.8 chipotle burritos per day. Which, if we factor in a reasonable amount of sleep, that’s 1 burrito every 2 hours for an indefinite amount of time.
If we continue down the line of services, I found Loggly, which offers a basic service for only 6.75 Chipotle Burritos per month, which is pretty good, but this is still a decent bit higher than my general allotment of Chipotle Burritos.
As an alternative, I could write my own logger and save all the data to DynamoDB without incurring any charge. However, after a long day of laborious coding, I will be left famished, and I will go to Chipotle and order a single burrito.
So if we tabulate the results:
Cost in Terms of Chipotle Burritos per Month
|Write my own logger and save data to DynamoDB||1 burrito (fixed)|
The choice appears clear. I will write my own logging engine.
A Really Awesome Anecdote
Hark back to my college days, and I was a cadet at the prestigious United States Military Academy at West Point, where for almost 4 years I stressed daily about my overall class rank in order to fulfill my ultimate goal of becoming a helicopter pilot. I changed my mind about becoming a pilot at the last minute and instead opted for the completely uncompetitive nerd branch of the Army, so in hindsight I really should have prioritized more important things in life: lifting weights with greater volume and intensity in order to make that one button on your uniform at chest level eventually break and pop to garner more respect from fellow peers.
But I digress. Every now and then we’d have to complete the tedious task of what was called a Saturday AM Inspection. In which, you’d need to make your room spotless and look like what you’d expect in dramatic military films. I checked on the internets, and you can see some dude put together a timelapse of cleaning his room:
But he’s doing it wrong. The more efficient way to tackle the problem at hand is to instead take all of your personal belongings that don’t necessarily need to be displayed and the bulk amount of all of your things and throw it into a few garbage bags. Then take the garbage bags down to the basement and lock them up. Now you’re already 90% done.
The approach above in the video of tediously managing every detail is liken to using SQL for the problem at hand and defining a schema and strict types as you go about the problem. Key-value storage, however, allows you to just throw a bunch of crap into a consolidated place, which is the approach I opt for in real life, and it’s how I went about collecting and savings logging information. And when you need to query your information, you still know where it is. It’s in the basement locked in the storage container inside of those garbage bags.
Generic Logger Class
With the intent to create classes that were responsible for managing exactly one task, I divvied out responsibilities for posting the information to AWS and actually logging the information specific to a certain use case. A sample generic logger is laid out below:
Specific Use Case Class
Subsequently, I wrote a class that would basically isolate logging statements and logic to one place. All too often I see code that’s been polluted with additional logic that relates specifically to logging and not to the method’s primary task.
One added bonus that I got pretty stoked about was that after I completed my first iteration of this code, I got tons of logging information, but it was difficult to isolate layers of abstraction. So I went ahead and took the current stack trace before every logging statement and tabbed the line over with white space to correspond to the stack’s depth. This made the logging statements appear in blocks that would mimic code but with statements that were in English.
The class below is an abridged version of the full implementation (you get the idea).
The code that ends up calling the logging class doesn’t need to get bogged down in details of how data gets formatted but instead can simply offload a well-express method call (“log_available_exercises” seems pretty self-explanatory, and how verbose that logging becomes isn’t a concern)
(Small Tangent) Profile the Code
Interestingly enough, I was about to write about how slow the added logging had made the application. The tests ran four times slower (I mocked out the actual commit to Amazon), so I assumed this had to do with Python’s notoriously slow string formatting. I was intending to profile the code to show how slow the logger ended up being.
But when I profiled the code, my theory was proven incorrect. The creation of strings was no problem, it was just that an SSL connection was being established with AWS because of my poor decision to establish a connection inside of a constructor (the connection should have been lazily evaluated then cached). And below is output of the code profiling:
After running the logging in production I wrote a separate admin interface that would fetch and display the Dynamo data. You can see a sample block of output below where it’s much easier to trace through and identify problems in a massive slew of business logic.
How it Could Be Better
Obviously, the paid services I didn’t use certainly have more features and filtering, but we’re maintaining a general theme here of side projects, small budget, and Chipotle Burritos. So the paid services clearly would have outperformed my little thrown together program.
For a little more than 5 Chipotle burritos, you could pay Heroku monthly to have a separate virtual machine as a worker, but I’m keeping the free tier, so all of my executed logic lives in the request/response cycle.
The obvious improvement then is to offload all logging to a task since nothing else depends on it and the required network calls will slow things down.