|
| 1 | +#!/usr/bin/env python |
| 2 | +"""Capture user-supplied connection-identifier provenance for in-band telemetry. |
| 3 | +
|
| 4 | +The shape captured here is consumed by ``SnowflakeConnection._log_connection_identifier_shape`` |
| 5 | +and emitted as a single ``client_connection_identifier_shape`` telemetry event |
| 6 | +per successful login. The capture function inspects the raw kwargs passed to |
| 7 | +``SnowflakeConnection.__config`` before any normalization (host inference, |
| 8 | +account stripping of ``.global``, region extraction from dotted account) runs, |
| 9 | +so the shape reflects user intent rather than the final post-normalization |
| 10 | +state of the connection. |
| 11 | +
|
| 12 | +Removal of this module and the emission it backs is tracked in SNOW-3548350 |
| 13 | +(target: 2026-11-30). |
| 14 | +""" |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +from dataclasses import dataclass |
| 18 | +from typing import Any, Mapping |
| 19 | + |
| 20 | +from .telemetry import TelemetryField |
| 21 | + |
| 22 | + |
| 23 | +@dataclass(frozen=True) |
| 24 | +class ConnectionIdentifierShape: |
| 25 | + """Provenance of connection-identifier fields the user supplied. |
| 26 | +
|
| 27 | + All fields describe what the user supplied at the moment of input — they |
| 28 | + reflect intent, not the final post-normalization state of the connection. |
| 29 | +
|
| 30 | + - ``account_provided``: the user explicitly set the ``account`` parameter |
| 31 | + (via ``connect(account=...)``, kwargs, or ``connections.toml`` merged |
| 32 | + into kwargs before ``__config`` runs). |
| 33 | + - ``account_with_region``: the raw account string the user typed contained |
| 34 | + a dot (e.g. ``"myacct.us-east-1"``), signaling the deprecated |
| 35 | + ``account.region`` embedded form. Set only on the raw input. |
| 36 | + - ``account_org_provided``: the raw account string carried a dash in its |
| 37 | + account portion (e.g. ``"myorg-myacct"``), signaling the org-prefixed |
| 38 | + form. Region-portion dashes (e.g. the ``-east-`` in |
| 39 | + ``"myacct.us-east-1"``) are intentionally not counted; only the portion |
| 40 | + before the first ``.`` is examined. |
| 41 | + - ``region_provided``: the user explicitly set the ``region`` parameter as |
| 42 | + a distinct kwarg. A region embedded inside a dotted account string is |
| 43 | + NOT ``region_provided``; that's ``account_with_region``. |
| 44 | + - ``host_provided``: the user explicitly set the ``host`` parameter. |
| 45 | + """ |
| 46 | + |
| 47 | + account_provided: bool = False |
| 48 | + account_with_region: bool = False |
| 49 | + account_org_provided: bool = False |
| 50 | + region_provided: bool = False |
| 51 | + host_provided: bool = False |
| 52 | + |
| 53 | + |
| 54 | +def _is_user_supplied_string(value: Any) -> bool: |
| 55 | + """A kwarg counts as user-supplied iff it's a non-empty string. Non-string |
| 56 | + truthy values (e.g. an accidentally-passed ``True`` / ``int``) are not |
| 57 | + treated as provided here — the regular ``__config`` validation will warn |
| 58 | + or error on them later, and shape capture is best kept conservative.""" |
| 59 | + return isinstance(value, str) and value != "" |
| 60 | + |
| 61 | + |
| 62 | +def record_input_shape(kwargs: Mapping[str, Any]) -> ConnectionIdentifierShape: |
| 63 | + """Capture the connection-identifier shape from the raw kwargs that |
| 64 | + ``SnowflakeConnection.__config`` receives. |
| 65 | +
|
| 66 | + Must be invoked before any normalization (the ``setattr`` loop in |
| 67 | + ``__config``, the ``construct_hostname`` call for host inference, or any |
| 68 | + ``parse_account`` stripping) — otherwise inferred values are |
| 69 | + indistinguishable from user-supplied ones and ``host_provided`` / |
| 70 | + ``account_provided`` are no longer trustworthy. |
| 71 | + """ |
| 72 | + account = kwargs.get("account") |
| 73 | + region = kwargs.get("region") |
| 74 | + host = kwargs.get("host") |
| 75 | + |
| 76 | + account_provided = _is_user_supplied_string(account) |
| 77 | + account_with_region = False |
| 78 | + account_org_provided = False |
| 79 | + if account_provided: |
| 80 | + # Only a dot at position > 0 splits the string into account / region; |
| 81 | + # a leading dot (pathological input like ``.us-east-1``) leaves the |
| 82 | + # whole string as the account portion. Mirrors gosnowflake's |
| 83 | + # ``recordAccountShape`` (internal/config/dsn.go), which gates on |
| 84 | + # ``i > 0`` so the dash search runs against the full raw value when |
| 85 | + # there is no real account/region split. |
| 86 | + dot_index = account.find(".") |
| 87 | + if dot_index > 0: |
| 88 | + account_with_region = True |
| 89 | + account_portion = account[:dot_index] |
| 90 | + else: |
| 91 | + account_portion = account |
| 92 | + # ``Contains(accountPortion, "-")`` in Go — any dash anywhere in the |
| 93 | + # account portion (including position 0) flips the flag. The |
| 94 | + # region-tail dashes are excluded by virtue of being outside |
| 95 | + # ``account_portion``, not by a position check. |
| 96 | + account_org_provided = "-" in account_portion |
| 97 | + |
| 98 | + return ConnectionIdentifierShape( |
| 99 | + account_provided=account_provided, |
| 100 | + account_with_region=account_with_region, |
| 101 | + account_org_provided=account_org_provided, |
| 102 | + region_provided=_is_user_supplied_string(region), |
| 103 | + host_provided=_is_user_supplied_string(host), |
| 104 | + ) |
| 105 | + |
| 106 | + |
| 107 | +def build_shape_telemetry_message(shape: ConnectionIdentifierShape) -> dict[str, str]: |
| 108 | + """Build the wire-format payload for the ``client_connection_identifier_shape`` |
| 109 | + in-band telemetry event from a captured ``ConnectionIdentifierShape``. |
| 110 | +
|
| 111 | + Hoisted out of the sync / async ``_log_connection_identifier_shape`` |
| 112 | + emitters so both branches stay in lockstep — the five payload keys |
| 113 | + (``account_provided``, ``account_with_region``, ``account_org_provided``, |
| 114 | + ``region_provided``, ``host_provided``) and their stringified-lowercase |
| 115 | + boolean values are byte-identical across sibling drivers and must remain |
| 116 | + so. Changing this builder is the only place that affects the wire format. |
| 117 | +
|
| 118 | + Booleans are stringified as lowercase ``"true"`` / ``"false"`` (matching |
| 119 | + JSON-style boolean text) for cross-driver parity with gosnowflake's |
| 120 | + ``strconv.FormatBool`` and the JDBC / Node.js siblings. |
| 121 | +
|
| 122 | + TODO(SNOW-3548350): remove with the telemetry emission |
| 123 | + (target: 2026-11-30). |
| 124 | + """ |
| 125 | + return { |
| 126 | + TelemetryField.KEY_TYPE.value: TelemetryField.CONNECTION_IDENTIFIER_SHAPE.value, |
| 127 | + TelemetryField.KEY_ACCOUNT_PROVIDED.value: str(shape.account_provided).lower(), |
| 128 | + TelemetryField.KEY_ACCOUNT_WITH_REGION.value: str( |
| 129 | + shape.account_with_region |
| 130 | + ).lower(), |
| 131 | + TelemetryField.KEY_ACCOUNT_ORG_PROVIDED.value: str( |
| 132 | + shape.account_org_provided |
| 133 | + ).lower(), |
| 134 | + TelemetryField.KEY_REGION_PROVIDED.value: str(shape.region_provided).lower(), |
| 135 | + TelemetryField.KEY_HOST_PROVIDED.value: str(shape.host_provided).lower(), |
| 136 | + } |
0 commit comments