621e8895c9
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
3.8 KiB
3.8 KiB
name, kind, lang, domain, version, purity, signature, description, tags, uses_functions, uses_types, returns, returns_optional, error_type, imports, params, output, tested, tests, test_file_path, file_path
| name | kind | lang | domain | version | purity | signature | description | tags | uses_functions | uses_types | returns | returns_optional | error_type | imports | params | output | tested | tests | test_file_path | file_path | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| docker_container_logs | function | go | infra | 2.0.0 | impure | func DockerContainerLogs(opts DockerLogsOpts) ([]DockerLogLine, error) | Tail/grep logs de container Docker via engine API. Snapshot (N lineas) o streaming (callback por linea con context cancel). Demux frame stdout/stderr. Capability docker.container.logs del device_agent. |
|
|
|
false | error_go_core |
|
|
Slice de DockerLogLine con stream (stdout/stderr), timestamp RFC3339 opcional y texto de la linea. | true |
|
functions/infra/docker_container_logs_test.go | functions/infra/docker_container_logs.go |
Ejemplo
// Snapshot: ultimas 50 lineas de stdout+stderr
lines, err := DockerContainerLogs(infra.DockerLogsOpts{
ContainerID: "registry_api",
Tail: 50,
Since: "10m",
Stdout: true,
Stderr: true,
Timestamps: true,
})
if err != nil {
log.Fatal(err)
}
for _, l := range lines {
fmt.Printf("[%s] %s %s\n", l.Stream, l.Timestamp, l.Line)
}
// Streaming: follow hasta cancelacion
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
err = infra.DockerContainerLogsStream(ctx, infra.DockerLogsOpts{
ContainerID: "registry_api",
Stdout: true,
Stderr: true,
}, func(line infra.DockerLogLine) error {
fmt.Printf("[%s] %s\n", line.Stream, line.Line)
if strings.Contains(line.Line, "FATAL") {
return fmt.Errorf("fatal error detectado")
}
return nil
})
Cuando usarla
Cuando el device_agent necesite leer o monitorizar logs de un container Docker en tiempo real. Usar modo snapshot para health checks puntuales (N ultimas lineas). Usar streaming para tail -f reactivo con procesamiento por linea.
Gotchas
- Containers sin
--ttyusan el protocolo de multiplexion de 8 bytes — esta funcion lo demuxea correctamente. Containers con--ttymezclan stdout/stderr en un stream plano sin headers, lo que puede darStream = "stdout"para todo o parsear mal el header (byte 0 podria ser el primer caracter de texto). - Streaming consume una goroutine/conexion hasta que
ctxse cancele ocbretorne error. El caller es responsable del ciclo de vida del contexto. Sinceacepta unix timestamp en string, RFC3339 o duracion Go ("10m", "1h30m"). El daemon Docker acepta los 3 formatos directamente.- Sin reconexion automatica en streaming. Si el daemon reinicia o la conexion se corta, el caller recibe error y decide si reintentar.
DockerHostvacio conecta a/var/run/docker.sock. En sistemas donde el socket esta en otra ruta (Docker Desktop macOS, Podman), pasar la URL explicitamente.
Capability growth log
v2.0.0 (2026-05-23) — reemplaza implementacion CLI (exec docker logs) por engine API HTTP con demux de frames. Anade DockerLogsOpts, DockerLogLine, modo streaming con callback y ctx cancel. Consumidor nordvpn_container_start actualizado.