API Analytics Documentation
repository·main·Indexed 20 days ago
https://github.com/tom-draper/api-analyticsAn API monitoring solution providing a visualization dashboard and a Data API for raw request logs. It offers lightweight middleware for various frameworks, including Node.js (Express, Fastify, Koa, Hono, NestJS, H3, Elysia, Bun, Oak), Python (FastAPI, Flask, Django, Tornado), and Ruby (Rails, Sinatra). Features include configurable privacy levels, custom user ID mapping, and a REST API for retrieving logged request data using an API key.
What's inside API Analytics
- The Backend Service Logger is an API designed to handle and store incoming requests that contain logged request information. This information is recorded and posted to the logger by analytics middleware installed on your API services.
What is the Monitor program?
mainThe Monitor is a utility designed to ping all user-registered URLs to monitor their availability. It records and stores both HTTP status codes and response times. By default, it is intended to be scheduled as a cron job to run every 30 minutes.Understand collected data and privacy
mainActix Analytics collects pseudo-anonymous data to populate your dashboard. Data is stored in compliance with GDPR.
Collected Data Points:
- Path requested
- Client IP address
- Client operating system
- Client browser
- Request method (GET, POST, etc.)
- Time of request
- Status code
- Response time
- API hostname
- API framework (e.g., FastAPI, Flask, Express)
The API key is the only link between you and the logged data. If lost, you cannot access your analytics.
Configure privacy levels and custom User IDs
mainYou can control how much client data is stored using
privacy_levelin theConfigobject:0(default): IP address is used to infer location and is stored.1: IP address is used to infer location, then discarded.2: IP address is never accessed and location is never inferred.
Example setting privacy level 2:
config = Config() config.privacy_level = 2You can also define a custom user identifier by providing a
get_user_idmapper function. This is useful if you want to track users by an ID found in your own request headers (e.g., an auth token):config = Config() config.get_user_id = lambda request: request.headers.get('X-AUTH-TOKEN', '')Understand collected request data
mainAPI Analytics records a specific subset of metadata for every request to populate your dashboard. The collected data includes:
- Request method (GET, POST, PUT, etc.)
- Endpoint requested
- User agent
- Client IP address (optional, depending on privacy level)
- Timestamp of the request
- Response status code
- Response time
- Hostname of API
- API framework in use (FastAPI, Flask, Express, etc.)
Data is pseudo-anonymous; the API key is the only link between your identity and the logged data.
Core components of @api-analytics/core
mainThe core logic of the JavaScript middleware is composed of the following exported entities:
Config: A configuration class used to customize analytics behavior. It allows you to provide optional mapper functions to override how request data is extracted.Mappers: A collection of default mapper implementations designed for Node.js-style request objects.Analytics: The central client responsible for batching request data and posting it to the analytics server.getIPAddress: A utility function that resolves the client's IP address while respecting the configured privacy settings.
Maintain and update self-hosted API Analytics
mainDatabase Management
The database is initialized using
database/schema.sql. To run custom SQL commands against theanalyticsdatabase:docker exec -it db psql -U postgres -d analytics -c "YOUR SQL COMMAND;"Updates
To update the backend to the latest version (note: this causes downtime):
docker compose down git pull origin main docker compose up -dLogs
If you encounter issues, check the following logs:
- Nginx:
docker logs nginx - Logger:
docker exec -it logger tail requests.log - API:
docker exec -it api tail api.log
# Run SQL docker exec -it db psql -U postgres -d analytics -c "SELECT 1;" # Update docker compose down git pull origin main docker compose up -d # Logs docker logs nginx docker exec -it logger tail requests.log docker exec -it api tail api.log- Nginx:
Install and use Fira Code fonts
mainFira Code is available as a single variable font file or as a collection of static font files.
Variable Font
If your application supports variable fonts, use the single file containing the
wghtaxis. This allows you to select intermediate weights between the standard styles.- File:
FiraCode-VariableFont_wght.ttf
Static Fonts
If your application does not support variable fonts, use the individual static files for specific weights:
static/FiraCode-Light.ttfstatic/FiraCode-Regular.ttfstatic/FiraCode-Medium.ttfstatic/FiraCode-SemiBold.ttfstatic/FiraCode-Bold.ttf
Quickstart
- Install the desired font files onto your system.
- Use your application's font picker to select the Fira Code family and the desired style.
- File:
Add API Analytics middleware to FastAPI
mainInstall the FastAPI integration using pip:
pip install api-analytics[fastapi]Then, add the
Analyticsmiddleware to your FastAPI application instance, providing yourapi_key.import uvicorn from fastapi import FastAPI from api_analytics.fastapi import Analytics app = FastAPI() app.add_middleware(Analytics, api_key="YOUR-API-KEY") # Add middleware @app.get('/') async def root(): return {'message': 'Hello World!'} if __name__ == "__main__": uvicorn.run("app:app", reload=True)Access your analytics via the Dashboard
mainYou can view visualizations and statistics for your API by visiting the Rocket Analytics dashboard. Paste your API key at apianalytics.dev/dashboard to access your data. Note that if you use the same API key across multiple API servers, all data will be aggregated into the same dashboard.Delete stored analytics data
mainYou can manually delete all data associated with your API key by visiting apianalytics.dev/delete and providing your API key.
Automatic Deletion Policy:
- API keys and request data are scheduled for deletion after 6 months of dashboard inactivity.
- Data is also deleted if 3 months have elapsed without a new request being logged.
Add API Analytics middleware to Rails
mainTo monitor a Rails application, add the
Analytics::Railsmiddleware inconfig/application.rb. You must provide your unique API key generated at apianalytics.dev/generate.require 'rails' require 'api_analytics' Bundler.require(*Rails.groups) module RailsMiddleware class Application < Rails::Application config.load_defaults 6.1 config.api_only = true config.middleware.use ::Analytics::Rails, "YOUR-API-KEY" # Add middleware end end