Skip to content

chore: Enhances User-Agent with additional metadata support #3463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

EspenAlbert
Copy link
Collaborator

@EspenAlbert EspenAlbert commented Jul 3, 2025

Description

This PR introduces enhanced user agent context propagation and analytics metadata for all resource, data source, and function operations. The changes include:

  • Addition of the UserAgentExtra to internal/config/transport.go struct and related helpers to capture operation type, name, and action for every provider call.
  • Addition of the WrappedProviderServer in internal/provider/wrapper_provider_server.go. A new file that wraps the provider server to inject detailed operation metadata into the context for every gRPC entrypoint. This wrapper ensures that all resource, data source, and function operations are enriched with type, name, and operation metadata, enabling improved observability, analytics, and debugging. The wrapper works by intercepting each call and using AddUserAgentExtra.
  • New TFSrcUserAgentAdder HTTP middleware that reads the UserAgentExtra and updates the request header.

These changes improve observability, analytics, and debugging capabilities by making operation context available for logging and downstream systems.

Link to any related issue(s): CLOUDP-329015

Sequence diagram

sequenceDiagram
    participant Terraform as Terraform CLI/Cloud
    participant gRPC as gRPC Server
    participant Wrapped as WrappedProviderServer
    participant Old as OldProviderServer
    participant Logic as Resource/Data Logic
    participant HTTP as HTTP RoundTripper/API

    Terraform->>gRPC: gRPC Request (e.g., ReadResource)
    gRPC->>Wrapped: Call e.g. ReadResource(ctx, req)
    Wrapped->>Wrapped: Build UserAgentExtra (type, name, operation)
    Wrapped->>Wrapped: AddUserAgentExtra(ctx, extra)
    Wrapped->>Old: Call OldProviderServer.ReadResource(ctx, req)
    Old->>Logic: Resource/Data Logic (uses ctx)
    Logic->>HTTP: Outgoing API call with User-Agent (from ctx)
    HTTP-->>Logic: API Response
    Logic-->>Old: Resource/Data Response
    Old-->>Wrapped: Response
    Wrapped-->>gRPC: Response
    gRPC-->>Terraform: Response
Loading
  1. gRPC request arrives
    A Terraform CLI or Cloud call triggers a gRPC request to the provider (for example, ReadResource: internal/provider/wrapper_provider_server.go, PlanResourceChange internal/provider/wrapper_provider_server.go, etc.).

  2. WrappedProviderServer internal/provider/wrapper_provider_server.go receives the request
    Instead of going directly to the “old” provider server, the request first hits the new WrappedProviderServer (see internal/provider/wrapper_provider_server.go)

  3. AddUserAgentExtra internal/config/transport.go is called
    The wrapper method (such as ReadResource internal/provider/wrapper_provider_server.go) creates a UserAgentExtra internal/config/transport.go struct. This struct includes:

    • The type of operation (Resource, Datasource, Function, Ephemeral)
    • The name of the resource/data/function (from the request)
    • The operation (the gRPC method name, e.g., ReadResource internal/provider/wrapper_provider_server.go)

    It then calls AddUserAgentExtra(ctx, extra) internal/config/transport.go to add this info to the context.

  4. Delegation to the old server
    The wrapper method calls the original provider server’s method, passing along the new context with the extra metadata.

  5. Downstream usage
    Any logic, logging, or HTTP requests made further down the stack (including SDK calls) can now read this metadata from the context.
    For example, the HTTP roundtripper can add this info to the User-Agent header for all outgoing API requests.

  6. Result returned
    The response is returned up the stack, now with enriched context and observability.

Type of change:

  • New feature (non-breaking change which adds functionality). Please, add the "enhancement" label to the PR. A migration guide must be created or updated if the new feature will go in a major version.
  • Bug fix (non-breaking change which fixes an issue). Please, add the "bug" label to the PR.
  • Breaking change (fix or feature that would cause existing functionality to not work as expected). Please, add the "breaking change" label to the PR. A migration guide must be created or updated.
  • This change requires a documentation update
  • Documentation fix/enhancement

Required Checklist:

  • I have signed the MongoDB CLA
  • I have read the contributing guides
  • I have checked that this change does not generate any credentials and that they are NOT accidentally logged anywhere.
  • I have added tests that prove my fix is effective or that my feature works per HashiCorp requirements
  • I have added any necessary documentation (if appropriate)
  • I have run make fmt and formatted my code
  • If changes include deprecations or removals I have added appropriate changelog entries.
  • If changes include removal or addition of 3rd party GitHub actions, I updated our internal document. Reach out to the APIx Integration slack channel to get access to the internal document.

@EspenAlbert EspenAlbert added the not_stale Not stale issue or PR label Jul 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
not_stale Not stale issue or PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant