github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/metrics/core/metrics.go (about)

     1  // Copyright 2015 Google Inc. All Rights Reserved.
     2  //
     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  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package core
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  
    21  	cadvisor "github.com/google/cadvisor/info/v1"
    22  	kube_api "k8s.io/api/core/v1"
    23  )
    24  
    25  const (
    26  	CustomMetricPrefix = "custom/"
    27  )
    28  
    29  // Provided by Kubelet/cadvisor.
    30  var StandardMetrics = []Metric{
    31  	MetricUptime,
    32  	MetricCpuUsage,
    33  	MetricCpuLoad,
    34  	MetricEphemeralStorageUsage,
    35  	MetricMemoryUsage,
    36  	MetricMemoryRSS,
    37  	MetricMemoryCache,
    38  	MetricMemoryWorkingSet,
    39  	MetricMemoryPageFaults,
    40  	MetricMemoryMajorPageFaults,
    41  	MetricNetworkRx,
    42  	MetricNetworkRxErrors,
    43  	MetricNetworkTx,
    44  	MetricNetworkTxErrors}
    45  
    46  // Metrics computed based on cluster state using Kubernetes API.
    47  var AdditionalMetrics = []Metric{
    48  	MetricCpuRequest,
    49  	MetricCpuLimit,
    50  	MetricMemoryRequest,
    51  	MetricMemoryLimit,
    52  	MetricEphemeralStorageRequest,
    53  	MetricEphemeralStorageLimit}
    54  
    55  // Computed based on corresponding StandardMetrics.
    56  var RateMetrics = []Metric{
    57  	MetricCpuUsageRate,
    58  	MetricMemoryPageFaultsRate,
    59  	MetricMemoryMajorPageFaultsRate,
    60  	MetricNetworkRxRate,
    61  	MetricNetworkRxErrorsRate,
    62  	MetricNetworkTxRate,
    63  	MetricNetworkTxErrorsRate,
    64  	MetricDiskIOReadRate,
    65  	MetricDiskIOWriteRate}
    66  
    67  var RateMetricsMapping = map[string]Metric{
    68  	MetricCpuUsage.MetricDescriptor.Name:              MetricCpuUsageRate,
    69  	MetricMemoryPageFaults.MetricDescriptor.Name:      MetricMemoryPageFaultsRate,
    70  	MetricMemoryMajorPageFaults.MetricDescriptor.Name: MetricMemoryMajorPageFaultsRate,
    71  	MetricNetworkRx.MetricDescriptor.Name:             MetricNetworkRxRate,
    72  	MetricNetworkRxErrors.MetricDescriptor.Name:       MetricNetworkRxErrorsRate,
    73  	MetricNetworkTx.MetricDescriptor.Name:             MetricNetworkTxRate,
    74  	MetricNetworkTxErrors.MetricDescriptor.Name:       MetricNetworkTxErrorsRate,
    75  	MetricDiskIORead.MetricDescriptor.Name:            MetricDiskIOReadRate,
    76  	MetricDiskIOWrite.MetricDescriptor.Name:           MetricDiskIOWriteRate}
    77  
    78  var LabeledMetrics = []Metric{
    79  	MetricDiskIORead,
    80  	MetricDiskIOReadRate,
    81  	MetricDiskIOWrite,
    82  	MetricDiskIOWriteRate,
    83  	MetricFilesystemUsage,
    84  	MetricFilesystemLimit,
    85  	MetricFilesystemAvailable,
    86  	MetricFilesystemInodes,
    87  	MetricFilesystemInodesFree,
    88  	MetricAcceleratorMemoryTotal,
    89  	MetricAcceleratorMemoryUsed,
    90  	MetricAcceleratorDutyCycle,
    91  }
    92  
    93  var NodeAutoscalingMetrics = []Metric{
    94  	MetricNodeCpuCapacity,
    95  	MetricNodeMemoryCapacity,
    96  	MetricNodeEphemeralStorageCapacity,
    97  	MetricNodeCpuAllocatable,
    98  	MetricNodeMemoryAllocatable,
    99  	MetricNodeEphemeralStorageAllocatable,
   100  	MetricNodeCpuUtilization,
   101  	MetricNodeMemoryUtilization,
   102  	MetricNodeEphemeralStorageUtilization,
   103  	MetricNodeCpuReservation,
   104  	MetricNodeMemoryReservation,
   105  	MetricNodeEphemeralStorageReservation,
   106  }
   107  
   108  var CpuMetrics = []Metric{
   109  	MetricCpuLimit,
   110  	MetricCpuRequest,
   111  	MetricCpuUsage,
   112  	MetricCpuLoad,
   113  	MetricCpuUsageRate,
   114  	MetricNodeCpuAllocatable,
   115  	MetricNodeCpuCapacity,
   116  	MetricNodeCpuReservation,
   117  	MetricNodeCpuUtilization,
   118  }
   119  var FilesystemMetrics = []Metric{
   120  	MetricFilesystemAvailable,
   121  	MetricFilesystemLimit,
   122  	MetricFilesystemUsage,
   123  	MetricFilesystemInodes,
   124  	MetricFilesystemInodesFree,
   125  }
   126  var MemoryMetrics = []Metric{
   127  	MetricMemoryLimit,
   128  	MetricMemoryMajorPageFaults,
   129  	MetricMemoryMajorPageFaultsRate,
   130  	MetricMemoryPageFaults,
   131  	MetricMemoryPageFaultsRate,
   132  	MetricMemoryRequest,
   133  	MetricMemoryUsage,
   134  	MetricMemoryRSS,
   135  	MetricMemoryCache,
   136  	MetricMemoryWorkingSet,
   137  	MetricNodeMemoryAllocatable,
   138  	MetricNodeMemoryCapacity,
   139  	MetricNodeMemoryUtilization,
   140  	MetricNodeMemoryReservation,
   141  }
   142  var NetworkMetrics = []Metric{
   143  	MetricNetworkRx,
   144  	MetricNetworkRxErrors,
   145  	MetricNetworkRxErrorsRate,
   146  	MetricNetworkRxRate,
   147  	MetricNetworkTx,
   148  	MetricNetworkTxErrors,
   149  	MetricNetworkTxErrorsRate,
   150  	MetricNetworkTxRate,
   151  }
   152  
   153  // Maps from resource name to the metric that tracks container resource request
   154  // on that resource. The name of the metric is ResourceName/request where ResourceName
   155  // is the name of the resource requested in container resource requests.
   156  var ResourceRequestMetrics = map[kube_api.ResourceName]Metric{
   157  	kube_api.ResourceCPU:              MetricCpuRequest,
   158  	kube_api.ResourceMemory:           MetricMemoryRequest,
   159  	kube_api.ResourceEphemeralStorage: MetricEphemeralStorageRequest,
   160  }
   161  
   162  type MetricFamily string
   163  
   164  const (
   165  	MetricFamilyCpu        MetricFamily = "cpu"
   166  	MetricFamilyFilesystem              = "filesystem"
   167  	MetricFamilyMemory                  = "memory"
   168  	MetricFamilyNetwork                 = "network"
   169  	MetricFamilyGeneral                 = "general"
   170  )
   171  
   172  var MetricFamilies = map[MetricFamily][]Metric{
   173  	MetricFamilyCpu:        CpuMetrics,
   174  	MetricFamilyFilesystem: FilesystemMetrics,
   175  	MetricFamilyMemory:     MemoryMetrics,
   176  	MetricFamilyNetwork:    NetworkMetrics,
   177  }
   178  
   179  func MetricFamilyForName(metricName string) MetricFamily {
   180  	for family, metrics := range MetricFamilies {
   181  		for _, metric := range metrics {
   182  			if metricName == metric.Name {
   183  				return family
   184  			}
   185  		}
   186  	}
   187  	return MetricFamilyGeneral
   188  }
   189  
   190  var AllMetrics = append(append(append(append(StandardMetrics, AdditionalMetrics...), RateMetrics...), LabeledMetrics...),
   191  	NodeAutoscalingMetrics...)
   192  
   193  // Definition of Standard Metrics.
   194  var MetricUptime = Metric{
   195  	MetricDescriptor: MetricDescriptor{
   196  		Name:        "uptime",
   197  		Description: "Number of milliseconds since the container was started",
   198  		Type:        MetricCumulative,
   199  		ValueType:   ValueInt64,
   200  		Units:       UnitsMilliseconds,
   201  	},
   202  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   203  		return !spec.CreationTime.IsZero()
   204  	},
   205  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   206  		return MetricValue{
   207  			ValueType:  ValueInt64,
   208  			MetricType: MetricCumulative,
   209  			IntValue:   time.Since(spec.CreationTime).Nanoseconds() / time.Millisecond.Nanoseconds()}
   210  	},
   211  }
   212  
   213  var MetricRestartCount = Metric{
   214  	MetricDescriptor: MetricDescriptor{
   215  		Name:        "restart_count",
   216  		Description: "Number of container restarts",
   217  		Type:        MetricCumulative,
   218  		ValueType:   ValueInt64,
   219  		Units:       UnitsCount,
   220  	},
   221  }
   222  
   223  var MetricCpuLoad = Metric{
   224  	MetricDescriptor: MetricDescriptor{
   225  		Name:        "cpu/load",
   226  		Description: "CPU load",
   227  		Type:        MetricGauge,
   228  		ValueType:   ValueInt64,
   229  		Units:       UnitsCount,
   230  	},
   231  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   232  		return spec.HasCpu
   233  	},
   234  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   235  		return MetricValue{
   236  			ValueType:  ValueInt64,
   237  			MetricType: MetricGauge,
   238  			IntValue:   int64(stat.Cpu.LoadAverage)}
   239  	},
   240  }
   241  
   242  var MetricCpuUsage = Metric{
   243  	MetricDescriptor: MetricDescriptor{
   244  		Name:        "cpu/usage",
   245  		Description: "Cumulative CPU usage on all cores",
   246  		Type:        MetricCumulative,
   247  		ValueType:   ValueInt64,
   248  		Units:       UnitsNanoseconds,
   249  	},
   250  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   251  		return spec.HasCpu
   252  	},
   253  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   254  		return MetricValue{
   255  			ValueType:  ValueInt64,
   256  			MetricType: MetricCumulative,
   257  			IntValue:   int64(stat.Cpu.Usage.Total)}
   258  	},
   259  }
   260  
   261  var MetricEphemeralStorageUsage = Metric{
   262  	MetricDescriptor: MetricDescriptor{
   263  		Name:        "ephemeral_storage/usage",
   264  		Description: "Ephemeral storage usage",
   265  		Type:        MetricGauge,
   266  		ValueType:   ValueInt64,
   267  		Units:       UnitsBytes,
   268  	},
   269  }
   270  var MetricMemoryUsage = Metric{
   271  	MetricDescriptor: MetricDescriptor{
   272  		Name:        "memory/usage",
   273  		Description: "Total memory usage",
   274  		Type:        MetricGauge,
   275  		ValueType:   ValueInt64,
   276  		Units:       UnitsBytes,
   277  	},
   278  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   279  		return spec.HasMemory
   280  	},
   281  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   282  		return MetricValue{
   283  			ValueType:  ValueInt64,
   284  			MetricType: MetricGauge,
   285  			IntValue:   int64(stat.Memory.Usage)}
   286  	},
   287  }
   288  
   289  var MetricMemoryCache = Metric{
   290  	MetricDescriptor: MetricDescriptor{
   291  		Name:        "memory/cache",
   292  		Description: "Cache memory",
   293  		Type:        MetricGauge,
   294  		ValueType:   ValueInt64,
   295  		Units:       UnitsBytes,
   296  	},
   297  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   298  		return spec.HasMemory
   299  	},
   300  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   301  		return MetricValue{
   302  			ValueType:  ValueInt64,
   303  			MetricType: MetricGauge,
   304  			IntValue:   int64(stat.Memory.Cache)}
   305  	},
   306  }
   307  
   308  var MetricMemoryRSS = Metric{
   309  	MetricDescriptor: MetricDescriptor{
   310  		Name:        "memory/rss",
   311  		Description: "RSS memory",
   312  		Type:        MetricGauge,
   313  		ValueType:   ValueInt64,
   314  		Units:       UnitsBytes,
   315  	},
   316  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   317  		return spec.HasMemory
   318  	},
   319  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   320  		return MetricValue{
   321  			ValueType:  ValueInt64,
   322  			MetricType: MetricGauge,
   323  			IntValue:   int64(stat.Memory.RSS)}
   324  	},
   325  }
   326  
   327  var MetricMemoryWorkingSet = Metric{
   328  	MetricDescriptor: MetricDescriptor{
   329  		Name:        "memory/working_set",
   330  		Description: "Total working set usage. Working set is the memory being used and not easily dropped by the kernel",
   331  		Type:        MetricGauge,
   332  		ValueType:   ValueInt64,
   333  		Units:       UnitsBytes,
   334  	},
   335  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   336  		return spec.HasMemory
   337  	},
   338  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   339  		return MetricValue{
   340  			ValueType:  ValueInt64,
   341  			MetricType: MetricGauge,
   342  			IntValue:   int64(stat.Memory.WorkingSet)}
   343  	},
   344  }
   345  
   346  var MetricMemoryPageFaults = Metric{
   347  	MetricDescriptor: MetricDescriptor{
   348  		Name:        "memory/page_faults",
   349  		Description: "Number of page faults",
   350  		Type:        MetricCumulative,
   351  		ValueType:   ValueInt64,
   352  		Units:       UnitsCount,
   353  	},
   354  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   355  		return spec.HasMemory
   356  	},
   357  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   358  		return MetricValue{
   359  			ValueType:  ValueInt64,
   360  			MetricType: MetricCumulative,
   361  			IntValue:   int64(stat.Memory.ContainerData.Pgfault)}
   362  	},
   363  }
   364  
   365  var MetricMemoryMajorPageFaults = Metric{
   366  	MetricDescriptor: MetricDescriptor{
   367  		Name:        "memory/major_page_faults",
   368  		Description: "Number of major page faults",
   369  		Type:        MetricCumulative,
   370  		ValueType:   ValueInt64,
   371  		Units:       UnitsCount,
   372  	},
   373  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   374  		return spec.HasMemory
   375  	},
   376  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   377  		return MetricValue{
   378  			ValueType:  ValueInt64,
   379  			MetricType: MetricCumulative,
   380  			IntValue:   int64(stat.Memory.ContainerData.Pgmajfault)}
   381  	},
   382  }
   383  
   384  var MetricNetworkRx = Metric{
   385  	MetricDescriptor: MetricDescriptor{
   386  		Name:        "network/rx",
   387  		Description: "Cumulative number of bytes received over the network",
   388  		Type:        MetricCumulative,
   389  		ValueType:   ValueInt64,
   390  		Units:       UnitsBytes,
   391  	},
   392  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   393  		return spec.HasNetwork
   394  	},
   395  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   396  		var rxBytes uint64 = 0
   397  		for _, interfaceStat := range stat.Network.Interfaces {
   398  			rxBytes += interfaceStat.RxBytes
   399  		}
   400  		return MetricValue{
   401  			ValueType:  ValueInt64,
   402  			MetricType: MetricCumulative,
   403  			IntValue:   int64(rxBytes),
   404  		}
   405  	},
   406  }
   407  
   408  var MetricNetworkRxErrors = Metric{
   409  	MetricDescriptor: MetricDescriptor{
   410  		Name:        "network/rx_errors",
   411  		Description: "Cumulative number of errors while receiving over the network",
   412  		Type:        MetricCumulative,
   413  		ValueType:   ValueInt64,
   414  		Units:       UnitsCount,
   415  	},
   416  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   417  		return spec.HasNetwork
   418  	},
   419  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   420  		var rxErrors uint64 = 0
   421  		for _, interfaceStat := range stat.Network.Interfaces {
   422  			rxErrors += interfaceStat.RxErrors
   423  		}
   424  		return MetricValue{
   425  			ValueType:  ValueInt64,
   426  			MetricType: MetricCumulative,
   427  			IntValue:   int64(rxErrors),
   428  		}
   429  	},
   430  }
   431  
   432  var MetricNetworkTx = Metric{
   433  	MetricDescriptor: MetricDescriptor{
   434  		Name:        "network/tx",
   435  		Description: "Cumulative number of bytes sent over the network",
   436  		Type:        MetricCumulative,
   437  		ValueType:   ValueInt64,
   438  		Units:       UnitsBytes,
   439  	},
   440  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   441  		return spec.HasNetwork
   442  	},
   443  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   444  		var txBytes uint64 = 0
   445  		for _, interfaceStat := range stat.Network.Interfaces {
   446  			txBytes += interfaceStat.TxBytes
   447  		}
   448  		return MetricValue{
   449  			ValueType:  ValueInt64,
   450  			MetricType: MetricCumulative,
   451  			IntValue:   int64(txBytes),
   452  		}
   453  	},
   454  }
   455  
   456  var MetricNetworkTxErrors = Metric{
   457  	MetricDescriptor: MetricDescriptor{
   458  		Name:        "network/tx_errors",
   459  		Description: "Cumulative number of errors while sending over the network",
   460  		Type:        MetricCumulative,
   461  		ValueType:   ValueInt64,
   462  		Units:       UnitsCount,
   463  	},
   464  	HasValue: func(spec *cadvisor.ContainerSpec) bool {
   465  		return spec.HasNetwork
   466  	},
   467  	GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue {
   468  		var txErrors uint64 = 0
   469  		for _, interfaceStat := range stat.Network.Interfaces {
   470  			txErrors += interfaceStat.TxErrors
   471  		}
   472  		return MetricValue{
   473  			ValueType:  ValueInt64,
   474  			MetricType: MetricCumulative,
   475  			IntValue:   int64(txErrors),
   476  		}
   477  	},
   478  }
   479  
   480  // Definition of Additional Metrics.
   481  var MetricCpuRequest = Metric{
   482  	MetricDescriptor: MetricDescriptor{
   483  		Name:        "cpu/request",
   484  		Description: "CPU request (the guaranteed amount of resources) in millicores. This metric is Kubernetes specific.",
   485  		Type:        MetricGauge,
   486  		ValueType:   ValueInt64,
   487  		Units:       UnitsCount,
   488  	},
   489  }
   490  
   491  var MetricCpuLimit = Metric{
   492  	MetricDescriptor: MetricDescriptor{
   493  		Name:        "cpu/limit",
   494  		Description: "CPU hard limit in millicores.",
   495  		Type:        MetricGauge,
   496  		ValueType:   ValueInt64,
   497  		Units:       UnitsCount,
   498  	},
   499  }
   500  
   501  var MetricMemoryRequest = Metric{
   502  	MetricDescriptor: MetricDescriptor{
   503  		Name:        "memory/request",
   504  		Description: "Memory request (the guaranteed amount of resources) in bytes. This metric is Kubernetes specific.",
   505  		Type:        MetricGauge,
   506  		ValueType:   ValueInt64,
   507  		Units:       UnitsBytes,
   508  	},
   509  }
   510  
   511  var MetricMemoryLimit = Metric{
   512  	MetricDescriptor: MetricDescriptor{
   513  		Name:        "memory/limit",
   514  		Description: "Memory hard limit in bytes.",
   515  		Type:        MetricGauge,
   516  		ValueType:   ValueInt64,
   517  		Units:       UnitsBytes,
   518  	},
   519  }
   520  
   521  var MetricEphemeralStorageRequest = Metric{
   522  	MetricDescriptor: MetricDescriptor{
   523  		Name:        "ephemeral_storage/request",
   524  		Description: "ephemeral storage request (the guaranteed amount of resources) in bytes. This metric is Kubernetes specific.",
   525  		Type:        MetricGauge,
   526  		ValueType:   ValueInt64,
   527  		Units:       UnitsBytes,
   528  	},
   529  }
   530  
   531  var MetricEphemeralStorageLimit = Metric{
   532  	MetricDescriptor: MetricDescriptor{
   533  		Name:        "ephemeral_storage/limit",
   534  		Description: "ephemeral storage hard limit in bytes.",
   535  		Type:        MetricGauge,
   536  		ValueType:   ValueInt64,
   537  		Units:       UnitsBytes,
   538  	},
   539  }
   540  
   541  // Definition of Rate Metrics.
   542  var MetricCpuUsageRate = Metric{
   543  	MetricDescriptor: MetricDescriptor{
   544  		Name:        "cpu/usage_rate",
   545  		Description: "CPU usage on all cores in millicores",
   546  		Type:        MetricGauge,
   547  		ValueType:   ValueInt64,
   548  		Units:       UnitsCount,
   549  	},
   550  }
   551  
   552  var MetricMemoryPageFaultsRate = Metric{
   553  	MetricDescriptor: MetricDescriptor{
   554  		Name:        "memory/page_faults_rate",
   555  		Description: "Rate of page faults in counts per second",
   556  		Type:        MetricGauge,
   557  		ValueType:   ValueFloat,
   558  		Units:       UnitsCount,
   559  	},
   560  }
   561  
   562  var MetricMemoryMajorPageFaultsRate = Metric{
   563  	MetricDescriptor: MetricDescriptor{
   564  		Name:        "memory/major_page_faults_rate",
   565  		Description: "Rate of major page faults in counts per second",
   566  		Type:        MetricGauge,
   567  		ValueType:   ValueFloat,
   568  		Units:       UnitsCount,
   569  	},
   570  }
   571  
   572  var MetricNetworkRxRate = Metric{
   573  	MetricDescriptor: MetricDescriptor{
   574  		Name:        "network/rx_rate",
   575  		Description: "Rate of bytes received over the network in bytes per second",
   576  		Type:        MetricGauge,
   577  		ValueType:   ValueFloat,
   578  		Units:       UnitsCount,
   579  	},
   580  }
   581  
   582  var MetricNetworkRxErrorsRate = Metric{
   583  	MetricDescriptor: MetricDescriptor{
   584  		Name:        "network/rx_errors_rate",
   585  		Description: "Rate of errors sending over the network in errors per second",
   586  		Type:        MetricGauge,
   587  		ValueType:   ValueFloat,
   588  		Units:       UnitsCount,
   589  	},
   590  }
   591  
   592  var MetricNetworkTxRate = Metric{
   593  	MetricDescriptor: MetricDescriptor{
   594  		Name:        "network/tx_rate",
   595  		Description: "Rate of bytes transmitted over the network in bytes per second",
   596  		Type:        MetricGauge,
   597  		ValueType:   ValueFloat,
   598  		Units:       UnitsCount,
   599  	},
   600  }
   601  
   602  var MetricNetworkTxErrorsRate = Metric{
   603  	MetricDescriptor: MetricDescriptor{
   604  		Name:        "network/tx_errors_rate",
   605  		Description: "Rate of errors transmitting over the network in errors per second",
   606  		Type:        MetricGauge,
   607  		ValueType:   ValueFloat,
   608  		Units:       UnitsCount,
   609  	},
   610  }
   611  
   612  var MetricNodeCpuCapacity = Metric{
   613  	MetricDescriptor: MetricDescriptor{
   614  		Name:        "cpu/node_capacity",
   615  		Description: "Cpu capacity of a node",
   616  		Type:        MetricGauge,
   617  		ValueType:   ValueFloat,
   618  		Units:       UnitsCount,
   619  	},
   620  }
   621  
   622  var MetricNodeMemoryCapacity = Metric{
   623  	MetricDescriptor: MetricDescriptor{
   624  		Name:        "memory/node_capacity",
   625  		Description: "Memory capacity of a node",
   626  		Type:        MetricGauge,
   627  		ValueType:   ValueFloat,
   628  		Units:       UnitsCount,
   629  	},
   630  }
   631  
   632  var MetricNodeEphemeralStorageCapacity = Metric{
   633  	MetricDescriptor: MetricDescriptor{
   634  		Name:        "ephemeral_storage/node_capacity",
   635  		Description: "Ephemeral storage capacity of a node",
   636  		Type:        MetricGauge,
   637  		ValueType:   ValueFloat,
   638  		Units:       UnitsCount,
   639  	},
   640  }
   641  
   642  var MetricNodeCpuAllocatable = Metric{
   643  	MetricDescriptor: MetricDescriptor{
   644  		Name:        "cpu/node_allocatable",
   645  		Description: "Cpu allocatable of a node",
   646  		Type:        MetricGauge,
   647  		ValueType:   ValueFloat,
   648  		Units:       UnitsCount,
   649  	},
   650  }
   651  
   652  var MetricNodeMemoryAllocatable = Metric{
   653  	MetricDescriptor: MetricDescriptor{
   654  		Name:        "memory/node_allocatable",
   655  		Description: "Memory allocatable of a node",
   656  		Type:        MetricGauge,
   657  		ValueType:   ValueFloat,
   658  		Units:       UnitsCount,
   659  	},
   660  }
   661  
   662  var MetricNodeEphemeralStorageAllocatable = Metric{
   663  	MetricDescriptor: MetricDescriptor{
   664  		Name:        "ephemeral_storage/node_allocatable",
   665  		Description: "Ephemeral storage allocatable of a node",
   666  		Type:        MetricGauge,
   667  		ValueType:   ValueFloat,
   668  		Units:       UnitsCount,
   669  	},
   670  }
   671  
   672  var MetricNodeCpuUtilization = Metric{
   673  	MetricDescriptor: MetricDescriptor{
   674  		Name:        "cpu/node_utilization",
   675  		Description: "Cpu utilization as a share of node capacity",
   676  		Type:        MetricGauge,
   677  		ValueType:   ValueFloat,
   678  		Units:       UnitsCount,
   679  	},
   680  }
   681  
   682  var MetricNodeMemoryUtilization = Metric{
   683  	MetricDescriptor: MetricDescriptor{
   684  		Name:        "memory/node_utilization",
   685  		Description: "Memory utilization as a share of memory capacity",
   686  		Type:        MetricGauge,
   687  		ValueType:   ValueFloat,
   688  		Units:       UnitsCount,
   689  	},
   690  }
   691  
   692  var MetricNodeEphemeralStorageUtilization = Metric{
   693  	MetricDescriptor: MetricDescriptor{
   694  		Name:        "ephemeral_storage/node_utilization",
   695  		Description: "Ephemeral storage utilization as a share of storage capacity",
   696  		Type:        MetricGauge,
   697  		ValueType:   ValueFloat,
   698  		Units:       UnitsCount,
   699  	},
   700  }
   701  
   702  var MetricNodeCpuReservation = Metric{
   703  	MetricDescriptor: MetricDescriptor{
   704  		Name:        "cpu/node_reservation",
   705  		Description: "Share of cpu that is reserved on the node",
   706  		Type:        MetricGauge,
   707  		ValueType:   ValueFloat,
   708  		Units:       UnitsCount,
   709  	},
   710  }
   711  
   712  var MetricNodeMemoryReservation = Metric{
   713  	MetricDescriptor: MetricDescriptor{
   714  		Name:        "memory/node_reservation",
   715  		Description: "Share of memory that is reserved on the node",
   716  		Type:        MetricGauge,
   717  		ValueType:   ValueFloat,
   718  		Units:       UnitsCount,
   719  	},
   720  }
   721  
   722  var MetricNodeEphemeralStorageReservation = Metric{
   723  	MetricDescriptor: MetricDescriptor{
   724  		Name:        "ephemeral_storage/node_reservation",
   725  		Description: "Share of ephemeral storage that is reserved on the node",
   726  		Type:        MetricGauge,
   727  		ValueType:   ValueFloat,
   728  		Units:       UnitsCount,
   729  	},
   730  }
   731  
   732  // Labeled metrics
   733  
   734  var MetricFilesystemUsage = Metric{
   735  	MetricDescriptor: MetricDescriptor{
   736  		Name:        "filesystem/usage",
   737  		Description: "Total number of bytes consumed on a filesystem",
   738  		Type:        MetricGauge,
   739  		ValueType:   ValueInt64,
   740  		Units:       UnitsBytes,
   741  		Labels:      metricLabels,
   742  	},
   743  	HasLabeledMetric: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) bool {
   744  		return spec.HasFilesystem
   745  	},
   746  	GetLabeledMetric: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) []LabeledMetric {
   747  		result := make([]LabeledMetric, 0, len(stat.Filesystem))
   748  		for _, fs := range stat.Filesystem {
   749  			result = append(result, LabeledMetric{
   750  				Name: "filesystem/usage",
   751  				Labels: map[string]string{
   752  					LabelResourceID.Key: fs.Device,
   753  				},
   754  				MetricValue: MetricValue{
   755  					ValueType:  ValueInt64,
   756  					MetricType: MetricGauge,
   757  					IntValue:   int64(fs.Usage),
   758  				},
   759  			})
   760  		}
   761  		return result
   762  	},
   763  }
   764  
   765  var MetricFilesystemLimit = Metric{
   766  	MetricDescriptor: MetricDescriptor{
   767  		Name:        "filesystem/limit",
   768  		Description: "The total size of filesystem in bytes",
   769  		Type:        MetricGauge,
   770  		ValueType:   ValueInt64,
   771  		Units:       UnitsBytes,
   772  		Labels:      metricLabels,
   773  	},
   774  	HasLabeledMetric: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) bool {
   775  		return spec.HasFilesystem
   776  	},
   777  	GetLabeledMetric: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) []LabeledMetric {
   778  		result := make([]LabeledMetric, 0, len(stat.Filesystem))
   779  		for _, fs := range stat.Filesystem {
   780  			result = append(result, LabeledMetric{
   781  				Name: "filesystem/limit",
   782  				Labels: map[string]string{
   783  					LabelResourceID.Key: fs.Device,
   784  				},
   785  				MetricValue: MetricValue{
   786  					ValueType:  ValueInt64,
   787  					MetricType: MetricGauge,
   788  					IntValue:   int64(fs.Limit),
   789  				},
   790  			})
   791  		}
   792  		return result
   793  	},
   794  }
   795  
   796  var MetricFilesystemAvailable = Metric{
   797  	MetricDescriptor: MetricDescriptor{
   798  		Name:        "filesystem/available",
   799  		Description: "The number of available bytes remaining in a the filesystem",
   800  		Type:        MetricGauge,
   801  		ValueType:   ValueInt64,
   802  		Units:       UnitsBytes,
   803  		Labels:      metricLabels,
   804  	},
   805  	HasLabeledMetric: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) bool {
   806  		return spec.HasFilesystem
   807  	},
   808  	GetLabeledMetric: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) []LabeledMetric {
   809  		result := make([]LabeledMetric, 0, len(stat.Filesystem))
   810  		for _, fs := range stat.Filesystem {
   811  			result = append(result, LabeledMetric{
   812  				Name: "filesystem/available",
   813  				Labels: map[string]string{
   814  					LabelResourceID.Key: fs.Device,
   815  				},
   816  				MetricValue: MetricValue{
   817  					ValueType:  ValueInt64,
   818  					MetricType: MetricGauge,
   819  					IntValue:   int64(fs.Available),
   820  				},
   821  			})
   822  		}
   823  		return result
   824  	},
   825  }
   826  
   827  var MetricFilesystemInodes = Metric{
   828  	MetricDescriptor: MetricDescriptor{
   829  		Name:        "filesystem/inodes",
   830  		Description: "Total number of inodes on a filesystem",
   831  		Type:        MetricGauge,
   832  		ValueType:   ValueInt64,
   833  		Units:       UnitsBytes,
   834  		Labels:      metricLabels,
   835  	},
   836  	HasLabeledMetric: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) bool {
   837  		return spec.HasFilesystem
   838  	},
   839  	GetLabeledMetric: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) []LabeledMetric {
   840  		result := []LabeledMetric{}
   841  		for _, fs := range stat.Filesystem {
   842  			if fs.HasInodes {
   843  				result = append(result, LabeledMetric{
   844  					Name: "filesystem/inodes",
   845  					Labels: map[string]string{
   846  						LabelResourceID.Key: fs.Device,
   847  					},
   848  					MetricValue: MetricValue{
   849  						ValueType:  ValueInt64,
   850  						MetricType: MetricGauge,
   851  						IntValue:   int64(fs.Inodes),
   852  					},
   853  				})
   854  			}
   855  		}
   856  		return result
   857  	},
   858  }
   859  
   860  var MetricFilesystemInodesFree = Metric{
   861  	MetricDescriptor: MetricDescriptor{
   862  		Name:        "filesystem/inodes_free",
   863  		Description: "Free number of inodes on a filesystem",
   864  		Type:        MetricGauge,
   865  		ValueType:   ValueInt64,
   866  		Units:       UnitsBytes,
   867  		Labels:      metricLabels,
   868  	},
   869  	HasLabeledMetric: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) bool {
   870  		return spec.HasFilesystem
   871  	},
   872  	GetLabeledMetric: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) []LabeledMetric {
   873  		result := []LabeledMetric{}
   874  		for _, fs := range stat.Filesystem {
   875  			if fs.HasInodes {
   876  				result = append(result, LabeledMetric{
   877  					Name: "filesystem/inodes_free",
   878  					Labels: map[string]string{
   879  						LabelResourceID.Key: fs.Device,
   880  					},
   881  					MetricValue: MetricValue{
   882  						ValueType:  ValueInt64,
   883  						MetricType: MetricGauge,
   884  						IntValue:   int64(fs.InodesFree),
   885  					},
   886  				})
   887  			}
   888  		}
   889  		return result
   890  	},
   891  }
   892  
   893  var MetricAcceleratorMemoryTotal = Metric{
   894  	MetricDescriptor: MetricDescriptor{
   895  		Name:        "accelerator/memory_total",
   896  		Description: "Total accelerator memory (in bytes)",
   897  		Labels:      acceleratorLabels,
   898  		Type:        MetricGauge,
   899  		ValueType:   ValueInt64,
   900  		Units:       UnitsBytes,
   901  	},
   902  	HasLabeledMetric: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) bool {
   903  		if len(stat.Accelerators) == 0 {
   904  			return false
   905  		}
   906  
   907  		return true
   908  	},
   909  	GetLabeledMetric: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) []LabeledMetric {
   910  		result := make([]LabeledMetric, 0, len(stat.Accelerators))
   911  		for _, ac := range stat.Accelerators {
   912  			result = append(result, LabeledMetric{
   913  				Name: "accelerator/memory_total",
   914  				Labels: map[string]string{
   915  					LabelAcceleratorMake.Key:  ac.Make,
   916  					LabelAcceleratorModel.Key: ac.Model,
   917  					LabelAcceleratorID.Key:    ac.ID,
   918  				},
   919  				MetricValue: MetricValue{
   920  					ValueType:  ValueInt64,
   921  					MetricType: MetricGauge,
   922  					IntValue:   int64(ac.MemoryTotal),
   923  				},
   924  			})
   925  		}
   926  		return result
   927  	},
   928  }
   929  
   930  var MetricAcceleratorMemoryUsed = Metric{
   931  	MetricDescriptor: MetricDescriptor{
   932  		Name:        "accelerator/memory_used",
   933  		Description: "Total accelerator memory allocated (in bytes)",
   934  		Labels:      acceleratorLabels,
   935  		Type:        MetricGauge,
   936  		ValueType:   ValueInt64,
   937  		Units:       UnitsBytes,
   938  	},
   939  	HasLabeledMetric: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) bool {
   940  		if len(stat.Accelerators) == 0 {
   941  			return false
   942  		}
   943  
   944  		return true
   945  	},
   946  	GetLabeledMetric: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) []LabeledMetric {
   947  		result := make([]LabeledMetric, 0, len(stat.Accelerators))
   948  		for _, ac := range stat.Accelerators {
   949  			result = append(result, LabeledMetric{
   950  				Name: "accelerator/memory_used",
   951  				Labels: map[string]string{
   952  					LabelAcceleratorMake.Key:  ac.Make,
   953  					LabelAcceleratorModel.Key: ac.Model,
   954  					LabelAcceleratorID.Key:    ac.ID,
   955  				},
   956  				MetricValue: MetricValue{
   957  					ValueType:  ValueInt64,
   958  					MetricType: MetricGauge,
   959  					IntValue:   int64(ac.MemoryUsed),
   960  				},
   961  			})
   962  		}
   963  		return result
   964  	},
   965  }
   966  
   967  var MetricAcceleratorDutyCycle = Metric{
   968  	MetricDescriptor: MetricDescriptor{
   969  		Name:        "accelerator/duty_cycle",
   970  		Description: "Percent of time over the past sample period (10s) during which the accelerator was actively processing",
   971  		Labels:      acceleratorLabels,
   972  		Type:        MetricGauge,
   973  		ValueType:   ValueInt64,
   974  		Units:       UnitsCount,
   975  	},
   976  	HasLabeledMetric: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) bool {
   977  		if len(stat.Accelerators) == 0 {
   978  			return false
   979  		}
   980  
   981  		return true
   982  	},
   983  	GetLabeledMetric: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) []LabeledMetric {
   984  		result := make([]LabeledMetric, 0, len(stat.Accelerators))
   985  		for _, ac := range stat.Accelerators {
   986  			result = append(result, LabeledMetric{
   987  				Name: "accelerator/duty_cycle",
   988  				Labels: map[string]string{
   989  					LabelAcceleratorMake.Key:  ac.Make,
   990  					LabelAcceleratorModel.Key: ac.Model,
   991  					LabelAcceleratorID.Key:    ac.ID,
   992  				},
   993  				MetricValue: MetricValue{
   994  					ValueType:  ValueInt64,
   995  					MetricType: MetricGauge,
   996  					IntValue:   int64(ac.DutyCycle),
   997  				},
   998  			})
   999  		}
  1000  		return result
  1001  	},
  1002  }
  1003  
  1004  var MetricDiskIORead = Metric{
  1005  	MetricDescriptor: MetricDescriptor{
  1006  		Name:        "disk/io_read_bytes",
  1007  		Description: "Cumulative number of bytes read over disk",
  1008  		Type:        MetricCumulative,
  1009  		ValueType:   ValueInt64,
  1010  		Units:       UnitsBytes,
  1011  		Labels:      metricLabels,
  1012  	},
  1013  	HasLabeledMetric: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) bool {
  1014  		return spec.HasDiskIo
  1015  	},
  1016  	GetLabeledMetric: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) []LabeledMetric {
  1017  		result := make([]LabeledMetric, 0, len(stat.DiskIo.IoServiceBytes))
  1018  		for _, ioServiceBytesPerPartition := range stat.DiskIo.IoServiceBytes {
  1019  			resourceIDKey := ioServiceBytesPerPartition.Device
  1020  			if resourceIDKey == "" {
  1021  				resourceIDKey = fmt.Sprintf("%d:%d", ioServiceBytesPerPartition.Major, ioServiceBytesPerPartition.Minor)
  1022  			}
  1023  
  1024  			var value uint64
  1025  			if v, exists := ioServiceBytesPerPartition.Stats["Read"]; exists {
  1026  				value = v
  1027  			}
  1028  
  1029  			result = append(result, LabeledMetric{
  1030  				Name: "disk/io_read_bytes",
  1031  				Labels: map[string]string{
  1032  					LabelResourceID.Key: resourceIDKey,
  1033  				},
  1034  				MetricValue: MetricValue{
  1035  					ValueType:  ValueInt64,
  1036  					MetricType: MetricGauge,
  1037  					IntValue:   int64(value),
  1038  				},
  1039  			})
  1040  		}
  1041  		return result
  1042  	},
  1043  }
  1044  
  1045  var MetricDiskIOWrite = Metric{
  1046  	MetricDescriptor: MetricDescriptor{
  1047  		Name:        "disk/io_write_bytes",
  1048  		Description: "Cumulative number of bytes write over disk",
  1049  		Type:        MetricCumulative,
  1050  		ValueType:   ValueInt64,
  1051  		Units:       UnitsBytes,
  1052  		Labels:      metricLabels,
  1053  	},
  1054  	HasLabeledMetric: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) bool {
  1055  		return spec.HasDiskIo
  1056  	},
  1057  	GetLabeledMetric: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) []LabeledMetric {
  1058  		result := make([]LabeledMetric, 0, len(stat.DiskIo.IoServiceBytes))
  1059  		for _, ioServiceBytesPerPartition := range stat.DiskIo.IoServiceBytes {
  1060  			resourceIDKey := ioServiceBytesPerPartition.Device
  1061  			if resourceIDKey == "" {
  1062  				resourceIDKey = fmt.Sprintf("%d:%d", ioServiceBytesPerPartition.Major, ioServiceBytesPerPartition.Minor)
  1063  			}
  1064  
  1065  			var value uint64
  1066  			if v, exists := ioServiceBytesPerPartition.Stats["Write"]; exists {
  1067  				value = v
  1068  			}
  1069  
  1070  			result = append(result, LabeledMetric{
  1071  				Name: "disk/io_write_bytes",
  1072  				Labels: map[string]string{
  1073  					LabelResourceID.Key: resourceIDKey,
  1074  				},
  1075  				MetricValue: MetricValue{
  1076  					ValueType:  ValueInt64,
  1077  					MetricType: MetricGauge,
  1078  					IntValue:   int64(value),
  1079  				},
  1080  			})
  1081  		}
  1082  		return result
  1083  	},
  1084  }
  1085  
  1086  var MetricDiskIOReadRate = Metric{
  1087  	MetricDescriptor: MetricDescriptor{
  1088  		Name:        "disk/io_read_bytes_rate",
  1089  		Description: "Rate of bytes read over disk in bytes per second",
  1090  		Type:        MetricGauge,
  1091  		ValueType:   ValueFloat,
  1092  		Units:       UnitsCount,
  1093  		Labels:      metricLabels,
  1094  	},
  1095  }
  1096  
  1097  var MetricDiskIOWriteRate = Metric{
  1098  	MetricDescriptor: MetricDescriptor{
  1099  		Name:        "disk/io_write_bytes_rate",
  1100  		Description: "Rate of bytes written over disk in bytes per second",
  1101  		Type:        MetricGauge,
  1102  		ValueType:   ValueFloat,
  1103  		Units:       UnitsCount,
  1104  		Labels:      metricLabels,
  1105  	},
  1106  }
  1107  
  1108  func IsNodeAutoscalingMetric(name string) bool {
  1109  	for _, autoscalingMetric := range NodeAutoscalingMetrics {
  1110  		if autoscalingMetric.MetricDescriptor.Name == name {
  1111  			return true
  1112  		}
  1113  	}
  1114  	return false
  1115  }
  1116  
  1117  type MetricDescriptor struct {
  1118  	// The unique name of the metric.
  1119  	Name string `json:"name,omitempty"`
  1120  
  1121  	// Description of the metric.
  1122  	Description string `json:"description,omitempty"`
  1123  
  1124  	// Descriptor of the labels specific to this metric.
  1125  	Labels []LabelDescriptor `json:"labels,omitempty"`
  1126  
  1127  	// Type and value of metric data.
  1128  	Type      MetricType `json:"type,omitempty"`
  1129  	ValueType ValueType  `json:"value_type,omitempty"`
  1130  	Units     UnitsType  `json:"units,omitempty"`
  1131  }
  1132  
  1133  // Metric represents a resource usage stat metric.
  1134  type Metric struct {
  1135  	MetricDescriptor
  1136  
  1137  	// Returns whether this metric is present.
  1138  	HasValue func(*cadvisor.ContainerSpec) bool
  1139  
  1140  	// Returns a slice of internal point objects that contain metric values and associated labels.
  1141  	GetValue func(*cadvisor.ContainerSpec, *cadvisor.ContainerStats) MetricValue
  1142  
  1143  	// Returns whether this metric is present.
  1144  	HasLabeledMetric func(*cadvisor.ContainerSpec, *cadvisor.ContainerStats) bool
  1145  
  1146  	// Returns a slice of internal point objects that contain metric values and associated labels.
  1147  	GetLabeledMetric func(*cadvisor.ContainerSpec, *cadvisor.ContainerStats) []LabeledMetric
  1148  }