Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4.8k
Add actions job log buffer and profiler#866
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
8a5efb9e6ef962271c7c2e65c0f0ade38525a76cbd8e60fb2e1c314375dc8e7b128e4488d16d24bf84b26b8f2ba8002fbd52e531e8f853980d19480f104e679d273b94e433272ff2d4fc6f5f7f1c1061c75b8c9471bfac8a43b03cd9c8825ec070ee0434b7efb301c6106d802516c0f76c3c31a82d4ce2e40e2894310db8e0767fe9677c07ed6bc2d696e5fd10e19952eb2e1647aaa016b910ff4a673c9556a41cFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| package profiler | ||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "runtime" | ||
| "strconv" | ||
| "time" | ||
| "log/slog" | ||
| "math" | ||
| ) | ||
| // Profile represents performance metrics for an operation | ||
| type Profile struct { | ||
| Operation string `json:"operation"` | ||
| Duration time.Duration `json:"duration_ns"` | ||
| MemoryBefore uint64 `json:"memory_before_bytes"` | ||
| MemoryAfter uint64 `json:"memory_after_bytes"` | ||
| MemoryDelta int64 `json:"memory_delta_bytes"` | ||
| LinesCount int `json:"lines_count,omitempty"` | ||
| BytesCount int64 `json:"bytes_count,omitempty"` | ||
| Timestamp time.Time `json:"timestamp"` | ||
| } | ||
| // String returns a human-readable representation of the profile | ||
| func (p *Profile) String() string { | ||
| return fmt.Sprintf("[%s] %s: duration=%v, memory_delta=%+dB, lines=%d, bytes=%d", | ||
| p.Timestamp.Format("15:04:05.000"), | ||
| p.Operation, | ||
| p.Duration, | ||
| p.MemoryDelta, | ||
| p.LinesCount, | ||
| p.BytesCount, | ||
| ) | ||
| } | ||
| func safeMemoryDelta(after, before uint64) int64 { | ||
| if after > math.MaxInt64 || before > math.MaxInt64 { | ||
| if after >= before { | ||
| diff := after - before | ||
| if diff > math.MaxInt64 { | ||
| return math.MaxInt64 | ||
| } | ||
| return int64(diff) | ||
| } | ||
| diff := before - after | ||
| if diff > math.MaxInt64 { | ||
| return -math.MaxInt64 | ||
| } | ||
| return -int64(diff) | ||
| } | ||
| return int64(after) - int64(before) | ||
| } | ||
| // Profiler provides minimal performance profiling capabilities | ||
| type Profiler struct { | ||
| logger *slog.Logger | ||
| enabled bool | ||
| } | ||
| // New creates a new Profiler instance | ||
| func New(logger *slog.Logger, enabled bool) *Profiler { | ||
| return &Profiler{ | ||
| logger: logger, | ||
| enabled: enabled, | ||
| } | ||
| } | ||
| // ProfileFunc profiles a function execution | ||
| func (p *Profiler) ProfileFunc(ctx context.Context, operation string, fn func() error) (*Profile, error) { | ||
| if !p.enabled { | ||
| return nil, fn() | ||
| } | ||
| profile := &Profile{ | ||
| Operation: operation, | ||
| Timestamp: time.Now(), | ||
| } | ||
| var memBefore runtime.MemStats | ||
| runtime.ReadMemStats(&memBefore) | ||
| profile.MemoryBefore = memBefore.Alloc | ||
| start := time.Now() | ||
| err := fn() | ||
| profile.Duration = time.Since(start) | ||
| var memAfter runtime.MemStats | ||
| runtime.ReadMemStats(&memAfter) | ||
| profile.MemoryAfter = memAfter.Alloc | ||
| profile.MemoryDelta = safeMemoryDelta(memAfter.Alloc, memBefore.Alloc) | ||
| if p.logger != nil { | ||
| p.logger.InfoContext(ctx, "Performance profile", "profile", profile.String()) | ||
| } | ||
| return profile, err | ||
| } | ||
| // ProfileFuncWithMetrics profiles a function execution and captures additional metrics | ||
| func (p *Profiler) ProfileFuncWithMetrics(ctx context.Context, operation string, fn func() (int, int64, error)) (*Profile, error) { | ||
| if !p.enabled { | ||
| _, _, err := fn() | ||
| return nil, err | ||
| } | ||
| profile := &Profile{ | ||
| Operation: operation, | ||
| Timestamp: time.Now(), | ||
| } | ||
| var memBefore runtime.MemStats | ||
| runtime.ReadMemStats(&memBefore) | ||
| profile.MemoryBefore = memBefore.Alloc | ||
| start := time.Now() | ||
| lines, bytes, err := fn() | ||
| profile.Duration = time.Since(start) | ||
| profile.LinesCount = lines | ||
| profile.BytesCount = bytes | ||
| var memAfter runtime.MemStats | ||
| runtime.ReadMemStats(&memAfter) | ||
| profile.MemoryAfter = memAfter.Alloc | ||
| profile.MemoryDelta = safeMemoryDelta(memAfter.Alloc, memBefore.Alloc) | ||
| if p.logger != nil { | ||
| p.logger.InfoContext(ctx, "Performance profile", "profile", profile.String()) | ||
| } | ||
| return profile, err | ||
| } | ||
| // Start begins timing an operation and returns a function to complete the profiling | ||
| func (p *Profiler) Start(ctx context.Context, operation string) func(lines int, bytes int64) *Profile { | ||
| if !p.enabled { | ||
| return func(int, int64) *Profile { return nil } | ||
| } | ||
| profile := &Profile{ | ||
| Operation: operation, | ||
| Timestamp: time.Now(), | ||
| } | ||
| var memBefore runtime.MemStats | ||
| runtime.ReadMemStats(&memBefore) | ||
| profile.MemoryBefore = memBefore.Alloc | ||
| start := time.Now() | ||
| return func(lines int, bytes int64) *Profile { | ||
| profile.Duration = time.Since(start) | ||
| profile.LinesCount = lines | ||
| profile.BytesCount = bytes | ||
| var memAfter runtime.MemStats | ||
| runtime.ReadMemStats(&memAfter) | ||
| profile.MemoryAfter = memAfter.Alloc | ||
| profile.MemoryDelta = safeMemoryDelta(memAfter.Alloc, memBefore.Alloc) | ||
| if p.logger != nil { | ||
| p.logger.InfoContext(ctx, "Performance profile", "profile", profile.String()) | ||
| } | ||
| return profile | ||
| } | ||
| } | ||
| var globalProfiler *Profiler | ||
| // IsProfilingEnabled checks if profiling is enabled via environment variables | ||
| func IsProfilingEnabled() bool { | ||
| if enabled, err := strconv.ParseBool(os.Getenv("GITHUB_MCP_PROFILING_ENABLED")); err == nil { | ||
| return enabled | ||
| } | ||
| return false | ||
| } | ||
| // Init initializes the global profiler | ||
| func Init(logger *slog.Logger, enabled bool) { | ||
| globalProfiler = New(logger, enabled) | ||
| } | ||
| // InitFromEnv initializes the global profiler using environment variables | ||
| func InitFromEnv(logger *slog.Logger) { | ||
| globalProfiler = New(logger, IsProfilingEnabled()) | ||
| } | ||
| // ProfileFunc profiles a function using the global profiler | ||
| func ProfileFunc(ctx context.Context, operation string, fn func() error) (*Profile, error) { | ||
| if globalProfiler == nil { | ||
| return nil, fn() | ||
| } | ||
| return globalProfiler.ProfileFunc(ctx, operation, fn) | ||
| } | ||
| // ProfileFuncWithMetrics profiles a function with metrics using the global profiler | ||
| func ProfileFuncWithMetrics(ctx context.Context, operation string, fn func() (int, int64, error)) (*Profile, error) { | ||
| if globalProfiler == nil { | ||
| _, _, err := fn() | ||
| return nil, err | ||
| } | ||
| return globalProfiler.ProfileFuncWithMetrics(ctx, operation, fn) | ||
| } | ||
| // Start begins timing using the global profiler | ||
| func Start(ctx context.Context, operation string) func(int, int64) *Profile { | ||
| if globalProfiler == nil { | ||
| return func(int, int64) *Profile { return nil } | ||
| } | ||
| return globalProfiler.Start(ctx, operation) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package buffer | ||
| import ( | ||
| "bufio" | ||
| "fmt" | ||
| "net/http" | ||
| "strings" | ||
| ) | ||
mattdholloway marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| // ProcessResponseAsRingBufferToEnd reads the body of an HTTP response line by line, | ||
| // storing only the last maxJobLogLines lines using a ring buffer (sliding window). | ||
| // This efficiently retains the most recent lines, overwriting older ones as needed. | ||
| // | ||
| // Parameters: | ||
| // | ||
| // httpResp: The HTTP response whose body will be read. | ||
| // maxJobLogLines: The maximum number of log lines to retain. | ||
| // | ||
| // Returns: | ||
| // | ||
| // string: The concatenated log lines (up to maxJobLogLines), separated by newlines. | ||
| // int: The total number of lines read from the response. | ||
| // *http.Response: The original HTTP response. | ||
| // error: Any error encountered during reading. | ||
| // | ||
| // The function uses a ring buffer to efficiently store only the last maxJobLogLines lines. | ||
| // If the response contains more lines than maxJobLogLines, only the most recent lines are kept. | ||
| func ProcessResponseAsRingBufferToEnd(httpResp *http.Response, maxJobLogLines int) (string, int, *http.Response, error) { | ||
| lines := make([]string, maxJobLogLines) | ||
| validLines := make([]bool, maxJobLogLines) | ||
| totalLines := 0 | ||
| writeIndex := 0 | ||
| scanner := bufio.NewScanner(httpResp.Body) | ||
| scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024) | ||
mattdholloway marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. mattdholloway marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| for scanner.Scan() { | ||
| line := scanner.Text() | ||
| totalLines++ | ||
| lines[writeIndex] = line | ||
| validLines[writeIndex] = true | ||
| writeIndex = (writeIndex + 1) % maxJobLogLines | ||
| } | ||
| if err := scanner.Err(); err != nil { | ||
| return "", 0, httpResp, fmt.Errorf("failed to read log content: %w", err) | ||
| } | ||
| var result []string | ||
| linesInBuffer := totalLines | ||
| if linesInBuffer > maxJobLogLines { | ||
| linesInBuffer = maxJobLogLines | ||
| } | ||
| startIndex := 0 | ||
| if totalLines > maxJobLogLines { | ||
| startIndex = writeIndex | ||
| } | ||
| for i := 0; i < linesInBuffer; i++ { | ||
| idx := (startIndex + i) % maxJobLogLines | ||
| if validLines[idx] { | ||
| result = append(result, lines[idx]) | ||
| } | ||
| } | ||
| return strings.Join(result, "\n"), totalLines, httpResp, nil | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
CopilotAIAug 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This condition checks if either value exceeds MaxInt64, but since these are uint64 values, they can legitimately be larger than MaxInt64. The logic should handle the conversion more clearly by checking if the difference itself would overflow int64 bounds.