2020-11-20 22:27:55 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Kubernetes Authors All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-12-02 03:37:15 +00:00
|
|
|
"flag"
|
2020-11-20 22:27:55 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2020-12-02 03:37:15 +00:00
|
|
|
"strings"
|
2020-11-20 22:27:55 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
_ "cloud.google.com/go/storage"
|
|
|
|
"contrib.go.opencensus.io/exporter/stackdriver"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"go.opencensus.io/stats"
|
|
|
|
"go.opencensus.io/stats/view"
|
|
|
|
"go.opencensus.io/trace"
|
2020-11-30 18:24:15 +00:00
|
|
|
pkgtrace "k8s.io/minikube/pkg/trace"
|
2020-11-20 22:27:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
customMetricName = "custom.googleapis.com/minikube/start_time"
|
|
|
|
profile = "cloud-monitoring"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-11-23 20:56:48 +00:00
|
|
|
// The task latency in seconds
|
2020-11-20 22:27:55 +00:00
|
|
|
latencyS = stats.Float64("repl/start_time", "start time in seconds", "s")
|
2020-12-02 03:37:15 +00:00
|
|
|
labels string
|
2020-12-02 19:02:27 +00:00
|
|
|
tmpFile string
|
2020-11-20 22:27:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if err := execute(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 03:37:15 +00:00
|
|
|
func init() {
|
|
|
|
flag.StringVar(&labels, "label", "", "Comma separated list of labels to add to metrics in key:value format")
|
2020-12-02 19:02:27 +00:00
|
|
|
flag.StringVar(&tmpFile, "file", "/tmp/minikube", "Download minikube to this file for testing")
|
2020-12-02 03:37:15 +00:00
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
2020-11-20 22:27:55 +00:00
|
|
|
func execute() error {
|
2020-11-30 18:24:15 +00:00
|
|
|
projectID := os.Getenv(pkgtrace.ProjectEnvVar)
|
2020-11-20 22:27:55 +00:00
|
|
|
if projectID == "" {
|
2020-11-30 18:24:15 +00:00
|
|
|
return fmt.Errorf("metrics collector requires a valid GCP project id set via the %s env variable", pkgtrace.ProjectEnvVar)
|
2020-11-20 22:27:55 +00:00
|
|
|
}
|
2020-12-04 19:09:15 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
if err := downloadMinikube(ctx, tmpFile); err != nil {
|
|
|
|
return errors.Wrap(err, "downloading minikube")
|
|
|
|
}
|
2020-11-20 22:27:55 +00:00
|
|
|
|
|
|
|
// Register the view. It is imperative that this step exists,
|
|
|
|
// otherwise recorded metrics will be dropped and never exported.
|
|
|
|
v := &view.View{
|
|
|
|
Name: customMetricName,
|
|
|
|
Measure: latencyS,
|
|
|
|
Description: "minikube start time",
|
|
|
|
Aggregation: view.LastValue(),
|
|
|
|
}
|
|
|
|
if err := view.Register(v); err != nil {
|
2020-11-24 00:21:13 +00:00
|
|
|
return errors.Wrap(err, "registering view")
|
2020-11-20 22:27:55 +00:00
|
|
|
}
|
2020-12-04 19:09:15 +00:00
|
|
|
for _, cr := range []string{"docker", "containerd", "crio"} {
|
2020-12-04 19:36:16 +00:00
|
|
|
if err := exportMinikubeStart(ctx, projectID, cr); err != nil {
|
|
|
|
log.Printf("error exporting minikube start data for runtime %v: %v", cr, err)
|
2020-12-04 19:09:15 +00:00
|
|
|
}
|
2020-12-04 19:36:16 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-04 19:09:15 +00:00
|
|
|
|
2020-12-04 19:36:16 +00:00
|
|
|
func exportMinikubeStart(ctx context.Context, projectID, containerRuntime string) error {
|
|
|
|
sd, err := getExporter(projectID, containerRuntime)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "getting stackdriver exporter")
|
|
|
|
}
|
|
|
|
// Register it as a trace exporter
|
|
|
|
trace.RegisterExporter(sd)
|
|
|
|
|
|
|
|
if err := sd.StartMetricsExporter(); err != nil {
|
|
|
|
return errors.Wrap(err, "starting metric exporter")
|
|
|
|
}
|
|
|
|
// track minikube start time and record it to metrics collector
|
|
|
|
st, err := minikubeStartTime(ctx, projectID, tmpFile, containerRuntime)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "collecting start time")
|
2020-12-04 19:09:15 +00:00
|
|
|
}
|
2020-12-04 19:36:16 +00:00
|
|
|
fmt.Printf("Latency: %f\n", st)
|
|
|
|
stats.Record(ctx, latencyS.M(st))
|
|
|
|
time.Sleep(30 * time.Second)
|
|
|
|
sd.Flush()
|
|
|
|
sd.StopMetricsExporter()
|
|
|
|
trace.UnregisterExporter(sd)
|
2020-12-04 19:09:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-11-20 22:27:55 +00:00
|
|
|
|
2020-12-04 19:09:15 +00:00
|
|
|
func getExporter(projectID, containerRuntime string) (*stackdriver.Exporter, error) {
|
|
|
|
return stackdriver.NewExporter(stackdriver.Options{
|
2020-11-20 22:27:55 +00:00
|
|
|
ProjectID: projectID,
|
|
|
|
// ReportingInterval sets the frequency of reporting metrics
|
|
|
|
// to stackdriver backend.
|
2021-12-06 18:45:50 +00:00
|
|
|
ReportingInterval: 11 * time.Second,
|
2020-12-04 19:09:15 +00:00
|
|
|
DefaultMonitoringLabels: getLabels(containerRuntime),
|
2020-11-20 22:27:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-04 19:09:15 +00:00
|
|
|
func getLabels(containerRuntime string) *stackdriver.Labels {
|
2020-12-02 03:37:15 +00:00
|
|
|
l := &stackdriver.Labels{}
|
2020-12-04 19:09:15 +00:00
|
|
|
l.Set("container-runtime", containerRuntime, "container-runtime")
|
2020-12-02 03:37:15 +00:00
|
|
|
if labels == "" {
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
separated := strings.Split(labels, ",")
|
|
|
|
for _, s := range separated {
|
|
|
|
sep := strings.Split(s, ":")
|
|
|
|
if len(sep) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Printf("Adding label %s=%s to metrics...", sep[0], sep[1])
|
|
|
|
l.Set(sep[0], sep[1], "")
|
|
|
|
}
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2020-12-04 19:09:15 +00:00
|
|
|
func minikubeStartTime(ctx context.Context, projectID, minikubePath, containerRuntime string) (float64, error) {
|
2020-11-24 00:14:06 +00:00
|
|
|
defer deleteMinikube(ctx, minikubePath)
|
2020-11-20 22:27:55 +00:00
|
|
|
|
2021-04-08 22:39:12 +00:00
|
|
|
cmd := exec.CommandContext(ctx, minikubePath, "start", "--driver=docker", "-p", profile, "--memory=2048", "--trace=gcp", fmt.Sprintf("--container-runtime=%s", containerRuntime))
|
2020-11-30 18:24:15 +00:00
|
|
|
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=%s", pkgtrace.ProjectEnvVar, projectID))
|
2020-11-20 22:27:55 +00:00
|
|
|
cmd.Stdout = os.Stderr
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
t := time.Now()
|
2020-12-01 03:03:30 +00:00
|
|
|
log.Printf("Running [%v]....", cmd.Args)
|
2020-11-20 22:27:55 +00:00
|
|
|
if err := cmd.Run(); err != nil {
|
2020-11-24 00:14:06 +00:00
|
|
|
return 0, errors.Wrapf(err, "running %v", cmd.Args)
|
2020-11-20 22:27:55 +00:00
|
|
|
}
|
2020-12-01 03:03:30 +00:00
|
|
|
totalTime := time.Since(t).Seconds()
|
|
|
|
return totalTime, nil
|
2020-11-20 22:27:55 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 00:14:06 +00:00
|
|
|
func deleteMinikube(ctx context.Context, minikubePath string) {
|
|
|
|
cmd := exec.CommandContext(ctx, minikubePath, "delete", "-p", profile)
|
2020-11-20 22:27:55 +00:00
|
|
|
cmd.Stdout = os.Stderr
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
log.Printf("error deleting: %v", err)
|
|
|
|
}
|
|
|
|
}
|