Skip to content

Commit 2f2d0e3

Browse files
committed
Feature & Refactoring
1. Remove localprom package (update behavior of the metrics cant be separated from the updating of container status) 2. Add some prometheus metrics of container (export at /metrics)
1 parent f505331 commit 2f2d0e3

File tree

351 files changed

+41428
-2753
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

351 files changed

+41428
-2753
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/morikuni/aec v1.0.0 // indirect
1616
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
1717
github.com/opencontainers/image-spec v1.0.1 // indirect
18-
github.com/prometheus/client_golang v1.2.1 // indirect
18+
github.com/prometheus/client_golang v1.2.1
1919
github.com/sirupsen/logrus v1.4.2
2020
github.com/urfave/cli v1.22.1
2121
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,14 @@ github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs
5454
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
5555
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
5656
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
57+
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
5758
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
5859
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
5960
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
6061
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
6162
github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwKs=
6263
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
64+
github.com/json-iterator/go v1.1.7 h1:KfgG9LzI+pYjr4xvmz/5H4FXjokeP+rlHLhv3iH62Fo=
6365
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
6466
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
6567
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
@@ -134,6 +136,7 @@ golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73r
134136
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
135137
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c h1:uOCk1iQW6Vc18bnC13MfzScl+wdKBmM9Y9kU7Z83/lw=
136138
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
139+
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980 h1:dfGZHvZk057jK2MCeWus/TowKpJ8y4AmooUzdBSR9GU=
137140
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
138141
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
139142
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -150,6 +153,7 @@ golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/p
150153
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
151154
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b h1:ag/x1USPSsqHud38I9BAC88qdNLDHHtQ4mlgQIZPPNA=
152155
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
156+
golang.org/x/sys v0.0.0-20191010194322-b09406accb47 h1:/XfQ9z7ib8eEJX2hdgFTZJ/ntt0swNk5oYBziWeTCvY=
153157
golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
154158
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
155159
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

localprom/localprom.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

localprom/task.go

Lines changed: 0 additions & 63 deletions
This file was deleted.

server/controller/container/container.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/YLonely/sysdig-monitor/sysdig"
1313
"github.com/docker/docker/api/types"
1414
"github.com/docker/docker/client"
15-
1615
)
1716

