github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/runsc/cmd/metric_server.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/MerlinKodo/gvisor/runsc/cmd/metricserver"
    23  	"github.com/MerlinKodo/gvisor/runsc/cmd/util"
    24  	"github.com/MerlinKodo/gvisor/runsc/config"
    25  	"github.com/MerlinKodo/gvisor/runsc/flag"
    26  	"github.com/google/subcommands"
    27  )
    28  
    29  // MetricServer implements subcommands.Command for the "metric-server" command.
    30  type MetricServer struct {
    31  	ExporterPrefix         string
    32  	PIDFile                string
    33  	ExposeProfileEndpoints bool
    34  	AllowUnknownRoot       bool
    35  }
    36  
    37  // Name implements subcommands.Command.Name.
    38  func (*MetricServer) Name() string {
    39  	return "metric-server"
    40  }
    41  
    42  // Synopsis implements subcommands.Command.Synopsis.
    43  func (*MetricServer) Synopsis() string {
    44  	return "implements Prometheus metrics HTTP endpoint"
    45  }
    46  
    47  // Usage implements subcommands.Command.Usage.
    48  func (*MetricServer) Usage() string {
    49  	return `-root=<root dir> -metric-server=<addr> metric-server [-exporter-prefix=<runsc_>]
    50  `
    51  }
    52  
    53  // SetFlags implements subcommands.Command.SetFlags.
    54  func (m *MetricServer) SetFlags(f *flag.FlagSet) {
    55  	f.StringVar(&m.ExporterPrefix, "exporter-prefix", "runsc_", "Prefix for all metric names, following Prometheus exporter convention")
    56  	f.StringVar(&m.PIDFile, "pid-file", "", "If set, write the metric server's own PID to this file after binding to the --metric-server address. The parent directory of this file must already exist.")
    57  	f.BoolVar(&m.ExposeProfileEndpoints, "allow-profiling", false, "If true, expose /runsc-metrics/profile-cpu and /runsc-metrics/profile-heap to get profiling data about the metric server")
    58  	f.BoolVar(&m.AllowUnknownRoot, "allow-unknown-root", false, "if set, the metric server will keep running regardless of the existence of --root or the metric server's ability to access it.")
    59  }
    60  
    61  // Execute implements subcommands.Command.Execute.
    62  func (m *MetricServer) Execute(ctx context.Context, f *flag.FlagSet, args ...any) subcommands.ExitStatus {
    63  	if f.NArg() != 0 {
    64  		f.Usage()
    65  		return subcommands.ExitUsageError
    66  	}
    67  	var newArgs []string
    68  	newArgs = append(newArgs, metricserver.BinaryName)
    69  	newArgs = append(newArgs, args[0].(*config.Config).ToFlags()...)
    70  	newArgs = append(newArgs,
    71  		fmt.Sprintf("--metricserver-exporter-prefix=%s", m.ExporterPrefix),
    72  		fmt.Sprintf("--metricserver-pid-file=%s", m.PIDFile),
    73  		fmt.Sprintf("--metricserver-allow-profiling=%t", m.ExposeProfileEndpoints),
    74  		fmt.Sprintf("--metricserver-allow-unknown-root=%t", m.AllowUnknownRoot),
    75  	)
    76  	err := metricserver.Exec(metricserver.Options{
    77  		Argv: newArgs,
    78  		Envv: os.Environ(),
    79  	})
    80  	if err != nil {
    81  		util.Fatalf("metric server: %v", err)
    82  	}
    83  	util.Fatalf("unreachable")
    84  	return subcommands.ExitFailure
    85  }