external-dns/provider/aws/instrumented_config.go

104 lines
3.2 KiB
Go

/*
Copyright 2025 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package aws
import (
"context"
"fmt"
"time"
"github.com/aws/smithy-go/middleware"
smithyhttp "github.com/aws/smithy-go/transport/http"
"github.com/prometheus/client_golang/prometheus"
extdnshttp "sigs.k8s.io/external-dns/pkg/http"
)
type requestMetrics struct {
StartTime time.Time
}
type requestMetricsKey struct{}
func getRequestMetric(ctx context.Context) requestMetrics {
requestMetrics, _ := middleware.GetStackValue(ctx, requestMetricsKey{}).(requestMetrics)
return requestMetrics
}
func setRequestMetric(ctx context.Context, requestMetrics requestMetrics) context.Context {
return middleware.WithStackValue(ctx, requestMetricsKey{}, requestMetrics)
}
var initializeTimedOperationMiddleware = middleware.InitializeMiddlewareFunc("timedOperation", func(
ctx context.Context, in middleware.InitializeInput, next middleware.InitializeHandler,
) (middleware.InitializeOutput, middleware.Metadata, error) {
requestMetrics := requestMetrics{}
requestMetrics.StartTime = time.Now()
ctx = setRequestMetric(ctx, requestMetrics)
return next.HandleInitialize(ctx, in)
})
var extractAWSRequestParameters = middleware.DeserializeMiddlewareFunc("extractAWSRequestParameters", func(
ctx context.Context, in middleware.DeserializeInput, next middleware.DeserializeHandler,
) (middleware.DeserializeOutput, middleware.Metadata, error) {
// Call the next middleware first to get the response
out, metadata, err := next.HandleDeserialize(ctx, in)
requestMetrics := getRequestMetric(ctx)
var host, scheme, method, path, status string
if req, ok := in.Request.(*smithyhttp.Request); ok && req != nil {
host = req.URL.Host
scheme = req.URL.Scheme
method = req.Method
path = req.URL.Path
}
// Try to access HTTP response and status code
if resp, ok := out.RawResponse.(*smithyhttp.Response); ok && resp != nil {
status = fmt.Sprintf("%d", resp.StatusCode)
}
labels := prometheus.Labels{
"scheme": scheme,
"host": host,
"path": extdnshttp.PathProcessor(path),
"method": method,
"status": status,
}
extdnshttp.RequestDuration.With(labels).Observe(time.Since(requestMetrics.StartTime).Seconds())
return out, metadata, err
})
func GetInstrumentationMiddlewares() []func(*middleware.Stack) error {
return []func(s *middleware.Stack) error{
func(s *middleware.Stack) error {
if err := s.Initialize.Add(initializeTimedOperationMiddleware, middleware.Before); err != nil {
return fmt.Errorf("error adding timedOperationMiddleware: %w", err)
}
if err := s.Deserialize.Add(extractAWSRequestParameters, middleware.After); err != nil {
return fmt.Errorf("error adding extractAWSRequestParameters: %w", err)
}
return nil
},
}
}