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
Active Transport Analytics Model: A new strategic transport modelling and data visualization framework

{ATAM} Active Transport Analytics Model Active Transport Analytics Model (“ATAM”

ATAM Analytics 2 Dec 21, 2022
Audio-analytics for music-producers! Automate tedious tasks such as musical scale detection, BPM rate classification and audio file conversion.

Click here to be re-directed to the Beat Inspect Streamlit Web-App You are a music producer? Let's get in touch via LinkedIn Fundamental Analytics for

Stefan Rummer 11 Dec 27, 2022
Simple and easy to use python API for the COVID registration booking system of the math department @ unipd (torre archimede)

Simple and easy to use python API for the COVID registration booking system of the math department @ unipd (torre archimede). This API creates an interface with the official browser, with more useful functionalities.

Guglielmo Camporese 4 Dec 24, 2021
This repository provides a set of easy to understand and tested Python samples for using Acronis Cyber Platform API.

Base Acronis Cyber Platform API operations with Python !!! info Copyright © 2019-2021 Acronis International GmbH. This is distributed under MIT licens

Acronis International GmbH 3 Aug 11, 2022
An easy way to access the Scratch API!

The majority of people are likely here because they want to easily access the Scratch API!

rgantzos 0 May 4, 2022
A Python utility belt containing simple tools, a stdlib like feel, and extra batteries. Hashing, Caching, Timing, Progress, and more made easy!

Ubelt is a small library of robust, tested, documented, and simple functions that extend the Python standard library. It has a flat API that all behav

Jon Crall 638 Dec 13, 2022
Built with Python programming language and QT library and Guess the number in three easy, medium and hard rolls

guess-the-numbers Built with Python programming language and QT library and Guess the number in three easy, medium and hard rolls Number guessing game

Amir Hussein Sharifnezhad 5 Oct 9, 2021
Built with Python programming language and QT library and Guess the number in three easy, medium and hard rolls

password-generator Built with Python programming language and QT library and Guess the number in three easy, medium and hard rolls Password generator

Amir Hussein Sharifnezhad 3 Oct 9, 2021
Sabe is a python framework written for easy web server setup.

Sabe is a python framework written for easy web server setup. Sabe, kolay web sunucusu kurulumu için yazılmış bir python çerçevesidir. Öğrenmesi kola

null 2 Jan 1, 2022
Easy way to build a SaaS application using Python and Dash

EasySaaS This project will be attempt to make a great starting point for your next big business as easy and efficent as possible. This project will cr

xianhu 3 Nov 17, 2022
A modern python module including many useful features that make discord bot programming extremely easy.

discord-super-utils Documentation Secondary Documentation A modern python module including many useful features that make discord bot programming extr

null 106 Dec 19, 2022
An easy python calculator for those who want's to know how if statements, loops, and imports works give it a try!

A usefull calculator for any student or anyone who want's to know how to build a simple 2 mode python based calculator.

Antonio Sánchez 1 Jan 6, 2022
A simple and easy to use Python's PIP configuration manager, similar to the Arch Linux's Java manager.

PIPCONF - The PIP configuration manager If you need to manage multiple configurations containing indexes and trusted hosts for PIP, this project was m

João Paulo Carvalho 11 Nov 30, 2022
A collection of common regular expressions bundled with an easy to use interface.

CommonRegex Find all times, dates, links, phone numbers, emails, ip addresses, prices, hex colors, and credit card numbers in a string. We did the har

Madison May 1.5k Dec 31, 2022
Connect Playground - easy way to fill in your account with production-like objects

Just set of scripts to initialise accpunt with production-like data: A - Basic Distributor Account Initialization INPUT Distributor Account Token ACTI

CloudBlue 5 Jun 25, 2021
Tie together `drf-spectacular` and `djangorestframework-dataclasses` for easy-to-use apis and openapi schemas.

Speccify Tie together drf-spectacular and djangorestframework-dataclasses for easy-to-use apis and openapi schemas. Usage @dataclass class MyQ

Lyst 4 Sep 26, 2022
tgEasy | Easy for a Brighter Shine | Monkey Patcher Addon for Pyrogram

tgEasy | Easy for a Brighter Shine | Monkey Patcher Addon for Pyrogram

Jayant Hegde Kageri 35 Nov 12, 2022
Generate Openbox Menus from a easy to write configuration file.

openbox-menu-generator Generate Openbox Menus from a easy to write configuration file. Example Configuration: ('#' indicate comments but not implement

null 3 Jul 14, 2022