log_metrics.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. "github.com/prometheus/client_golang/prometheus"
  16. "k8s.io/klog"
  17. statsapi "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1"
  18. )
  19. var (
  20. descLogSize = prometheus.NewDesc(
  21. "kubelet_container_log_filesystem_used_bytes",
  22. "Bytes used by the container's logs on the filesystem.",
  23. []string{
  24. "namespace",
  25. "pod",
  26. "container",
  27. }, nil,
  28. )
  29. )
  30. type logMetricsCollector struct {
  31. podStats func() ([]statsapi.PodStats, error)
  32. }
  33. // NewLogMetricsCollector implements the prometheus.Collector interface and
  34. // exposes metrics about container's log volume size.
  35. func NewLogMetricsCollector(podStats func() ([]statsapi.PodStats, error)) prometheus.Collector {
  36. return &logMetricsCollector{
  37. podStats: podStats,
  38. }
  39. }
  40. // Describe implements the prometheus.Collector interface.
  41. func (c *logMetricsCollector) Describe(ch chan<- *prometheus.Desc) {
  42. ch <- descLogSize
  43. }
  44. // Collect implements the prometheus.Collector interface.
  45. func (c *logMetricsCollector) Collect(ch chan<- prometheus.Metric) {
  46. podStats, err := c.podStats()
  47. if err != nil {
  48. klog.Errorf("failed to get pod stats: %v", err)
  49. return
  50. }
  51. for _, ps := range podStats {
  52. for _, c := range ps.Containers {
  53. if c.Logs != nil && c.Logs.UsedBytes != nil {
  54. ch <- prometheus.MustNewConstMetric(
  55. descLogSize,
  56. prometheus.GaugeValue,
  57. float64(*c.Logs.UsedBytes),
  58. ps.PodRef.Namespace,
  59. ps.PodRef.Name,
  60. c.Name,
  61. )
  62. }
  63. }
  64. }
  65. }