AI-Driven Observability: Helping AI to help you!

Gaurav Behere
7 min readJun 15, 2024

--

Enhancing AI-Powered End-to-End Observability - A Developer’s Guide

Photo by Growtika on Unsplash

Artificial Intelligence (AI) has emerged as a powerful tool for enhancing observability. AI-driven observability can help developers detect anomalies, predict issues, and provide actionable insights to optimize performance. However, to leverage AI effectively, developers need to implement certain practices and tools that facilitate AI in gathering, processing, and analyzing data comprehensively.

This article delves into how developers can aid AI in improving end-to-end observability, focusing on better logging practices, the use of correlation IDs, integrating AI with existing observability tools, and utilizing custom prompts and large language models (LLMs) for advanced insights.

The Role of AI in Observability

AI can revolutionize observability by automating data analysis, identifying patterns, and predicting potential failures. By analyzing vast amounts of log data, metrics, and traces, AI can provide:

  1. Anomaly Detection: Identifying unusual patterns that could indicate problems.
  2. Root Cause Analysis: Pinpointing the exact source of issues in complex systems.
  3. Predictive Maintenance: Forecasting potential failures before they occur.
  4. Performance Optimization: Offering insights to enhance application performance.

For AI to perform these tasks effectively, it requires high-quality data from various sources within the application. This is where developers play a crucial role.

Better Logging Practices

Logging is the backbone of observability. Effective logging provides the necessary data for AI to analyze and draw insights. Here are some best practices for better logging:

1. Structured Logging

Structured logging involves outputting logs in a consistent, machine-readable format such as JSON. This enables AI tools to parse and analyze logs more effectively. For example:

{
"timestamp": "2024-06-15T12:34:56Z",
"level": "INFO",
"message": "User login successful",
"userId": "12345",
"sessionId": "abcde",
"correlation-id": '123-34-3333',
"ui-component": "Login Screen"
}

Structured logs can be easily indexed and searched, making it simpler for AI algorithms to detect patterns and anomalies.

2. Log Levels

Using appropriate log levels (DEBUG, INFO, WARN, ERROR) helps in filtering and analyzing logs based on the severity of the events. This categorization aids AI in focusing on critical issues while ignoring less significant events.

import logging

logging.basicConfig(level=logging.INFO)

logging.debug("This is a debug message")
logging.info("This is an info message")
logging.warning("This is a warning message")
logging.error("This is an error message")
logging.critical("This is a critical message")

3. Contextual Information

Including contextual information in logs, such as user IDs, session IDs, and transaction IDs, helps in correlating events across different parts of the system. This is crucial for AI to perform accurate root cause analysis and trace user actions across the application.

4. Avoid Logging Sensitive Information

While detailed logs are essential, it is equally important to avoid logging sensitive information like passwords, credit card numbers, and personal data. This ensures compliance with data privacy regulations and protects user information.

Correlation IDs in Backend & Frontend Logs

Correlation IDs are unique identifiers assigned to a particular transaction or request. They play a critical role in tracing a request through the entire system, from frontend to backend, making it easier to diagnose issues and understand the flow of data.

1. Implementing Correlation IDs in the Backend

In backend systems, correlation IDs can be generated at the entry point of a request (e.g., API gateway) and passed through all subsequent services. Here’s an example in a Python Flask application:

from flask import Flask, request, g
import uuid

app = Flask(__name__)

@app.before_request
def before_request():
g.correlation_id = request.headers.get('X-Correlation-ID', str(uuid.uuid4()))

@app.route('/process', methods=['POST'])
def process_request():
correlation_id = g.correlation_id
app.logger.info(f"Processing request with Correlation ID: {correlation_id}")
# Process the request
return {"message": "Request processed", "correlation_id": correlation_id}

if __name__ == "__main__":
app.run()

In this example, the before_request function checks for an existing correlation ID in the request headers. If not found, it generates a new one. The correlation ID is then logged and can be used throughout the processing of the request.

2. Passing Correlation IDs to Frontend

Frontend applications should also be designed to handle and propagate correlation IDs. When making API calls, the front end should include the correlation ID in the request headers. Here’s an example in JavaScript:

function generateCorrelationId() {
return 'xxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0,
v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}

const correlationId = generateCorrelationId();

fetch('/api/process', {
method: 'POST',
headers: {
'X-Correlation-ID': correlationId,
'Content-Type': 'application/json'
},
body: JSON.stringify({ data: 'sampleData' })
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));

In this example, a correlation ID is generated and included in the request headers for the API call. This correlation ID can then be logged by the backend, allowing the traceability of the request from frontend to backend.

Integrating AI with Observability Tools

To maximize the benefits of AI in observability, developers should integrate AI with existing observability tools. This can be done through various means:

