• Stars
    star
    154
  • Rank 233,691 (Top 5 %)
  • Language
    Elixir
  • License
    MIT License
  • Created over 7 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A toolkit for building excellent APIs with Elixir

Plexy

Build Status

Interagent-compatible web services in Elixir, inspired by Pliny.

Plexy helps developers write and maintain excellent APIs. It integrates well with other services by implementing logs-as-data, request/response instrumentation, request IDs, and encourages good defaults for logging and exception reporting.

Notably, Plexy is not a framework or a set of code generators. You can use Plexy's plugs with an existing Phoenix or even a vanilla Elixir web app. Plexy will work with you, not dictate how you work. It is also database agnostic.

Installation

  1. Add plexy to your list of dependencies in mix.exs:
def deps do
  [{:plexy, "~> 0.3.0"}]
end
  1. Require Plexy plugs in your application:
defmodule MyRouter do
  use Plug.Router

  plug Plexy.RequestId
  plug Plexy.Instrumentor
  plug :match
  plug :dispatch
end
  1. Call the Plexy.Logger wherever you wish to log information:
Plexy.Logger.count(:http_clients_404, 1)

Usage

Import the plugs into your Plug router or your Phoenix router pipeline as necessary. You can also import the plugs into your own plugs, as needed.

Concepts

Logs-as-data

By publishing consistent and machine-interpretable logs, we are able to monitor our applications and trace requests with tools like Librato and splunk.

It is useful to think of logs as data, instead of just as strings. For logs-as-data, we want to publish logs like

flag1 key1=val1 key2=val2 flag2

This is known as an l2met compatible log format. Outputting logs in this format will allow integrations like Librato to easily show graphs of this data, and allows us to search Splunk for matching loglines.

To output arbitrary data to the log, you can pass a hash to Plexy.Logger and it will format it correctly. For example:

Plexy.Logger.info(test: true, foo: "bar")

becomes

21:02:24.882 request_id=fc06cbd2-b8b6-4257-801d-89253ed83962  test=true foo=bar

You can pass in maps, structs and keyword lists to Plexy.Logger methods, and it will log them correctly without asking you to worry about converting them to strings.

Request IDs

Request IDs are a unique identifier associated with every single request your Plexy app receives. Importing the Plexy.RequestId plug will append the request ID to the connection and also make it available in the Logger metadata.

Request IDs are read on the request coming in to the app, so if you are on a platform like Heroku that adds Request ID headers, Plexy can read the existing Request ID(s) and append its own Request ID to the list. By default, it reads out of the list of headers ["Request-Id", "X-Request-Id"] for incoming requests, but you can configure this list by setting req_headers in Config.

Similarly, it defaults to setting a Request-Id header on the response so that you can easily trace requests. To customize this header, set res_header in Config.

Request/response instrumentation

By default, Plexy will instrument each request and output the timing information in loglines. This, in addition with a request ID, makes it possible to splunk for a particular request and see information about its performance, HTTP response code, etc. An example request / response might look like this in logs:

21:02:24.882 request_id=fc06cbd2-b8b6-4257-801d-89253ed83962  at=start instrumentation=true method=GET path=/hello
21:02:24.884 request_id=fc06cbd2-b8b6-4257-801d-89253ed83962  <metrics>
21:02:24.884 request_id=fc06cbd2-b8b6-4257-801d-89253ed83962  at=finish instrumentation=true method=GET path=/hello elapsed=1.387 status=200

Metrics

Plexy provides some helper functions for taking metric measurements and outputting those metrics to the logs.

Counts

Plexy.Logger.count(metric_name, count) will log the given metric as a count for easy graphing in Librato.

Measures

Plexy.Logger.measure(metric_name, 123) expects a time in milliseconds and logs it as the given metric name.

Plexy.Logger.measure(metric_name, func) will measure the amount of time in milliseconds required to run the given function and logs it as the given metric name. This also returns the value of the given function. It also adds .ms to the metric in case you forget.

Plexy.Logger.measure(:fast, func)
# logs => measure#plexy.fast.ms=123

Plexy.Logger.measure("fast.ms", func)
# logs => measure#my_app.fast.ms=123

