blob: 1fe3167821465aa4439a98192dd49cfb7af72165 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2017 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package value
18
19import (
20 "sync"
21 "time"
22
23 "github.com/prometheus/client_golang/prometheus"
24)
25
26const (
27 namespace = "apiserver"
28 subsystem = "storage"
29)
30
31var (
32 transformerLatencies = prometheus.NewHistogramVec(
33 prometheus.HistogramOpts{
34 Namespace: namespace,
35 Subsystem: subsystem,
36 Name: "transformation_latencies_microseconds",
37 Help: "Latencies in microseconds of value transformation operations.",
38 // In-process transformations (ex. AES CBC) complete on the order of 20 microseconds. However, when
39 // external KMS is involved latencies may climb into milliseconds.
40 Buckets: prometheus.ExponentialBuckets(5, 2, 14),
41 },
42 []string{"transformation_type"},
43 )
44 transformerFailuresTotal = prometheus.NewCounterVec(
45 prometheus.CounterOpts{
46 Namespace: namespace,
47 Subsystem: subsystem,
48 Name: "transformation_failures_total",
49 Help: "Total number of failed transformation operations.",
50 },
51 []string{"transformation_type"},
52 )
53
54 envelopeTransformationCacheMissTotal = prometheus.NewCounter(
55 prometheus.CounterOpts{
56 Namespace: namespace,
57 Subsystem: subsystem,
58 Name: "envelope_transformation_cache_misses_total",
59 Help: "Total number of cache misses while accessing key decryption key(KEK).",
60 },
61 )
62
63 dataKeyGenerationLatencies = prometheus.NewHistogram(
64 prometheus.HistogramOpts{
65 Namespace: namespace,
66 Subsystem: subsystem,
67 Name: "data_key_generation_latencies_microseconds",
68 Help: "Latencies in microseconds of data encryption key(DEK) generation operations.",
69 Buckets: prometheus.ExponentialBuckets(5, 2, 14),
70 },
71 )
72 dataKeyGenerationFailuresTotal = prometheus.NewCounter(
73 prometheus.CounterOpts{
74 Namespace: namespace,
75 Subsystem: subsystem,
76 Name: "data_key_generation_failures_total",
77 Help: "Total number of failed data encryption key(DEK) generation operations.",
78 },
79 )
80)
81
82var registerMetrics sync.Once
83
84func RegisterMetrics() {
85 registerMetrics.Do(func() {
86 prometheus.MustRegister(transformerLatencies)
87 prometheus.MustRegister(transformerFailuresTotal)
88 prometheus.MustRegister(envelopeTransformationCacheMissTotal)
89 prometheus.MustRegister(dataKeyGenerationLatencies)
90 prometheus.MustRegister(dataKeyGenerationFailuresTotal)
91 })
92}
93
94// RecordTransformation records latencies and count of TransformFromStorage and TransformToStorage operations.
95func RecordTransformation(transformationType string, start time.Time, err error) {
96 if err != nil {
97 transformerFailuresTotal.WithLabelValues(transformationType).Inc()
98 return
99 }
100
101 since := sinceInMicroseconds(start)
102 transformerLatencies.WithLabelValues(transformationType).Observe(float64(since))
103}
104
105// RecordCacheMiss records a miss on Key Encryption Key(KEK) - call to KMS was required to decrypt KEK.
106func RecordCacheMiss() {
107 envelopeTransformationCacheMissTotal.Inc()
108}
109
110// RecordDataKeyGeneration records latencies and count of Data Encryption Key generation operations.
111func RecordDataKeyGeneration(start time.Time, err error) {
112 if err != nil {
113 dataKeyGenerationFailuresTotal.Inc()
114 return
115 }
116
117 since := sinceInMicroseconds(start)
118 dataKeyGenerationLatencies.Observe(float64(since))
119}
120
121func sinceInMicroseconds(start time.Time) int64 {
122 elapsedNanoseconds := time.Since(start).Nanoseconds()
123 return elapsedNanoseconds / int64(time.Microsecond)
124}