Skip to content

Commit 410e27d

Browse files
authored
ErrorPatternLogs query log examples along with the patterns (#113)
1 parent c153e5e commit 410e27d

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

tools/sift.go

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ const (
2323
investigationStatusFailed investigationStatus = "failed"
2424
)
2525

26+
// errorPatternLogExampleLimit controls how many log examples are fetched per error pattern.
27+
const errorPatternLogExampleLimit = 3
28+
2629
type analysisStatus string
2730

2831
type investigationRequest struct {
@@ -39,10 +42,10 @@ type investigationRequest struct {
3942

4043
// Interesting: The analysis complete with results that indicate a probable cause for failure.
4144
type analysisResult struct {
42-
Successful bool `json:"successful"`
43-
Interesting bool `json:"interesting"`
44-
Message string `json:"message"`
45-
Details map[string]interface{} `json:"details"`
45+
Successful bool `json:"successful"`
46+
Interesting bool `json:"interesting"`
47+
Message string `json:"message"`
48+
Details map[string]any `json:"details"`
4649
}
4750

4851
type analysisMeta struct {
@@ -68,6 +71,12 @@ type analysis struct {
6871
Result analysisResult `json:"result"`
6972
}
7073

74+
type InvestigationDatasources struct {
75+
LokiDatasource struct {
76+
UID string `json:"uid"`
77+
} `json:"lokiDatasource"`
78+
}
79+
7180
type Investigation struct {
7281
ID uuid.UUID `json:"id"`
7382
CreatedAt time.Time `json:"created"`
@@ -89,6 +98,8 @@ type Investigation struct {
8998
FailureReason string `json:"failureReason,omitempty"`
9099

91100
Analyses analysisMeta `json:"analyses"`
101+
102+
Datasources InvestigationDatasources `json:"datasources"`
92103
}
93104

94105
// siftClient represents a client for interacting with the Sift API.
@@ -285,6 +296,20 @@ func findErrorPatternLogs(ctx context.Context, args FindErrorPatternLogsParams)
285296
return nil, fmt.Errorf("ErrorPatternLogs analysis not found in investigation %s", completedInvestigation.ID)
286297
}
287298

299+
datasourceUID := completedInvestigation.Datasources.LokiDatasource.UID
300+
301+
for _, pattern := range errorPatternLogsAnalysis.Result.Details["patterns"].([]any) {
302+
patternMap, ok := pattern.(map[string]any)
303+
if !ok {
304+
continue
305+
}
306+
examples, err := fetchErrorPatternLogExamples(ctx, patternMap, datasourceUID)
307+
if err != nil {
308+
return nil, err
309+
}
310+
patternMap["examples"] = examples
311+
}
312+
288313
return errorPatternLogsAnalysis, nil
289314
}
290315

@@ -550,3 +575,22 @@ func (c *siftClient) listSiftInvestigations(ctx context.Context, limit int) ([]I
550575

551576
return response.Data, nil
552577
}
578+
579+
func fetchErrorPatternLogExamples(ctx context.Context, patternMap map[string]any, datasourceUID string) ([]string, error) {
580+
query, _ := patternMap["query"].(string)
581+
logEntries, err := queryLokiLogs(ctx, QueryLokiLogsParams{
582+
DatasourceUID: datasourceUID,
583+
LogQL: query,
584+
Limit: errorPatternLogExampleLimit,
585+
})
586+
if err != nil {
587+
return nil, fmt.Errorf("querying Loki: %w", err)
588+
}
589+
var examples []string
590+
for _, entry := range logEntries {
591+
if entry.Line != "" {
592+
examples = append(examples, entry.Line)
593+
}
594+
}
595+
return examples, nil
596+
}

0 commit comments

Comments
 (0)