Configuring logging

You may need to configure your logging slightly differently, but in general, this pattern will help you create l2met-compatible loglines with UTC times and the Request ID on each line. In config/config.exs:

config :logger,
  utc_log: true

config :logger, :console,
  format: "$time $metadata $message\n",
  metadata: [:request_id]

Make sure that the Plexy.RequestId plug is included in your Elixir app per the Installation instructions, and you will have the request_id in the log metadata.

By default Plexy looks for an ENV variable named APP_NAME and will then fall back to plexy if it is not provided. This value is used for metrics logging.

Plexy.Logger.measure(:fast, 123)
# logs => measure#plexy.fast=123

# APP_NAME env set to "my_app"
Plexy.Logger.measure(:fast, 123)
# logs => measure#my_app.fast=123

If you would like to use a different env var for the app name you can set it.

# config/config.exs
config :plexy,
  app_name: {:system, "HEROKU_APP_NAME"}

# HEROKU_APP_NAME env set to "much_amazing"
Plexy.Logger.measure(:fast, 123)
# logs => measure#much_amazing.fast=123

# or with a default
# config/config.exs
config, :plexy,
  app_name: {:system, "HEROKU_APP_NAME", "such_wow"}

# HEROKU_APP_NAME env is not set
Plexy.Logger.measure(:fast, 123)
# logs => measure#such_wow.fast=123

Configuring exception reporting

We recommend Rollbax as it lives in your logging backends pipeline. This means that the params retracted by the Plexy Redactor won't ever show up in Rollbar. To hide certain keys from your Rollbar reporting, see the next section.

Protecting secrets from appearing in logs or Rollbar

A common pain point for production systems can be inadvertent leaks of secrets to log lines or to exception reporters like Rollbar. While each app will have different values that it considers secret, and how much about a customer or end-user can be logged will depend on the industry, we have provided a generic way to redact certain keys from appearing in logs or being passed through the logging backend pipeline to Rollbax.

To use it, add this to config/config.exs:

 config :plexy, :logger,
   redactors: [
     {Plexy.Logger.SimpleRedactor, [
       redact: ["username"],
       filter: ["password"]
     ]}
   ]

You can also write your own Redactor module and configure it here. The Redactor runs before the data is passed to each logging backend in Plexy.Logger.

Keys that appear in the redact list will appear with the value REDACTED in logs. Keys that appear in the filter list will cause the entire logline to be redacted from the record. Examples:

iex> SimpleRedactor.run("username=bob", redact: ["username"])
{:cont, "username=REDACTED"}
iex> SimpleRedactor.run("password=mysecred", filter: ["password"])
{:cont, ""}

License

Created at Heroku by:

Released under the MIT license.

More Repositories

1

react-refetch

A simple, declarative, and composable way to fetch data for React components
JavaScript
3,439
star
2

legacy-cli

Heroku CLI
Ruby
1,370
star
3

heroku-pg-extras

A heroku plugin for awesome pg:* commands that are also great and fun and super.
JavaScript
1,306
star
4

heroku-buildpack-nodejs

The official Heroku buildpack for Node.js apps.
Shell
1,265
star
5

node-js-getting-started

Getting Started with Node on Heroku
EJS
1,054
star
6

logplex

[DEPRECATED] Heroku log router
Erlang
984
star
7

heroku-buildpack-python

The official Heroku buildpack for Python apps
Ruby
953
star
8

heroku-django-template

A Django 2.0 base template featuring all recommended best practices for deployment on Heroku and local development.
Python
901
star
9

node-js-sample

This repository is deprecated. Head over to https://github.com/heroku/node-js-getting-started
JavaScript
847
star
10

cli

Heroku CLI
JavaScript
847
star
11

rails_12factor

Ruby
845
star
12

python-getting-started

Getting Started with Python on Heroku.
Python
818
star
13

heroku-buildpack-php

The official PHP buildpack for Heroku.
Shell
799
star
14

heroku-buildpack-go

Heroku Go Buildpack
Shell
790
star
15

heroku-buildpack-ruby

