github.com/ttpreport/gvisor-ligolo@v0.0.0-20240123134145-a858404967ba/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 20 "github.com/google/subcommands" 21 "github.com/ttpreport/gvisor-ligolo/runsc/cmd/util" 22 "github.com/ttpreport/gvisor-ligolo/runsc/config" 23 "github.com/ttpreport/gvisor-ligolo/runsc/flag" 24 "github.com/ttpreport/gvisor-ligolo/runsc/metricserver" 25 ) 26 27 // MetricServer implements subcommands.Command for the "metric-server" command. 28 type MetricServer struct { 29 Server metricserver.Server 30 } 31 32 // Name implements subcommands.Command.Name. 33 func (*MetricServer) Name() string { 34 return "metric-server" 35 } 36 37 // Synopsis implements subcommands.Command.Synopsis. 38 func (*MetricServer) Synopsis() string { 39 return "implements Prometheus metrics HTTP endpoint" 40 } 41 42 // Usage implements subcommands.Command.Usage. 43 func (*MetricServer) Usage() string { 44 return `-root=<root dir> -metric-server=<addr> metric-server [-exporter-prefix=<runsc_>] 45 ` 46 } 47 48 // SetFlags implements subcommands.Command.SetFlags. 49 func (m *MetricServer) SetFlags(f *flag.FlagSet) { 50 f.StringVar(&m.Server.ExporterPrefix, "exporter-prefix", "runsc_", "Prefix for all metric names, following Prometheus exporter convention") 51 f.StringVar(&m.Server.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.") 52 f.BoolVar(&m.Server.ExposeProfileEndpoints, "allow-profiling", false, "If true, expose /runsc-metrics/profile-cpu and /runsc-metrics/profile-heap to get profiling data about the metric server") 53 f.BoolVar(&m.Server.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.") 54 } 55 56 // Execute implements subcommands.Command.Execute. 57 func (m *MetricServer) Execute(ctx context.Context, f *flag.FlagSet, args ...any) subcommands.ExitStatus { 58 if f.NArg() != 0 { 59 f.Usage() 60 return subcommands.ExitUsageError 61 } 62 m.Server.Config = args[0].(*config.Config) 63 if m.Server.Config.MetricServer == "" || m.Server.Config.RootDir == "" { 64 f.Usage() 65 return subcommands.ExitUsageError 66 } 67 if err := m.Server.Run(ctx); err != nil { 68 return util.Errorf("%v", err) 69 } 70 return subcommands.ExitSuccess 71 }