github.com/ttpreport/gvisor-ligolo@v0.0.0-20240123134145-a858404967ba/runsc/cmd/metric_export.go (about) 1 // Copyright 2022 The gVisor Authors. 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 cmd 16 17 import ( 18 "context" 19 "fmt" 20 "os" 21 22 "github.com/google/subcommands" 23 "github.com/ttpreport/gvisor-ligolo/pkg/prometheus" 24 "github.com/ttpreport/gvisor-ligolo/pkg/sentry/control" 25 "github.com/ttpreport/gvisor-ligolo/runsc/cmd/util" 26 "github.com/ttpreport/gvisor-ligolo/runsc/config" 27 "github.com/ttpreport/gvisor-ligolo/runsc/container" 28 "github.com/ttpreport/gvisor-ligolo/runsc/flag" 29 "github.com/ttpreport/gvisor-ligolo/runsc/metricserver" 30 ) 31 32 // MetricExport implements subcommands.Command for the "metric-export" command. 33 type MetricExport struct { 34 exporterPrefix string 35 sandboxMetricsFilter string 36 } 37 38 // Name implements subcommands.Command.Name. 39 func (*MetricExport) Name() string { 40 return "export-metrics" 41 } 42 43 // Synopsis implements subcommands.Command.Synopsis. 44 func (*MetricExport) Synopsis() string { 45 return "export metric data for the sandbox" 46 } 47 48 // Usage implements subcommands.Command.Usage. 49 func (*MetricExport) Usage() string { 50 return `export-metrics [-exporter-prefix=<runsc_>] <container id> - prints sandbox metric data in Prometheus metric format 51 ` 52 } 53 54 // SetFlags implements subcommands.Command.SetFlags. 55 func (m *MetricExport) SetFlags(f *flag.FlagSet) { 56 f.StringVar(&m.exporterPrefix, "exporter-prefix", "runsc_", "Prefix for all metric names, following Prometheus exporter convention") 57 f.StringVar(&m.sandboxMetricsFilter, "sandbox-metrics-filter", "", "If set, filter exported metrics using the specified regular expression. This filtering is applied before adding --exporter-prefix.") 58 } 59 60 // Execute implements subcommands.Command.Execute. 61 func (m *MetricExport) Execute(ctx context.Context, f *flag.FlagSet, args ...any) subcommands.ExitStatus { 62 if f.NArg() < 1 { 63 f.Usage() 64 return subcommands.ExitUsageError 65 } 66 67 id := f.Arg(0) 68 conf := args[0].(*config.Config) 69 70 cont, err := container.Load(conf.RootDir, container.FullID{ContainerID: id}, container.LoadOpts{}) 71 if err != nil { 72 util.Fatalf("loading container: %v", err) 73 } 74 75 prometheusLabels, err := metricserver.SandboxPrometheusLabels(cont) 76 if err != nil { 77 util.Fatalf("Cannot compute Prometheus labels of sandbox: %v", err) 78 } 79 80 snapshot, err := cont.Sandbox.ExportMetrics(control.MetricsExportOpts{ 81 OnlyMetrics: m.sandboxMetricsFilter, 82 }) 83 if err != nil { 84 util.Fatalf("ExportMetrics failed: %v", err) 85 } 86 commentHeader := fmt.Sprintf("Command-line export for sandbox %s", cont.Sandbox.ID) 87 if m.sandboxMetricsFilter != "" { 88 commentHeader = fmt.Sprintf("%s (filtered using regular expression: %q)", commentHeader, m.sandboxMetricsFilter) 89 } 90 written, err := prometheus.Write(os.Stdout, prometheus.ExportOptions{ 91 CommentHeader: commentHeader, 92 }, map[*prometheus.Snapshot]prometheus.SnapshotExportOptions{ 93 snapshot: { 94 ExporterPrefix: m.exporterPrefix, 95 ExtraLabels: prometheusLabels, 96 }, 97 }) 98 if err != nil { 99 util.Fatalf("Cannot write metrics to stdout: %v", err) 100 } 101 util.Infof("Wrote %d bytes of Prometheus metric data to stdout", written) 102 103 return subcommands.ExitSuccess 104 }