Skip to main content

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.

Metadata

Release files for reddit-edgecontext 1.10.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for reddit-edgecontext 1.10.0
File Size Uploaded Provenance Actions
reddit_edgecontext-1.10.0.tar.gz 21.6 kB Without provenance Details for reddit_edgecontext-1.10.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for reddit-edgecontext 1.10.0
File Interpreter ABI Platform Actions
reddit_edgecontext-1.10.0-py3-none-any.whl Python 3 none any Details for reddit_edgecontext-1.10.0-py3-none-any.whl

Total release size: 34.0 kB

Release files / reddit_edgecontext-1.10.0.tar.gz

Download URL reddit_edgecontext-1.10.0.tar.gz
Size 21.6 kB
Tags Source
SHA-256 checksum
How to use checksums
2f72025143b82e008f89ede92f05c53174e0428d6aec761e973c60a4714684f5
BLAKE2b-256 checksum
How to use checksums
8c48a5c3c836016fd6311c4ef7c8fae1b4820fcaa334eca977b7e45b21fb750b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.23

Release files / reddit_edgecontext-1.10.0-py3-none-any.whl

Download URL reddit_edgecontext-1.10.0-py3-none-any.whl
Size 12.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e10902ce2e1de7e606e18b02a42a1f0f16bcb736c113a8f3f76926dd0f693956
BLAKE2b-256 checksum
How to use checksums
d34842a94a4d9a70ebc54f86abf586867b6296f9f813db9ebbdd6b4e50ac72bd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.23

Security for reddit-edgecontext 1.10.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page