1817
type containerController struct {
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package container
2+
3+
import (
4+
"github.com/prometheus/client_golang/prometheus"
5+
"github.com/prometheus/client_golang/prometheus/promauto"
6+
)
7+
8+
const (
9+
metricsNamespace = "sysdig_monitor"
10+
metricsSubsystem = "container"
11+
12+
containerIDLabel = "id"
13+
syscallTypeLabel = "syscall_type"
14+
containerLayerDirLabel = "layer_dir"
15+
containerFileLabel = "file_name"
16+
containerConnectionSrcIP = "src_ip"
17+
containerConnectionDestIP = "dest_ip"
18+
containerConnectionSrcPort = "src_port"
19+
containerConnectionDestPort = "dest_port"
20+
)
21+
22+
func newGaugeOpts(name, help string) prometheus.GaugeOpts {
23+
return prometheus.GaugeOpts{Subsystem: metricsSubsystem, Namespace: metricsNamespace, Name: name, Help: help}
24+
}
25+
26+
var (
27+
// containerIDMetric is a metric tracks the container's name
28+
containerIDMetric = promauto.NewGaugeVec(
29+
newGaugeOpts(
30+
"container_id",
31+
"The container's id, the value is always 1",
32+
),
33+
[]string{containerIDLabel},
34+
)
35+
36+
//systemCallCount tracks the total invoke times of different types of system call for every container
37+
systemCallCount = promauto.NewGaugeVec(
38+
newGaugeOpts(
39+
"syscall_total",
40+
"Total invoke times of different system call",
41+
),
42+
[]string{containerIDLabel, syscallTypeLabel},
43+
)
44+
45+
//systemCallTotalLatency tracks the total latency of different system call in seconds for every container
46+
systemCallTotalLatency = promauto.NewGaugeVec(
47+
newGaugeOpts(
48+
"system_call_total_latency_seconds",
49+
"Total latency of the system calls of container",
50+
),
51+
[]string{containerIDLabel, syscallTypeLabel},
52+
)
53+
54+
// containerLayerDir is metric tracks the layer dir of each container
55+
containerLayerDir = promauto.NewGaugeVec(
56+
newGaugeOpts(
57+
"layer_dir",
58+
"The dir of the layer of each container, the upper layer's value is 1, the deeper the bigger",
59+
),
60+
[]string{containerIDLabel, containerLayerDirLabel},
61+
)
62+
63+
// containerLayerFileRead tracks the total bytes a container read from a file which belongs to a layer
64+
containerLayerFileRead = promauto.NewGaugeVec(
65+
newGaugeOpts(
66+
"layer_file_read_bytes",
67+
"The total bytes a container read from a file which belongs to a layer",
68+
),
69+
[]string{containerIDLabel, containerLayerDirLabel, containerFileLabel},
70+
)
71+
72+
// containerLayerFileWrite tracks the total bytes a container write to a file which belongs to a layer
73+
containerLayerFileWrite = promauto.NewGaugeVec(
74+
newGaugeOpts(
75+
"layer_file_write_bytes",
76+
"The total bytes a container write from a file which belongs to a layer",
77+
),
78+
[]string{containerIDLabel, containerLayerDirLabel, containerFileLabel},
79+
)
80+
81+
// containerActiveConnectionRead tracks the total bytes a container read from a active net connection
82+
containerActiveConnectionRead = promauto.NewGaugeVec(
83+
newGaugeOpts(
84+
"active_connection_read_bytes",
85+
"The total bytes a container read from a active net connection",
86+
),
87+
[]string{containerIDLabel, containerConnectionSrcIP, containerConnectionSrcPort, containerConnectionDestIP, containerConnectionDestPort},
88+
)
89+
90+
// containerActiveConnectionWrite tracks the total bytes a container write to a active new connection
91+
containerActiveConnectionWrite = promauto.NewGaugeVec(
92+
newGaugeOpts(
93+
"active_connection_write_bytes",
94+
"The total bytes a container write to a active net connection",
95+
),
96+
[]string{containerIDLabel, containerConnectionSrcIP, containerConnectionSrcPort, containerConnectionDestIP, containerConnectionDestPort},
97+
)
98+
)

server/controller/container/prometheus.go

Lines changed: 0 additions & 2 deletions
This file was deleted.

server/controller/container/util.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ type containerEvent struct {
3636
func processLoop(ctx context.Context, c *mutexContainer, ch chan containerEvent) error {
3737
var e containerEvent
3838
var err error
39+
containerIDMetric.WithLabelValues(c.ID).Set(1)
40+
depth := 1
41+
for _, l := range c.LayersInOrder {
42+
containerLayerDir.WithLabelValues(c.ID, l).Set(float64(depth))
43+
depth++
44+
}
3945
for {
4046
select {
4147
case e = <-ch:
@@ -48,6 +54,7 @@ func processLoop(ctx context.Context, c *mutexContainer, ch chan containerEvent)
4854
// container exits
4955
if e.eventType == "procexit" && e.virtualtid == 1 {
5056
log.L.WithField("container-id", c.ID).Debug("container exits")
57+
cleanUpMetrics(c.Container)
5158
return nil
5259
}
5360
if e.eventDir == "<" {
@@ -91,6 +98,8 @@ func handleSysCall(c *model.Container, e containerEvent) error {
9198
call.Calls++
9299
call.TotalTime += latency
93100
c.SystemCalls.TotalCalls++
101+
systemCallCount.WithLabelValues(c.ID, syscall).Set(float64(call.Calls))
102+
systemCallTotalLatency.WithLabelValues(c.ID, syscall).Set(float64(call.TotalTime) / float64(time.Second))
94103
return nil
95104
}
96105

@@ -119,12 +128,15 @@ func handleNetIO(c *model.Container, e containerEvent) error {
119128
conn = &model.Connection{Type: e.fdType}
120129
c.ActiveConnections[meta] = conn
121130
}
131+
sip, sport, dip, dport := meta.SourceIP, strconv.Itoa(meta.SourcePort), meta.DestIP, strconv.Itoa(meta.DestPort)
122132
if e.isIORead {
123133
conn.ReadIn += int64(bufLen)
124134
c.Network.TotalReadIn += int64(bufLen)
135+
containerActiveConnectionRead.WithLabelValues(c.ID, sip, sport, dip, dport).Set(float64(conn.ReadIn))
125136
} else if e.isIOWrite {
126137
conn.WriteOut += int64(bufLen)
127138
c.Network.TotalWriteOut += int64(bufLen)
139+
containerActiveConnectionWrite.WithLabelValues(c.ID, sip, sport, dip, dport).Set(float64(conn.WriteOut))
128140
}
129141
return nil
130142
}
@@ -145,14 +157,17 @@ func handleFileIO(c *model.Container, e containerEvent) error {
145157
}
146158
c.AccessedFiles[fileName] = file
147159
}
160+
layerDir := file.Layer.Dir
148161
if e.isIOWrite {
149162
file.WriteOut += int64(bufLen)
150163
file.Layer.WriteOut += int64(bufLen)
151164
c.FileSystem.TotalWriteOut += int64(bufLen)
165+
containerLayerFileWrite.WithLabelValues(c.ID, layerDir, fileName).Set(float64(file.WriteOut))
152166
} else if e.isIORead {
153167
file.Layer.ReadIn += int64(bufLen)
154168
file.ReadIn += int64(bufLen)
155169
c.FileSystem.TotalReadIn += int64(bufLen)
170+
containerLayerFileRead.WithLabelValues(c.ID, layerDir, fileName).Set(float64(file.ReadIn))
156171
}
157172
latency := e.latency
158173
latency /= time.Millisecond
@@ -188,8 +203,11 @@ func handleNetwork(c *model.Container, e containerEvent) error {
188203
return err
189204
}
190205
// should return nonexists error?
206+
sip, sport, dip, dport := meta.SourceIP, strconv.Itoa(meta.SourcePort), meta.DestIP, strconv.Itoa(meta.DestPort)
191207
if _, exists := c.ActiveConnections[meta]; exists {
192208
delete(c.ActiveConnections, meta)
209+
containerActiveConnectionRead.DeleteLabelValues(c.ID, sip, sport, dip, dport)
210+
containerActiveConnectionWrite.DeleteLabelValues(c.ID, sip, sport, dip, dport)
193211
}
194212
}
195213
return nil
@@ -243,3 +261,30 @@ func attachToLayer(c *model.Container, file *model.File) error {
243261
}
244262
return fmt.Errorf("cant find file %v in any of lower layers", fileName)
245263
}
264+
265+
func cleanUpMetrics(container *model.Container) {
266+
// clean up connection metrics
267+
for meta, _ := range container.ActiveConnections {
268+
sip, sport, dip, dport := meta.SourceIP, strconv.Itoa(meta.SourcePort), meta.DestIP, strconv.Itoa(meta.DestPort)
269+
containerActiveConnectionRead.DeleteLabelValues(container.ID, sip, sport, dip, dport)
270+
containerActiveConnectionWrite.DeleteLabelValues(container.ID, sip, sport, dip, dport)
271+
}
272+
273+
// clean up layer file metrics and layer metric
274+
for layerDir, layerInfo := range container.AccessedLayers {
275+
for fileName, _ := range layerInfo.AccessedFiles {
276+
containerLayerFileRead.DeleteLabelValues(container.ID, layerDir, fileName)
277+
containerLayerFileWrite.DeleteLabelValues(container.ID, layerDir, fileName)
278+
}
279+
containerLayerDir.DeleteLabelValues(container.ID, layerDir)
280+
}
281+
282+
// clean up system call count and latency metrics
283+
for syscall, _ := range container.IndividualCalls {
284+
systemCallCount.DeleteLabelValues(container.ID, syscall)
285+
systemCallTotalLatency.DeleteLabelValues(container.ID, syscall)
286+
}
287+
288+
// delete this container
289+
containerIDMetric.DeleteLabelValues(container.ID)
290+
}
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package prometheus
22

33
import (
4-
"github.com/YLonely/sysdig-monitor/localprom"
54
"github.com/gin-gonic/gin"
65
)
76

87
func (pc *prometheusContorller) metrics(c *gin.Context) {
9-
localprom.RunMetrics()
108
pc.handler.ServeHTTP(c.Writer, c.Request)
119
}

server/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (s *server) Start(ctx context.Context) chan error {
5454
errch <- err
5555
}
5656
}()
57-
log.L.Info("web server start")
57+
log.L.Info("web server start. Listening on port " + s.conf.Port)
5858
return errch
5959
}
6060

0 commit comments

Comments
 (0)