From 7ca93f76992d230ae3898fcd199cfcc2694e8ccc Mon Sep 17 00:00:00 2001 From: Matt Bush Date: Sun, 21 Apr 2024 22:03:34 -0700 Subject: [PATCH] Revert "Count external API calls." This reverts commit 28c4ff2ac558bc291d763d9c6627de67d6a196a6. Signed-off-by: Matt Bush --- internal/clients/aws.go | 70 ++--------------------------- internal/clients/provider_config.go | 1 - 2 files changed, 4 insertions(+), 67 deletions(-) diff --git a/internal/clients/aws.go b/internal/clients/aws.go index 26825b443b..62e7baebc4 100644 --- a/internal/clients/aws.go +++ b/internal/clients/aws.go @@ -6,16 +6,14 @@ package clients import ( "context" + "reflect" + "unsafe" "github.com/aws/aws-sdk-go-v2/aws" - awsmiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware" "github.com/aws/aws-sdk-go-v2/feature/ec2/imds" "github.com/aws/aws-sdk-go-v2/service/sts" - awsrequest "github.com/aws/aws-sdk-go/aws/request" - "github.com/aws/smithy-go/middleware" "github.com/crossplane/crossplane-runtime/pkg/logging" "github.com/crossplane/crossplane-runtime/pkg/resource" - "github.com/crossplane/upjet/pkg/metrics" "github.com/crossplane/upjet/pkg/terraform" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-provider-aws/xpprovider" @@ -140,48 +138,6 @@ func (m *metaOnlyPrimary) Meta() any { return m.meta } -// withExternalAPICallCounter configures an AWS SDK v2 stack (client) -// with an API call counter. AWS SDK v2 offers configuring -// "middlewares" to customize a request. Middlewares can be plugged -// into different steps of the stack. Middlewares can save and access -// metadata in the stack, such as ServiceID (EC2, IAM, etc.) and -// OperationName (DescribeVPCs, etc.). For documentation, see: -// https://aws.github.io/aws-sdk-go-v2/docs/middleware/ -func withExternalAPICallCounter(stack *middleware.Stack) error { - externalAPICallCounterMiddleware := middleware.DeserializeMiddlewareFunc("externalAPICallCounter", - func(ctx context.Context, input middleware.DeserializeInput, next middleware.DeserializeHandler) (middleware.DeserializeOutput, middleware.Metadata, error) { - serviceID := awsmiddleware.GetServiceID(ctx) - operationName := awsmiddleware.GetOperationName(ctx) - - // next.HandleDeserialize() calls the next middleware function - // in the stack, which in turn calls the next. Finally, the - // request is performed. Each middleware function receives the - // output from the middleware function it invoked, processes it, - // and returns its result to the middleware function that - // invoked itself. - output, metadata, err := next.HandleDeserialize(ctx, input) - if err == nil { - metrics.ExternalAPICalls.WithLabelValues(serviceID, operationName).Inc() - } - return output, metadata, err - }, - ) - - // We register the call counter to the end of the deserialization - // step, so that we're right next to Transport handler - // (http.RoundTripper) in the stack (see - // https://aws.github.io/aws-sdk-go-v2/docs/middleware/). In this - // case, it's easy to distinguish API errors from connection - // errors, because only connection errors cause a non-nil error - // returned by next.HandleDeserialize() (see middleware - // implementation above). If we were to register the call counter - // to any other position (such as earlier stack steps (finalize, - // build, etc.) or even the beginning of deserialization step), we - // would have to implement a logic to distinguish between API - // errors and connection errors. - return stack.Deserialize.Add(externalAPICallCounterMiddleware, middleware.After) -} - // configureNoForkAWSClient populates the supplied *terraform.Setup with // Terraform Plugin SDK style AWS client (Meta) and Terraform Plugin Framework // style FrameworkProvider @@ -217,7 +173,8 @@ func configureNoForkAWSClient(ctx context.Context, ps *terraform.Setup, config * // only used for retrieving the ServicePackages from the singleton provider instance p := config.TerraformProvider.Meta() tfAwsConnsClient, diags := tfAwsConnsCfg.GetClient(ctx, &xpprovider.AWSClient{ - ServicePackages: p.(*xpprovider.AWSClient).ServicePackages, + // #nosec G103 + ServicePackages: (*xpprovider.AWSClient)(unsafe.Pointer(reflect.ValueOf(p).Pointer())).ServicePackages, }) if diags.HasError() { return errors.Errorf("cannot construct TF AWS Client from TF AWS Config, %v", diags) @@ -232,24 +189,5 @@ func configureNoForkAWSClient(ctx context.Context, ps *terraform.Setup, config * ps.Meta = tfAwsConnsClient fwProvider := xpprovider.GetFrameworkProviderWithMeta(&metaOnlyPrimary{meta: tfAwsConnsClient}) ps.FrameworkProvider = fwProvider - - // Register AWS SDK v1 call counter. Unlike AWS SDK v2, v1 doesn't - // store service ID (EC2, IAM, etc.) and operation name - // (DescribeVPCs, etc.) in request context. Therefore, it's not - // possible to implement this session handler's functionality with - // an http.RoundTripper. To learn how SDK v1 session handler phases - // map to SDK v2 middleware stack steps, see: - // https://aws.github.io/aws-sdk-go-v2/docs/migrating/#handler-phases - tfAwsConnsClient.Session.Handlers.Send.PushBack(func(r *awsrequest.Request) { - // In case of API errors (or no errors), r.Error is nil. - // In case of connection errors, r.Error is non-nil. - if r.Error == nil { - metrics.ExternalAPICalls.WithLabelValues(r.ClientInfo.ServiceID, r.Operation.Name).Inc() - } - }) - - // Register AWS SDK v2 call counter - tfAwsConnsClient.AppendAPIOptions(withExternalAPICallCounter) - return nil } diff --git a/internal/clients/provider_config.go b/internal/clients/provider_config.go index f941401e25..4e16df5564 100644 --- a/internal/clients/provider_config.go +++ b/internal/clients/provider_config.go @@ -68,7 +68,6 @@ const ( var userAgentV2 = config.WithAPIOptions([]func(*middleware.Stack) error{ awsmiddleware.AddUserAgentKeyValue("upbound-provider-aws", version.Version), awsmiddleware.AddUserAgentKeyValue("crossplane-provider-aws", version.Version), - withExternalAPICallCounter, }) func getRegion(obj runtime.Object) (string, error) {