Repository navigation
Expand file tree
/
Copy pathmetrics.go
More file actions
153 lines (132 loc) · 4.52 KB
/
Copy pathmetrics.go
File metadata and controls
153 lines (132 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
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"
"flag"
"fmt"
"log"
"os"
"os/exec"
"strings"
"time"
_ "cloud.google.com/go/storage"
mexporter "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric"
"github.com/pkg/errors"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
pkgtrace "k8s.io/minikube/pkg/trace"
)
const (
customMetricName = "custom.googleapis.com/minikube/start_time"
profile = "cloud-monitoring"
)
var (
labels string
tmpFile string
)
func main() {
if err := execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
flag.StringVar(&labels, "label", "", "Comma separated list of labels to add to metrics in key:value format")
flag.StringVar(&tmpFile, "file", "/tmp/minikube", "Download minikube to this file for testing")
flag.Parse()
}
func execute() error {
projectID := os.Getenv(pkgtrace.ProjectEnvVar)
if projectID == "" {
return fmt.Errorf("metrics collector requires a valid GCP project id set via the %s env variable", pkgtrace.ProjectEnvVar)
}
ctx := context.Background()
if err := downloadMinikube(ctx, tmpFile); err != nil {
return errors.Wrap(err, "downloading minikube")
}
826D
for _, cr := range []string{"docker", "containerd", "crio"} {
if err := exportMinikubeStart(ctx, projectID, cr); err != nil {
log.Printf("error exporting minikube start data for runtime %v: %v", cr, err)
}
}
return nil
}
func exportMinikubeStart(ctx context.Context, projectID, containerRuntime string) error {
mp, attrs, err := getMeterProvider(projectID, containerRuntime)
if err != nil {
return errors.Wrap(err, "creating meter provider")
}
defer func() { _ = mp.Shutdown(ctx) }()
meter := mp.Meter("minikube")
latency, err := meter.Float64Histogram(customMetricName)
if err != nil {
return errors.Wrap(err, "creating histogram")
}
st, err := minikubeStartTime(ctx, projectID, tmpFile, containerRuntime)
if err != nil {
return errors.Wrap(err, "collecting start time")
}
fmt.Printf("Latency: %f\n", st)
latency.Record(ctx, st, metric.WithAttributes(attrs...))
time.Sleep(30 * time.Second)
return nil
}
func getMeterProvider(projectID, containerRuntime string) (*sdkmetric.MeterProvider, []attribute.KeyValue, error) {
exporter, err := mexporter.New(mexporter.WithProjectID(projectID))
if err != nil {
return nil, nil, err
}
reader := sdkmetric.NewPeriodicReader(exporter, sdkmetric.WithInterval(11*time.Second))
mp := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader))
return mp, getAttributes(containerRuntime), nil
}
func getAttributes(containerRuntime string) []attribute.KeyValue {
attrs := []attribute.KeyValue{attribute.String("container-runtime", containerRuntime)}
if labels == "" {
return attrs
}
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])
attrs = append(attrs, attribute.String(sep[0], sep[1]))
}
return attrs
}
func minikubeStartTime(ctx context.Context, projectID, minikubePath, containerRuntime string) (float64, error) {
defer deleteMinikube(ctx, minikubePath)
cmd := exec.CommandContext(ctx, minikubePath, "start", "--driver=docker", "-p", profile, "--memory=3072", "--trace=gcp", fmt.Sprintf("--container-runtime=%s", containerRuntime))
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=%s", pkgtrace.ProjectEnvVar, projectID))
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
t := time.Now()
log.Printf("Running [%v]....", cmd.Args)
if err := cmd.Run(); err != nil {
return 0, errors.Wrapf(err, "running %v", cmd.Args)
}
totalTime := time.Since(t).Seconds()
return totalTime, nil
}
func deleteMinikube(ctx context.Context, minikubePath string) {
cmd := exec.CommandContext(ctx, minikubePath, "delete", "-p", profile)
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Printf("error deleting: %v", err)
}
}