Rails 4 app on Heroku to log SendGrid webhook events.
It improves on SendGrid's email activity page in these ways:
- The history is not truncated (unless you truncate it yourself)
- Filtering on an address is a GET with a query parameter, so you can link from internal systems
- Shows more useful information if you follow some conventions
Uses gridhook to parse the events.
Check out this repo (clone it first if you like).
Create and set up a Heroku app:
heroku create my-gridlook # Choose a better name. # Set up DB per https://devcenter.heroku.com/articles/heroku-postgresql # If you're not cheap and know you'll need more than you get with the free plan, # you might want to set up another plan and save yourself the bother of a plan migration. heroku addons:add heroku-postgresql:dev heroku config | grep HEROKU_POSTGRESQL # See what ENV was used for this DB. heroku pg:promote HEROKU_POSTGRESQL_RED_URL # Replace "RED" with whatever ENV was used. # Configure HTTP auth. heroku config:set HTTP_USER=alibaba HTTP_PASSWORD=sesame # ^ there is also JWT (Json Web Token) auth, see spec/features/jwt_authentication_spec.rb # Configure Rails secret key (not actually used yet). heroku config:set SECRET_KEY_BASE=`rake secret` # Configure Rails time zone. heroku config:set RAILS_TZ=CET # Whatever you prefer. # Optional custom header: # heroku config:set CUSTOM_HTML_HEADER="<div>Your custom header on top of the page, can include ERB like <%= params[:action] %></div>" # Deploy app. git push heroku # Load DB schema. heroku run rake db:schema:load # Open app in browser. heroku open
Note that this sets you up for the free Postgres plan which has a maximum of 10 000 rows. You may need to change plans pretty quick if you send a lot of mail. Each mail may generate several rows, one per mail event.
Now your app is deployed. The next step is to configure SendGrid. Follow their setup instructions.
Don't forget to both configure and enable the app.
Hit "Select all" if you want all events. Enable batch event notifications.
The URL you configure should be something like
https://alibaba:firstname.lastname@example.org/events. Make sure to use your own values for HTTP auth username, password, and the Heroku app name.
As long as you use Heroku, you get https for free. If you use your own domain, you need to set stuff up.
That should be it. SendGrid should start sending you events and your app should start logging and showing them.
More information with conventions
If you configure your mails to include the mailer as a category containing a "#", e.g. "CustomerMailer#signup", that information will be shown at the top and can be filtered.
Don't worry about deploy downtime etc. SendGrid will retry:
SendGrid expects a 200 HTTP response to the POST, otherwise the event notification will be retried. If your URL returns a non-200 HTTP code it will be deferred and retried for 24 hours.
The license for SendGrid's event icons is unclear. We asked and received no response. We will replace them if they complain.
Non-obvious places in the app:
config/initializers/gridhook.rbhandles incoming events.
config/environments/production.rbsets up HTTP auth.
Also note that Gridlook uses Postgres-specific DB triggers (to show an accurate event count).
script/bootstrap rake # Run tests
Some useful commands working with heroku:
heroku pg:psql # database console # Needs to be installed as a heroku addon (pgbackups) heroku pg:backups capture # database backup heroku pg:backups public-url # get an download url to the latest backup curl -L <url> -o $(date +%y-%m-%d)\_gridlook_db # Download <url> (url given by heroku pg:backups public-url) with format: year-month-day_gridlook_db # Sometimes it's nice to get more info in the production server log. heroku config:add LOG_LEVEL="<level>" # level = info/debug heroku logs -t # Show log tailed heroku run console # Rails console
You can use Heroku Scheduler for some recurring tasks.
You find the scheduler dashboard here.
Gridlook defines some Rake task suitable for scheduling, here.
If you find that some queries are slow or that you have other db issues it could be smart to set up some auto tuning of the postgres db.
Auto tuning is done to clean the postgres database and also help the db planner (caching and so on). Read more about it here.
If you want this, you may schedule
rake scheduler:database_tuning to run e.g. daily.
Remove events older than a limit
If you have a limited heroku db plan and a lot of events coming in, you will sooner or later need to delete old events.
To automate that, you may schedule
rake scheduler:remove_events to run e.g. hourly.
Currently, you must edit the source code of the task to tweak the event limit.
- Tests that auth is applied in prod
- Postman, backed by keen.io. May cost significantly more than Gridlook/Heroku Postgres for large event volumes.
Credits and license
By Barsoom under the MIT license:
Copyright (c) 2012 Barsoom AB
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.