somewhat working ebpf bpftrace
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
"time"
|
||||
|
||||
"nannyagentv2/internal/config"
|
||||
"nannyagentv2/internal/logging"
|
||||
"nannyagentv2/internal/types"
|
||||
)
|
||||
|
||||
@@ -103,7 +104,7 @@ func (am *AuthManager) StartDeviceAuthorization() (*types.DeviceAuthResponse, er
|
||||
|
||||
// PollForToken polls the token endpoint until authorization is complete
|
||||
func (am *AuthManager) PollForToken(deviceCode string) (*types.TokenResponse, error) {
|
||||
fmt.Println("⏳ Waiting for user authorization...")
|
||||
logging.Info("Waiting for user authorization...")
|
||||
|
||||
for attempts := 0; attempts < MaxPollAttempts; attempts++ {
|
||||
tokenReq := types.TokenRequest{
|
||||
@@ -151,7 +152,7 @@ func (am *AuthManager) PollForToken(deviceCode string) (*types.TokenResponse, er
|
||||
}
|
||||
|
||||
if tokenResp.AccessToken != "" {
|
||||
fmt.Println("\n✅ Authorization successful!")
|
||||
logging.Info("Authorization successful!")
|
||||
return &tokenResp, nil
|
||||
}
|
||||
|
||||
@@ -230,7 +231,7 @@ func (am *AuthManager) SaveToken(token *types.AuthToken) error {
|
||||
refreshTokenPath := filepath.Join(TokenStorageDir, RefreshTokenFile)
|
||||
if err := os.WriteFile(refreshTokenPath, []byte(token.RefreshToken), 0600); err != nil {
|
||||
// Don't fail if refresh token backup fails, just log
|
||||
fmt.Printf("Warning: Failed to save backup refresh token: %v\n", err)
|
||||
logging.Warning("Failed to save backup refresh token: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -271,8 +272,8 @@ func (am *AuthManager) RegisterDevice() (*types.AuthToken, error) {
|
||||
return nil, fmt.Errorf("failed to start device authorization: %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Please visit: %s\n", deviceAuth.VerificationURI)
|
||||
fmt.Printf("And enter code: %s\n", deviceAuth.UserCode)
|
||||
logging.Info("Please visit: %s", deviceAuth.VerificationURI)
|
||||
logging.Info("And enter code: %s", deviceAuth.UserCode)
|
||||
|
||||
// Step 2: Poll for token
|
||||
tokenResp, err := am.PollForToken(deviceAuth.DeviceCode)
|
||||
@@ -318,13 +319,13 @@ func (am *AuthManager) EnsureAuthenticated() (*types.AuthToken, error) {
|
||||
// Try to load refresh token from backup file
|
||||
if backupRefreshToken, backupErr := am.loadRefreshTokenFromBackup(); backupErr == nil {
|
||||
refreshToken = backupRefreshToken
|
||||
fmt.Println("🔄 Found backup refresh token, attempting to use it...")
|
||||
logging.Debug("Found backup refresh token, attempting to use it...")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if refreshToken != "" {
|
||||
fmt.Println("🔄 Attempting to refresh access token...")
|
||||
logging.Debug("Attempting to refresh access token...")
|
||||
|
||||
refreshResp, refreshErr := am.RefreshAccessToken(refreshToken)
|
||||
if refreshErr == nil {
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"nannyagentv2/internal/logging"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
@@ -39,9 +41,9 @@ func LoadConfig() (*Config, error) {
|
||||
envFile := findEnvFile()
|
||||
if envFile != "" {
|
||||
if err := godotenv.Load(envFile); err != nil {
|
||||
fmt.Printf("Warning: Could not load .env file from %s: %v\n", envFile, err)
|
||||
logging.Warning("Could not load .env file from %s: %v", envFile, err)
|
||||
} else {
|
||||
fmt.Printf("Loaded configuration from %s\n", envFile)
|
||||
logging.Info("Loaded configuration from %s", envFile)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,8 +126,8 @@ func (c *Config) PrintConfig() {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("Configuration:")
|
||||
fmt.Printf(" Supabase Project URL: %s\n", c.SupabaseProjectURL)
|
||||
fmt.Printf(" Metrics Interval: %d seconds\n", c.MetricsInterval)
|
||||
fmt.Printf(" Debug: %v\n", c.Debug)
|
||||
logging.Debug("Configuration:")
|
||||
logging.Debug(" Supabase Project URL: %s", c.SupabaseProjectURL)
|
||||
logging.Debug(" Metrics Interval: %d seconds", c.MetricsInterval)
|
||||
logging.Debug(" Debug: %v", c.Debug)
|
||||
}
|
||||
|
||||
@@ -5,11 +5,39 @@ import (
|
||||
"log"
|
||||
"log/syslog"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// LogLevel defines the logging level
|
||||
type LogLevel int
|
||||
|
||||
const (
|
||||
LevelDebug LogLevel = iota
|
||||
LevelInfo
|
||||
LevelWarning
|
||||
LevelError
|
||||
)
|
||||
|
||||
func (l LogLevel) String() string {
|
||||
switch l {
|
||||
case LevelDebug:
|
||||
return "DEBUG"
|
||||
case LevelInfo:
|
||||
return "INFO"
|
||||
case LevelWarning:
|
||||
return "WARN"
|
||||
case LevelError:
|
||||
return "ERROR"
|
||||
default:
|
||||
return "INFO"
|
||||
}
|
||||
}
|
||||
|
||||
// Logger provides structured logging with configurable levels
|
||||
type Logger struct {
|
||||
syslogWriter *syslog.Writer
|
||||
debugMode bool
|
||||
level LogLevel
|
||||
showEmoji bool
|
||||
}
|
||||
|
||||
var defaultLogger *Logger
|
||||
@@ -18,9 +46,16 @@ func init() {
|
||||
defaultLogger = NewLogger()
|
||||
}
|
||||
|
||||
// NewLogger creates a new logger with default configuration
|
||||
func NewLogger() *Logger {
|
||||
return NewLoggerWithLevel(getLogLevelFromEnv())
|
||||
}
|
||||
|
||||
// NewLoggerWithLevel creates a logger with specified level
|
||||
func NewLoggerWithLevel(level LogLevel) *Logger {
|
||||
l := &Logger{
|
||||
debugMode: os.Getenv("DEBUG") == "true",
|
||||
level: level,
|
||||
showEmoji: os.Getenv("LOG_NO_EMOJI") != "true",
|
||||
}
|
||||
|
||||
// Try to connect to syslog
|
||||
@@ -31,39 +66,87 @@ func NewLogger() *Logger {
|
||||
return l
|
||||
}
|
||||
|
||||
func (l *Logger) Info(format string, args ...interface{}) {
|
||||
msg := fmt.Sprintf(format, args...)
|
||||
if l.syslogWriter != nil {
|
||||
l.syslogWriter.Info(msg)
|
||||
// getLogLevelFromEnv parses log level from environment variable
|
||||
func getLogLevelFromEnv() LogLevel {
|
||||
level := strings.ToUpper(os.Getenv("LOG_LEVEL"))
|
||||
switch level {
|
||||
case "DEBUG":
|
||||
return LevelDebug
|
||||
case "INFO", "":
|
||||
return LevelInfo
|
||||
case "WARN", "WARNING":
|
||||
return LevelWarning
|
||||
case "ERROR":
|
||||
return LevelError
|
||||
default:
|
||||
return LevelInfo
|
||||
}
|
||||
log.Printf("[INFO] %s", msg)
|
||||
}
|
||||
|
||||
// logMessage handles the actual logging
|
||||
func (l *Logger) logMessage(level LogLevel, format string, args ...interface{}) {
|
||||
if level < l.level {
|
||||
return
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf(format, args...)
|
||||
prefix := fmt.Sprintf("[%s]", level.String())
|
||||
|
||||
// Add emoji prefix if enabled
|
||||
if l.showEmoji {
|
||||
switch level {
|
||||
case LevelDebug:
|
||||
prefix = "🔍 " + prefix
|
||||
case LevelInfo:
|
||||
prefix = "ℹ️ " + prefix
|
||||
case LevelWarning:
|
||||
prefix = "⚠️ " + prefix
|
||||
case LevelError:
|
||||
prefix = "❌ " + prefix
|
||||
}
|
||||
}
|
||||
|
||||
// Log to syslog if available
|
||||
if l.syslogWriter != nil {
|
||||
switch level {
|
||||
case LevelDebug:
|
||||
l.syslogWriter.Debug(msg)
|
||||
case LevelInfo:
|
||||
l.syslogWriter.Info(msg)
|
||||
case LevelWarning:
|
||||
l.syslogWriter.Warning(msg)
|
||||
case LevelError:
|
||||
l.syslogWriter.Err(msg)
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("%s %s", prefix, msg)
|
||||
}
|
||||
|
||||
func (l *Logger) Debug(format string, args ...interface{}) {
|
||||
if !l.debugMode {
|
||||
return
|
||||
}
|
||||
msg := fmt.Sprintf(format, args...)
|
||||
if l.syslogWriter != nil {
|
||||
l.syslogWriter.Debug(msg)
|
||||
}
|
||||
log.Printf("[DEBUG] %s", msg)
|
||||
l.logMessage(LevelDebug, format, args...)
|
||||
}
|
||||
|
||||
func (l *Logger) Info(format string, args ...interface{}) {
|
||||
l.logMessage(LevelInfo, format, args...)
|
||||
}
|
||||
|
||||
func (l *Logger) Warning(format string, args ...interface{}) {
|
||||
msg := fmt.Sprintf(format, args...)
|
||||
if l.syslogWriter != nil {
|
||||
l.syslogWriter.Warning(msg)
|
||||
}
|
||||
log.Printf("[WARNING] %s", msg)
|
||||
l.logMessage(LevelWarning, format, args...)
|
||||
}
|
||||
|
||||
func (l *Logger) Error(format string, args ...interface{}) {
|
||||
msg := fmt.Sprintf(format, args...)
|
||||
if l.syslogWriter != nil {
|
||||
l.syslogWriter.Err(msg)
|
||||
}
|
||||
log.Printf("[ERROR] %s", msg)
|
||||
l.logMessage(LevelError, format, args...)
|
||||
}
|
||||
|
||||
// SetLevel changes the logging level
|
||||
func (l *Logger) SetLevel(level LogLevel) {
|
||||
l.level = level
|
||||
}
|
||||
|
||||
// GetLevel returns current logging level
|
||||
func (l *Logger) GetLevel() LogLevel {
|
||||
return l.level
|
||||
}
|
||||
|
||||
func (l *Logger) Close() {
|
||||
@@ -73,14 +156,14 @@ func (l *Logger) Close() {
|
||||
}
|
||||
|
||||
// Global logging functions
|
||||
func Info(format string, args ...interface{}) {
|
||||
defaultLogger.Info(format, args...)
|
||||
}
|
||||
|
||||
func Debug(format string, args ...interface{}) {
|
||||
defaultLogger.Debug(format, args...)
|
||||
}
|
||||
|
||||
func Info(format string, args ...interface{}) {
|
||||
defaultLogger.Info(format, args...)
|
||||
}
|
||||
|
||||
func Warning(format string, args ...interface{}) {
|
||||
defaultLogger.Warning(format, args...)
|
||||
}
|
||||
@@ -88,3 +171,13 @@ func Warning(format string, args ...interface{}) {
|
||||
func Error(format string, args ...interface{}) {
|
||||
defaultLogger.Error(format, args...)
|
||||
}
|
||||
|
||||
// SetLevel sets the global logger level
|
||||
func SetLevel(level LogLevel) {
|
||||
defaultLogger.SetLevel(level)
|
||||
}
|
||||
|
||||
// GetLevel gets the global logger level
|
||||
func GetLevel() LogLevel {
|
||||
return defaultLogger.GetLevel()
|
||||
}
|
||||
|
||||
@@ -241,7 +241,7 @@ type CommandResult struct {
|
||||
type EBPFEnhancedDiagnosticResponse struct {
|
||||
ResponseType string `json:"response_type"`
|
||||
Reasoning string `json:"reasoning"`
|
||||
Commands []Command `json:"commands"`
|
||||
Commands []string `json:"commands"` // Changed to []string to match current prompt format
|
||||
EBPFPrograms []EBPFRequest `json:"ebpf_programs"`
|
||||
NextActions []string `json:"next_actions,omitempty"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user