-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandle_errors.go
More file actions
68 lines (56 loc) · 1.89 KB
/
Copy pathhandle_errors.go
File metadata and controls
68 lines (56 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"encoding/json"
"fmt"
"net/http"
)
// handleErrors shows error information from the metrics system
func handleErrors(w http.ResponseWriter, r *http.Request) {
// Check for clear parameter
if r.URL.Query().Get("clear") != "" {
// Reset the metrics collector (create a new one)
globalMetricsCollector = NewMetricsCollector()
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "Error metrics cleared.\n")
return
}
// Get current metrics
metrics := GetMetricsCollector().GetMetrics()
// Check if JSON format is requested
if r.URL.Query().Get("format") == "json" {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(metrics)
return
}
// Serve human-readable format
w.Header().Set("Content-Type", "text/plain")
if metrics.TotalErrors == 0 {
fmt.Fprintf(w, "No errors recorded yet.\n")
return
}
fmt.Fprintf(w, "=== DALLE Server Error Report ===\n")
fmt.Fprintf(w, "Total Errors: %d\n", metrics.TotalErrors)
fmt.Fprintf(w, "Last Updated: %s\n\n", metrics.LastUpdated.Format("2006-01-02 15:04:05"))
if len(metrics.ErrorsByCode) > 0 {
fmt.Fprintf(w, "Errors by Code:\n")
for code, count := range metrics.ErrorsByCode {
fmt.Fprintf(w, " %s: %d\n", code, count)
}
fmt.Fprintf(w, "\n")
}
if len(metrics.ErrorsByEndpoint) > 0 {
fmt.Fprintf(w, "Errors by Endpoint:\n")
for endpoint, count := range metrics.ErrorsByEndpoint {
fmt.Fprintf(w, " %s: %d\n", endpoint, count)
}
fmt.Fprintf(w, "\n")
}
if metrics.OpenAIErrors > 0 {
fmt.Fprintf(w, "OpenAI API Errors: %d (out of %d requests)\n", metrics.OpenAIErrors, metrics.OpenAIRequests)
}
if metrics.FileOperationErrors > 0 {
fmt.Fprintf(w, "File Operation Errors: %d (out of %d operations)\n", metrics.FileOperationErrors, metrics.FileOperations)
}
fmt.Fprintf(w, "\nUse ?clear to reset error metrics\n")
fmt.Fprintf(w, "Use ?format=json for JSON output\n")
}