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 3 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 UserAgentTransport 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
    autonumber
    participant Terraform as Terraform CLI/Cloud
    participant Wrapped as WrappedProviderServer
    participant Old as OldProviderServer
    participant Logic as Resource/Data Logic
    participant HTTP as HTTP RoundTripper/API

    Terraform->>Wrapped: gRPC Request (e.g., ReadResource)
    Wrapped->>Wrapped: AddUserAgentExtra(ctx, type, name, operation)
    Wrapped->>Old: Call OldProviderServer.ReadResource(ctx, req)
    Old->>Logic: Resource/Data Logic (uses ctx)
    Logic->>HTTP: HTTP Request with context    
    HTTP->>HTTP: UserAgentTransport:<br/>Read UserAgentExtra from ctx<br/>Modify User-Agent header
    HTTP->>HTTP: TerraformLoggingTransport:<br/>Log request with final User-Agent
    HTTP->>HTTP: DigestTransport:<br/>Prepare for digest auth
    HTTP->>HTTP: Rest of the chain
    HTTP-->>Logic: API Response
    Logic-->>Old: Resource/Data Response
    Old-->>Wrapped: Response
    Wrapped-->>Terraform: Response
Loading
  1. Terraform CLI initiates the grpc call
  2. gRPC request is received by the WrappedProviderServer.{FuncName} (internal/provider/wrapper_provider_server.go) which adds AddUserAgentExtra (internal/config/transport.go)
    • 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)
  3. It then calls the OldProviderServer.{FuncName}(updated_ctx, req) (the new context has the extra metadata)
  4. Any Resource/Data source is called as before.
    Any logic, logging, or HTTP requests made further down the stack (including SDK calls) can now read this metadata from the context.
  5. For any API Call the Transport chain processes the API Calls.
  6. UserAgentTransport reads the ctx and updates the user agent
  7. Processing as before

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
@EspenAlbert EspenAlbert marked this pull request as ready for review July 31, 2025 05:56
@Copilot Copilot AI review requested due to automatic review settings July 31, 2025 05:56
@EspenAlbert EspenAlbert requested a review from a team as a code owner July 31, 2025 05:56
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances user agent functionality by adding comprehensive operation metadata support to all provider operations. The changes enable detailed tracking and analytics by propagating operation type, name, and action information through context to HTTP requests.

  • Addition of UserAgentExtra struct and related utilities for capturing operation metadata
  • Implementation of a WrappedProviderServer that intercepts all gRPC calls to inject operation context
  • Integration of UserAgentTransport middleware to modify HTTP headers based on context metadata

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
internal/config/transport.go Adds UserAgentExtra struct, context utilities, and UserAgentTransport middleware
internal/config/transport_test.go Comprehensive test coverage for new user agent functionality
internal/provider/wrapper_provider_server.go New wrapper that intercepts all provider operations to inject metadata
internal/provider/provider.go Updates provider factory to use the new wrapped server
internal/config/client.go Integrates UserAgentTransport into the HTTP transport chain
Comments suppressed due to low confidence (1)

internal/config/transport.go:110

  • [nitpick] The variable name newVar is unclear and doesn't describe its purpose. Consider renaming to updatedUserAgent or enhancedUserAgent.
		newVar := extra.ToHeaderValue(userAgent)

@marcosuma
Copy link
Collaborator

I am guessing we are going to wait for checking for other use cases and ask Hashicorp, yes?

@EspenAlbert
Copy link
Collaborator Author

I am guessing we are going to wait for checking for other use cases and ask Hashicorp, yes?

Yes, putting this back to draft meanwhile

@EspenAlbert EspenAlbert marked this pull request as draft August 1, 2025 13:33
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.

2 participants