Heroku's Ruby Buildpack
Ruby
778
star
16

hk

DEPRECATED: see
Go
709
star
17

heroku-buildpack-static

[DEPRECATED] Heroku buildpack for handling static sites and single page web apps
Ruby
681
star
18

heroku-repo

Plugin for heroku CLI that can manipulate the repo
JavaScript
680
star
19

vegur

Vegur: HTTP Proxy Library
Erlang
620
star
20

heroku-accounts

Helps use multiple accounts on Heroku.
JavaScript
548
star
21

django-heroku

[DEPRECATED] Do not use! See https://github.com/heroku/django-heroku/issues/56
Python
465
star
22

heroku-buildpack-pgbouncer

Run pgbouncer in a dyno along with your application
Shell
335
star
23

devcenter-embedded-tomcat

Java
330
star
24

webapp-runner

Lightweight Application Launcher. Launch your webapp in the most popular open source web container available with a single command.
Java
319
star
25

docker-registry-client

A Go API client for the v2 Docker Registry API
Go
287
star
26

heroku-buildpack-google-chrome

Run (headless) Google Chrome on Heroku
Shell
283
star
27

stack-images

Recipies for building Heroku's stack images
Shell
264
star
28

java-getting-started

Getting Started with Java on Heroku
HTML
248
star
29

identity

[DEPRECATED] Login and OAuth management service for Heroku
CSS
247
star
30

go-getting-started

Getting Started with Go on Heroku https://devcenter.heroku.com/articles/getting-started-with-go
Dockerfile
246
star
31

heroku-buildpack-nginx

Run NGINX in a Heroku app
Shell
242
star
32

heroku-buildpack-apt

Buildpack that installs APT based dependencies
Shell
239
star
33

log-shuttle

HTTP log transport.
Go
236
star
34

terrier

Terrier is a Image and Container analysis tool that can be used to scan Images and Containers to identify and verify the presence of specific files according to their hashes.
Go
226
star
35

umpire

HTTP metrics monitoring endpoint
Ruby
221
star
36

platform-api

Ruby HTTP client for the Heroku API
Ruby
211
star
37

starboard

onboarding, offboarding, or crossboarding made easy
SCSS
204
star
38

salesforce-bulk

Python interface to the Salesforce.com Bulk API
Python
203
star
39

php-getting-started

Getting Started with PHP on Heroku
Twig
200
star
40

heroku-container-tools

DEPRECATED Heroku Toolbelt plugin to help configure, test and release apps to Heroku using local containers.
JavaScript
195
star
41

heroku-buildpack-scala

Heroku buildpack: Scala
Shell
190
star
42

node-heroku-client

A wrapper around the Heroku API for Node.js
JavaScript
188
star
43

roadmap

This is the public roadmap for Salesforce Heroku services.
187
star
44

vulcan

A build server in the cloud.
Ruby
172
star
45

pg_lock

Use Postgres advisory lock to isolate code execution across machines
Ruby
168
star
46

awsdetailedbilling

A toolkit for importing AWS detailed billing reports into Redshift
JavaScript
167
star
47

heroku-buildpack-java

A Heroku buildpack for Java apps.
Shell
167
star
48

pulse

DEPRECATED: Real-time Heroku operations dashboard
Clojure
161
star
49

heroku.rb

DEPRECATED! Official Heroku Ruby Legacy API wrapper
Ruby
161
star
50

heroku-buildpack-multi

[DEPRECATED] Please use https://devcenter.heroku.com/articles/using-multiple-buildpacks-for-an-app instead
Shell
157
star
51

erlang-in-anger

A little guide about how to be the Erlang medic in a time of war. It is first and foremost a collection of tips and tricks to help understand where failures come from, and a dictionary of different code snippets and practices that helped developers debug production systems that were built in Erlang.
TeX
157
star
52

heroku-buildpack-multi-procfile

Everyone gets a Procfile!
Shell
150
star
53

log2viz

DEFUNCT: Realtime analysis of your Heroku app logs.
Ruby
145
star
54

heroku.py

DEPRECATED! Heroku API wrapper for Python.
Python
142
star
55

facebook-template-nodejs

