feat(ccotel): add debug config for raw JSON output#155
Conversation
Add debug option to CCOtel config that writes raw OTEL metrics and logs to files in /tmp/shelltime/ for troubleshooting purposes. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Summary of ChangesHello @AnnatarHe, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the CCOtel processor by adding a new debugging feature. It allows developers and operators to inspect the raw OpenTelemetry metrics and logs in JSON format directly on the filesystem. This capability is invaluable for verifying the structure and content of the data being processed, aiding in troubleshooting and ensuring data integrity before it is forwarded to the backend. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
Codecov Report❌ Patch coverage is
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 1 file with indirect coverage changes 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Code Review
This pull request introduces a helpful debugging feature for the CCOtel processor, allowing raw OTEL data to be logged to files. The implementation is straightforward and follows the description.
My main feedback concerns a potential race condition in the new writeDebugFile function. Since the OTEL processor handles requests concurrently, multiple goroutines could attempt to write to the same debug file simultaneously, leading to corrupted output. I've suggested adding a mutex to synchronize file access and ensure thread safety. I've also included a minor suggestion to improve file writing performance within the same fix.
Overall, this is a valuable addition for troubleshooting. Addressing the concurrency issue will make it robust.
| config model.ShellTimeConfig | ||
| endpoint model.Endpoint | ||
| hostname string | ||
| debug bool |
There was a problem hiding this comment.
To prevent race conditions when writing to debug files from concurrent goroutines, a mutex should be added to this struct. This will be used in the writeDebugFile function to ensure thread-safe file access. You will also need to add import "sync" to the file's import block.
debug bool
debugMtx sync.Mutex| func (p *CCOtelProcessor) writeDebugFile(filename string, data interface{}) { | ||
| debugDir := filepath.Join(os.TempDir(), "shelltime") | ||
| if err := os.MkdirAll(debugDir, 0755); err != nil { | ||
| slog.Error("CCOtel: Failed to create debug directory", "error", err) | ||
| return | ||
| } | ||
|
|
||
| filePath := filepath.Join(debugDir, filename) | ||
| f, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | ||
| if err != nil { | ||
| slog.Error("CCOtel: Failed to open debug file", "error", err, "path", filePath) | ||
| return | ||
| } | ||
| defer f.Close() | ||
|
|
||
| jsonData, err := json.MarshalIndent(data, "", " ") | ||
| if err != nil { | ||
| slog.Error("CCOtel: Failed to marshal debug data", "error", err) | ||
| return | ||
| } | ||
|
|
||
| timestamp := time.Now().Format(time.RFC3339) | ||
| if _, err := f.WriteString(fmt.Sprintf("\n--- %s ---\n%s\n", timestamp, jsonData)); err != nil { | ||
| slog.Error("CCOtel: Failed to write debug data", "error", err) | ||
| } | ||
| } |
There was a problem hiding this comment.
This function writes to a file but doesn't handle concurrent access, which can lead to race conditions and corrupted debug logs since ProcessMetrics and ProcessLogs can be called from multiple goroutines. To ensure thread safety, the file writing operations should be synchronized using a mutex.
Additionally, using fmt.Sprintf with f.WriteString can be inefficient for large JSON payloads as it allocates a single large string. Using fmt.Fprintf is more memory-efficient.
The suggested change below incorporates both thread safety with a mutex and more efficient file writing. Note that this change depends on another suggestion to add the debugMtx field to the CCOtelProcessor struct.
func (p *CCOtelProcessor) writeDebugFile(filename string, data interface{}) {
p.debugMtx.Lock()
defer p.debugMtx.Unlock()
debugDir := filepath.Join(os.TempDir(), "shelltime")
if err := os.MkdirAll(debugDir, 0755); err != nil {
slog.Error("CCOtel: Failed to create debug directory", "error", err)
return
}
filePath := filepath.Join(debugDir, filename)
f, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
slog.Error("CCOtel: Failed to open debug file", "error", err, "path", filePath)
return
}
defer f.Close()
jsonData, err := json.MarshalIndent(data, "", " ")
if err != nil {
slog.Error("CCOtel: Failed to marshal debug data", "error", err)
return
}
timestamp := time.Now().Format(time.RFC3339)
if _, err := fmt.Fprintf(f, "\n--- %s ---\n%s\n", timestamp, jsonData); err != nil {
slog.Error("CCOtel: Failed to write debug data", "error", err)
}
}
Summary
debugoption to CCOtel config for troubleshooting OTEL data/tmp/shelltime/ccotel-debug-{metrics,logs}.txtConfig Example
Test plan
/tmp/shelltime/🤖 Generated with Claude Code