Skip to content

Commit f505331

Browse files
committed
Code
Add localprom package into project
1 parent fb7464b commit f505331

File tree

9 files changed

+136
-13
lines changed

9 files changed

+136
-13
lines changed

localprom/localprom.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package localprom
2+
3+
var tasks = []PromTask{}
4+
5+
func Register(t PromTask) {
6+
tasks = append(tasks, t)
7+
}
8+
9+
func RunMetrics() {
10+
for _, t := range tasks {
11+
t.Run()
12+
}
13+
}

localprom/task.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package localprom
2+
3+
import (
4+
"github.com/YLonely/sysdig-monitor/log"
5+
"github.com/prometheus/client_golang/prometheus"
6+
)
7+
8+
// PromTask contains logic to record the prometheus metrics
9+
type PromTask interface {
10+
// Run() should be nonblocking
11+
Run()
12+
}
13+
14+
type taskHandler func() error
15+
type promTask struct {
16+
th taskHandler
17+
}
18+
19+
func NewCounterTask(counter *prometheus.Counter, h func(*prometheus.Counter) error) PromTask {
20+
th := func() error { return h(counter) }
21+
return &promTask{th: th}
22+
}
23+
24+
func NewCounterVecTask(counterVec *prometheus.CounterVec, h func(*prometheus.CounterVec) error) PromTask {
25+
th := func() error { return h(counterVec) }
26+
return &promTask{th: th}
27+
}
28+
29+
func NewGaugeTask(gauge *prometheus.Gauge, h func(*prometheus.Gauge) error) PromTask {
30+
th := func() error { return h(gauge) }
31+
return &promTask{th: th}
32+
}
33+
34+
func NewGaugeVecTask(gaugeVec *prometheus.GaugeVec, h func(*prometheus.GaugeVec) error) PromTask {
35+
th := func() error { return h(gaugeVec) }
36+
return &promTask{th: th}
37+
}
38+
func NewHistogramTask(histogram *prometheus.Histogram, h func(*prometheus.Histogram) error) PromTask {
39+
th := func() error { return h(histogram) }
40+
return &promTask{th: th}
41+
}
42+
43+
func NewHistogramVecTask(histogramVec *prometheus.HistogramVec, h func(*prometheus.HistogramVec) error) PromTask {
44+
th := func() error { return h(histogramVec) }
45+
return &promTask{th: th}
46+
}
47+
48+
func NewSummaryTask(summary *prometheus.Summary, h func(*prometheus.Summary) error) PromTask {
49+
th := func() error { return h(summary) }
50+
return &promTask{th: th}
51+
}
52+
53+
func NewSummaryVecTask(summaryVec *prometheus.SummaryVec, h func(*prometheus.SummaryVec) error) PromTask {
54+
th := func() error { return h(summaryVec) }
55+
return &promTask{th: th}
56+
}
57+
58+
func (pt *promTask) Run() {
59+
err := pt.th()
60+
if err != nil {
61+
log.L.WithError(err).Error()
62+
}
63+
}

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func main() {
1717
app := cli.NewApp()
1818
app.Name = "sysdig-monitor"
1919
app.Usage = "Monitor using sysdig to trace all containers running on host."
20+
app.Version = "v0.0.1"
2021

2122
app.Flags = []cli.Flag{
2223
cli.StringFlag{

server/controller/container/container.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import (
1212
"github.com/YLonely/sysdig-monitor/sysdig"
1313
"github.com/docker/docker/api/types"
1414
"github.com/docker/docker/client"
15+
1516
)
1617

17-
// Container represents a top level controller for container
18-
type ContainerController struct {
18+
type containerController struct {
1919
containerRouter router.Router
2020
// event chan
2121
ec chan sysdig.Event
@@ -54,7 +54,7 @@ const incompleteContainerName = "incomplete"
5454
func NewController(ctx context.Context, serverErrorChannel chan<- error) (controller.Controller, error) {
5555
r := router.NewGroupRouter("/container")
5656
sysdigServer := sysdig.NewServer()
57-
res := &ContainerController{containerRouter: r, ec: sysdigServer.Subscribe(), containers: map[string]*mutexContainer{}, containerCh: map[string]chan containerEvent{}}
57+
res := &containerController{containerRouter: r, ec: sysdigServer.Subscribe(), containers: map[string]*mutexContainer{}, containerCh: map[string]chan containerEvent{}}
5858

5959
cli, err := client.NewClientWithOpts(client.FromEnv)
6060
if err != nil {
@@ -77,18 +77,18 @@ func NewController(ctx context.Context, serverErrorChannel chan<- error) (contro
7777
return res, nil
7878
}
7979

80-
var _ controller.Controller = &ContainerController{}
80+
var _ controller.Controller = &containerController{}
8181

82-
func (cc *ContainerController) BindedRoutes() []router.Route {
82+
func (cc *containerController) BindedRoutes() []router.Route {
8383
return cc.containerRouter.Routes()
8484
}
8585

86-
func (cc *ContainerController) initRouter() {
86+
func (cc *containerController) initRouter() {
8787
cc.containerRouter.AddRoute("/", router.MethodGet, cc.getAllContainers)
8888
cc.containerRouter.AddRoute("/:id", router.MethodGet, cc.getContainer)
8989
}
9090

91-
func (cc *ContainerController) start(ctx context.Context) error {
91+
func (cc *containerController) start(ctx context.Context) error {
9292
go func() {
9393
var (
9494
e sysdig.Event
@@ -140,15 +140,15 @@ func (cc *ContainerController) start(ctx context.Context) error {
140140
return nil
141141
}
142142

143-
func (cc *ContainerController) containerJSON(ctx context.Context, id string) (*types.ContainerJSON, error) {
143+
func (cc *containerController) containerJSON(ctx context.Context, id string) (*types.ContainerJSON, error) {
144144
j, err := cc.dockerCli.ContainerInspect(ctx, id)
145145
if err != nil {
146146
return nil, err
147147
}
148148
return &j, nil
149149
}
150150

151-
func (cc *ContainerController) containerProcessLoop(ctx context.Context, container *mutexContainer, ch chan containerEvent) {
151+
func (cc *containerController) containerProcessLoop(ctx context.Context, container *mutexContainer, ch chan containerEvent) {
152152
log.L.WithField("container-id", container.ID).Info("processLoop start")
153153
err := processLoop(ctx, container, ch)
154154
log.L.WithField("container-id", container.ID).Info("processLoop exits")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package container
2+

server/controller/container/routes.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"github.com/gin-gonic/gin"
66
)
77

8-
func (cc *ContainerController) getAllContainers(c *gin.Context) {
8+
func (cc *containerController) getAllContainers(c *gin.Context) {
99
res := map[string]string{}
1010
cc.cm.RLock()
1111
for id, container := range cc.containers {
@@ -26,7 +26,7 @@ type GetContainerResponse struct {
2626
AccessedLayers []*model.LayerInfo `json:"accessed_layers"`
2727
}
2828

29-
func (cc *ContainerController) getContainer(c *gin.Context) {
29+
func (cc *containerController) getContainer(c *gin.Context) {
3030
cid := c.Param("id")
3131
cc.cm.RLock()
3232
container, exists := cc.containers[cid]
@@ -47,4 +47,4 @@ func (cc *ContainerController) getContainer(c *gin.Context) {
4747
}
4848

4949
c.JSON(200, GetContainerResponse{Container: container.Container, ActiveConnections: flattenConns, AccessedLayers: flattenLayersInfo})
50-
}
50+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package prometheus
2+
3+
import (
4+
"context"
5+
"net/http"
6+
7+
8+
"github.com/YLonely/sysdig-monitor/server/controller"
9+
"github.com/YLonely/sysdig-monitor/server/router"
10+
"github.com/prometheus/client_golang/prometheus/promhttp"
11+
)
12+
13+
type prometheusContorller struct {
14+
router router.Router
15+
handler http.Handler
16+
}
17+
18+
func NewController(ctx context.Context) controller.Controller {
19+
res := &prometheusContorller{router: router.NewRouter(), handler: promhttp.Handler()}
20+
res.initRouter()
21+
return res
22+
}
23+
24+
func (pc *prometheusContorller) BindedRoutes() []router.Route {
25+
return pc.router.Routes()
26+
}
27+
28+
func (pc *prometheusContorller) initRouter() {
29+
pc.router.AddRoute("/metrics", router.MethodGet, pc.metrics)
30+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package prometheus
2+
3+
import (
4+
"github.com/YLonely/sysdig-monitor/localprom"
5+
"github.com/gin-gonic/gin"
6+
)
7+
8+
func (pc *prometheusContorller) metrics(c *gin.Context) {
9+
localprom.RunMetrics()
10+
pc.handler.ServeHTTP(c.Writer, c.Request)
11+
}

server/server.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"net/http"
77
"time"
88

9+
"github.com/YLonely/sysdig-monitor/server/controller/prometheus"
10+
911
"github.com/YLonely/sysdig-monitor/server/controller"
1012

1113
"github.com/gin-gonic/gin"
@@ -37,14 +39,15 @@ func NewServer(conf Config) Server {
3739
func (s *server) Start(ctx context.Context) chan error {
3840
errch := make(chan error, 1)
3941
containerContorller, err := container.NewController(ctx, errch)
42+
promContorller := prometheus.NewController(ctx)
4043
if err != nil {
4144
errch <- err
4245
return errch
4346
}
4447
gin.SetMode(gin.ReleaseMode)
4548
gin.DefaultWriter = ioutil.Discard
4649
ginServer := gin.Default()
47-
initRoutes(ginServer, containerContorller) // may be more controller?
50+
initRoutes(ginServer, containerContorller, promContorller) // may be more controller?
4851
s.httpServer = &http.Server{Addr: s.conf.Port, Handler: ginServer}
4952
go func() {
5053
if err := s.httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {

0 commit comments

Comments
 (0)