github.com/nicocha30/gvisor-ligolo@v0.0.0-20230726075806-989fa2c0a413/runsc/metricserver/metricserver_metrics.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 metricserver
    16  
    17  import (
    18  	"crypto/sha256"
    19  	"encoding/binary"
    20  	"io"
    21  	"strconv"
    22  
    23  	"github.com/nicocha30/gvisor-ligolo/pkg/prometheus"
    24  	"github.com/nicocha30/gvisor-ligolo/runsc/container"
    25  )
    26  
    27  // Metrics generated by the metrics server itself.
    28  var (
    29  	SandboxPresenceMetric = prometheus.Metric{
    30  		Name: "sandbox_presence",
    31  		Type: prometheus.TypeGauge,
    32  		Help: "Boolean metric set to 1 for each known sandbox.",
    33  	}
    34  	SandboxRunningMetric = prometheus.Metric{
    35  		Name: "sandbox_running",
    36  		Type: prometheus.TypeGauge,
    37  		Help: "Boolean metric set to 1 for each running sandbox.",
    38  	}
    39  	SandboxMetadataMetric = prometheus.Metric{
    40  		Name: "sandbox_metadata",
    41  		Type: prometheus.TypeGauge,
    42  		Help: "Key-value pairs about per-sandbox metadata.",
    43  	}
    44  	SandboxCapabilitiesMetric = prometheus.Metric{
    45  		Name: "sandbox_capabilities",
    46  		Type: prometheus.TypeGauge,
    47  		Help: "Linux capabilities added within containers of the sandbox.",
    48  	}
    49  	SandboxCapabilitiesMetricLabel = "capability"
    50  	SpecMetadataMetric             = prometheus.Metric{
    51  		Name: "spec_metadata",
    52  		Type: prometheus.TypeGauge,
    53  		Help: "Key-value pairs about OCI spec metadata.",
    54  	}
    55  	SandboxCreationMetric = prometheus.Metric{
    56  		Name: "sandbox_creation_time_seconds",
    57  		Type: prometheus.TypeGauge,
    58  		Help: "When the sandbox was created, as a unix timestamp in seconds.",
    59  	}
    60  	NumRunningSandboxesMetric = prometheus.Metric{
    61  		Name: "num_sandboxes_running",
    62  		Type: prometheus.TypeGauge,
    63  		Help: "Number of sandboxes running at present.",
    64  	}
    65  	NumCannotExportSandboxesMetric = prometheus.Metric{
    66  		Name: "num_sandboxes_broken_metrics",
    67  		Type: prometheus.TypeGauge,
    68  		Help: "Number of sandboxes from which we cannot export metrics.",
    69  	}
    70  	NumTotalSandboxesMetric = prometheus.Metric{
    71  		Name: "num_sandboxes_total",
    72  		Type: prometheus.TypeCounter,
    73  		Help: "Counter of sandboxes that have ever been started.",
    74  	}
    75  )
    76  
    77  // Metrics is a list of metrics that the metric server generates.
    78  var Metrics = []*prometheus.Metric{
    79  	&SandboxPresenceMetric,
    80  	&SandboxRunningMetric,
    81  	&SandboxMetadataMetric,
    82  	&SandboxCapabilitiesMetric,
    83  	&SpecMetadataMetric,
    84  	&SandboxCreationMetric,
    85  	&NumRunningSandboxesMetric,
    86  	&NumCannotExportSandboxesMetric,
    87  	&NumTotalSandboxesMetric,
    88  	&prometheus.ProcessStartTimeSeconds,
    89  }
    90  
    91  // SandboxPrometheusLabels returns a set of Prometheus labels that identifies the sandbox running
    92  // the given root container.
    93  func SandboxPrometheusLabels(rootContainer *container.Container) (map[string]string, error) {
    94  	s := rootContainer.Sandbox
    95  	labels := make(map[string]string, 4)
    96  	labels[prometheus.SandboxIDLabel] = s.ID
    97  
    98  	// Compute iteration ID label in a stable manner.
    99  	// This uses sha256(ID + ":" + creation time).
   100  	h := sha256.New()
   101  	if _, err := io.WriteString(h, s.ID); err != nil {
   102  		return nil, err
   103  	}
   104  	if _, err := io.WriteString(h, ":"); err != nil {
   105  		return nil, err
   106  	}
   107  	if _, err := io.WriteString(h, rootContainer.CreatedAt.UTC().String()); err != nil {
   108  		return nil, err
   109  	}
   110  	labels[prometheus.IterationIDLabel] = strconv.FormatUint(binary.BigEndian.Uint64(h.Sum(nil)[:8]), 36)
   111  
   112  	if s.PodName != "" {
   113  		labels[prometheus.PodNameLabel] = s.PodName
   114  	}
   115  	if s.Namespace != "" {
   116  		labels[prometheus.NamespaceLabel] = s.Namespace
   117  	}
   118  	return labels, nil
   119  }
   120  
   121  // ComputeSpecMetadata returns the labels for the `spec_metadata` metric.
   122  // It merges data from the Specs of multiple containers running within the
   123  // same sandbox.
   124  // This function must support being called with `allContainers` being nil.
   125  // It must return the same set of label keys regardless of how many containers
   126  // are in `allContainers`.
   127  func ComputeSpecMetadata(allContainers []*container.Container) map[string]string {
   128  	const (
   129  		unknownOCIVersion      = "UNKNOWN"
   130  		inconsistentOCIVersion = "INCONSISTENT"
   131  	)
   132  
   133  	hasUID0Container := false
   134  	ociVersion := unknownOCIVersion
   135  	for _, cont := range allContainers {
   136  		if cont.RunsAsUID0() {
   137  			hasUID0Container = true
   138  		}
   139  		if ociVersion == unknownOCIVersion {
   140  			ociVersion = cont.Spec.Version
   141  		} else if ociVersion != cont.Spec.Version {
   142  			ociVersion = inconsistentOCIVersion
   143  		}
   144  	}
   145  	return map[string]string{
   146  		"hasuid0":    strconv.FormatBool(hasUID0Container),
   147  		"ociversion": ociVersion,
   148  	}
   149  }