Skip to content

Commit 92b22ac

Browse files
committed
collect last logs
1 parent b40a60e commit 92b22ac

File tree

1 file changed

+10
-37
lines changed

1 file changed

+10
-37
lines changed

internal/metrics/log_collector.go

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@ import (
99
"os/exec"
1010
"path/filepath"
1111
"regexp"
12-
"runtime"
1312
"strconv"
1413
"strings"
1514
"time"
1615
)
1716

1817
const (
19-
maxLogLines = 100 // Maximum log lines to collect per service (increased for AI analysis)
20-
logTimeout = 10 // Timeout in seconds for log collection (increased for thorough collection)
21-
maxLogLineLen = 2000 // Maximum length per log line (increased to capture full stack traces)
18+
maxLogLines = 500 // Maximum log lines to collect per container
19+
logTimeout = 10 // Timeout in seconds for log collection
20+
maxLogLineLen = 2000 // Maximum length per log line
2221
)
2322

2423
// DockerContainer represents a running docker container
@@ -169,65 +168,39 @@ func (lc *LogCollector) findContainerForService(service *ServiceInfo) *DockerCon
169168
return nil
170169
}
171170

172-
// CollectServiceLogs collects logs for a service based on its type and container status
171+
// CollectServiceLogs collects logs for a service - only Docker containers
173172
func (lc *LogCollector) CollectServiceLogs(service *ServiceInfo) ([]string, string) {
173+
// Only collect Docker container logs (simple approach like self-hosted)
174+
174175
// 1. Try to find docker container for this service
175176
container := lc.findContainerForService(service)
176177
if container != nil {
177178
logs, _ := lc.collectDockerLogs(container.ID)
178179
if len(logs) > 0 {
179-
// Update service with container info
180180
service.ContainerID = container.ID
181181
service.IsContainer = true
182182
return logs, "docker"
183183
}
184184
}
185185

186-
// 2. If explicitly marked as container but no logs yet, try by container ID
186+
// 2. If explicitly marked as container, try by container ID
187187
if service.IsContainer && service.ContainerID != "" {
188188
logs, err := lc.collectDockerLogs(service.ContainerID)
189189
if err == nil && len(logs) > 0 {
190190
return logs, "docker"
191191
}
192192
}
193193

194-
// 3. Try pm2 logs for Node.js apps
195-
if service.ServiceType == ServiceTypeNodeApp {
196-
logs := lc.collectPM2Logs(service.PID)
197-
if len(logs) > 0 {
198-
return logs, "pm2"
199-
}
200-
}
201-
202-
// Skip journald on non-Linux systems (macOS, Windows don't have journalctl)
203-
if runtime.GOOS != "linux" {
204-
return nil, ""
205-
}
206-
207-
// 4. Try journald for system services (nginx, redis, postgres, etc.)
208-
if lc.isSystemService(service.ServiceType) {
209-
logs, err := lc.collectJournaldLogs(service.ServiceType)
210-
if err == nil && len(logs) > 0 {
211-
return logs, "journald"
212-
}
213-
}
214-
215-
// 5. Try journald by PID for any service
216-
logs, err := lc.collectJournaldByPID(service.PID)
217-
if err == nil && len(logs) > 0 {
218-
return logs, "journald"
219-
}
220-
221194
return nil, ""
222195
}
223196

224-
// collectDockerLogs collects recent error logs from a Docker container
197+
// collectDockerLogs collects recent logs from a Docker container (last 1 minute only)
225198
func (lc *LogCollector) collectDockerLogs(containerID string) ([]string, error) {
226199
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(logTimeout)*time.Second)
227200
defer cancel()
228201

229-
// Get last N lines (increased for better AI analysis)
230-
cmd := exec.CommandContext(ctx, "docker", "logs", "--tail", fmt.Sprintf("%d", maxLogLines*2), "--timestamps", containerID)
202+
// Get logs from last 1 minute only (to avoid duplicates on each 30s collection)
203+
cmd := exec.CommandContext(ctx, "docker", "logs", "--since", "1m", "--tail", fmt.Sprintf("%d", maxLogLines), "--timestamps", containerID)
231204
output, err := cmd.CombinedOutput()
232205
if err != nil {
233206
return nil, err

0 commit comments

Comments
 (0)