Baseplate.py¶
It’s much easier to manage a bunch of services when they all have the same shape: the way they’re developed, the way they interact with the infrastructure they run on, and the way they interact with each other. Baseplate is reddit’s specification for the common shape of our services. This library, Baseplate.py, is the Python implementation of that specification.
Baseplate.py integrates with existing application frameworks and provides battle-tested libraries to give you everything you need to build a well-behaving production service without having to reinvent the wheel.
Here’s a simple Baseplate.py HTTP service built using the Pyramid web framework:
from baseplate import Baseplate
from baseplate.clients.sqlalchemy import SQLAlchemySession
from baseplate.frameworks.pyramid import BaseplateConfigurator
from pyramid.config import Configurator
from pyramid.view import view_config
@view_config(route_name="hello_world", renderer="json")
def hello_world(request):
result = request.db.execute("SELECT date('now');")
return {"Hello": "World", "Now": result.scalar()}
def make_wsgi_app(app_config):
baseplate = Baseplate(app_config)
baseplate.configure_observers()
baseplate.configure_context({"db": SQLAlchemySession()})
configurator = Configurator(settings=app_config)
configurator.include(BaseplateConfigurator(baseplate).includeme)
configurator.add_route("hello_world", "/", request_method="GET")
configurator.scan()
return configurator.make_wsgi_app()
Every request to this example service will automatically emit telemetry that allows you to dig into how the service is performing under the hood:
Timers for how long the whole request took and how long was spent talking to the database.
Counters for the success/failure of the whole request and each query to the database.
Distributed tracing spans (including carrying over trace metadata from upstream services and onwards to downstream ones).
Reporting of stack traces to Sentry on crash.
And you don’t have to write any of that.
To get started, dive into the tutorial. Or if you need an API reference, look below.
Table of Contents¶
- Tutorial
- User Guide
- API Documentation
- Observability Framework
- baseplate: The skeleton of the telemetry framework
- baseplate.clients: Integration with client libraries
- baseplate.clients.cassandra: Cassandra CQL Client
- baseplate.clients.kombu: Client for publishing to queues
- baseplate.clients.memcache: Memcached Client
- baseplate.clients.redis: Redis Client
- baseplate.clients.redis_cluster: Redis Cluster Client
- baseplate.clients.requests: Requests (HTTP) Client
- baseplate.clients.sqlalchemy: SQL Client for relational databases (e.g. PostgreSQL)
- baseplate.clients.thrift: Thrift client for RPC to other backend services
- baseplate.frameworks: Integration with application frameworks
- baseplate.observers: Diagnostics observers
- The Library
- baseplate.lib.config: Configuration parsing
- baseplate.lib.crypto: Cryptographic Primitives
- baseplate.lib.datetime: Extensions to the standard library's datetime module
- baseplate.lib.edgecontext: Information about the original request from the client
- baseplate.lib.events: Events for the data pipeline
- baseplate.lib.experiments: Experiments framework
- baseplate.lib.file_watcher: Read files from disk as they change
- baseplate.lib.live_data: Tools for centralized data that updates near instantly
- baseplate.lib.message_queue: POSIX IPC Message Queues
- baseplate.lib.metrics: Counters, timers, gauges, and histograms for statsd
- baseplate.lib.random: Extensions to the standard library's random module
- baseplate.lib.ratelimit: Ratelimit counters in memcached or redis
- baseplate.lib.retry: Policies for retrying operations
- baseplate.lib.secrets: Secure storage and access to secret tokens and credentials
- baseplate.lib.thrift_pool: A Thrift client connection pool
- baseplate.lib.service_discovery: Integration with Synapse service discovery
- Observability Framework
- CLI Tools
- Linters