mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-26 15:32:30 +00:00
Merge pull request #1816 from egernst/get-sandbox-metrics-cli
Get sandbox metrics cli
This commit is contained in:
commit
bffb099d99
38
src/runtime/cli/kata-metrics.go
Normal file
38
src/runtime/cli/kata-metrics.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
// Copyright (c) 2021 Apple Inc.
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
kataMonitor "github.com/kata-containers/kata-containers/src/runtime/pkg/kata-monitor"
|
||||||
|
"github.com/kata-containers/kata-containers/src/runtime/pkg/katautils"
|
||||||
|
"github.com/urfave/cli"
|
||||||
|
)
|
||||||
|
|
||||||
|
var kataMetricsCLICommand = cli.Command{
|
||||||
|
Name: "metrics",
|
||||||
|
Usage: "gather metrics associated with infrastructure used to run a sandbox",
|
||||||
|
UsageText: "metrics <sandbox id>",
|
||||||
|
Action: func(context *cli.Context) error {
|
||||||
|
|
||||||
|
sandboxID := context.Args().Get(0)
|
||||||
|
|
||||||
|
if err := katautils.VerifyContainerID(sandboxID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the metrics!
|
||||||
|
metrics, err := kataMonitor.GetSandboxMetrics(sandboxID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("%s\n", metrics)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
@ -122,6 +122,7 @@ var runtimeCommands = []cli.Command{
|
|||||||
kataCheckCLICommand,
|
kataCheckCLICommand,
|
||||||
kataEnvCLICommand,
|
kataEnvCLICommand,
|
||||||
kataExecCLICommand,
|
kataExecCLICommand,
|
||||||
|
kataMetricsCLICommand,
|
||||||
factoryCLICommand,
|
factoryCLICommand,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,7 +176,7 @@ func (km *KataMonitor) aggregateSandboxMetrics(encoder expfmt.Encoder) error {
|
|||||||
for sandboxID, namespace := range sandboxes {
|
for sandboxID, namespace := range sandboxes {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(sandboxID, namespace string, results chan<- []*dto.MetricFamily) {
|
go func(sandboxID, namespace string, results chan<- []*dto.MetricFamily) {
|
||||||
sandboxMetrics, err := getSandboxMetrics(sandboxID)
|
sandboxMetrics, err := getParsedMetrics(sandboxID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
monitorLog.WithError(err).WithField("sandbox_id", sandboxID).Errorf("failed to get metrics for sandbox")
|
monitorLog.WithError(err).WithField("sandbox_id", sandboxID).Errorf("failed to get metrics for sandbox")
|
||||||
}
|
}
|
||||||
@ -229,12 +229,11 @@ func (km *KataMonitor) aggregateSandboxMetrics(encoder expfmt.Encoder) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// getSandboxMetrics will get sandbox's metrics from shim
|
func getParsedMetrics(sandboxID string) ([]*dto.MetricFamily, error) {
|
||||||
func getSandboxMetrics(sandboxID string) ([]*dto.MetricFamily, error) {
|
|
||||||
body, err := doGet(sandboxID, defaultTimeout, "metrics")
|
body, err := doGet(sandboxID, defaultTimeout, "metrics")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -243,6 +242,16 @@ func getSandboxMetrics(sandboxID string) ([]*dto.MetricFamily, error) {
|
|||||||
return parsePrometheusMetrics(sandboxID, body)
|
return parsePrometheusMetrics(sandboxID, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetSandboxMetrics will get sandbox's metrics from shim
|
||||||
|
func GetSandboxMetrics(sandboxID string) (string, error) {
|
||||||
|
body, err := doGet(sandboxID, defaultTimeout, "metrics")
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(body), nil
|
||||||
|
}
|
||||||
|
|
||||||
// parsePrometheusMetrics will decode metrics from Prometheus text format
|
// parsePrometheusMetrics will decode metrics from Prometheus text format
|
||||||
// and return array of *dto.MetricFamily with an ASC order
|
// and return array of *dto.MetricFamily with an ASC order
|
||||||
func parsePrometheusMetrics(sandboxID string, body []byte) ([]*dto.MetricFamily, error) {
|
func parsePrometheusMetrics(sandboxID string, body []byte) ([]*dto.MetricFamily, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user