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
Automation of VASP DFT workflows with ASE - application scripts

This repo contains a library that aims at automatizing some Density Functional Theory (DFT) workflows in VASP by using the ASE toolkit.

Frank Niessen 5 Sep 06, 2022
An extended version of the hotkeys demo code using action classes

An extended version of the hotkeys application using action classes. In adafruit's Hotkeys code, a macro is using a series of integers, assumed to be

Neradoc 5 May 01, 2022
a package that provides a marketstrategy for whitelisting on golem

filterms a package that provides a marketstrategy for whitelisting on golem watching requestor logs distribute 10 tasks asynchronously is fun. but you

KJM 3 Aug 03, 2022
Projeto job insights - Projeto avaliativo da Trybe do Bloco 32: Introdução à Python

Termos e acordos Ao iniciar este projeto, você concorda com as diretrizes do Código de Ética e Conduta e do Manual da Pessoa Estudante da Trybe. Boas

Lucas Muffato 1 Dec 09, 2021
NFT generator for Solana!

Solseum NFT Generator for Solana! Check this guide here! Creating your randomized uniques NFTs, getting rarity information and displaying it on a webp

Solseum™ VR NFTs 145 Dec 30, 2022
Tool to generate wrappers for Linux libraries allowing for dlopen()ing them without writing any boilerplate

Dynload wrapper This program will generate a wrapper to make it easy to dlopen() shared objects on Linux without writing a ton of boilerplate code. Th

Hein-Pieter van Braam 25 Oct 24, 2022
Tesla App Update Differences Extractor

Tesla App Update Differences Extractor Python program that finds the differences between two versions of the Tesla App. When Tesla updates the app a l

Adrian 5 Apr 11, 2022
Data on COVID-19 (coronavirus) cases, deaths, hospitalizations, tests • All countries • Updated daily by Our World in Data

COVID-19 Dataset by Our World in Data Find our data on COVID-19 and its documentation in public/data. Documentation Data: complete COVID-19 dataset Da

Our World in Data 5.5k Jan 03, 2023
Account Manager / Nuker with GUI.

Account Manager / Nuker Remove all friends Block all friends Leave all servers Mass create servers Close all dms Mass dm Exit Setup git clone https://

Lodi#0001 1 Oct 23, 2021
Companion Web site for Fluent Python, Second Edition

Fluent Python, the site Source code and content for fluentpython.com. The site complements Fluent Python, Second Edition with extra content that did n

Fluent Python 49 Dec 08, 2022
PyPIContents is an application that generates a Module Index from the Python Package Index (PyPI) and also from various versions of the Python Standard Library.

PyPIContents is an application that generates a Module Index from the Python Package Index (PyPI) and also from various versions of the Python Standar

Collage Labs 10 Nov 19, 2022
Lagrange Interpolation Method-Python

Lagrange Interpolation Method-Python The Lagrange interpolation formula is a way to find a polynomial, called Lagrange polynomial, that takes on certa

Motahare Soltani 2 Jul 05, 2022
Easily Generate Revolut Business Cards

RevBusinessCardGen Easily Generate Revolut Business Cards Prerequisites Before you begin, ensure you have met the following requirements: You have ins

Younes™ 35 Dec 14, 2022
Minos-python - A framework which helps you create reactive microservices in Python

minos-python Summary [TODO] Packages minos-microservice-aggregate minos-microser

Minos Framework 380 Jan 04, 2023
Animation picker for Audodesk Maya 2017 (or higher)

Dreamwall Picker Animation picker for Audodesk Maya 2017 (or higher) Authors: Lionel Brouyère, Olivier Evers This tool is a fork of Hotbox Designer (L

DreamWall 93 Dec 21, 2022
Code and yara rules to detect and analyze Cobalt Strike

Cobalt Strike Resources This repository contains: analyze.py: a script to analyze a Cobalt Strike beacon (python analyze.py BEACON) extract.py; extrac

Tek 224 Jan 04, 2023
A very simple boarding app with DRF

CRUD project with DRF A very simple boarding app with DRF. About The Project 유저 정보를 갖고 게시판을 다루는 프로젝트 입니다. Version Python: 3.9 DB: PostgreSQL 13 Django

1 Nov 13, 2021
A calculator to test numbers against the collatz conjecture

The Collatz Calculator This is an algorithm custom built by Kyle Dickey, used to test numbers against the simple rules of the Collatz Conjecture.

Kyle Dickey 2 Jun 14, 2022
Load, explore and analyse data from Scotland and rest of the world related to Covid19.

Streamlit Examples This is my first attempt with Streamlit. It is an open-source framework, free, Python-based and easy to use tool to build and deplo

Eyad Elyan 12 Mar 01, 2021
Test for using pyIIIFpres for rara magnetica project

raramagnetica_pyIIIFpres Test for using pyIIIFpres for rara magnetica project. This test show how to use pyIIIFpres for creating mannifest compliant t

Giacomo Marchioro 1 Dec 03, 2021