github.com/ttpreport/gvisor-ligolo@v0.0.0-20240123134145-a858404967ba/runsc/cmd/metric_metadata.go (about)

     1  // Copyright 2023 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  
    21  	"github.com/google/subcommands"
    22  	"github.com/ttpreport/gvisor-ligolo/pkg/metric"
    23  	"github.com/ttpreport/gvisor-ligolo/runsc/cmd/util"
    24  	"github.com/ttpreport/gvisor-ligolo/runsc/flag"
    25  	"google.golang.org/protobuf/encoding/prototext"
    26  )
    27  
    28  // MetricMetadata implements subcommands.Command for the "metric-metadata" command.
    29  type MetricMetadata struct {
    30  }
    31  
    32  // Name implements subcommands.Command.Name.
    33  func (*MetricMetadata) Name() string {
    34  	return "metric-metadata"
    35  }
    36  
    37  // Synopsis implements subcommands.Command.Synopsis.
    38  func (*MetricMetadata) Synopsis() string {
    39  	return "export metric metadata of metrics registered in this build, in text proto format"
    40  }
    41  
    42  // Usage implements subcommands.Command.Usage.
    43  func (*MetricMetadata) Usage() string {
    44  	return "metric-metadata"
    45  }
    46  
    47  // SetFlags implements subcommands.Command.SetFlags.
    48  func (m *MetricMetadata) SetFlags(f *flag.FlagSet) {
    49  }
    50  
    51  // Execute implements subcommands.Command.Execute.
    52  func (m *MetricMetadata) Execute(ctx context.Context, f *flag.FlagSet, args ...any) subcommands.ExitStatus {
    53  	if err := metric.Initialize(); err != nil {
    54  		util.Fatalf("Cannot initialize metrics: %v", err)
    55  	}
    56  	registration, err := metric.GetMetricRegistration()
    57  	if err != nil {
    58  		util.Fatalf("Cannot get metric registration data: %v", err)
    59  	}
    60  	fmt.Println(prototext.MarshalOptions{Multiline: true, EmitASCII: true}.Format(registration))
    61  	return subcommands.ExitSuccess
    62  }