-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
244 lines (210 loc) · 7.66 KB
/
main.go
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"context"
"flag"
"fmt"
"log"
"regexp"
"strconv"
"time"
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
"go.opentelemetry.io/otel/metric"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
)
var (
config *rest.Config
durationPullHistogram metric.Int64Histogram
durationPullWaitOnlyHistogram metric.Int64Histogram
imageSizeGauge metric.Int64Gauge
)
func main() {
var kubeconfig *string
if home := homedir.HomeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
ctx := flag.String("context", "", "The name of the kubeconfig context to use")
flag.Parse()
var err error
// Use in-cluster config if kubeconfig is not provided
if *kubeconfig == "" {
config, err = rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
} else {
config, err = clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
}
// Override the context if specified
if *ctx != "" {
configOverrides := &clientcmd.ConfigOverrides{CurrentContext: *ctx}
config, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: *kubeconfig},
configOverrides,
).ClientConfig()
if err != nil {
panic(err.Error())
}
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
// OpenTelemetry metrics initialization
res, err := newResource()
if err != nil {
panic(err)
}
// Create a meter provider.
// You can pass this instance directly to your instrumented code if it
// accepts a MeterProvider instance.
meterProvider, err := newMeterProvider(context.Background(), res)
if err != nil {
panic(err)
}
// Handle shutdown properly so nothing leaks.
defer func() {
if err := meterProvider.Shutdown(context.Background()); err != nil {
log.Println(err)
}
}()
// Register as global meter provider so that it can be used via otel.Meter
// and accessed using otel.GetMeterProvider.
// Most instrumentation libraries use the global meter provider as default.
// If the global meter provider is not set then a no-op implementation
// is used, which fails to generate data.
otel.SetMeterProvider(meterProvider)
var meter = otel.Meter("pokgak.xyz/k8s-image-pull-metrics")
durationPullHistogram, _ = meter.Int64Histogram(
"k8s.image.pull.duration",
metric.WithDescription("The duration of image pull."),
metric.WithUnit("ms"),
metric.WithExplicitBucketBoundaries([]float64{15000, 30000, 45000, 60000, 120000, 180000, 240000, 300000}...),
)
durationPullWaitOnlyHistogram, _ = meter.Int64Histogram(
"k8s.image.pull_wait_only.duration",
metric.WithDescription("The duration of image pull including waiting time."),
metric.WithUnit("ms"),
metric.WithExplicitBucketBoundaries([]float64{15000, 30000, 45000, 60000, 120000, 180000, 240000, 300000}...),
)
imageSizeGauge, _ = meter.Int64Gauge(
"k8s.image.size",
metric.WithDescription("The size of the image in bytes."),
metric.WithUnit("bytes"),
)
// setup informer to watch for events
factory := informers.NewSharedInformerFactory(clientset, 0)
informer := factory.Core().V1().Events().Informer()
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: handleAddFunc,
})
stopCh := make(chan struct{})
defer close(stopCh)
// Start the informer
go informer.Run(stopCh)
// Wait for the informer to sync
if !cache.WaitForCacheSync(stopCh, informer.HasSynced) {
log.Fatalf("Error syncing cache")
}
// Block forever
select {}
}
func handleAddFunc(obj interface{}) {
event, ok := obj.(*v1.Event)
if !ok {
return
}
if event.Source.Component != "kubelet" || event.InvolvedObject.Kind != "Pod" || event.Reason != "Pulled" {
return
}
msg := event.Message
// skip if the message starts with "Container image" as it is not the message we are interested in
if len(msg) >= 15 && msg[:15] == "Container image" {
log.Println("Skipping event message:", msg)
return
}
log.Println("Pod event added: ", event.Message)
// input: "Successfully pulled image \"<account-id>.dkr.ecr.ap-southeast-1.amazonaws.com/example-service:99cd3b4\" in 1m44.643s (1m44.643s including waiting). Image size: 1169083618 bytes.",
// extract the image name, tag, duration pull, duration wait, and image size
var imageName, durationPullStr, durationWaitStr, imageSize string
n, err := fmt.Sscanf(msg, "Successfully pulled image %q in %s (%s including waiting). Image size: %s bytes.", &imageName, &durationPullStr, &durationWaitStr, &imageSize)
if err == nil && n == 4 {
durationPull, err := time.ParseDuration(durationPullStr)
if err != nil {
log.Println("Failed to parse durationPull:", err)
return
}
durationWithWait, err := time.ParseDuration(durationWaitStr)
if err != nil {
log.Println("Failed to parse durationWait:", err)
return
}
imageSizeInt, err := strconv.ParseInt(imageSize, 10, 64)
if err != nil {
log.Println("Failed to parse imageSize:", err)
return
}
commonAttributes := []attribute.KeyValue{
attribute.Int64("observed.timestamp", event.LastTimestamp.UnixMilli()),
attribute.String("exported.namespace", event.Namespace),
attribute.String("exported.pod.image", imageName),
attribute.String("exported.host", event.Source.Host),
}
// extract the prefix of the pod name
// given: k8s-image-pull-metrics-5f588dd8cf-8lnm4
// extract: k8s-image-pull-metrics
podName := event.InvolvedObject.Name
re := regexp.MustCompile(`^(.*)-.*-.*$`)
matches := re.FindStringSubmatch(podName)
if len(matches) > 1 {
commonAttributes = append(commonAttributes, attribute.String("exported.pod.prefix", matches[1]))
}
imageSizeGauge.Record(context.Background(), imageSizeInt, metric.WithAttributes(commonAttributes...))
durationPullHistogram.Record(context.Background(), durationPull.Milliseconds(), metric.WithAttributes(commonAttributes...))
durationPullWaitOnlyHistogram.Record(context.Background(), time.Duration(durationWithWait - durationPull).Milliseconds(), metric.WithAttributes(commonAttributes...))
log.Println("Recorded metrics: durationPull:", durationPull.Seconds(), "durationWait:", time.Duration(durationWithWait - durationPull).Seconds(), "imageSize:", imageSizeInt)
}
if err != nil || n != 4 {
log.Println("Failed to parse event message:", err)
return
}
}
func newResource() (*resource.Resource, error) {
return resource.Merge(resource.Default(),
resource.NewWithAttributes(semconv.SchemaURL,
semconv.ServiceName("k8s-image-pull-metrics"),
// semconv.ServiceVersion("0.1.0"),
))
}
func newMeterProvider(ctx context.Context, res *resource.Resource) (*sdkmetric.MeterProvider, error) {
opts := []otlpmetrichttp.Option{}
opts = append(opts, otlpmetrichttp.WithInsecure())
// opts = append(opts, otlpmetrichttp.WithEndpoint("http://collector.monitoring.svc.cluster.local:4318"))
// opts = append(opts, otlpmetrichttp.WithURLPath("/v1/metrics"))
opts = append(opts, otlpmetrichttp.WithCompression(otlpmetrichttp.GzipCompression))
metricExporter, err := otlpmetrichttp.New(ctx, opts...)
if err != nil {
return nil, err
}
meterProvider := sdkmetric.NewMeterProvider(
sdkmetric.WithResource(res),
sdkmetric.WithReader(sdkmetric.NewPeriodicReader(
metricExporter,
sdkmetric.WithInterval(30*time.Second),
)),
)
return meterProvider, nil
}