Apilytics for Python - Easy API analytics for Python backends

Overview

apilytics-python

pypi ci codecov mypy checked code style: black python versions license

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:

import os

APILYTICS_API_KEY = os.getenv("APILYTICS_API_KEY")

MIDDLEWARE = [
    "apilytics.django.ApilyticsMiddleware",
]

FastAPI:

main.py:

import os

from apilytics.fastapi import ApilyticsMiddleware
from fastapi import FastAPI

app = FastAPI()

app.add_middleware(ApilyticsMiddleware, 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:

import os

from apilytics.core import ApilyticsSender


def my_apilytics_middleware(request, get_response):
  with ApilyticsSender(
      api_key=os.getenv("APILYTICS_API_KEY"),
      path=request.path,
      method=request.method,
  ) as sender:
      response = get_response(request)
      sender.set_response_info(status_code=response.status_code)
  return response

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?

Comments
  • Explicitly handle closing `urllib.request.urlopen` requests

    Explicitly handle closing `urllib.request.urlopen` requests

    It does close self.close() in its __del__ method[1], but it's better to not just rely on the garbage collector here.

    [1]: At least in CPython: https://github.com/python/cpython/blob/cd26595232ac1b5061460d5949d5204c90287c1c/Lib/urllib/request.py#L1737

    opened by ruohola 1
  • Send user's IP address with metrics

    Send user's IP address with metrics

    Used for visualization aggregate geolocation data. The IP is never stored, and it is never sent to 3rd parties.

    We read the IP from the X-Forwarded-For header since in production one's backend service is most likely behind a reverse proxy of some sort. We use the left-most value in case there are multiple IPs in it, since that's most likely not from "our" infra and we don't have to worry about spoofing too much because it's not used for anything security related.

    Some additional reading about getting the real IP address: https://adam-p.ca/blog/2022/03/x-forwarded-for/

    opened by ruohola 1
  • Remove 'OS Independent' trove classifier

    Remove 'OS Independent' trove classifier

    We are not strictly OS independent anymore, since we access Linux specific system functionality if it's available. The package does still work on any system though.

    opened by ruohola 1
  • Don't cancel concurrent CI builds running for merged commits on master

    Don't cancel concurrent CI builds running for merged commits on master

    This would happen for the commit that was merged first:

    Canceling since a higher priority waiting request for 'ci-' exists

    More info: https://docs.github.com/en/actions/using-jobs/using-concurrency#example-using-a-fallback-value

    opened by ruohola 1
  • Add platform name to sent Apilytics version info

    Add platform name to sent Apilytics version info

    Use sys.platform for this (instead of platform.system()) since it's more consistent with Node.js's process.platform values.

    Atm rebased on top of #14, it should be merged first.

    opened by ruohola 1
  • Send memory usage information together with metrics on Linux

    Send memory usage information together with metrics on Linux

    We handle only Linux since getting accurate memory usage values on it is most important. Memory cannot be read platform independently in Python and we don't want at this point to include any external dependencies, such as psutil. We might want to add support for other systems when the need arises for some user, it likely won't happen soon since since production web servers are not often ran on non-Linux systems.

    Good to note that MemAvailable in /proc/meminfo is only available on Linux kernel 3.14 and up, but since 3.14 has already reached end of life we can pretty safely ignore support for systems before it.

    opened by ruohola 1
  • Initial version

    Initial version

    Add first version of the middleware package.

    • Out-of-the-box support for Django and FastAPI.
    • Supports Python 3.7, 3.8, 3.9, and 3.10.
    • 100% test coverage.
    opened by ruohola 1
Releases(v1.6.0)
Owner
Apilytics
API analytics made easy
Apilytics
This simple script generates a backup of a given Python and R environment

Python Environment Backup It’s always good to maintain your Python and R Anaconda environment packages properly listed and well-kept in case you have

Andrew Laganaro 1 Jul 13, 2022
Python script which allows for automatic registration in Golfbox

Python script which allows for automatic registration in Golfbox

Guðni Þór Björnsson 8 Dec 04, 2021
Recreate the joys of Office Assistant from the comfort of the Python interpreter

Recreate the joys of Office Assistant from the comfort of the Python interpreter.

Louis Sven Goulet 3 May 21, 2022
Feapder的管道扩展

FEAPDER 管道扩展 简介 此模块为feapder的pipelines扩展,感谢广大开发者对feapder的贡献 随着feapder支持的pipelines越来越多,为减少feapder的体积,特将pipelines提出,使用者可按需安装 管道 PostgreSQL 贡献者:沈瑞祥 联系方式:r

boris 9 Dec 07, 2022
[draft] tools for schnetpack

schnetkit some tooling for schnetpack EXPERIMENTAL/IN DEVELOPMENT DO NOT USE This is an early draft of some infrastructure built around schnetpack. In

Marcel 1 Nov 08, 2021
Start and stop your NiceHash miners using this script.

NiceHash Mining Scheduler Use this script to schedule your NiceHash Miner(s). Electricity costs between 4-9pm are high in my area and I want NiceHash

SeaRoth 2 Sep 30, 2022
Curso de Python 3 do Básico ao Avançado

Curso de Python 3 do Básico ao Avançado Desafio: Buscador de arquivos Criar um programa que faça a pesquisa de arquivos. É fornecido o caminho e um te

Diego Guedes 1 Jan 21, 2022
Resources for the 2021 offering of COMP 598

comp598-2021 Resources for the 2021 offering of COMP 598 General submission instructions Important Please read these instructions located in the corre

Derek Ruths 23 May 18, 2022
A python implementation of differentiable quality diversity.

Differentiable Quality Diversity This repository is the official implementation of Differentiable Quality Diversity.

ICAROS 41 Nov 30, 2022
🌈Python cheatsheet for all standard libraries(Continuously Updated)

Python Standard Libraries Cheatsheet Depend on Python v3.9.8 All code snippets have been tested to ensure they work properly. Fork me on GitHub. 中文 En

nick 12 Dec 27, 2022
FantasyBballHelper - Espn Fantasy Basketball Helper

ESPN FANTASY BASKETBALL HELPER The simple goal of this project is to allow fanta

1 Jan 18, 2022
Boot.img patcher for Tolino ebook readers to enable ADB and root.

I'm not responsible for any damage to your devices by running this tool. Please note that you may loose warranty when using this, although (This is no

Aaron Dewes 9 Nov 13, 2022
This repo contains scripts that add functionality to xbar.

xbar-custom-plugins This repo contains scripts that add functionality to xbar. Usage You have to add scripts to xbar plugin folder. If you don't find

osman uygar 1 Jan 10, 2022
A custom advent of code I am completing

advent-of-code-custom A custom advent of code I am doing in python. The link to the problems I am solving is here: https://github.com/seldoncode/Adven

Rocio PV 2 Dec 11, 2021
Powerful Assistant

Delta-Assistant Hi I'm Phoenix This project is a smart assistant This is the 1.0 version of this project I am currently working on the third version o

1 Nov 17, 2021
About Python's multithreading and GIL

About Python's multithreading and GIL

Souvik Ghosh 3 Mar 01, 2022
Make your Discord Account Online 24/7!

Online-Forever Make your Discord Account Online 24/7! A Code written in Python that helps you to keep your account 24/7. The main.py is the main file.

SealedSaucer 0 Mar 16, 2022
This repository contains a lot of short scripting programs implemented both in Python (Flask) and TypeScript (NodeJS).

fast-scripts This repository contains a lot of short scripting programs implemented both in Python (Flask) and TypeScript (NodeJS). In python These wi

Nahum Maurice 3 Dec 10, 2022
Release for Improved Denoising Diffusion Probabilistic Models

improved-diffusion This is the codebase for Improved Denoising Diffusion Probabilistic Models. Usage This section of the README walks through how to t

OpenAI 1.2k Dec 30, 2022
Automatically find solutions when your Python code encounters an issue.

What The Python?! Helping you find answers to the errors Python spits out. Installation You can find the source code on GitHub at: https://github.com/

What The Python?! 139 Dec 14, 2022