Skip to main content

Middleware to integrate aws x-ray with chocs.

Project description

chocs-aws-xray

AWS X-Ray middleware for chocs library.

Installation

through poetry:

poetry add chocs_middleware.xray

or through pip:

pip install chocs_middleware.xray

Usage

The following snippet is the simplest integration example.

Please note x-ray won't work in WSGI mode, it has to be deployed as aws lambda in order to work.

from chocs import Application, HttpResponse, serve
from chocs_middleware.xray import AwsXRayMiddleware

app = Application(AwsXRayMiddleware())


@app.get("/hello")
def say_hello(request):
    return HttpResponse("Hello")

serve(app)

Setting up custom error handler

AWS X-Ray middleware provides a way to setup a custom error handler which may become handy when you need to supplement your logs with additional information. Please consider the following example:

from chocs import Application, HttpResponse, HttpStatus
from chocs_middleware.xray import AwsXRayMiddleware

def error_handler(request, error, segment):
    segment.add_exception(error)
    
    return HttpResponse("NOT OK", HttpStatus.INTERNAL_SERVER_ERROR)

app = Application(AwsXRayMiddleware(error_handler=error_handler))


@app.get("/hello")
def say_hello(request):
    raise Exception("Not Today!")
    return HttpResponse("Hello")

To learn more about error_handler interface please click here.

Accessing x-ray recorded from within your application layer

from chocs import Application, HttpResponse
from chocs_middleware.xray import AwsXRayMiddleware

app = Application(AwsXRayMiddleware())

@app.get("/hello")
def say_hello(request):
    xray_recorder = request.attributes["aws_xray_recorder"] # Here is the instance of your recorder.
    
    return HttpResponse("OK")

That's all.

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

chocs_middleware.xray-1.0.0.tar.gz (4.5 kB view details)

Uploaded Source

File details

Details for the file chocs_middleware.xray-1.0.0.tar.gz.

File metadata

  • Download URL: chocs_middleware.xray-1.0.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.8.12 Linux/5.11.0-1025-azure

File hashes

Hashes for chocs_middleware.xray-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4cdeeef1cb88422e36624520d1815c2d05176094eeb1d353ad14c6467a8270a4
MD5 f0e3d5b5f120d7de8ab959abbf18235c
BLAKE2b-256 6792bff1cd72721d381064b0d9e4941dabf321e0d520e329ad216c2f2525a983

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