|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes 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 metrics provides a way to capture request metrics. |
| 18 | +package metricsv2 |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "errors" |
| 23 | + "fmt" |
| 24 | + "net/http" |
| 25 | + "strconv" |
| 26 | + "time" |
| 27 | + |
| 28 | + awsmiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware" |
| 29 | + "github.com/aws/smithy-go" |
| 30 | + "github.com/aws/smithy-go/middleware" |
| 31 | + smithyhttp "github.com/aws/smithy-go/transport/http" |
| 32 | + "github.com/prometheus/client_golang/prometheus" |
| 33 | + "k8s.io/apimachinery/pkg/runtime" |
| 34 | + "sigs.k8s.io/controller-runtime/pkg/metrics" |
| 35 | + |
| 36 | + "sigs.k8s.io/cluster-api-provider-aws/v2/pkg/record" |
| 37 | + "sigs.k8s.io/cluster-api-provider-aws/v2/version" |
| 38 | +) |
| 39 | + |
| 40 | +const ( |
| 41 | + metricAWSSubsystem = "aws" |
| 42 | + metricRequestCountKey = "api_requests_total_v2" |
| 43 | + metricRequestDurationKey = "api_request_duration_seconds_v2" |
| 44 | + metricAPICallRetries = "api_call_retries_v2" |
| 45 | + metricServiceLabel = "service" |
| 46 | + metricRegionLabel = "region" |
| 47 | + metricOperationLabel = "operation" |
| 48 | + metricControllerLabel = "controller" |
| 49 | + metricStatusCodeLabel = "status_code" |
| 50 | + metricErrorCodeLabel = "error_code" |
| 51 | +) |
| 52 | + |
| 53 | +var ( |
| 54 | + awsRequestCount = prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 55 | + Subsystem: metricAWSSubsystem, |
| 56 | + Name: metricRequestCountKey, |
| 57 | + Help: "Total number of AWS requests", |
| 58 | + }, []string{metricControllerLabel, metricServiceLabel, metricRegionLabel, metricOperationLabel, metricStatusCodeLabel, metricErrorCodeLabel}) |
| 59 | + awsRequestDurationSeconds = prometheus.NewHistogramVec(prometheus.HistogramOpts{ |
| 60 | + Subsystem: metricAWSSubsystem, |
| 61 | + Name: metricRequestDurationKey, |
| 62 | + Help: "Latency of HTTP requests to AWS", |
| 63 | + }, []string{metricControllerLabel, metricServiceLabel, metricRegionLabel, metricOperationLabel}) |
| 64 | + awsCallRetries = prometheus.NewHistogramVec(prometheus.HistogramOpts{ |
| 65 | + Subsystem: metricAWSSubsystem, |
| 66 | + Name: metricAPICallRetries, |
| 67 | + Help: "Number of retries made against an AWS API", |
| 68 | + Buckets: []float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, |
| 69 | + }, []string{metricControllerLabel, metricServiceLabel, metricRegionLabel, metricOperationLabel}) |
| 70 | + getRawResponse = func(metadata middleware.Metadata) *http.Response { |
| 71 | + switch res := awsmiddleware.GetRawResponse(metadata).(type) { |
| 72 | + case *http.Response: |
| 73 | + return res |
| 74 | + default: |
| 75 | + return nil |
| 76 | + } |
| 77 | + } |
| 78 | +) |
| 79 | + |
| 80 | +func init() { |
| 81 | + metrics.Registry.MustRegister(awsRequestCount) |
| 82 | + metrics.Registry.MustRegister(awsRequestDurationSeconds) |
| 83 | + metrics.Registry.MustRegister(awsCallRetries) |
| 84 | +} |
| 85 | + |
| 86 | +type requestContextKey struct{} |
| 87 | + |
| 88 | +type RequestData struct { |
| 89 | + RequestStartTime time.Time |
| 90 | + RequestEndTime time.Time |
| 91 | + StatusCode int |
| 92 | + ErrorCode string |
| 93 | + RequestCount int |
| 94 | + Service string |
| 95 | + OperationName string |
| 96 | + Region string |
| 97 | + UserAgent string |
| 98 | + Controller string |
| 99 | + Target runtime.Object |
| 100 | + Attempts int |
| 101 | +} |
| 102 | + |
| 103 | +// Inspired by https://github.com/jonathan-innis/aws-sdk-go-prometheus/v2 |
| 104 | +func WithMiddlewares(controller string, target runtime.Object) func(stack *middleware.Stack) error { |
| 105 | + return func(stack *middleware.Stack) error { |
| 106 | + if err := stack.Initialize.Add(getMetricCollectionMiddleware(controller, target), middleware.Before); err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + if err := stack.Build.Add(getAddToUserAgentMiddleware(), middleware.Before); err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + if err := stack.Finalize.Add(getRequestMetricContextMiddleware(), middleware.Before); err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + if err := stack.Finalize.Insert(getAttemptContextMiddleware(), "Retry", middleware.After); err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + if err := stack.Deserialize.Add(getRecordAWSPermissionsIssueMiddleware(target), middleware.After); err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + return nil |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func getMetricCollectionMiddleware(controller string, target runtime.Object) middleware.InitializeMiddleware { |
| 126 | + return middleware.InitializeMiddlewareFunc("capa/MetricCollectionMiddleware", func(ctx context.Context, input middleware.InitializeInput, handler middleware.InitializeHandler) (middleware.InitializeOutput, middleware.Metadata, error) { |
| 127 | + ctx = initRequestContext(ctx, controller, target) |
| 128 | + request := getContext(ctx) |
| 129 | + |
| 130 | + request.RequestStartTime = time.Now().UTC() |
| 131 | + out, metadata, err := handler.HandleInitialize(ctx, input) |
| 132 | + request.RequestEndTime = time.Now().UTC() |
| 133 | + |
| 134 | + request.CaptureRequestMetrics() |
| 135 | + |
| 136 | + return out, metadata, err |
| 137 | + }) |
| 138 | +} |
| 139 | + |
| 140 | +func getRequestMetricContextMiddleware() middleware.FinalizeMiddleware { |
| 141 | + return middleware.FinalizeMiddlewareFunc("capa/RequestMetricContextMiddleware", func(ctx context.Context, input middleware.FinalizeInput, handler middleware.FinalizeHandler) (middleware.FinalizeOutput, middleware.Metadata, error) { |
| 142 | + request := getContext(ctx) |
| 143 | + request.Service = awsmiddleware.GetServiceID(ctx) |
| 144 | + request.OperationName = awsmiddleware.GetOperationName(ctx) |
| 145 | + request.Region = awsmiddleware.GetRegion(ctx) |
| 146 | + |
| 147 | + return handler.HandleFinalize(ctx, input) |
| 148 | + }) |
| 149 | +} |
| 150 | + |
| 151 | +// For capturing retry count and status codes |
| 152 | +func getAttemptContextMiddleware() middleware.FinalizeMiddleware { |
| 153 | + return middleware.FinalizeMiddlewareFunc("capa/AttemptMetricContextMiddleware", func(ctx context.Context, input middleware.FinalizeInput, handler middleware.FinalizeHandler) (middleware.FinalizeOutput, middleware.Metadata, error) { |
| 154 | + request := getContext(ctx) |
| 155 | + request.Attempts++ |
| 156 | + out, metadata, err := handler.HandleFinalize(ctx, input) |
| 157 | + response := getRawResponse(metadata) |
| 158 | + |
| 159 | + // This will record only last attempts status code. |
| 160 | + // Can be further extended to capture status codes of all attempts |
| 161 | + if response != nil { |
| 162 | + request.StatusCode = response.StatusCode |
| 163 | + } else { |
| 164 | + request.StatusCode = -1 |
| 165 | + } |
| 166 | + |
| 167 | + return out, metadata, err |
| 168 | + }) |
| 169 | +} |
| 170 | + |
| 171 | +func getRecordAWSPermissionsIssueMiddleware(target runtime.Object) middleware.DeserializeMiddleware { |
| 172 | + return middleware.DeserializeMiddlewareFunc("capa/RecordAWSPermissionsIssueMiddleware", func(ctx context.Context, input middleware.DeserializeInput, handler middleware.DeserializeHandler) (middleware.DeserializeOutput, middleware.Metadata, error) { |
| 173 | + r, ok := input.Request.(*smithyhttp.ResponseError) |
| 174 | + if !ok { |
| 175 | + return middleware.DeserializeOutput{}, middleware.Metadata{}, fmt.Errorf("unknown transport type %T", input.Request) |
| 176 | + } |
| 177 | + |
| 178 | + var ae smithy.APIError |
| 179 | + if errors.As(r.Err, &ae) { |
| 180 | + switch ae.ErrorCode() { |
| 181 | + case "AuthFailure", "UnauthorizedOperation", "NoCredentialProviders": |
| 182 | + record.Warnf(target, ae.ErrorCode(), "Operation %s failed with a credentials or permission issue", awsmiddleware.GetOperationName(ctx)) |
| 183 | + } |
| 184 | + } |
| 185 | + return handler.HandleDeserialize(ctx, input) |
| 186 | + }) |
| 187 | +} |
| 188 | + |
| 189 | +func getAddToUserAgentMiddleware() middleware.BuildMiddleware { |
| 190 | + return middleware.BuildMiddlewareFunc("capa/AddUserAgentMiddleware", func(ctx context.Context, input middleware.BuildInput, handler middleware.BuildHandler) (middleware.BuildOutput, middleware.Metadata, error) { |
| 191 | + request := getContext(ctx) |
| 192 | + r, ok := input.Request.(*smithyhttp.Request) |
| 193 | + if !ok { |
| 194 | + return middleware.BuildOutput{}, middleware.Metadata{}, fmt.Errorf("unknown transport type %T", input.Request) |
| 195 | + } |
| 196 | + |
| 197 | + if curUA := r.Header.Get("User-Agent"); curUA != "" { |
| 198 | + request.UserAgent = curUA + " " + request.UserAgent |
| 199 | + } |
| 200 | + r.Header.Set("User-Agent", request.UserAgent) |
| 201 | + |
| 202 | + return handler.HandleBuild(ctx, input) |
| 203 | + }) |
| 204 | +} |
| 205 | + |
| 206 | +func initRequestContext(ctx context.Context, controller string, target runtime.Object) context.Context { |
| 207 | + if middleware.GetStackValue(ctx, requestContextKey{}) == nil { |
| 208 | + ctx = middleware.WithStackValue(ctx, requestContextKey{}, &RequestData{ |
| 209 | + Controller: controller, |
| 210 | + Target: target, |
| 211 | + UserAgent: fmt.Sprintf("aws.cluster.x-k8s.io/%s", version.Get().String()), |
| 212 | + }) |
| 213 | + } |
| 214 | + return ctx |
| 215 | +} |
| 216 | + |
| 217 | +func getContext(ctx context.Context) *RequestData { |
| 218 | + rctx := middleware.GetStackValue(ctx, requestContextKey{}) |
| 219 | + if rctx == nil { |
| 220 | + return nil |
| 221 | + } |
| 222 | + return rctx.(*RequestData) |
| 223 | +} |
| 224 | + |
| 225 | +// CaptureRequestMetrics will monitor and capture request metrics. |
| 226 | +func (r *RequestData) CaptureRequestMetrics() { |
| 227 | + requestDuration := r.RequestStartTime.Sub(r.RequestEndTime) |
| 228 | + retryCount := r.Attempts - 1 |
| 229 | + |
| 230 | + awsRequestCount.WithLabelValues(r.Controller, r.Service, r.Region, r.OperationName, strconv.Itoa(r.StatusCode), r.ErrorCode).Inc() |
| 231 | + awsRequestDurationSeconds.WithLabelValues(r.Controller, r.Service, r.Region, r.OperationName).Observe(requestDuration.Seconds()) |
| 232 | + awsCallRetries.WithLabelValues(r.Controller, r.Service, r.Region, r.OperationName).Observe(float64(retryCount)) |
| 233 | +} |
0 commit comments