Skip to content
This repository was archived by the owner on Sep 2, 2022. It is now read-only.

Repository files navigation

Apilytics for Python

pypicicodecovmypy checkedcode style: blackpython versionslicense

Apilytics is a service that lets you analyze operational, performance and security metrics from your APIs easily.

Apilytics dashboard animation

Installation

  1. Sign up and get your API key from https://apilytics.io - we offer a completely free trial with no credit card required!

  2. Install this package:

pip install apilytics
  1. Enable the middleware and set your API key:
    A good practice is to securely store the API key as an environment variable. You can leave the env variable unset in e.g. development and test environments, the middleware will be automatically disabled if the key is None.

Django

settings.py:

importosAPILYTICS_API_KEY=os.getenv("APILYTICS_API_KEY")
MIDDLEWARE= [
"apilytics.django.ApilyticsMiddleware", # Ideally the first middleware in the list.# ...
]

FastAPI

main.py:

importosfromapilytics.fastapiimportApilyticsMiddlewarefromfastapiimportFastAPIapp=FastAPI()
# Ideally the last middleware you add.app.add_middleware(ApilyticsMiddleware, api_key=os.getenv("APILYTICS_API_KEY"))

Flask

app.py:

importosfromapilytics.flaskimportapilytics_middlewarefromflaskimportFlaskapp=Flask(__name__)
# Ideally wrap your app with the middleware before you do anything else with it.app=apilytics_middleware(app, api_key=os.getenv("APILYTICS_API_KEY"))

Other Python Frameworks

You can easily build your own middleware which measures the execution time and sends the metrics:

my_apilytics_middleware.py:

importosfromapilytics.coreimportApilyticsSenderdefmy_apilytics_middleware(request, get_response):
api_key=os.getenv("APILYTICS_API_KEY")
ifnotapi_key:
returnget_response(request)
withApilyticsSender(
api_key=api_key,
path=request.path,
query=request.query_string,
method=request.method,
request_size=len(request.body),
user_agent=request.headers.get("user-agent"),
ip=request.headers.get("x-forwarded-for", "").split(",")[0].strip(),
) assender:
response=get_response(request)
sender.set_response_info(
status_code=response.status_code,
response_size=len(response.body),
)
returnresponse

Frequently Asked Questions

Does the middleware slow down my backend?

  • No. The middleware does all of its requests to the Apilytics API in a background thread pool, so it will not slow down your normal request handling.

What 3rd party dependencies does apilytics have?

  • None besides the frameworks that you use it in.

What Python versions does the package work with?

About

Apilytics for Python - Easy API analytics for Python backends

Topics

Resources

Stars

6 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages