A tracking library for Databricks demo analytics.
Project description
dbdemos-tracker
A Python library for tracking your asset usage among bricksters. This will filter and collect actions on @databricks.com emails only (used by dbdemos among other)
The data is synched on logfood (soon in opal self service).
Data is aggregated at team level, we're anonymizing the report and not tracking individual.
Installation
Add dbdemos-tracker in your requirements.txt or equivalent:
pip install dbdemos-tracker
Usage
App Name
Please use your repository name as app name so that we can easily match the demo.
Fast API example
FastAPI Middleware (recommended, easiest way if you're using FastAPI backend)
We provide a tool to track all app views out of the box.
Note: Try to filter on the main actions so that we don't have too many views using patterns=["/my/.*/url/to/track", "..."]
from fastapi import FastAPI
from dbdemos_tracker import Tracker
# Create FastAPI app
app = FastAPI(title="My Demo App")
# Add tracking middleware - this will track ALL requests automatically
Tracker.add_tracker_fastapi(app, "app-name"))
# Or alternatively, you can filter to only track some URLs using the patterns arg:
Tracker.add_tracker_fastapi(app, "app-name", patterns=['/', '/demos/.*/load']))
# Then all your API endpoints will be tracked out of the box (no need to change them)
@app.get("/")
def home():
return {"message": "Welcome to my demo app"}
Manual tracking with FastAPI
Use this if you want to track a single backend API call
from dbdemos_tracker import Tracker
from fastapi import FastAPI, Request
from databricks.sdk import WorkspaceClient
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = FastAPI(title="Databricks App with Tracking")
# Create tracker for app - Use your repo name as app name
ws = WorkspaceClient()
tracker = Tracker(org_id=ws.get_workspace_id(), demo_name="app-name-use-repo-name")
# Track app views in functions
@app.get("/")
def home(request: Request):
user_email = request.headers.get("X-Forwarded-Email")
path = request.url.path
if user_email:
try:
tracker.track_app_view(user_email=user_email, app_path=path)
except Exception as e:
logger.warning(f"Tracking error: {e}")
# ... Then add your custom implementation
return {"message": "Welcome to my demo app"}
Manual App Usage Tracking
Use this to track classic, non databricks apps (you'll have to figure out the user email somehow)
from dbdemos_tracker import Tracker
# Create tracker for app - Use your repo name as app name
ws = WorkspaceClient()
tracker = Tracker(org_id=ws.get_workspace_id(), demo_name="app-name")
# Track app views in functions
@app.get("/dashboard")
def home():
#Get the user email from your app
user_email="user@databricks.com"
tracker.track_app_view(user_email=user_email, app_path="/dashboard")
return {"message": "Welcome to my demo app"}
Flask Example
Databricks App Usage Tracking example with Flask backend
from flask import Flask, request, jsonify
from databricks.sdk import WorkspaceClient
from dbdemos_tracker import Tracker
import logging
app = Flask(__name__)
logger = logging.getLogger(__name__)
ws = WorkspaceClient()
tracker = Tracker(org_id=ws.get_workspace_id(), demo_name="app-name")
@app.route("/", methods=["GET"])
def home():
user_email = request.headers.get("X-Forwarded-Email")
path = request.path
if user_email:
try:
tracker.track_app_view(user_email=user_email, app_path=path)
except Exception as e:
logger.warning(f"Tracking error: {e}")
return jsonify({"message": "Welcome to my demo app"})
Streamlit Integration
Initialize Tracker (call once at the top of your app):
import streamlit as st
from dbdemos_tracker import Tracker
# Initialize tracker - only runs once per session
Tracker.setup_streamlit_tracker("app-name")
# Optional: Set user email for tracking
st.session_state.user_email = "user@databricks.com"
Option 1: Track Button Clicks with Callbacks (Recommended)
# Simple callback tracking
st.button(
"Process Data",
on_click=Tracker.create_tracked_callback("process_data_clicked")
)
# Callback with custom logic
def my_callback():
st.session_state.counter += 1
st.button(
"Increment Counter",
on_click=Tracker.create_tracked_callback("increment_clicked", my_callback)
)
Option 2: Manual Tracking After User Actions
if st.button("Analyze Data"):
Tracker.track_streamlit_event("analyze_clicked")
# ... do your analysis
if st.button("Export Results"):
Tracker.track_streamlit_event(
"export_clicked",
industry="Financial Services",
demo_catalog_id="catalog-123"
)
# ... export logic
Option 3: Track with Decorators
@Tracker.streamlit_callback_tracker("data_processed", industry="Healthcare")
def process_data():
# Heavy computation
return result
st.button("Process", on_click=process_data)
Track Form Submissions:
with st.form("my_form"):
name = st.text_input("Name")
age = st.slider("Age", 0, 100)
submitted = st.form_submit_button(
"Submit",
on_click=Tracker.create_tracked_callback("form_submitted")
)
if submitted:
st.write(f"Processing {name}, age {age}")
Track Widget Changes:
def on_model_change():
Tracker.track_streamlit_event(
"model_selected",
app_path=f"/model/{st.session_state.selected_model}"
)
st.selectbox(
"Choose Model",
["GPT-4", "Claude", "Llama"],
key="selected_model",
on_change=on_model_change
)
Track Page Views (Multi-page Apps):
# In pages/dashboard.py
import streamlit as st
from dbdemos_tracker import Tracker
Tracker.setup_streamlit_tracker("my-demo-app")
Tracker.track_page_view("dashboard")
st.title("Dashboard")
# ... page content
Configuration
Disable Tracking
# Disable tracking globally
Tracker.enable_tracker = False
# Re-enable when needed
Tracker.enable_tracker = True
Email Filtering
The tracker automatically filters emails to only track Databricks email addresses (@databricks.com). Non-Databricks emails are ignored for privacy.
Test Workspace
Tracking is automatically disabled for test workspace ID 1660015457675682.
Features
- Track demo usage analytics
- Configurable tracking enable/disable
- Automatic Databricks email filtering
- Error handling with timeout protection
- FastAPI middleware for automatic request tracking
- Streamlit integration for app interaction tracking
- Support for both notebook and app path tracking
- User hash generation for privacy
License
Databricks License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dbdemos_tracker-0.1.7-py3-none-any.whl.
File metadata
- Download URL: dbdemos_tracker-0.1.7-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bdc12f2f35f650cbdd192c8c30de4512ed4594119d295dd2ac97925139bff97a
|
|
| MD5 |
0617117444f016677e17fb203dd6907b
|
|
| BLAKE2b-256 |
e6b5406d419a843e7b2d80cdcb6ad453ff31da2d3a4fcbcd64c9327200f9b6ae
|