log_metrics.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package collectors
  14. import (
  15. "k8s.io/component-base/metrics"
  16. "k8s.io/klog"
  17. statsapi "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1"
  18. )
  19. var (
  20. descLogSize = metrics.NewDesc(
  21. "kubelet_container_log_filesystem_used_bytes",
  22. "Bytes used by the container's logs on the filesystem.",
  23. []string{
  24. "uid",
  25. "namespace",
  26. "pod",
  27. "container",
  28. }, nil,
  29. metrics.ALPHA,
  30. "",
  31. )
  32. )
  33. type logMetricsCollector struct {
  34. metrics.BaseStableCollector
  35. podStats func() ([]statsapi.PodStats, error)
  36. }
  37. // Check if logMetricsCollector implements necessary interface
  38. var _ metrics.StableCollector = &logMetricsCollector{}
  39. // NewLogMetricsCollector implements the metrics.StableCollector interface and
  40. // exposes metrics about container's log volume size.
  41. func NewLogMetricsCollector(podStats func() ([]statsapi.PodStats, error)) metrics.StableCollector {
  42. return &logMetricsCollector{
  43. podStats: podStats,
  44. }
  45. }
  46. // DescribeWithStability implements the metrics.StableCollector interface.
  47. func (c *logMetricsCollector) DescribeWithStability(ch chan<- *metrics.Desc) {
  48. ch <- descLogSize
  49. }
  50. // CollectWithStability implements the metrics.StableCollector interface.
  51. func (c *logMetricsCollector) CollectWithStability(ch chan<- metrics.Metric) {
  52. podStats, err := c.podStats()
  53. if err != nil {
  54. klog.Errorf("failed to get pod stats: %v", err)
  55. return
  56. }
  57. for _, ps := range podStats {
  58. for _, c := range ps.Containers {
  59. if c.Logs != nil && c.Logs.UsedBytes != nil {
  60. ch <- metrics.NewLazyConstMetric(
  61. descLogSize,
  62. metrics.GaugeValue,
  63. float64(*c.Logs.UsedBytes),
  64. ps.PodRef.UID,
  65. ps.PodRef.Namespace,
  66. ps.PodRef.Name,
  67. c.Name,
  68. )
  69. }
  70. }
  71. }
  72. }