Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Rails 4 app on Heroku to receive SendGrid event hooks and provide a searchable archive.
branch: master
Failed to load latest commit information.
app Clarify why we use word-break: break-all
bin
config Add more complete development dependency info.
db Fix missing syntax
lib Fix remove events scheduler
log Generated Rails 4 project.
public humans.txt
script Change db dump schema format to sql
spec Update gems: attr_extras kaminari, nokogiri, sass, sprockets-rails
vendor/assets Generated Rails 4 project.
.gitignore Generated Rails 4 project.
.rspec Prepare for request tests.
.ruby-gemset .rvmrc -> .ruby-{gemset,version}
Gemfile Update gems: attr_extras, multi_json, rspec-core,
Gemfile.lock Update gems: attr_extras, multi_json, rspec-core,
Procfile Use unicorn as webserver
README.md Add CircleCI badge
Rakefile Generated Rails 4 project.
circle.yml Change to a shell script for deploy
config.ru Enable gzip compression
dev.yml Add more complete development dependency info.

README.md

CircleCi

gridlook

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.

Installation

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
# 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.

# 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:sesame@my-gridlook.herokuapp.com/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.

Notes

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.rb handles incoming events.
  • config/environments/production.rb sets up HTTP auth.

Also note that Gridlook uses Postgres-specific DB triggers (to show an accurate event count).

Development

script/bootstrap
rake  # Run tests

Maintenance

Some useful commands working with heroku:

heroku pg:psql # database console

# Needs to be installed as a heroku addon (pgbackups)
heroku pgbackups:capture # database backup
heroku pgbackups: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 pgbackups: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

Heroku scheduler

Auto tuning

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. To do that you could use the heroku scheduler. Follow the the heroku scheduler guide. You find the specific task here.

Auto tuning is done to clean the postgres database and also help the db planner (caching and so on). Read more about it here.

Remove events older than six month

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. You can do that by adding a task to the scheduler (see links above).

TODO

  • Tests that auth is applied in prod

Also see

  • Postman, backed by keen.io. May cost significantly more than Gridlook/Heroku Postgres for large event volumes.

Contributors

Barsoom

Johan Lundström

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.

Something went wrong with that request. Please try again.