Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
Add a prometheus HTTP server per ignite-spawn process in an unix socket
Browse files Browse the repository at this point in the history
  • Loading branch information
luxas committed Jul 1, 2019
1 parent bce9fde commit 94abc52
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/ignite-spawn/ignite-spawn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions pkg/container/prometheus/server.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 94abc52

Please sign in to comment.