github.com/jonaz/heapster@v1.3.0-beta.0.0.20170208112634-cd3c15ca3d29/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 "time" 19 20 cadvisor "github.com/google/cadvisor/info/v1" 21 ) 22 23 const ( 24 CustomMetricPrefix = "custom/" 25 ) 26 27 // Provided by Kubelet/cadvisor. 28 var StandardMetrics = []Metric{ 29 MetricUptime, 30 MetricCpuUsage, 31 MetricMemoryUsage, 32 MetricMemoryWorkingSet, 33 MetricMemoryPageFaults, 34 MetricMemoryMajorPageFaults, 35 MetricNetworkRx, 36 MetricNetworkRxErrors, 37 MetricNetworkTx, 38 MetricNetworkTxErrors} 39 40 // Metrics computed based on cluster state using Kubernetes API. 41 var AdditionalMetrics = []Metric{ 42 MetricCpuRequest, 43 MetricCpuLimit, 44 MetricMemoryRequest, 45 MetricMemoryLimit} 46 47 // Computed based on corresponding StandardMetrics. 48 var RateMetrics = []Metric{ 49 MetricCpuUsageRate, 50 MetricMemoryPageFaultsRate, 51 MetricMemoryMajorPageFaultsRate, 52 MetricNetworkRxRate, 53 MetricNetworkRxErrorsRate, 54 MetricNetworkTxRate, 55 MetricNetworkTxErrorsRate} 56 57 var RateMetricsMapping = map[string]Metric{ 58 MetricCpuUsage.MetricDescriptor.Name: MetricCpuUsageRate, 59 MetricMemoryPageFaults.MetricDescriptor.Name: MetricMemoryPageFaultsRate, 60 MetricMemoryMajorPageFaults.MetricDescriptor.Name: MetricMemoryMajorPageFaultsRate, 61 MetricNetworkRx.MetricDescriptor.Name: MetricNetworkRxRate, 62 MetricNetworkRxErrors.MetricDescriptor.Name: MetricNetworkRxErrorsRate, 63 MetricNetworkTx.MetricDescriptor.Name: MetricNetworkTxRate, 64 MetricNetworkTxErrors.MetricDescriptor.Name: MetricNetworkTxErrorsRate} 65 66 var LabeledMetrics = []Metric{ 67 MetricFilesystemUsage, 68 MetricFilesystemLimit, 69 MetricFilesystemAvailable, 70 } 71 72 var NodeAutoscalingMetrics = []Metric{ 73 MetricNodeCpuCapacity, 74 MetricNodeMemoryCapacity, 75 MetricNodeCpuAllocatable, 76 MetricNodeMemoryAllocatable, 77 MetricNodeCpuUtilization, 78 MetricNodeMemoryUtilization, 79 MetricNodeCpuReservation, 80 MetricNodeMemoryReservation, 81 } 82 83 var CpuMetrics = []Metric{ 84 MetricCpuLimit, 85 MetricCpuRequest, 86 MetricCpuUsage, 87 MetricCpuUsageRate, 88 MetricNodeCpuAllocatable, 89 MetricNodeCpuCapacity, 90 MetricNodeCpuReservation, 91 MetricNodeCpuUtilization, 92 } 93 var FilesystemMetrics = []Metric{ 94 MetricFilesystemAvailable, 95 MetricFilesystemLimit, 96 MetricFilesystemUsage, 97 } 98 var MemoryMetrics = []Metric{ 99 MetricMemoryLimit, 100 MetricMemoryMajorPageFaults, 101 MetricMemoryMajorPageFaultsRate, 102 MetricMemoryPageFaults, 103 MetricMemoryPageFaultsRate, 104 MetricMemoryRequest, 105 MetricMemoryUsage, 106 MetricMemoryWorkingSet, 107 MetricNodeMemoryAllocatable, 108 MetricNodeMemoryCapacity, 109 MetricNodeMemoryUtilization, 110 MetricNodeMemoryReservation, 111 } 112 var NetworkMetrics = []Metric{ 113 MetricNetworkRx, 114 MetricNetworkRxErrors, 115 MetricNetworkRxErrorsRate, 116 MetricNetworkRxRate, 117 MetricNetworkTx, 118 MetricNetworkTxErrors, 119 MetricNetworkTxErrorsRate, 120 MetricNetworkTxRate, 121 } 122 123 type MetricFamily string 124 125 const ( 126 MetricFamilyCpu MetricFamily = "cpu" 127 MetricFamilyFilesystem = "filesystem" 128 MetricFamilyMemory = "memory" 129 MetricFamilyNetwork = "network" 130 MetricFamilyGeneral = "general" 131 ) 132 133 var MetricFamilies = map[MetricFamily][]Metric{ 134 MetricFamilyCpu: CpuMetrics, 135 MetricFamilyFilesystem: FilesystemMetrics, 136 MetricFamilyMemory: MemoryMetrics, 137 MetricFamilyNetwork: NetworkMetrics, 138 } 139 140 func MetricFamilyForName(metricName string) MetricFamily { 141 for family, metrics := range MetricFamilies { 142 for _, metric := range metrics { 143 if metricName == metric.Name { 144 return family 145 } 146 } 147 } 148 return MetricFamilyGeneral 149 } 150 151 var AllMetrics = append(append(append(append(StandardMetrics, AdditionalMetrics...), RateMetrics...), LabeledMetrics...), 152 NodeAutoscalingMetrics...) 153 154 // Definition of Standard Metrics. 155 var MetricUptime = Metric{ 156 MetricDescriptor: MetricDescriptor{ 157 Name: "uptime", 158 Description: "Number of milliseconds since the container was started", 159 Type: MetricCumulative, 160 ValueType: ValueInt64, 161 Units: UnitsMilliseconds, 162 }, 163 HasValue: func(spec *cadvisor.ContainerSpec) bool { 164 return !spec.CreationTime.IsZero() 165 }, 166 GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue { 167 return MetricValue{ 168 ValueType: ValueInt64, 169 MetricType: MetricCumulative, 170 IntValue: time.Since(spec.CreationTime).Nanoseconds() / time.Millisecond.Nanoseconds()} 171 }, 172 } 173 174 var MetricCpuUsage = Metric{ 175 MetricDescriptor: MetricDescriptor{ 176 Name: "cpu/usage", 177 Description: "Cumulative CPU usage on all cores", 178 Type: MetricCumulative, 179 ValueType: ValueInt64, 180 Units: UnitsNanoseconds, 181 }, 182 HasValue: func(spec *cadvisor.ContainerSpec) bool { 183 return spec.HasCpu 184 }, 185 GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue { 186 return MetricValue{ 187 ValueType: ValueInt64, 188 MetricType: MetricCumulative, 189 IntValue: int64(stat.Cpu.Usage.Total)} 190 }, 191 } 192 193 var MetricMemoryUsage = Metric{ 194 MetricDescriptor: MetricDescriptor{ 195 Name: "memory/usage", 196 Description: "Total memory usage", 197 Type: MetricGauge, 198 ValueType: ValueInt64, 199 Units: UnitsBytes, 200 }, 201 HasValue: func(spec *cadvisor.ContainerSpec) bool { 202 return spec.HasMemory 203 }, 204 GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue { 205 return MetricValue{ 206 ValueType: ValueInt64, 207 MetricType: MetricGauge, 208 IntValue: int64(stat.Memory.Usage)} 209 }, 210 } 211 212 var MetricMemoryWorkingSet = Metric{ 213 MetricDescriptor: MetricDescriptor{ 214 Name: "memory/working_set", 215 Description: "Total working set usage. Working set is the memory being used and not easily dropped by the kernel", 216 Type: MetricGauge, 217 ValueType: ValueInt64, 218 Units: UnitsBytes, 219 }, 220 HasValue: func(spec *cadvisor.ContainerSpec) bool { 221 return spec.HasMemory 222 }, 223 GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue { 224 return MetricValue{ 225 ValueType: ValueInt64, 226 MetricType: MetricGauge, 227 IntValue: int64(stat.Memory.WorkingSet)} 228 }, 229 } 230 231 var MetricMemoryPageFaults = Metric{ 232 MetricDescriptor: MetricDescriptor{ 233 Name: "memory/page_faults", 234 Description: "Number of page faults", 235 Type: MetricCumulative, 236 ValueType: ValueInt64, 237 Units: UnitsCount, 238 }, 239 HasValue: func(spec *cadvisor.ContainerSpec) bool { 240 return spec.HasMemory 241 }, 242 GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue { 243 return MetricValue{ 244 ValueType: ValueInt64, 245 MetricType: MetricCumulative, 246 IntValue: int64(stat.Memory.ContainerData.Pgfault)} 247 }, 248 } 249 250 var MetricMemoryMajorPageFaults = Metric{ 251 MetricDescriptor: MetricDescriptor{ 252 Name: "memory/major_page_faults", 253 Description: "Number of major page faults", 254 Type: MetricCumulative, 255 ValueType: ValueInt64, 256 Units: UnitsCount, 257 }, 258 HasValue: func(spec *cadvisor.ContainerSpec) bool { 259 return spec.HasMemory 260 }, 261 GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue { 262 return MetricValue{ 263 ValueType: ValueInt64, 264 MetricType: MetricCumulative, 265 IntValue: int64(stat.Memory.ContainerData.Pgmajfault)} 266 }, 267 } 268 269 var MetricNetworkRx = Metric{ 270 MetricDescriptor: MetricDescriptor{ 271 Name: "network/rx", 272 Description: "Cumulative number of bytes received over the network", 273 Type: MetricCumulative, 274 ValueType: ValueInt64, 275 Units: UnitsBytes, 276 }, 277 HasValue: func(spec *cadvisor.ContainerSpec) bool { 278 return spec.HasNetwork 279 }, 280 GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue { 281 return MetricValue{ 282 ValueType: ValueInt64, 283 MetricType: MetricCumulative, 284 IntValue: int64(stat.Network.RxBytes)} 285 }, 286 } 287 288 var MetricNetworkRxErrors = Metric{ 289 MetricDescriptor: MetricDescriptor{ 290 Name: "network/rx_errors", 291 Description: "Cumulative number of errors while receiving over the network", 292 Type: MetricCumulative, 293 ValueType: ValueInt64, 294 Units: UnitsCount, 295 }, 296 HasValue: func(spec *cadvisor.ContainerSpec) bool { 297 return spec.HasNetwork 298 }, 299 GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue { 300 return MetricValue{ 301 ValueType: ValueInt64, 302 MetricType: MetricCumulative, 303 IntValue: int64(stat.Network.RxErrors)} 304 }, 305 } 306 307 var MetricNetworkTx = Metric{ 308 MetricDescriptor: MetricDescriptor{ 309 Name: "network/tx", 310 Description: "Cumulative number of bytes sent over the network", 311 Type: MetricCumulative, 312 ValueType: ValueInt64, 313 Units: UnitsBytes, 314 }, 315 HasValue: func(spec *cadvisor.ContainerSpec) bool { 316 return spec.HasNetwork 317 }, 318 GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue { 319 return MetricValue{ 320 ValueType: ValueInt64, 321 MetricType: MetricCumulative, 322 IntValue: int64(stat.Network.TxBytes)} 323 }, 324 } 325 326 var MetricNetworkTxErrors = Metric{ 327 MetricDescriptor: MetricDescriptor{ 328 Name: "network/tx_errors", 329 Description: "Cumulative number of errors while sending over the network", 330 Type: MetricCumulative, 331 ValueType: ValueInt64, 332 Units: UnitsCount, 333 }, 334 HasValue: func(spec *cadvisor.ContainerSpec) bool { 335 return spec.HasNetwork 336 }, 337 GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) MetricValue { 338 return MetricValue{ 339 ValueType: ValueInt64, 340 MetricType: MetricCumulative, 341 IntValue: int64(stat.Network.TxErrors)} 342 }, 343 } 344 345 // Definition of Additional Metrics. 346 var MetricCpuRequest = Metric{ 347 MetricDescriptor: MetricDescriptor{ 348 Name: "cpu/request", 349 Description: "CPU request (the guaranteed amount of resources) in millicores. This metric is Kubernetes specific.", 350 Type: MetricGauge, 351 ValueType: ValueInt64, 352 Units: UnitsCount, 353 }, 354 } 355 356 var MetricCpuLimit = Metric{ 357 MetricDescriptor: MetricDescriptor{ 358 Name: "cpu/limit", 359 Description: "CPU hard limit in millicores.", 360 Type: MetricGauge, 361 ValueType: ValueInt64, 362 Units: UnitsCount, 363 }, 364 } 365 366 var MetricMemoryRequest = Metric{ 367 MetricDescriptor: MetricDescriptor{ 368 Name: "memory/request", 369 Description: "Memory request (the guaranteed amount of resources) in bytes. This metric is Kubernetes specific.", 370 Type: MetricGauge, 371 ValueType: ValueInt64, 372 Units: UnitsBytes, 373 }, 374 } 375 376 var MetricMemoryLimit = Metric{ 377 MetricDescriptor: MetricDescriptor{ 378 Name: "memory/limit", 379 Description: "Memory hard limit in bytes.", 380 Type: MetricGauge, 381 ValueType: ValueInt64, 382 Units: UnitsBytes, 383 }, 384 } 385 386 // Definition of Rate Metrics. 387 var MetricCpuUsageRate = Metric{ 388 MetricDescriptor: MetricDescriptor{ 389 Name: "cpu/usage_rate", 390 Description: "CPU usage on all cores in millicores", 391 Type: MetricGauge, 392 ValueType: ValueInt64, 393 Units: UnitsCount, 394 }, 395 } 396 397 var MetricMemoryPageFaultsRate = Metric{ 398 MetricDescriptor: MetricDescriptor{ 399 Name: "memory/page_faults_rate", 400 Description: "Rate of page faults in counts per second", 401 Type: MetricGauge, 402 ValueType: ValueFloat, 403 Units: UnitsCount, 404 }, 405 } 406 407 var MetricMemoryMajorPageFaultsRate = Metric{ 408 MetricDescriptor: MetricDescriptor{ 409 Name: "memory/major_page_faults_rate", 410 Description: "Rate of major page faults in counts per second", 411 Type: MetricGauge, 412 ValueType: ValueFloat, 413 Units: UnitsCount, 414 }, 415 } 416 417 var MetricNetworkRxRate = Metric{ 418 MetricDescriptor: MetricDescriptor{ 419 Name: "network/rx_rate", 420 Description: "Rate of bytes received over the network in bytes per second", 421 Type: MetricGauge, 422 ValueType: ValueFloat, 423 Units: UnitsCount, 424 }, 425 } 426 427 var MetricNetworkRxErrorsRate = Metric{ 428 MetricDescriptor: MetricDescriptor{ 429 Name: "network/rx_errors_rate", 430 Description: "Rate of errors sending over the network in errors per second", 431 Type: MetricGauge, 432 ValueType: ValueFloat, 433 Units: UnitsCount, 434 }, 435 } 436 437 var MetricNetworkTxRate = Metric{ 438 MetricDescriptor: MetricDescriptor{ 439 Name: "network/tx_rate", 440 Description: "Rate of bytes transmitted over the network in bytes per second", 441 Type: MetricGauge, 442 ValueType: ValueFloat, 443 Units: UnitsCount, 444 }, 445 } 446 447 var MetricNetworkTxErrorsRate = Metric{ 448 MetricDescriptor: MetricDescriptor{ 449 Name: "network/tx_errors_rate", 450 Description: "Rate of errors transmitting over the network in errors per second", 451 Type: MetricGauge, 452 ValueType: ValueFloat, 453 Units: UnitsCount, 454 }, 455 } 456 457 var MetricNodeCpuCapacity = Metric{ 458 MetricDescriptor: MetricDescriptor{ 459 Name: "cpu/node_capacity", 460 Description: "Cpu capacity of a node", 461 Type: MetricGauge, 462 ValueType: ValueFloat, 463 Units: UnitsCount, 464 }, 465 } 466 467 var MetricNodeMemoryCapacity = Metric{ 468 MetricDescriptor: MetricDescriptor{ 469 Name: "memory/node_capacity", 470 Description: "Memory capacity of a node", 471 Type: MetricGauge, 472 ValueType: ValueFloat, 473 Units: UnitsCount, 474 }, 475 } 476 477 var MetricNodeCpuAllocatable = Metric{ 478 MetricDescriptor: MetricDescriptor{ 479 Name: "cpu/node_allocatable", 480 Description: "Cpu allocatable of a node", 481 Type: MetricGauge, 482 ValueType: ValueFloat, 483 Units: UnitsCount, 484 }, 485 } 486 487 var MetricNodeMemoryAllocatable = Metric{ 488 MetricDescriptor: MetricDescriptor{ 489 Name: "memory/node_allocatable", 490 Description: "Memory allocatable of a node", 491 Type: MetricGauge, 492 ValueType: ValueFloat, 493 Units: UnitsCount, 494 }, 495 } 496 497 var MetricNodeCpuUtilization = Metric{ 498 MetricDescriptor: MetricDescriptor{ 499 Name: "cpu/node_utilization", 500 Description: "Cpu utilization as a share of node capacity", 501 Type: MetricGauge, 502 ValueType: ValueFloat, 503 Units: UnitsCount, 504 }, 505 } 506 507 var MetricNodeMemoryUtilization = Metric{ 508 MetricDescriptor: MetricDescriptor{ 509 Name: "memory/node_utilization", 510 Description: "Memory utilization as a share of memory capacity", 511 Type: MetricGauge, 512 ValueType: ValueFloat, 513 Units: UnitsCount, 514 }, 515 } 516 517 var MetricNodeCpuReservation = Metric{ 518 MetricDescriptor: MetricDescriptor{ 519 Name: "cpu/node_reservation", 520 Description: "Share of cpu that is reserved on the node", 521 Type: MetricGauge, 522 ValueType: ValueFloat, 523 Units: UnitsCount, 524 }, 525 } 526 527 var MetricNodeMemoryReservation = Metric{ 528 MetricDescriptor: MetricDescriptor{ 529 Name: "memory/node_reservation", 530 Description: "Share of memory that is reserved on the node", 531 Type: MetricGauge, 532 ValueType: ValueFloat, 533 Units: UnitsCount, 534 }, 535 } 536 537 // Labeled metrics 538 539 var MetricFilesystemUsage = Metric{ 540 MetricDescriptor: MetricDescriptor{ 541 Name: "filesystem/usage", 542 Description: "Total number of bytes consumed on a filesystem", 543 Type: MetricGauge, 544 ValueType: ValueInt64, 545 Units: UnitsBytes, 546 Labels: metricLabels, 547 }, 548 HasLabeledMetric: func(spec *cadvisor.ContainerSpec) bool { 549 return spec.HasFilesystem 550 }, 551 GetLabeledMetric: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) []LabeledMetric { 552 result := make([]LabeledMetric, 0, len(stat.Filesystem)) 553 for _, fs := range stat.Filesystem { 554 result = append(result, LabeledMetric{ 555 Name: "filesystem/usage", 556 Labels: map[string]string{ 557 LabelResourceID.Key: fs.Device, 558 }, 559 MetricValue: MetricValue{ 560 ValueType: ValueInt64, 561 MetricType: MetricGauge, 562 IntValue: int64(fs.Usage), 563 }, 564 }) 565 } 566 return result 567 }, 568 } 569 570 var MetricFilesystemLimit = Metric{ 571 MetricDescriptor: MetricDescriptor{ 572 Name: "filesystem/limit", 573 Description: "The total size of filesystem in bytes", 574 Type: MetricGauge, 575 ValueType: ValueInt64, 576 Units: UnitsBytes, 577 Labels: metricLabels, 578 }, 579 HasLabeledMetric: func(spec *cadvisor.ContainerSpec) bool { 580 return spec.HasFilesystem 581 }, 582 GetLabeledMetric: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) []LabeledMetric { 583 result := make([]LabeledMetric, 0, len(stat.Filesystem)) 584 for _, fs := range stat.Filesystem { 585 result = append(result, LabeledMetric{ 586 Name: "filesystem/limit", 587 Labels: map[string]string{ 588 LabelResourceID.Key: fs.Device, 589 }, 590 MetricValue: MetricValue{ 591 ValueType: ValueInt64, 592 MetricType: MetricGauge, 593 IntValue: int64(fs.Limit), 594 }, 595 }) 596 } 597 return result 598 }, 599 } 600 601 var MetricFilesystemAvailable = Metric{ 602 MetricDescriptor: MetricDescriptor{ 603 Name: "filesystem/available", 604 Description: "The number of available bytes remaining in a the filesystem", 605 Type: MetricGauge, 606 ValueType: ValueInt64, 607 Units: UnitsBytes, 608 Labels: metricLabels, 609 }, 610 } 611 612 func IsNodeAutoscalingMetric(name string) bool { 613 for _, autoscalingMetric := range NodeAutoscalingMetrics { 614 if autoscalingMetric.MetricDescriptor.Name == name { 615 return true 616 } 617 } 618 return false 619 } 620 621 type MetricDescriptor struct { 622 // The unique name of the metric. 623 Name string `json:"name,omitempty"` 624 625 // Description of the metric. 626 Description string `json:"description,omitempty"` 627 628 // Descriptor of the labels specific to this metric. 629 Labels []LabelDescriptor `json:"labels,omitempty"` 630 631 // Type and value of metric data. 632 Type MetricType `json:"type,omitempty"` 633 ValueType ValueType `json:"value_type,omitempty"` 634 Units UnitsType `json:"units,omitempty"` 635 } 636 637 // Metric represents a resource usage stat metric. 638 type Metric struct { 639 MetricDescriptor 640 641 // Returns whether this metric is present. 642 HasValue func(*cadvisor.ContainerSpec) bool 643 644 // Returns a slice of internal point objects that contain metric values and associated labels. 645 GetValue func(*cadvisor.ContainerSpec, *cadvisor.ContainerStats) MetricValue 646 647 // Returns whether this metric is present. 648 HasLabeledMetric func(*cadvisor.ContainerSpec) bool 649 650 // Returns a slice of internal point objects that contain metric values and associated labels. 651 GetLabeledMetric func(*cadvisor.ContainerSpec, *cadvisor.ContainerStats) []LabeledMetric 652 }