Skip to content

Commit 3e44498

Browse files
committed
Add logging volume metrics to Containerd CRI plugin
Signed-off-by: Sophie Liu <[email protected]>
1 parent 3bfa855 commit 3e44498

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

pkg/cri/io/logger.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,10 @@ func redirectLogs(path string, rc io.ReadCloser, w io.Writer, s StreamType, maxL
143143
lineBuffer.Write(l)
144144
}
145145
lineBuffer.WriteByte(eol)
146-
if _, err := lineBuffer.WriteTo(w); err != nil {
146+
if n, err := lineBuffer.WriteTo(w); err == nil {
147+
outputEntries.Inc()
148+
outputBytes.Inc(float64(n))
149+
} else {
147150
logrus.WithError(err).Errorf("Fail to write %q log to log file %q", s, path)
148151
// Continue on write error to drain the container output.
149152
}
@@ -153,6 +156,8 @@ func redirectLogs(path string, rc io.ReadCloser, w io.Writer, s StreamType, maxL
153156
newLine, isPrefix, err := readLine(r)
154157
// NOTE(random-liu): readLine can return actual content even if there is an error.
155158
if len(newLine) > 0 {
159+
inputEntries.Inc()
160+
inputBytes.Inc(float64(len(newLine)))
156161
// Buffer returned by ReadLine will change after
157162
// next read, copy it.
158163
l := make([]byte, len(newLine))
@@ -183,6 +188,7 @@ func redirectLogs(path string, rc io.ReadCloser, w io.Writer, s StreamType, maxL
183188
}
184189
buf[len(buf)-1] = last[:len(last)-exceedLen]
185190
writeLineBuffer(partial, buf)
191+
splitEntries.Inc()
186192
buf = [][]byte{last[len(last)-exceedLen:]}
187193
length = exceedLen
188194
}

pkg/cri/io/metrics.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package io
18+
19+
import "github.com/docker/go-metrics"
20+
21+
var (
22+
inputEntries metrics.Counter
23+
outputEntries metrics.Counter
24+
inputBytes metrics.Counter
25+
outputBytes metrics.Counter
26+
splitEntries metrics.Counter
27+
)
28+
29+
func init() {
30+
// These CRI metrics record input and output logging volume.
31+
ns := metrics.NewNamespace("containerd", "cri", nil)
32+
33+
inputEntries = ns.NewCounter("input_entries", "Number of log entries received")
34+
outputEntries = ns.NewCounter("output_entries", "Number of log entries successfully written to disk")
35+
inputBytes = ns.NewCounter("input_bytes", "Size of logs received")
36+
outputBytes = ns.NewCounter("output_bytes", "Size of logs successfully written to disk")
37+
splitEntries = ns.NewCounter("split_entries", "Number of extra log entries created by splitting the "+
38+
"original log entry. This happens when the original log entry exceeds length limit. "+
39+
"This metric does not count the original log entry.")
40+
41+
metrics.Register(ns)
42+
}

0 commit comments

Comments
 (0)