Skip to content

Commit

Permalink
handle pod failure data in db
Browse files Browse the repository at this point in the history
  • Loading branch information
AdheipSingh committed Nov 27, 2024
1 parent c334732 commit 290a4d5
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 16 deletions.
8 changes: 4 additions & 4 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ var AnalyzeCmd = &cobra.Command{

// debug for empty results
if result == nil {
fmt.Println(yellow + "No results found in DuckDB." + reset)
return nil
fmt.Println(yellow + "No results found for pod in DuckDB, analyzing the namespace." + reset)
result, err = duckdb.FetchNamespaceEventsfromDb(namespace)
}

s.Suffix = " Analyzing events with LLM..."
Expand All @@ -167,7 +167,6 @@ var AnalyzeCmd = &cobra.Command{
} else if llmProvider == "ollama" {
// Use Ollama's respective function (assuming a similar function exists)
gptResponse, err = ollama.AnalyzeEventsWithOllama(pod, namespace, result)
fmt.Println(gptResponse)
} else {
// This should never happen since validateLLMConfig ensures the provider is valid
return fmt.Errorf("invalid LLM provider: %s", llmProvider)
Expand Down Expand Up @@ -362,7 +361,8 @@ func parseAndSelectAnalysis(response string) bool {
var analysis AnalysisResponse
err := json.Unmarshal([]byte(response), &analysis)
if err != nil {
log.Println("Failed to parse LLM response: %v", err)
fmt.Println(green + "Summary:\n" + reset + response)
return true
}

// Display the summary by default
Expand Down
46 changes: 46 additions & 0 deletions cmd/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2024 Parseable, Inc
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package cmd

// import (
// "fmt"
// "log"
// "os"
// "sync"
// "time"

// internalHTTP "pb/pkg/http"

// "github.com/briandowns/spinner"

// "pb/pkg/analyze/anthropic"
// "pb/pkg/analyze/duckdb"
// "pb/pkg/analyze/k8s"
// "pb/pkg/analyze/ollama"
// "pb/pkg/analyze/openai"

// _ "github.com/marcboeker/go-duckdb"
// "github.com/spf13/cobra"
// )

// var GenerateCmd = &cobra.Command{
// Use: "k8s",
// Short: "Generate k8s events on your k8s cluster by deploying apps in different states.",
// Example: "pb generate k8s events",
// Args: cobra.ExactArgs(1),
// RunE: func(cmd *cobra.Command, args []string) error {
// }
// }
12 changes: 7 additions & 5 deletions pkg/analyze/anthropic/anthropic.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,17 @@ func AnalyzeEventsWithAnthropic(podName, namespace string, data []duckdb.Summary
I have a table containing those events and want to debug what is happening in this pod (%s) / namespace (%s).
Give me a detailed summary of what happened by looking at these events.
Provide a root cause analysis and suggest steps to mitigate the error if present.
Make sure summary is a string, root_cause_analysis is a string and mitigation_steps is []string. Please respect the data
In case where podName is empty or not provided analyse within the namespace, but strictly adher to the json struct mentioned. Do not send any response which is not compatible with json struct provided".
When sending the response give it in a structured body json. With fields summary, root_cause_analysis and mitigation_steps.

Check failure on line 59 in pkg/analyze/anthropic/anthropic.go

View workflow job for this annotation

GitHub Actions / Build and Test the Go code

`analyse` is a misspelling of `analyze` (misspell)
Don't add any json keywords in the response, make sure it just a clean json dump. Please adhere to the following structure
type AnalysisResponse struct {
Summary string json:"summary"
RootCauseAnalysis string json:"root_cause_analysis"
MitigationSteps []string json:"mitigation_steps"
summary string json:"summary"
root_cause_analysis string json:"root_cause_analysis"
mitigation_steps []string json:"mitigation_steps"
}
In mitigation steps give a command to get logs.
In case you are unable to figure out what happened, just say "I'm unable to figure out what is happening here.".
In mitigation steps give a command to get logs. The general command to get logs is 'kubectl logs <pod_name> -n <namespace>.
Make sure you give a clean json response, without an '''json in the beginning. Please respect the json tags should be summary, root_cause_analysis and mitigation_steps.
%s`, podName, namespace, formattedData)

// Build the Anthropic request payload
Expand Down
47 changes: 47 additions & 0 deletions pkg/analyze/duckdb/duckdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,50 @@ func FetchPodEventsfromDb(podName string) ([]SummaryStat, error) {

return stats, nil
}

// FetchNamespaceEventsfromDb fetches summary statistics for a given namespace from DuckDB.
func FetchNamespaceEventsfromDb(namespace string) ([]SummaryStat, error) {
// Open a connection to DuckDB
db, err := sql.Open("duckdb", "k8s_events.duckdb")
if err != nil {
return nil, fmt.Errorf("error connecting to DuckDB: %w", err)
}
defer db.Close()

// Prepare the query with a placeholder for podName
query := `
SELECT DISTINCT ON (message) *
FROM k8s_events
WHERE involvedObject_namespace = ?
ORDER BY "timestamp";
`

// Execute the query with namespace as a parameter
rows, err := db.Query(query, namespace)
if err != nil {
return nil, fmt.Errorf("error executing summary query: %w", err)
}
defer rows.Close()

var stats []SummaryStat
for rows.Next() {
var reason, message, objectName, objectNamespace, reportingComponent, timestamp string
if err := rows.Scan(&reason, &message, &objectName, &objectNamespace, &reportingComponent, &timestamp); err != nil {
return nil, fmt.Errorf("error scanning summary row: %w", err)
}
stats = append(stats, SummaryStat{
Reason: reason,
Message: message,
ObjectName: objectName,
ObjectNamespace: objectNamespace,
ReportingComponent: reportingComponent,
Timestamp: timestamp,
})
}

if err := rows.Err(); err != nil {
return nil, fmt.Errorf("error reading rows: %w", err)
}

return stats, nil
}
4 changes: 2 additions & 2 deletions pkg/analyze/ollama/ollama.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func AnalyzeEventsWithOllama(podName, namespace string, data []duckdb.SummarySta
I have a table containing those events and want to debug what is happening in this pod (%s) / namespace (%s).
Give me a detailed summary of what happened by looking at these events.
Provide a root cause analysis and suggest steps to mitigate the error if present.
Make sure summary is a string, root_cause_analysis is a string and mitigation_steps is []string. Please respect the data
In case where podName is empty or not provided analyse within the namespace, but strictly adher to the json struct mentioned. Do not send any response which is not compatible with json struct provided".
When sending the response give it in a structured body json. With fields summary, root_cause_analysis and mitigation_steps.

Check failure on line 41 in pkg/analyze/ollama/ollama.go

View workflow job for this annotation

GitHub Actions / Build and Test the Go code

`analyse` is a misspelling of `analyze` (misspell)
Don't add any json keywords in the response, make sure it just a clean json dump. Please adhere to the following structure
type AnalysisResponse struct {
Expand All @@ -45,8 +47,6 @@ func AnalyzeEventsWithOllama(podName, namespace string, data []duckdb.SummarySta
}
In mitigation steps give a command to get logs. The general command to get logs is 'kubectl logs <pod_name> -n <namespace>.
Make sure you give a clean json response, without an '''json in the beginning. Please respect the json tags should be summary, root_cause_analysis and mitigation_steps.
Make sure summary is a string, root_cause_analysis is a string and mitigation_steps is []string. Please respect the datatypes.
In case you are unable to figure out what happened, just say "I'm unable to figure out what is happening here.".
%s`, podName, namespace, formattedData)

// Build the Ollama request payload
Expand Down
12 changes: 7 additions & 5 deletions pkg/analyze/openai/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@ func AnalyzeEventsWithGPT(podName, namespace string, data []duckdb.SummaryStat)
I have a table containing those events and want to debug what is happening in this pod (%s) / namespace (%s).
Give me a detailed summary of what happened by looking at these events.
Provide a root cause analysis and suggest steps to mitigate the error if present.
Make sure summary is a string, root_cause_analysis is a string and mitigation_steps is []string. Please respect the data
In case where podName is empty or not provided analyse within the namespace, but strictly adher to the json struct mentioned. Do not send any response which is not compatible with json struct provided".
When sending the response give it in a structured body json. With fields summary, root_cause_analysis and mitigation_steps.

Check failure on line 46 in pkg/analyze/openai/openai.go

View workflow job for this annotation

GitHub Actions / Build and Test the Go code

`analyse` is a misspelling of `analyze` (misspell)
Don't add any json keywords in the response, make sure it just a clean json dump. Please adhere to the following structure
type AnalysisResponse struct {
Summary string json:"summary"
RootCauseAnalysis string json:"root_cause_analysis"
MitigationSteps []string json:"mitigation_steps"
summary string json:"summary"
root_cause_analysis string json:"root_cause_analysis"
mitigation_steps []string json:"mitigation_steps"
}
In mitigation steps give a command to get logs.
In case you are unable to figure out what happened, just say "I'm unable to figure out what is happening here.".
In mitigation steps give a command to get logs. The general command to get logs is 'kubectl logs <pod_name> -n <namespace>.
Make sure you give a clean json response, without an '''json in the beginning. Please respect the json tags should be summary, root_cause_analysis and mitigation_steps.
%s`, podName, namespace, formattedData)

// Build the OpenAI request payload
Expand Down

0 comments on commit 290a4d5

Please sign in to comment.