| 1 |
/* |
| 2 |
Copyright The containerd Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package commonmetrics |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"sync" |
| 22 |
"time" |
| 23 |
|
| 24 |
"github.com/containerd/containerd/log" |
| 25 |
digest "github.com/opencontainers/go-digest" |
| 26 |
"github.com/prometheus/client_golang/prometheus" |
| 27 |
"github.com/sirupsen/logrus" |
| 28 |
) |
| 29 |
|
| 30 |
const ( |
| 31 |
// OperationLatencyKeyMilliseconds is the key for stargz operation latency metrics in milliseconds. |
| 32 |
OperationLatencyKeyMilliseconds = "operation_duration_milliseconds" |
| 33 |
|
| 34 |
// OperationLatencyKeyMicroseconds is the key for stargz operation latency metrics in microseconds. |
| 35 |
OperationLatencyKeyMicroseconds = "operation_duration_microseconds" |
| 36 |
|
| 37 |
// OperationCountKey is the key for stargz operation count metrics. |
| 38 |
OperationCountKey = "operation_count" |
| 39 |
|
| 40 |
// BytesServedKey is the key for any metric related to counting bytes served as the part of specific operation. |
| 41 |
BytesServedKey = "bytes_served" |
| 42 |
|
| 43 |
// Keep namespace as stargz and subsystem as fs. |
| 44 |
namespace = "stargz" |
| 45 |
subsystem = "fs" |
| 46 |
) |
| 47 |
|
| 48 |
// Lists all metric labels. |
| 49 |
const ( |
| 50 |
// prometheus metrics |
| 51 |
Mount = "mount" |
| 52 |
RemoteRegistryGet = "remote_registry_get" |
| 53 |
NodeReaddir = "node_readdir" |
| 54 |
StargzHeaderGet = "stargz_header_get" |
| 55 |
StargzFooterGet = "stargz_footer_get" |
| 56 |
StargzTocGet = "stargz_toc_get" |
| 57 |
DeserializeTocJSON = "stargz_toc_json_deserialize" |
| 58 |
PrefetchesCompleted = "all_prefetches_completed" |
| 59 |
ReadOnDemand = "read_on_demand" |
| 60 |
MountLayerToLastOnDemandFetch = "mount_layer_to_last_on_demand_fetch" |
| 61 |
|
| 62 |
OnDemandReadAccessCount = "on_demand_read_access_count" |
| 63 |
OnDemandRemoteRegistryFetchCount = "on_demand_remote_registry_fetch_count" |
| 64 |
OnDemandBytesServed = "on_demand_bytes_served" |
| 65 |
OnDemandBytesFetched = "on_demand_bytes_fetched" |
| 66 |
|
| 67 |
// logs metrics |
| 68 |
PrefetchTotal = "prefetch_total" |
| 69 |
PrefetchDownload = "prefetch_download" |
| 70 |
PrefetchDecompress = "prefetch_decompress" |
| 71 |
BackgroundFetchTotal = "background_fetch_total" |
| 72 |
BackgroundFetchDownload = "background_fetch_download" |
| 73 |
BackgroundFetchDecompress = "background_fetch_decompress" |
| 74 |
PrefetchSize = "prefetch_size" |
| 75 |
) |
| 76 |
|
| 77 |
var ( |
| 78 |
// Buckets for OperationLatency metrics. |
| 79 |
latencyBucketsMilliseconds = []float64{1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384} // in milliseconds |
| 80 |
latencyBucketsMicroseconds = []float64{1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024} // in microseconds |
| 81 |
|
| 82 |
// operationLatencyMilliseconds collects operation latency numbers in milliseconds grouped by |
| 83 |
// operation, type and layer digest. |
| 84 |
operationLatencyMilliseconds = prometheus.NewHistogramVec( |
| 85 |
prometheus.HistogramOpts{ |
| 86 |
Namespace: namespace, |
| 87 |
Subsystem: subsystem, |
| 88 |
Name: OperationLatencyKeyMilliseconds, |
| 89 |
Help: "Latency in milliseconds of stargz snapshotter operations. Broken down by operation type and layer sha.", |
| 90 |
Buckets: latencyBucketsMilliseconds, |
| 91 |
}, |
| 92 |
[]string{"operation_type", "layer"}, |
| 93 |
) |
| 94 |
|
| 95 |
// operationLatencyMicroseconds collects operation latency numbers in microseconds grouped by |
| 96 |
// operation, type and layer digest. |
| 97 |
operationLatencyMicroseconds = prometheus.NewHistogramVec( |
| 98 |
prometheus.HistogramOpts{ |
| 99 |
Namespace: namespace, |
| 100 |
Subsystem: subsystem, |
| 101 |
Name: OperationLatencyKeyMicroseconds, |
| 102 |
Help: "Latency in microseconds of stargz snapshotter operations. Broken down by operation type and layer sha.", |
| 103 |
Buckets: latencyBucketsMicroseconds, |
| 104 |
}, |
| 105 |
[]string{"operation_type", "layer"}, |
| 106 |
) |
| 107 |
|
| 108 |
// operationCount collects operation count numbers by operation |
| 109 |
// type and layer sha. |
| 110 |
operationCount = prometheus.NewCounterVec( |
| 111 |
prometheus.CounterOpts{ |
| 112 |
Namespace: namespace, |
| 113 |
Subsystem: subsystem, |
| 114 |
Name: OperationCountKey, |
| 115 |
Help: "The count of stargz snapshotter operations. Broken down by operation type and layer sha.", |
| 116 |
}, |
| 117 |
[]string{"operation_type", "layer"}, |
| 118 |
) |
| 119 |
|
| 120 |
// bytesCount reflects the number of bytes served as the part of specitic operation type per layer sha. |
| 121 |
bytesCount = prometheus.NewGaugeVec( |
| 122 |
prometheus.GaugeOpts{ |
| 123 |
Namespace: namespace, |
| 124 |
Subsystem: subsystem, |
| 125 |
Name: BytesServedKey, |
| 126 |
Help: "The number of bytes served per stargz snapshotter operations. Broken down by operation type and layer sha.", |
| 127 |
}, |
| 128 |
[]string{"operation_type", "layer"}, |
| 129 |
) |
| 130 |
) |
| 131 |
|
| 132 |
var register sync.Once |
| 133 |
var logLevel logrus.Level = logrus.DebugLevel |
| 134 |
|
| 135 |
// sinceInMilliseconds gets the time since the specified start in milliseconds. |
| 136 |
// The division by 1e6 is made to have the milliseconds value as floating point number, since the native method |
| 137 |
// .Milliseconds() returns an integer value and you can lost a precision for sub-millisecond values. |
| 138 |
func sinceInMilliseconds(start time.Time) float64 { |
| 139 |
return float64(time.Since(start).Nanoseconds()) / 1e6 |
| 140 |
} |
| 141 |
|
| 142 |
// sinceInMicroseconds gets the time since the specified start in microseconds. |
| 143 |
// The division by 1e3 is made to have the microseconds value as floating point number, since the native method |
| 144 |
// .Microseconds() returns an integer value and you can lost a precision for sub-microsecond values. |
| 145 |
func sinceInMicroseconds(start time.Time) float64 { |
| 146 |
return float64(time.Since(start).Nanoseconds()) / 1e3 |
| 147 |
} |
| 148 |
|
| 149 |
// Register registers metrics. This is always called only once. |
| 150 |
func Register(l logrus.Level) { |
| 151 |
register.Do(func() { |
| 152 |
logLevel = l |
| 153 |
prometheus.MustRegister(operationLatencyMilliseconds) |
| 154 |
prometheus.MustRegister(operationLatencyMicroseconds) |
| 155 |
prometheus.MustRegister(operationCount) |
| 156 |
prometheus.MustRegister(bytesCount) |
| 157 |
}) |
| 158 |
} |
| 159 |
|
| 160 |
// MeasureLatencyInMilliseconds wraps the labels attachment as well as calling Observe into a single method. |
| 161 |
// Right now we attach the operation and layer digest, so it's possible to see the breakdown for latency |
| 162 |
// by operation and individual layers. |
| 163 |
// If you want this to be layer agnostic, just pass the digest from empty string, e.g. |
| 164 |
// layerDigest := digest.FromString("") |
| 165 |
func MeasureLatencyInMilliseconds(operation string, layer digest.Digest, start time.Time) { |
| 166 |
operationLatencyMilliseconds.WithLabelValues(operation, layer.String()).Observe(sinceInMilliseconds(start)) |
| 167 |
} |
| 168 |
|
| 169 |
// MeasureLatencyInMicroseconds wraps the labels attachment as well as calling Observe into a single method. |
| 170 |
// Right now we attach the operation and layer digest, so it's possible to see the breakdown for latency |
| 171 |
// by operation and individual layers. |
| 172 |
// If you want this to be layer agnostic, just pass the digest from empty string, e.g. |
| 173 |
// layerDigest := digest.FromString("") |
| 174 |
func MeasureLatencyInMicroseconds(operation string, layer digest.Digest, start time.Time) { |
| 175 |
operationLatencyMicroseconds.WithLabelValues(operation, layer.String()).Observe(sinceInMicroseconds(start)) |
| 176 |
} |
| 177 |
|
| 178 |
// IncOperationCount wraps the labels attachment as well as calling Inc into a single method. |
| 179 |
func IncOperationCount(operation string, layer digest.Digest) { |
| 180 |
operationCount.WithLabelValues(operation, layer.String()).Inc() |
| 181 |
} |
| 182 |
|
| 183 |
// AddBytesCount wraps the labels attachment as well as calling Add into a single method. |
| 184 |
func AddBytesCount(operation string, layer digest.Digest, bytes int64) { |
| 185 |
bytesCount.WithLabelValues(operation, layer.String()).Add(float64(bytes)) |
| 186 |
} |
| 187 |
|
| 188 |
// WriteLatencyLogValue wraps writing the log info record for latency in milliseconds. The log record breaks down by operation and layer digest. |
| 189 |
func WriteLatencyLogValue(ctx context.Context, layer digest.Digest, operation string, start time.Time) { |
| 190 |
ctx = log.WithLogger(ctx, log.G(ctx).WithField("metrics", "latency").WithField("operation", operation).WithField("layer_sha", layer.String())) |
| 191 |
log.G(ctx).Logf(logLevel, "value=%v milliseconds", sinceInMilliseconds(start)) |
| 192 |
} |
| 193 |
|
| 194 |
// WriteLatencyWithBytesLogValue wraps writing the log info record for latency in milliseconds with adding the size in bytes. |
| 195 |
// The log record breaks down by operation, layer digest and byte value. |
| 196 |
func WriteLatencyWithBytesLogValue(ctx context.Context, layer digest.Digest, latencyOperation string, start time.Time, bytesMetricName string, bytesMetricValue int64) { |
| 197 |
ctx = log.WithLogger(ctx, log.G(ctx).WithField("metrics", "latency").WithField("operation", latencyOperation).WithField("layer_sha", layer.String())) |
| 198 |
log.G(ctx).Logf(logLevel, "value=%v milliseconds; %v=%v bytes", sinceInMilliseconds(start), bytesMetricName, bytesMetricValue) |
| 199 |
} |
| 200 |
|
| 201 |
// LogLatencyForLastOnDemandFetch implements a special case for measuring the latency of last on demand fetch, which must be invoked at the end of |
| 202 |
// background fetch operation only. Since this is expected to happen only once per container launch, it writes a log line, |
| 203 |
// instead of directly emitting a metric. |
| 204 |
// We do that in the following way: |
| 205 |
// 1. We record the mount start time |
| 206 |
// 2. We constantly record the timestamps when we do on demand fetch for each layer sha |
| 207 |
// 3. On background fetch completed we measure the difference between the last on demand fetch and mount start time |
| 208 |
// and record it as a metric |
| 209 |
func LogLatencyForLastOnDemandFetch(ctx context.Context, layer digest.Digest, start time.Time, end time.Time) { |
| 210 |
diffInMilliseconds := float64(end.Sub(start).Milliseconds()) |
| 211 |
// value can be negative if we pass the default value for time.Time as `end` |
| 212 |
// this can happen if there were no on-demand fetch for the particular layer |
| 213 |
if diffInMilliseconds > 0 { |
| 214 |
ctx = log.WithLogger(ctx, log.G(ctx).WithField("metrics", "latency").WithField("operation", MountLayerToLastOnDemandFetch).WithField("layer_sha", layer.String())) |
| 215 |
log.G(ctx).Logf(logLevel, "value=%v milliseconds", diffInMilliseconds) |
| 216 |
} |
| 217 |
} |