Skip to content

[WIP]: feat adding AI logger #390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

Conversation

sauravpanda
Copy link
Member

@sauravpanda sauravpanda commented Aug 9, 2024

Enhance Logging and Error Handling with KaizenLog

Overview

This pull request introduces a new logging and error handling system called KaizenLog. The main purpose is to provide enhanced logging capabilities, including automatic log analysis and issue detection, to improve the overall observability and maintainability of the application.

Changes

Key Changes

  • Integrated the KaizenLog service into the application, allowing logs to be automatically sent to the service for analysis.
  • Implemented a custom KaizenLogHandler that extends the standard Python logging handler, enabling seamless integration with the existing logging infrastructure.
  • Added an exception handling mechanism that captures and sends exceptions to the KaizenLog service for analysis.

New Features

  • Automatic log analysis: The KaizenLog service analyzes the sent logs and provides a summary of potential issues or errors, including their severity, timestamp, and relevant details.
  • Improved observability: The application now has enhanced logging capabilities, allowing for better visibility into runtime behavior and easier identification of potential problems.

Refactoring

  • Moved the log analysis functionality into a separate module (kaizen/logger/analyzer.py) to improve code organization and maintainability.
  • Refactored the exception handling logic into a dedicated function (exception_handler) to centralize the error reporting mechanism.
Implementation Details

The key components of the KaizenLog integration are:

  1. KaizenLogHandler: This custom logging handler is responsible for sending log entries to the KaizenLog service. When a log record is emitted, the handler sends the log data to the KaizenLog service for analysis.

  2. exception_handler: This function is registered as the global exception hook using sys.excepthook. When an unhandled exception occurs, the function captures the exception information and sends it to the KaizenLog service for analysis.

  3. analyze_logs: This function is responsible for sending the log data to the KaizenLog service and processing the response. It creates a prompt for the KaizenLog service, sends the request, and parses the response to extract the identified issues or errors.

The integration with the KaizenLog service is configured using the service_url parameter, which should be set to the appropriate URL for the KaizenLog service.

✨ Generated with love by Kaizen ❤️

Original Description

@sauravpanda sauravpanda linked an issue Aug 9, 2024 that may be closed by this pull request
@sauravpanda sauravpanda marked this pull request as draft August 9, 2024 05:42
@sauravpanda
Copy link
Member Author

!review

1 similar comment
@sauravpanda
Copy link
Member Author

!review

Copy link
Contributor

kaizen-bot bot commented Aug 20, 2024

🔍 Code Review Summary

Attention Required: This push has potential issues. 🚨

📊 Stats

  • Total Feedbacks: 4
  • Critical: 2
  • Suggested Refinements: 2
  • Files Affected: 3

🏆 Code Quality

[██████████████████░░] 90% (Excellent)

🚨 Critical Issues

Logging Analysis Integration (2 issues)
1. The new 'analyze-logs' endpoint has been added to the 'github_app/main.py' file, but the 'analyze_logs' function is not implemented.

📁 File: github_app/main.py:24
⚖️ Severity: 9/10
🔍 Description:
The 'analyze-logs' endpoint calls the 'analyze_logs' function, but this function is not defined in the provided code. This will cause the application to fail when the endpoint is accessed.
💡 Solution:
Implement the 'analyze_logs' function in the 'kaizen/logger/analyzer.py' file. This function should take the log data from the request, create a prompt for the Ollama server, send the prompt to the Ollama server, and return the analysis results.

Current Code:

Suggested Code:

def analyze_logs(log_data):
    prompt = create_prompt(log_data)
    ollama_server_url = os.getenv('OLLAMA_SERVER_URL')
    model_response = analyze_logs(prompt, ollama_server_url)
    if model_response:
        parsed_response = parse_response(model_response, log_data)
        return parsed_response
    else:
        return{'error': 'Failed to analyze logs'}
2. Changes made to sensitive file

📁 File: docker-compose.yml:21
⚖️ Severity: 10/10
🔍 Description:
Changes were made to docker-compose.yml, which needs review
💡 Solution:
NA


🟠 Refinement Suggestions:

These are not critical issues, but addressing them could further improve the code:

Docker Compose Configuration (2 issues)
1. The new 'ollama' service has been added to the Docker Compose configuration, but the environment variable 'OLLAMA_SERVER_URL' is not defined.

