mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: include rotated agent logs in support bundles (#26055)
Support bundles previously captured only the active coder-agent.log, losing history across agent restarts. Add an optional `after` filter to the agent's /debug/logs endpoint: without it the endpoint is unchanged (active log only, 10 MiB cap); with it the response includes the active log plus rotated coder-agent-*.log files modified after the cutoff, newest first. Support bundles request the last 24h. Closes #25395
This commit is contained in:
@@ -98,7 +98,7 @@ type AgentConn interface {
|
||||
CallMCPTool(ctx context.Context, req CallMCPToolRequest) (CallMCPToolResponse, error)
|
||||
Close() error
|
||||
ContextConfig(ctx context.Context) (ContextConfigResponse, error)
|
||||
DebugLogs(ctx context.Context) ([]byte, error)
|
||||
DebugLogs(ctx context.Context, opts ...DebugLogsOption) ([]byte, error)
|
||||
DebugMagicsock(ctx context.Context) ([]byte, error)
|
||||
DebugManifest(ctx context.Context) ([]byte, error)
|
||||
DialContext(ctx context.Context, network string, addr string) (net.Conn, error)
|
||||
@@ -443,11 +443,29 @@ func (c *agentConn) DebugManifest(ctx context.Context) ([]byte, error) {
|
||||
return bs, nil
|
||||
}
|
||||
|
||||
// DebugLogs returns up to the last 10MB of `/tmp/coder-agent.log`
|
||||
func (c *agentConn) DebugLogs(ctx context.Context) ([]byte, error) {
|
||||
// DebugLogsOption configures a DebugLogs request.
|
||||
type DebugLogsOption func(*debugLogsConfig)
|
||||
|
||||
type debugLogsConfig struct {
|
||||
after time.Time
|
||||
}
|
||||
|
||||
// WithLogsAfter also returns rotated logs modified at or after t, separated by
|
||||
// boundary markers (100 MiB combined cap).
|
||||
func WithLogsAfter(t time.Time) DebugLogsOption {
|
||||
return func(c *debugLogsConfig) { c.after = t }
|
||||
}
|
||||
|
||||
// DebugLogs returns up to 10 MiB of the active agent log. Pass WithLogsAfter to
|
||||
// also include rotated logs.
|
||||
func (c *agentConn) DebugLogs(ctx context.Context, opts ...DebugLogsOption) ([]byte, error) {
|
||||
var cfg debugLogsConfig
|
||||
for _, opt := range opts {
|
||||
opt(&cfg)
|
||||
}
|
||||
ctx, span := tracing.StartSpan(ctx)
|
||||
defer span.End()
|
||||
res, err := c.apiRequest(ctx, http.MethodGet, "/debug/logs", nil)
|
||||
res, err := c.apiRequest(ctx, http.MethodGet, debugLogsPath(cfg.after), nil)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("do request: %w", err)
|
||||
}
|
||||
@@ -462,6 +480,14 @@ func (c *agentConn) DebugLogs(ctx context.Context) ([]byte, error) {
|
||||
return bs, nil
|
||||
}
|
||||
|
||||
func debugLogsPath(after time.Time) string {
|
||||
query := neturl.Values{}
|
||||
if !after.IsZero() {
|
||||
query.Set("after", after.UTC().Format(time.RFC3339Nano))
|
||||
}
|
||||
return agentAPIPath("/debug/logs", query)
|
||||
}
|
||||
|
||||
// PrometheusMetrics returns a response from the agent's prometheus metrics endpoint
|
||||
func (c *agentConn) PrometheusMetrics(ctx context.Context) ([]byte, error) {
|
||||
ctx, span := tracing.StartSpan(ctx)
|
||||
|
||||
@@ -3,6 +3,7 @@ package workspacesdk
|
||||
import (
|
||||
neturl "net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -48,4 +49,22 @@ func TestAgentAPIPath(t *testing.T) {
|
||||
require.Equal(t, "50", parsed.Query().Get("max_response_lines"))
|
||||
require.Equal(t, "60", parsed.Query().Get("max_response_bytes"))
|
||||
})
|
||||
|
||||
t.Run("debug logs zero after", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
got := debugLogsPath(time.Time{})
|
||||
require.Equal(t, "/debug/logs", got)
|
||||
})
|
||||
|
||||
t.Run("debug logs after", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
after := time.Date(2026, 5, 18, 12, 34, 56, 789, time.FixedZone("test", -7*60*60))
|
||||
got := debugLogsPath(after)
|
||||
parsed, err := neturl.Parse(got)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "/debug/logs", parsed.Path)
|
||||
require.Equal(t, after.UTC().Format(time.RFC3339Nano), parsed.Query().Get("after"))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -130,18 +130,23 @@ func (mr *MockAgentConnMockRecorder) ContextConfig(ctx any) *gomock.Call {
|
||||
}
|
||||
|
||||
// DebugLogs mocks base method.
|
||||
func (m *MockAgentConn) DebugLogs(ctx context.Context) ([]byte, error) {
|
||||
func (m *MockAgentConn) DebugLogs(ctx context.Context, opts ...workspacesdk.DebugLogsOption) ([]byte, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "DebugLogs", ctx)
|
||||
varargs := []any{ctx}
|
||||
for _, a := range opts {
|
||||
varargs = append(varargs, a)
|
||||
}
|
||||
ret := m.ctrl.Call(m, "DebugLogs", varargs...)
|
||||
ret0, _ := ret[0].([]byte)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// DebugLogs indicates an expected call of DebugLogs.
|
||||
func (mr *MockAgentConnMockRecorder) DebugLogs(ctx any) *gomock.Call {
|
||||
func (mr *MockAgentConnMockRecorder) DebugLogs(ctx any, opts ...any) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DebugLogs", reflect.TypeOf((*MockAgentConn)(nil).DebugLogs), ctx)
|
||||
varargs := append([]any{ctx}, opts...)
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DebugLogs", reflect.TypeOf((*MockAgentConn)(nil).DebugLogs), varargs...)
|
||||
}
|
||||
|
||||
// DebugMagicsock mocks base method.
|
||||
|
||||
Reference in New Issue
Block a user