How to Check R2R Logs and Use Analytics & Observability Features

This guide covers various methods to access and analyze R2R logs, as well as leverage R2R’s powerful analytics and observability features. These capabilities allow you to monitor system performance, track usage patterns, and gain valuable insights into your RAG application’s behavior.

1. Checking R2R Logs

1.1 Docker Deployment

If you’re running R2R using Docker:

  1. List running containers:

    $docker ps
  2. View real-time logs:

    $docker logs -f <container_id_or_name>
  3. Using Docker Compose:

    $docker-compose logs -f r2r

1.2 Local Deployment

For local deployments without Docker:

  1. Check the R2R configuration for log file locations.
  2. Use standard Unix tools to view logs:
    $tail -f /path/to/r2r.log

1.3 Cloud Deployments

  • For Azure: Use “Log stream” or “Diagnose and solve problems” in the Azure portal.
  • For AWS: Use CloudWatch or check EC2/ECS logs directly.

2. Using R2R’s Logging and Analytics Features

The features described in this section are typically restricted to superusers. Ensure you have the necessary permissions before attempting to access these features.

2.1 Fetching Logs

You can fetch logs using the client-server architecture:

$r2r logs

Expected Output:

1[
2 {
3 'run_id': UUID('27f124ad-6f70-4641-89ab-f346dc9d1c2f'),
4 'run_type': 'rag',
5 'entries': [
6 {'key': 'search_query', 'value': 'Who is Aristotle?'},
7 {'key': 'search_latency', 'value': '0.39'},
8 {'key': 'search_results', 'value': '["{\\"id\\":\\"7ed3a01c-88dc-5a58-a68b-6e5d9f292df2\\",...}"]'},
9 {'key': 'rag_generation_latency', 'value': '3.79'},
10 {'key': 'llm_response', 'value': 'Aristotle (Greek: Ἀριστοτέλης Aristotélēs; 384–322 BC) was...'}
11 ]
12 },
13 # More log entries...
14]

2.2 Using Analytics

R2R offers an analytics feature for aggregating and analyzing log data:

$r2r analytics --filters '{"search_latencies": "search_latency"}' --analysis-types '{"search_latencies": ["basic_statistics", "search_latency"]}'

Expected Output:

1{
2 'results': {
3 'filtered_logs': {
4 'search_latencies': [
5 {
6 'timestamp': '2024-06-20 21:29:06',
7 'log_id': UUID('0f28063c-8b87-4934-90dc-4cd84dda5f5c'),
8 'key': 'search_latency',
9 'value': '0.66',
10 'rn': 3
11 },
12 ...
13 ]
14 },
15 'search_latencies': {
16 'Mean': 0.734,
17 'Median': 0.523,
18 'Mode': 0.495,
19 'Standard Deviation': 0.213,
20 'Variance': 0.0453
21 }
22 }
23}

3. Advanced Analytics and Observability

3.1 Custom Analytics

You can specify different filters and analysis types to focus on specific aspects of your application’s performance:

1from r2r import FilterCriteria, AnalysisTypes
2
3# Analyze RAG latencies
4rag_filter = FilterCriteria(filters={"rag_latencies": "rag_generation_latency", "rag_eval": "rag_eval_metric"})
5rag_analysis = AnalysisTypes(analysis_types={"rag_latencies": ["basic_statistics", "rag_generation_latency"]})
6rag_analytics = app.analytics(rag_filter, rag_analysis)
7
8# Track usage patterns by user
9user_filter = FilterCriteria(filters={"user_patterns": "user_id"})
10user_analysis = AnalysisTypes(analysis_types={"user_patterns": ["bar_chart", "user_id"]})
11user_analytics = app.analytics(user_filter, user_analysis)
12
13# Monitor error rates
14error_filter = FilterCriteria(filters={"error_rates": "error"})
15error_analysis = AnalysisTypes(analysis_types={"error_rates": ["basic_statistics", "error"]})
16error_analytics = app.analytics(error_filter, error_analysis)

3.2 Preloading Data for Analysis

To get meaningful analytics, you can preload your database with random searches:

1import random
2from r2r import R2R, GenerationConfig
3
4app = R2R()
5
6queries = [
7 "What is artificial intelligence?",
8 "Explain machine learning.",
9 "How does natural language processing work?",
10 "What are neural networks?",
11 "Describe deep learning.",
12 # Add more queries as needed
13]
14
15for _ in range(1000):
16 query = random.choice(queries)
17 app.rag(query, GenerationConfig(model="openai/gpt-4o-mini"))
18
19print("Preloading complete. You can now run analytics on this data.")

3.3 User-Level Analytics

To get analytics for a specific user:

1user_id = "your_user_id_here"
2
3user_filter = FilterCriteria(filters={"user_analytics": "user_id"})
4user_analysis = AnalysisTypes(analysis_types={
5 "user_analytics": ["basic_statistics", "user_id"],
6 "user_search_latencies": ["basic_statistics", "search_latency"]
7})
8
9user_analytics = app.analytics(user_filter, user_analysis)
10print(f"Analytics for user {user_id}:")
11print(user_analytics)

4. Log Analysis Tips

  1. Look for ERROR or WARNING level logs first.
  2. Check timestamps to correlate logs with observed issues.
  3. Use tools like grep, awk, or sed to filter logs.
  4. For large log files, use less with search functionality.

5. Log Aggregation Tools

Consider using log aggregation tools for more advanced setups:

  1. ELK Stack (Elasticsearch, Logstash, Kibana)
  2. Prometheus and Grafana
  3. Datadog
  4. Splunk

Summary

R2R’s logging, analytics, and observability features provide powerful tools for understanding and optimizing your RAG application. By leveraging these capabilities, you can:

  • Monitor system performance in real-time
  • Analyze trends in search and RAG operations
  • Identify potential bottlenecks or areas for improvement
  • Track user behavior and usage patterns
  • Make data-driven decisions to enhance your application’s performance and user experience

Remember to rotate logs regularly and set up log retention policies to manage disk space, especially in production environments.

For more advanced usage and customization options, consider joining the R2R Discord community or referring to the detailed R2R documentation.