|
| 1 | +/* |
| 2 | +Copyright 2025 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 aws |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/aws/smithy-go/middleware" |
| 25 | + smithyhttp "github.com/aws/smithy-go/transport/http" |
| 26 | + |
| 27 | + extdnshttp "sigs.k8s.io/external-dns/pkg/http" |
| 28 | + "sigs.k8s.io/external-dns/pkg/metrics" |
| 29 | +) |
| 30 | + |
| 31 | +type requestMetrics struct { |
| 32 | + StartTime time.Time |
| 33 | +} |
| 34 | + |
| 35 | +type requestMetricsKey struct{} |
| 36 | + |
| 37 | +func getRequestMetric(ctx context.Context) requestMetrics { |
| 38 | + requestMetrics, _ := middleware.GetStackValue(ctx, requestMetricsKey{}).(requestMetrics) |
| 39 | + return requestMetrics |
| 40 | +} |
| 41 | + |
| 42 | +func setRequestMetric(ctx context.Context, requestMetrics requestMetrics) context.Context { |
| 43 | + return middleware.WithStackValue(ctx, requestMetricsKey{}, requestMetrics) |
| 44 | +} |
| 45 | + |
| 46 | +var initializeTimedOperationMiddleware = middleware.InitializeMiddlewareFunc("timedOperation", func( |
| 47 | + ctx context.Context, in middleware.InitializeInput, next middleware.InitializeHandler, |
| 48 | +) (middleware.InitializeOutput, middleware.Metadata, error) { |
| 49 | + requestMetrics := requestMetrics{} |
| 50 | + requestMetrics.StartTime = time.Now() |
| 51 | + ctx = setRequestMetric(ctx, requestMetrics) |
| 52 | + |
| 53 | + return next.HandleInitialize(ctx, in) |
| 54 | +}) |
| 55 | + |
| 56 | +var extractAWSRequestParameters = middleware.DeserializeMiddlewareFunc("extractAWSRequestParameters", func( |
| 57 | + ctx context.Context, in middleware.DeserializeInput, next middleware.DeserializeHandler, |
| 58 | +) (middleware.DeserializeOutput, middleware.Metadata, error) { |
| 59 | + // Call the next middleware first to get the response |
| 60 | + out, metadata, err := next.HandleDeserialize(ctx, in) |
| 61 | + |
| 62 | + requestMetrics := getRequestMetric(ctx) |
| 63 | + |
| 64 | + if req, ok := in.Request.(*smithyhttp.Request); ok && req != nil { |
| 65 | + extdnshttp.RequestDurationLabels.WithOptions( |
| 66 | + metrics.WithLabel("scheme", req.URL.Scheme), |
| 67 | + metrics.WithLabel("host", req.URL.Host), |
| 68 | + metrics.WithLabel("path", metrics.PathProcessor(req.URL.Path)), |
| 69 | + metrics.WithLabel("method", req.Method), |
| 70 | + ) |
| 71 | + } |
| 72 | + |
| 73 | + // Try to access HTTP response and status code |
| 74 | + if resp, ok := out.RawResponse.(*smithyhttp.Response); ok && resp != nil { |
| 75 | + extdnshttp.RequestDurationLabels.WithOptions( |
| 76 | + metrics.WithLabel("status", fmt.Sprintf("%d", resp.StatusCode)), |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + extdnshttp.RequestDurationMetric.SetWithLabels(time.Since(requestMetrics.StartTime).Seconds(), extdnshttp.RequestDurationLabels) |
| 81 | + |
| 82 | + return out, metadata, err |
| 83 | +}) |
| 84 | + |
| 85 | +func GetInstrumentationMiddlewares() []func(*middleware.Stack) error { |
| 86 | + return []func(s *middleware.Stack) error{ |
| 87 | + func(s *middleware.Stack) error { |
| 88 | + if err := s.Initialize.Add(initializeTimedOperationMiddleware, middleware.Before); err != nil { |
| 89 | + return fmt.Errorf("error adding timedOperationMiddleware: %w", err) |
| 90 | + } |
| 91 | + |
| 92 | + if err := s.Deserialize.Add(extractAWSRequestParameters, middleware.After); err != nil { |
| 93 | + return fmt.Errorf("error adding extractAWSRequestParameters: %w", err) |
| 94 | + } |
| 95 | + |
| 96 | + return nil |
| 97 | + }, |
| 98 | + } |
| 99 | +} |
0 commit comments