diff --git a/cmd/ignite-spawn/ignite-spawn.go b/cmd/ignite-spawn/ignite-spawn.go index 14424f5c1..5ef3b6a4d 100644 --- a/cmd/ignite-spawn/ignite-spawn.go +++ b/cmd/ignite-spawn/ignite-spawn.go @@ -3,8 +3,10 @@ package main import ( "fmt" "os" + "path" "github.com/weaveworks/ignite/pkg/container" + "github.com/weaveworks/ignite/pkg/container/prometheus" "github.com/weaveworks/ignite/pkg/metadata/loader" "github.com/weaveworks/ignite/pkg/metadata/vmmd" ) @@ -43,6 +45,10 @@ func StartVM(co *options) error { return err } + // Serve metrics over an unix socket in the VM's own directory + metricsSocket := path.Join(co.vm.ObjectPath(), "prometheus.sock") + go prometheus.ServeMetrics(metricsSocket) + // VM state handling if err := co.vm.SetState(vmmd.Running); err != nil { return fmt.Errorf("failed to update VM state: %v", err) diff --git a/pkg/container/prometheus/server.go b/pkg/container/prometheus/server.go new file mode 100644 index 000000000..1f5415b73 --- /dev/null +++ b/pkg/container/prometheus/server.go @@ -0,0 +1,30 @@ +package prometheus + +import ( + "net/http" + "net" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" +) + +func ServeMetrics(socketPath string) error { + // Create a registry to register metrics into + registry := prometheus.NewRegistry() + // Register the default collectors with this registry + registry.MustRegister(prometheus.NewBuildInfoCollector()) + registry.MustRegister(prometheus.NewGoCollector()) + registry.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{})) + + // Create a HTTP server to handle metrics requests + server := http.Server{ + Handler: promhttp.HandlerFor(registry, promhttp.HandlerOpts{}), + } + + // Listen on the unix socket and serve the web server + unixListener, err := net.Listen("unix", socketPath) + if err != nil { + panic(err) + } + return server.Serve(unixListener) +}