Add minio support and forntend analitycs
This commit is contained in:
@@ -308,6 +308,47 @@ def add_metrics_middleware(app, metrics_collector: MetricsCollector):
|
||||
return metrics_collector
|
||||
|
||||
|
||||
def track_user_activity(user_id: str, action: str, service_name: str = "unknown-service", metadata: dict = None):
|
||||
"""Track user activity metrics using the appropriate metrics collector"""
|
||||
if metadata is None:
|
||||
metadata = {}
|
||||
|
||||
# Add user-specific attributes
|
||||
attributes = {
|
||||
"user.id": user_id,
|
||||
"action": action,
|
||||
**metadata
|
||||
}
|
||||
|
||||
# Get the metrics collector for the specified service
|
||||
metrics_collector = get_metrics_collector(service_name)
|
||||
|
||||
if metrics_collector:
|
||||
# Use the collector's counter registration system
|
||||
counter_name = "user_activity_total"
|
||||
|
||||
# Check if counter already exists, if not register it
|
||||
if counter_name not in metrics_collector._counters:
|
||||
metrics_collector.register_counter(
|
||||
name=counter_name,
|
||||
documentation="Total user activity events"
|
||||
)
|
||||
|
||||
# Increment the counter with attributes
|
||||
metrics_collector.increment_counter(counter_name, value=1, labels=attributes)
|
||||
else:
|
||||
# Fallback: create a temporary counter if no collector exists
|
||||
from opentelemetry import metrics
|
||||
|
||||
meter = metrics.get_meter(__name__)
|
||||
user_activity_counter = meter.create_counter(
|
||||
name="user_activity_total",
|
||||
description="User activity events",
|
||||
unit="events"
|
||||
)
|
||||
user_activity_counter.add(1, attributes)
|
||||
|
||||
|
||||
def setup_metrics_early(
|
||||
app,
|
||||
service_name: str = None,
|
||||
|
||||
Reference in New Issue
Block a user