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
BlackIP-Rep is a tool designed to gather the reputation and information of Bulk IP's.

BlackIP-Rep is a tool designed to gather the reputation and information of Bulk IP's. Focused on increasing the workflow of Security Operations(SOC) team during investigation.

0LiVEr 6 Dec 12, 2022
EloGGs 🎮 is a 1v1.LOL Trophy Boosting Program (PATCHED)

EloGGs 🎮 is an old patched 1v1.LOL boosting program I developed months ago, My team made around $1000 total off of this, but now it's been patched by the developers.

doop 1 Jul 22, 2022
App and Python library for parsing, writing, and validation of the STAND013 file format.

python-stand013 python-stand013 is a Python app and library for parsing, writing, and validation of the STAND013 file format. Features The following i

Oda 3 Nov 09, 2022
Custom SLURM wrapper scripts to make finding job histories and system resource usage more easily accessible

SLURM Wrappers Executables job-history A simple wrapper for grabbing data for completed and running jobs. nodes-busy Developed for the HPC systems at

Sara 2 Dec 13, 2021
A simple string parser based on CLR to check whether a string is acceptable or not for a given grammar.

A simple string parser based on CLR to check whether a string is acceptable or not for a given grammar.

Bharath M Kulkarni 1 Dec 15, 2021
Runnable Python demo of ArtLine

artline-demo How to run? pip3 install -r requirements.txt python3 app.py How to use? Run the Flask app Open localhost:5000 in browser Select an image(

Jiang Wenjian 134 Jul 29, 2022
Add your recently blog and douban states in your GitHub Profile

Add your recently blog and douban states in your GitHub Profile

Bingjie Yan 4 Dec 12, 2022
🗽 Like yarn outdated/upgrade, but for pip. Upgrade all your pip packages and automate your Python Dependency Management.

pipupgrade The missing command for pip Table of Contents Features Quick Start Usage Basic Usage Docker Environment Variables FAQ License Features Upda

Achilles Rasquinha 529 Dec 31, 2022
This is a small compiler to demonstrate how compilers work.

This is a small compiler to demonstrate how compilers work. It compiles our own dialect to C, while being written in Python.

Md. Tonoy Akando 2 Jul 19, 2022
Unofficial package for fetching users information based on National ID Number (Tanzania)

Nida Unofficial package for fetching users information based on National ID Number made by kalebu Installation You can install it directly or using pi

Jordan Kalebu 57 Dec 28, 2022
Python solution of advent-of-code 2021

Advent of code 2021 Python solutions of Advent of Code 2021 written by Eric Bouteillon Requirements The solutions were developed and tested using Pyth

Eric Bouteillon 3 Oct 25, 2022
A Python script to convert your favorite TV series into an Anki deck.

Ankiniser A Python3.8 script to convert your favorite TV series into an Anki deck. How to install? Download the script with git or download it manualy

37 Nov 03, 2022
Este script añade la config de s4vitar a bspwm automaticamente!

Se ha testeado este script en ParrotOS, Kali y Ubuntu. Funciona para todos los sistemas operativos basados en Debian. Instalación git clone https://gi

yorkox 201 Dec 30, 2022
Traductor de webs desde consola usando el servicio de Google Traductor.

proxiGG Traductor de webs desde consola usando el servicio de Google Traductor. Se adjunta el código fuente para Python3 y un binario compilado en C p

@as_informatico 2 Oct 20, 2021
The Python Achievements Framework!

Pychievements: The Python Achievements Framework! Pychievements is a framework for creating and tracking achievements within a Python application. It

Brian 114 Jul 21, 2022
Your copilot to studies and work (Pomodoro-timer, Translate and Notes app)

Copylot Your copilot to studies and work (Pomodoro-timer, Translate and Notes app) Copylot are three applications in one: Pomodoro Translate Notes Cop

Eduardo Mendes 20 Dec 16, 2022
An animal facts python module

An animal facts python module

Fayas Noushad 3 Dec 19, 2021
Blender addon that simplifies access to useful operators and adds missing functionality

Quick Menu is a Blender addon that simplifies common tasks Compatible with Blender 3.x.x Install through Edit - Preferences - Addons - Install... -

passivestar 94 Dec 27, 2022
Data Applications Project

DBMS project- Hotel Franchise Data and application project By TEAM Kurukunda Bhargavi Pamulapati Pallavi Greeshma Amaraneni What is this project about

Greeshma 1 Nov 28, 2021
Library to emulate the Sneakers movie effect

py-sneakers Port to python of the libnms C library To recreate the famous data decryption effect shown in the 1992 film Sneakers. Install pip install

Nicolas Rebagliati 11 Aug 27, 2021