📁 File: docker-compose.yml:25
⚖️ Severity: 7/10
🔍 Description:
The 'analyze_logs' function in the 'github_app/main.py' file requires the 'OLLAMA_SERVER_URL' environment variable to be set, but it is not defined in the Docker Compose configuration.
💡 Solution:
Add the 'OLLAMA_SERVER_URL' environment variable to the 'ollama' service in the Docker Compose configuration, and set it to the appropriate URL for the Ollama server.

Current Code:

Suggested Code:

    ollama:
      image: ollama/ollama:latest
      container_name: ollama
      environment:
        - OLLAMA_SERVER_URL=http://your-ollama-server.com/analyze
      volumes:
        - ollama:/root/.ollama
      ports:
        - 11434:11434
      restart: unless-stopped
2. The 'kaizen/logger/analyzer.py' file has been added, which contains the implementation of the 'analyze_logs' function. However, the 'OLLAMA_SERVER_URL' environment variable is hardcoded in the 'main' function.

📁 File: kaizen/logger/analyzer.py:61
⚖️ Severity: 6/10
🔍 Description:
The 'OLLAMA_SERVER_URL' environment variable should be retrieved from the environment, as it may vary depending on the deployment environment. Hardcoding the URL makes the code less flexible and harder to maintain.
💡 Solution:
Replace the hardcoded 'OLLAMA_SERVER_URL' value in the 'main' function with a call to 'os.getenv' to retrieve the environment variable. This will allow the URL to be configured externally, making the code more flexible and easier to maintain.

Current Code:

    ollama_server_url = "http://your-ollama-server.com/analyze"

Suggested Code:

    ollama_server_url = os.getenv('OLLAMA_SERVER_URL', 'http://your-ollama-server.com/analyze')

🧪 Test Cases

Test Cases need updates: Run !unittest to generate
Tests Not Found

The following files are missing corresponding test files:

  1. github_app/main.py
  2. kaizen/logger/analyzer.py
  3. kaizen/logger/kaizenlog.py
Tests Found But May Need Update

The following test files may need to be updated to reflect recent changes:

Generate Unit Tests

To generate unit test cases for the code, please type !unittest in a comment. This will create a new pull request with suggested unit tests.

---------------------------------------

✨ Generated with love by Kaizen ❤️

Useful Commands
  • Feedback: Reply with !feedback [your message]
  • Ask PR: Reply with !ask-pr [your question]
  • Review: Reply with !review
  • Explain: Reply with !explain [issue number] for more details on a specific issue
  • Ignore: Reply with !ignore [issue number] to mark an issue as false positive
  • Update Tests: Reply with !unittest to create a PR with test changes

Comment on lines +24 to +32
@app.route("/analyze-logs", methods=["POST"])
async def analyze_logs_endpoint(request: Request):
payload = await request.json()
log_data = payload.get("log_data")
if log_data:
analysis_result = analyze_logs(log_data)
return JSONResponse(content={"analysis": analysis_result})
else:
return JSONResponse(content={"error": "Missing log data"}, status_code=400)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment: The new 'analyze-logs' endpoint has been added to the 'github_app/main.py' file, but the 'analyze_logs' function is not implemented.

Solution: Implement the 'analyze_logs' function in the 'kaizen/logger/analyzer.py' file. This function should take the log data from the request, create a prompt for the Ollama server, send the prompt to the Ollama server, and return the analysis results.

Reason For Comment: The 'analyze-logs' endpoint calls the 'analyze_logs' function, but this function is not defined in the provided code. This will cause the application to fail when the endpoint is accessed.

Suggested change
@app.route("/analyze-logs", methods=["POST"])
async def analyze_logs_endpoint(request: Request):
payload = await request.json()
log_data = payload.get("log_data")
if log_data:
analysis_result = analyze_logs(log_data)
return JSONResponse(content={"analysis": analysis_result})
else:
return JSONResponse(content={"error": "Missing log data"}, status_code=400)
def analyze_logs(log_data):
prompt = create_prompt(log_data)
ollama_server_url = os.getenv('OLLAMA_SERVER_URL')
model_response = analyze_logs(prompt, ollama_server_url)
if model_response:
parsed_response = parse_response(model_response, log_data)
return parsed_response
else:
return{'error': 'Failed to analyze logs'}

@@ -21,6 +21,15 @@ services:
- REDIS_PASSWORD=${REDIS_PASSWORD}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment: Changes made to sensitive file

Solution: NA

Reason For Comment: Changes were made to docker-compose.yml, which needs review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: AI powered logger
1 participant