Skip to main content

reddit edge request context baggage

Project description

edgecontext

Python Documentation: https://reddit-edgecontext.readthedocs.io/en/latest/

Go Documentation: https://pkg.go.dev/github.com/reddit/edgecontext/lib/go/edgecontext

Services deep within the backend often need to know information about the client that originated the request, such as what user is authenticated or what country they're in. Baseplate services can get this information from the edge context which is automatically propagated along with calls between services.

This library provides a Thrift specification of an edge context payload and a corresponding implementation of the EdgeContextFactory interface from Baseplate.py.

And an implementation of ecinterface from Baseplate.go.

Usage

Python

Add the EdgeContextFactory to application startup:

from baseplate import Baseplate
from baseplate.lib.secrets import secrets_store_from_config
from reddit_edgecontext import EdgeContextFactory


def make_processor(app_config):
    secrets = secrets_store_from_config(app_config, timeout=60)
    edgecontext_factory = EdgeContextFactory(secrets)

    # pass edgecontext_factory to your framework's integration
    # for Thrift: baseplate.frameworks.thrift.baseplateify_processor
    # for Pyramid: baseplate.frameworks.pyramid.BaseplateConfigurator

Then read fields while handling requests:

def my_view(request):
    return request.edge_context.user.id

See the documentation for all the available fields.

Go

Use edgecontext.Factory to create an ecinterface.Factory implementation that's expected by baseplate.New:

ctx, bp, err := baseplate.New(context.Background(), baseplate.NewArgs{
  ConfigPath: configPath,
  ServiceCfg: &cfg, // or nil if you don't have additional config to parse
  EdgeContextFactory: edgecontext.Factory(edgecontext.Config{
    Logger: log.ErrorWithSentryWrapper(),
  }),
})

When using it, get the *EdgeRequestContext object out of context:

if ec, ok := edgecontext.GetEdgeContext(ctx); ok {
  user := ec.User()
  loid, ok := user.LoID()
  // Do something with loid
}

Development

A Dockerfile is provided to get a development environment running. To use it, build the base Docker image:

$ docker build -t edgecontext .

And then fire up the environment and use the provided Makefile targets to do common tasks:

$ docker run -it -v $PWD:/src --user "$(id -u):$(id -g)" -w /src edgecontext
$ make fmt

The following make targets are provided:

  • fmt: Apply automatic formatting to the source code.
  • thrift: Generate code from the Thrift IDL. Run fmt after doing this.
  • lint: Run linters on the code.
  • test: Run the test suite.
  • docs: Build docs.
    • Python output can be found in lib/py/build/html/.

The generated Thrift code is committed to the Git repo, so if you change edgecontext.thrift make sure to run make thrift fmt and commit those changes as well.

For Go, we do the same linting checks as Baseplate.go, so please follow Baseplate.go's Editor guide to make sure you are doing the same linting locally correctly. Please also follow Baseplate.go's Style guide for code style.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

reddit_edgecontext-1.6.3.tar.gz (20.9 kB view details)

Uploaded Source

Built Distribution

reddit_edgecontext-1.6.3-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file reddit_edgecontext-1.6.3.tar.gz.

File metadata

  • Download URL: reddit_edgecontext-1.6.3.tar.gz
  • Upload date:
  • Size: 20.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.9.6 requests/2.28.2 setuptools/47.3.0 requests-toolbelt/0.10.1 tqdm/4.64.1 CPython/3.8.16

File hashes

Hashes for reddit_edgecontext-1.6.3.tar.gz
Algorithm Hash digest
SHA256 201fdb59f13204f6b6e1ae42fff496d95df1f79cd21ab7601eb15d7661255a7f
MD5 fc42ac29506068e20906a2709816ef49
BLAKE2b-256 701b8db0840910a39955e5169f3731bc7fc2d37effad1cff9cb59dca455aa19d

See more details on using hashes here.

File details

Details for the file reddit_edgecontext-1.6.3-py3-none-any.whl.

File metadata

  • Download URL: reddit_edgecontext-1.6.3-py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.9.6 requests/2.28.2 setuptools/47.3.0 requests-toolbelt/0.10.1 tqdm/4.64.1 CPython/3.8.16

File hashes

Hashes for reddit_edgecontext-1.6.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6958a6ff7d8069616142e9acbec4393dc7909f59af24862a43ef571bbcb44282
MD5 76d094866fe53285e4f538ef6ae669c1
BLAKE2b-256 98d83043ee2f4673bfc1036ce59ed284a6966d0bedc0ac93d77d23112372d225

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page