|
| 1 | +package llamacpp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "os/signal" |
| 9 | + "strconv" |
| 10 | + "syscall" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +// Config holds configuration for the llama.cpp monitor |
| 15 | +type Config struct { |
| 16 | + // Base URL for the llama.cpp API |
| 17 | + BaseURL string |
| 18 | + |
| 19 | + // Model name for labeling metrics |
| 20 | + ModelName string |
| 21 | + |
| 22 | + // Exporter address for Prometheus metrics |
| 23 | + ExporterAddr string |
| 24 | + |
| 25 | + // Interval between metrics scrapes |
| 26 | + ScrapeInterval time.Duration |
| 27 | + |
| 28 | + // HTTP client timeout |
| 29 | + ClientTimeout time.Duration |
| 30 | +} |
| 31 | + |
| 32 | +// Monitor handles monitoring of a llama.cpp instance |
| 33 | +type Monitor struct { |
| 34 | + config Config |
| 35 | + client *http.Client |
| 36 | + exporter *Exporter |
| 37 | +} |
| 38 | + |
| 39 | +// DefaultConfig returns the default configuration |
| 40 | +func DefaultConfig() Config { |
| 41 | + return Config{ |
| 42 | + BaseURL: getEnvOrDefault("LLAMACPP_BASE_URL", "http://model-runner.docker.internal/engines/llama.cpp/v1"), |
| 43 | + ModelName: getEnvOrDefault("LLAMACPP_MODEL", "llama"), |
| 44 | + ExporterAddr: getEnvOrDefault("LLAMACPP_EXPORTER_ADDR", ":9100"), |
| 45 | + ScrapeInterval: getDurationEnvOrDefault("LLAMACPP_SCRAPE_INTERVAL", 5*time.Second), |
| 46 | + ClientTimeout: getDurationEnvOrDefault("LLAMACPP_CLIENT_TIMEOUT", 3*time.Second), |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// NewMonitor creates a new llama.cpp monitor |
| 51 | +func NewMonitor(config Config) *Monitor { |
| 52 | + return &Monitor{ |
| 53 | + config: config, |
| 54 | + client: &http.Client{ |
| 55 | + Timeout: config.ClientTimeout, |
| 56 | + }, |
| 57 | + exporter: NewExporter(config.ExporterAddr), |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// Start starts the monitor |
| 62 | +func (m *Monitor) Start() error { |
| 63 | + log.Printf("Starting llama.cpp monitor for model %s at %s", m.config.ModelName, m.config.BaseURL) |
| 64 | + |
| 65 | + // Set up graceful shutdown |
| 66 | + ctx, cancel := context.WithCancel(context.Background()) |
| 67 | + defer cancel() |
| 68 | + |
| 69 | + // Handle signals for graceful shutdown |
| 70 | + c := make(chan os.Signal, 1) |
| 71 | + signal.Notify(c, os.Interrupt, syscall.SIGTERM) |
| 72 | + |
| 73 | + go func() { |
| 74 | + <-c |
| 75 | + log.Println("Shutting down llama.cpp monitor...") |
| 76 | + cancel() |
| 77 | + if err := m.exporter.Stop(); err != nil { |
| 78 | + log.Printf("Error stopping exporter: %v", err) |
| 79 | + } |
| 80 | + }() |
| 81 | + |
| 82 | + // Start the exporter |
| 83 | + return m.exporter.Start(m.config.BaseURL, m.config.ModelName, m.config.ScrapeInterval, m.client) |
| 84 | +} |
| 85 | + |
| 86 | +// GetMetrics returns the metrics collector |
| 87 | +func (m *Monitor) GetMetrics() *Metrics { |
| 88 | + return m.exporter.Metrics() |
| 89 | +} |
| 90 | + |
| 91 | +// Helper function to get environment variables with defaults |
| 92 | +func getEnvOrDefault(key, defaultValue string) string { |
| 93 | + if value := os.Getenv(key); value != "" { |
| 94 | + return value |
| 95 | + } |
| 96 | + return defaultValue |
| 97 | +} |
| 98 | + |
| 99 | +// Helper function to get durations from environment variables with defaults |
| 100 | +func getDurationEnvOrDefault(key string, defaultValue time.Duration) time.Duration { |
| 101 | + if value := os.Getenv(key); value != "" { |
| 102 | + if duration, err := time.ParseDuration(value); err == nil { |
| 103 | + return duration |
| 104 | + } |
| 105 | + |
| 106 | + // If not a duration, try to parse as seconds |
| 107 | + if seconds, err := strconv.Atoi(value); err == nil { |
| 108 | + return time.Duration(seconds) * time.Second |
| 109 | + } |
| 110 | + } |
| 111 | + return defaultValue |
| 112 | +} |
0 commit comments