1. Using AI-Powered Observability Platforms

Several observability platforms incorporate AI to enhance monitoring and diagnostics. Tools like Splunk, Dynatrace, and Datadog provide AI-driven insights and anomaly detection. Integrating these tools into your observability stack can significantly improve your ability to monitor and troubleshoot applications.

2. Custom AI Models

For organizations with specific needs, developing custom AI models to analyze log data and metrics can be beneficial. Leveraging machine learning frameworks like TensorFlow or PyTorch, developers can build models tailored to their applications’ unique requirements.

3. Automated Alerting and Remediation

AI can be integrated with alerting systems to provide automated responses to detected issues. For example, if AI identifies an anomaly indicating a potential service outage, it can trigger automated scripts to mitigate the issue or alert the on-call team with detailed diagnostic information.

Leveraging Custom Prompts and LLMs for Advanced Observability

Large Language Models (LLMs), such as GPT-4, can be used to further enhance observability. LLMs can process and analyze unstructured data, generate natural language explanations, and offer advanced insights that traditional tools might miss. Here’s how developers can leverage custom prompts and LLMs for observability:

1. Custom Prompts for Log Analysis

Developers can create custom prompts to query logs and metrics using natural language. This can simplify the process of extracting insights from large datasets. For example:

from transformers import GPT2Tokenizer, GPT2LMHeadModel
import torch

tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
model = GPT2LMHeadModel.from_pretrained('gpt2')

def generate_insight(prompt):
inputs = tokenizer.encode(prompt, return_tensors='pt')
outputs = model.generate(inputs, max_length=150)
return tokenizer.decode(outputs[0], skip_special_tokens=True)

prompt = "Analyze the following logs and identify potential issues:\n"
logs = """
2024-06-15T12:34:56Z INFO User login successful userId=12345 sessionId=abcde
2024-06-15T12:35:00Z ERROR Database connection failed userId=12345 sessionId=abcde
2024-06-15T12:35:05Z WARN API response time is high userId=12345 sessionId=abcde
"""
response = generate_insight(prompt + logs)
print(response)

In this example, a custom prompt is created to analyze logs and identify potential issues. The LLM processes the logs and generates a natural language summary of potential problems.

2. Natural Language Alerts and Explanations

LLMs can be used to convert complex log data and metrics into human-readable alerts and explanations. This can help non-technical stakeholders understand the issues and their impact. For example:

def generate_alert_explanation(log_data):
prompt = f"Explain the following log data in simple terms:\n{log_data}"
return generate_insight(prompt)

log_data = """
2024-06-15T12:35:00Z ERROR Database connection failed userId=12345 sessionId=abcde
"""
explanation = generate_alert_explanation(log_data)
print(explanation)

The LLM translates the log data into a simple explanation, making it easier for stakeholders to grasp the issue.

3. Interactive Debugging and Root Cause Analysis

Using LLMs, developers can interactively query their observability data to perform root cause analysis. By asking questions in natural language, they can quickly pinpoint the source of an issue. For example:

def interactive_debugging(query):
prompt = f"Given the following logs, answer the query: {query}\n{logs}"
return generate_insight(prompt)

query = "What caused the database connection to fail?"
debugging_response = interactive_debugging(query)
print(debugging_response)

The Big Picture

Holistically when structured logging & contextual logging are in place & you have LLMs watching over the logs & the correlation IDs connect the pieces of the puzzles, you can query LLM with custom prompts or use some existing solution like Splunk.

Conclusion

End-to-end observability is essential for maintaining the performance, reliability, and user satisfaction of modern applications. By adopting better logging practices, using correlation IDs, integrating AI with observability tools, and leveraging custom prompts and LLMs, developers can significantly enhance their ability to monitor and troubleshoot their systems.

AI-powered observability not only helps in detecting and resolving issues faster but also provides predictive insights that can prevent problems before they impact users. As developers, by providing high-quality, structured data and leveraging advanced observability tools, we can harness the full potential of AI to ensure our applications run smoothly and efficiently. Utilizing LLMs further enhances our capabilities, enabling natural language interaction with observability data, simplifying complex analysis, and making insights accessible to a broader audience.

Note: The Python code samples are indicative & are used to convey the pseudo-code.

Also, Read — the preface to this article https://medium.com/@gaurav-techgeek/human-friendly-observability-with-generative-ai-8919eb42fcc6

You can follow for more such articles & stay connected on LinkedIn

https://www.linkedin.com/in/gauravbehere/

--

--

Gaurav Behere
Gaurav Behere

Written by Gaurav Behere

Full-stack engineer, drummer, running on JS & Music. “Frameworks & Libraries come & go, JS stays ❤” https://www.linkedin.com/in/gauravbehere/

No responses yet