JavaScript
136
star
56

ruby-getting-started

Getting Started with Ruby on Heroku
Ruby
120
star
57

heroku-buildpack-chromedriver

Installs chromedriver in a Heroku slug
Shell
117
star
58

heroku-buildpack-clojure

Heroku's buildpack for Clojure applications.
Shell
115
star
59

mobile-template1

JavaScript
115
star
60

instruments

Collecting metrics over discrete time intervals
Go
112
star
61

heroku-sbt-plugin

An sbt plugin for deploying Heroku Scala applications
Scala
111
star
62

heroku-buildpack-erlang

Erlang buildpack
Shell
107
star
63

cli-engine

TypeScript
97
star
64

semver.io

*DEPRECATED* The semver.io instance has now been sunset: https://github.com/heroku/semver.io/issues/74
CoffeeScript
96
star
65

facebook-template-php

example facebook app for heroku
PHP
96
star
66

terraform-provider-heroku

Terraform Heroku provider
Go
95
star
67

dotnet-buildpack

ASP.NET 5 Buildpack
Shell
92
star
68

kensa

A tool to help Heroku add-on providers integrate their services with Heroku
Ruby
92
star
69

netrc

Reads and writes netrc files.
Ruby
89
star
70

hstore_example

Ruby
89
star
71

alpinehelloworld

An Alpine-based Docker example
Python
85
star
72

heroku-kong

πŸ’ Kong API gateway as a Heroku app
Lua
84
star
73

heroku-buildpack-hello

Shell
82
star
74

heroku-releases-retry

CLI plugin to allow retrying the latest release-phase command
JavaScript
79
star
75

faceplate

A Node.js wrapper for Facebook authentication and API
JavaScript
76
star
76

rails_stdout_logging

Logs to stdout so you don't have to
Ruby
76
star
77

shaas

Shell as a Service: API to inspect and execute scripts in a server's environment via HTTP and WebSockets
Go
75
star
78

devcenter-spring-mvc-hibernate

AspectJ
75
star
79

heroku-buildpack-core-data

A Heroku Buildpack that generates a REST webservice from a Core Data model
Shell
74
star
80

heroku-buildpack-emberjs

**This buildpack is deprecated!** Please use the official Node.js buildpack combined with the static or nginx buildpack instead.
Ruby
72
star
81

facebook-template-python

Python
69
star
82

devcenter-java

Java
62
star
83

heroku-buildpack-c

C Language Pack
Shell
62
star
84

nibs

JavaScript
61
star
85

heroku-buildpack-gradle

This is a Heroku buildpack for Gradle apps. It uses Gradle to build your application and OpenJDK to run it.
Shell
61
star
86

heroku-buildpack-ember-cli

A Heroku buildpack for ember-cli apps; powers dashboard.heroku.com
Shell
60
star
87

heroku-guardian

Easy to use CLI security checks for the Heroku platform. Validate baseline security configurations for your own Heroku deployments.
Python
59
star
88

list-of-ingredients

An example of using Create React App with Rails 5 API and ActiveAdmin on Heroku
Ruby
56
star
89

heroku-fork

Heroku CLI plugin to fork an existing app into a new app
JavaScript
55
star
90

salesforce-buildpack

Heroku Buildpack for Salesforce
Shell
53
star
91

ruby-rails-sample

Ruby
52
star
92

facebook-template-ruby

CSS
52
star
93

heroku-jupyterlab

An example of running JupyterLab on Heroku, with Amazon S3.
Python
52
star
94

heroku-maven-plugin

This plugin is used to deploy Java applications directly to Heroku without pushing to a Git repository.
Java
51
star
95

cnb-builder-images

Recipes for building Heroku's Cloud Native Buildpacks builder images
Shell
51
star
96

stillir

Cache environment variables as Erlang app variables
Erlang
51
star
97

heroku-gradle-plugin

A Gradle plugin for deploying JAR and WAR files to Heroku.
Java
49
star
98

rails_serve_static_assets

Ruby
49
star
99

x

A set of packages for reuse within Heroku Go applications
Go
49
star
100

template-java-spring-hibernate

Java
48
star