Skip to content

Commit 8bc95b7

Browse files
denikIsaac
andcommitted
acc: register exact cmd-exec-id and dynamic values via add_repl
The global regex now masks only the cmd-exec-id user agent token. The telemetry cmd_exec_id field is replaced only via the exact id that extract_command_exec_id.py reads from the user agent and registers with add_repl, so goldens prove the two match. Telemetry scripts also use add_repl instead of update_file.py for bundle uuid and deployment id. Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 696c5a1 commit 8bc95b7

15 files changed

Lines changed: 82 additions & 26 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Print the cmd-exec-id from the first recorded request that has one in its User-Agent, and update
4+
ACC_REPLS to replace it with [CMD_EXEC_ID_HASH]. The telemetry cmd_exec_id field is then only
5+
replaced if it matches the id the CLI actually sent.
6+
7+
Requires IncludeRequestHeaders = ["User-Agent"].
8+
"""
9+
10+
import json
11+
import os
12+
import re
13+
import sys
14+
from pathlib import Path
15+
16+
sys.path.insert(0, str(Path(__file__).parent))
17+
from add_repl import add_repl
18+
19+
20+
def extract_cmd_exec_id():
21+
requests_file = Path(os.environ["OUT_REQUESTS"])
22+
23+
# Read JSON objects one at a time and find the first one with a cmd-exec-id
24+
# in the User-Agent header. Some requests (e.g. .well-known/databricks-config)
25+
# are made before the command execution context is set and lack cmd-exec-id.
26+
with requests_file.open("r") as f:
27+
json_str = ""
28+
while True:
29+
line = f.readline()
30+
if not line:
31+
break
32+
33+
json_str += line
34+
try:
35+
data = json.loads(json_str)
36+
except json.JSONDecodeError:
37+
continue
38+
39+
# Reset for next JSON object
40+
json_str = ""
41+
42+
user_agent = data.get("headers", {}).get("User-Agent", [""])[0]
43+
match = re.search(r"cmd-exec-id/([^\s]+)", user_agent)
44+
if match:
45+
return match.group(1)
46+
47+
raise SystemExit(f"No command execution ID found in any request in {requests_file}")
48+
49+
50+
if __name__ == "__main__":
51+
exec_id = extract_cmd_exec_id()
52+
add_repl(exec_id, "CMD_EXEC_ID_HASH")
53+
print(exec_id)

‎acceptance/bundle/telemetry/deploy-error/script‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ errcode trace $CLI bundle deploy
22

33
trace cat "$OUT_REQUESTS" | jq 'select(has("path") and .path == "/telemetry-ext") | .body.protoLogs[] | fromjson | .entry.databricks_cli_log | {execution_context}' > out.telemetry.txt
44

5-
5+
extract_command_exec_id.py > /dev/null
66
rm "$OUT_REQUESTS"

‎acceptance/bundle/telemetry/deploy/script‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,9 @@ cat telemetry.json | jq '[.entry.databricks_cli_log.bundle_deploy_event.experime
3737
# the sync.paths restriction in databricks.yml (see the comment there).
3838
cat telemetry.json | jq 'del(.entry.databricks_cli_log.bundle_deploy_event.experimental.bundle_mutator_execution_time_ms, .entry.databricks_cli_log.bundle_deploy_event.resources_metadata) | .entry.databricks_cli_log.bundle_deploy_event.experimental.bool_values |= map(select(.key | (startswith("direct_drymigrate_") or startswith("engine_terraform_")) | not))' > out.telemetry.txt
3939

40-
deployment_id=$(cat .databricks/bundle/default/deployment.json | jq -r .id)
41-
42-
update_file.py out.telemetry.txt "11111111-2222-3333-4444-555555555555" '[BUNDLE_UUID]'
43-
update_file.py out.telemetry.txt $deployment_id '[DEPLOYMENT_ID]'
40+
extract_command_exec_id.py > /dev/null
41+
add_repl "11111111-2222-3333-4444-555555555555" BUNDLE_UUID
42+
add_repl "$(jq -r .id .databricks/bundle/default/deployment.json)" DEPLOYMENT_ID
4443

4544
rm "$OUT_REQUESTS"
4645
rm telemetry.json

‎acceptance/bundle/templates/telemetry/custom-template/script‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ trace $CLI bundle init . --config-file input.json --output-dir output
55
mv output/my_custom_template/databricks.yml out.databricks.yml
66
rm -r output
77

8-
bundle_uuid=$(cat out.databricks.yml | grep -o 'uuid: [^\n]*' | cut -d ' ' -f2)
9-
10-
update_file.py "$OUT_REQUESTS" $bundle_uuid '[BUNDLE-UUID]'
11-
update_file.py out.databricks.yml $bundle_uuid '[BUNDLE-UUID]'
8+
extract_command_exec_id.py > /dev/null
9+
add_repl "$(cat out.databricks.yml | grep -o 'uuid: [^\n]*' | cut -d ' ' -f2)" BUNDLE-UUID
1210

1311
# pretty print the telemetry payload.
1412
trace cat "$OUT_REQUESTS" | jq 'select(has("path") and .path == "/telemetry-ext") | .body.protoLogs[] | fromjson'

‎acceptance/bundle/templates/telemetry/dbt-sql/script‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ $CLI bundle init dbt-sql --config-file input.json --output-dir output
55
mv output/my_dbt_sql/databricks.yml out.databricks.yml
66
rm -r output
77

8-
bundle_uuid=$(cat out.databricks.yml | grep -o 'uuid: [^\n]*' | cut -d ' ' -f2)
9-
10-
update_file.py "$OUT_REQUESTS" $bundle_uuid '[BUNDLE-UUID]'
11-
update_file.py out.databricks.yml $bundle_uuid '[BUNDLE-UUID]'
8+
extract_command_exec_id.py > /dev/null
9+
add_repl "$(cat out.databricks.yml | grep -o 'uuid: [^\n]*' | cut -d ' ' -f2)" BUNDLE-UUID
1210

1311
# pretty print the telemetry payload.
1412
trace cat "$OUT_REQUESTS" | jq 'select(has("path") and .path == "/telemetry-ext") | .body.protoLogs[] | fromjson'

‎acceptance/bundle/templates/telemetry/default-python/script‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ $CLI bundle init default-python --config-file input.json --output-dir output
55
mv output/my_default_python/databricks.yml out.databricks.yml
66
rm -r output
77

8-
bundle_uuid=$(cat out.databricks.yml | grep -o 'uuid: [^\n]*' | cut -d ' ' -f2)
9-
10-
update_file.py "$OUT_REQUESTS" $bundle_uuid '[BUNDLE-UUID]'
11-
update_file.py out.databricks.yml $bundle_uuid '[BUNDLE-UUID]'
8+
extract_command_exec_id.py > /dev/null
9+
add_repl "$(cat out.databricks.yml | grep -o 'uuid: [^\n]*' | cut -d ' ' -f2)" BUNDLE-UUID
1210

1311
# pretty print the telemetry payload.
1412
trace cat "$OUT_REQUESTS" | jq 'select(has("path") and .path == "/telemetry-ext") | .body.protoLogs[] | fromjson'

‎acceptance/bundle/templates/telemetry/default-sql/script‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ $CLI bundle init default-sql --config-file input.json --output-dir output
55
mv output/my_default_sql/databricks.yml out.databricks.yml
66
rm -r output
77

8-
bundle_uuid=$(cat out.databricks.yml | grep -o 'uuid: [^\n]*' | cut -d ' ' -f2)
9-
10-
update_file.py "$OUT_REQUESTS" $bundle_uuid '[BUNDLE-UUID]'
11-
update_file.py out.databricks.yml $bundle_uuid '[BUNDLE-UUID]'
8+
extract_command_exec_id.py > /dev/null
9+
add_repl "$(cat out.databricks.yml | grep -o 'uuid: [^\n]*' | cut -d ' ' -f2)" BUNDLE-UUID
1210

1311
# pretty print the telemetry payload.
1412
trace cat "$OUT_REQUESTS" | jq 'select(has("path") and .path == "/telemetry-ext") | .body.protoLogs[] | fromjson'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
trace $CLI selftest send-telemetry --debug
2+
extract_command_exec_id.py > /dev/null
23

34
# pretty print the telemetry payload.
45
trace cat "$OUT_REQUESTS" | jq 'select(has("path") and .path == "/telemetry-ext") | .body.protoLogs[] | fromjson'

‎acceptance/telemetry/failure/test.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
IncludeRequestHeaders = ["User-Agent"]
2+
13
[[Server]]
24
Pattern = "POST /telemetry-ext"
35
Response.Body = '''
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
trace $CLI selftest send-telemetry --debug
2+
extract_command_exec_id.py > /dev/null
23

34
# pretty print the telemetry payload.
45
trace cat "$OUT_REQUESTS" | jq 'select(has("path") and .path == "/telemetry-ext") | .body.protoLogs[] | fromjson'

0 commit comments

Comments
 (0)