Skip to content

Commit 6fb92c1

Browse files
authored
metric: Track if artifacts variable references are used (#3368)
## Changes Track if the artifacts variable references are used ## Why This can be a useful insight to see if such variables are used at all, and if not, we might move to resolving such variables altogether. Prompted by #3364 (comment) ## Tests Added an acceptance test <!-- If your PR needs to be included in the release notes for next release, add a separate entry in NEXT_CHANGELOG.md as part of your PR. -->
1 parent 172aec3 commit 6fb92c1

6 files changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
bundle:
2+
name: deploy-artifacts-variables
3+
4+
artifacts:
5+
test_artifact:
6+
type: whl
7+
path: .
8+
build: "echo 'Building...'"
9+
files:
10+
- source: ./databricks.yml
11+
12+
resources:
13+
jobs:
14+
test-job:
15+
name: test-job
16+
17+
targets:
18+
dev: {}
19+
prod:
20+
resources:
21+
jobs:
22+
test-job:
23+
name: "test-job that does ${artifacts.test_artifact.build}"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Local = true
2+
Cloud = false
3+
4+
[EnvMatrix]
5+
DATABRICKS_CLI_DEPLOYMENT = ["terraform", "direct-exp"]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
>>> [CLI] bundle deploy -t dev
3+
Building test_artifact...
4+
Uploading databricks.yml...
5+
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/deploy-artifacts-variables/dev/files...
6+
Deploying resources...
7+
Updating deployment state...
8+
Deployment complete!
9+
10+
=== No artifact metrics expected
11+
>>> cat out.requests.txt
12+
13+
>>> [CLI] bundle deploy -t prod
14+
Building test_artifact...
15+
Uploading databricks.yml...
16+
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/deploy-artifacts-variables/prod/files...
17+
Deploying resources...
18+
Updating deployment state...
19+
Deployment complete!
20+
21+
=== Artifact metrics expected
22+
>>> cat out.requests.txt
23+
{
24+
"key": "artifacts_reference_used",
25+
"value": true
26+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
trace $CLI bundle deploy -t dev
2+
3+
title "No artifact metrics expected"
4+
trace cat out.requests.txt | jq 'select(has("path") and .path == "/telemetry-ext") | .body.protoLogs[] | fromjson | .entry.databricks_cli_log.bundle_deploy_event.experimental.bool_values[] | select(.key == "artifacts_reference_used")'
5+
6+
trace $CLI bundle deploy -t prod
7+
8+
title "Artifact metrics expected"
9+
trace cat out.requests.txt | jq 'select(has("path") and .path == "/telemetry-ext") | .body.protoLogs[] | fromjson | .entry.databricks_cli_log.bundle_deploy_event.experimental.bool_values[] | select(.key == "artifacts_reference_used")'
10+
11+
rm out.requests.txt

bundle/bundle.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ type Metrics struct {
5656
ExecutionTimes []protos.IntMapEntry
5757
}
5858

59+
// SetBoolValue sets the value of a boolean metric.
60+
// If the metric does not exist, it is created.
61+
// If the metric exists, it is updated.
62+
// Ensures that the metric is unique
63+
func (m *Metrics) SetBoolValue(key string, value bool) {
64+
for i, v := range m.BoolValues {
65+
if v.Key == key {
66+
m.BoolValues[i].Value = value
67+
return
68+
}
69+
}
70+
m.BoolValues = append(m.BoolValues, protos.BoolMapEntry{Key: key, Value: value})
71+
}
72+
5973
func (m *Metrics) AddBoolValue(key string, value bool) {
6074
m.BoolValues = append(m.BoolValues, protos.BoolMapEntry{Key: key, Value: value})
6175
}

bundle/config/mutator/resolve_variable_references.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ var defaultPrefixes = []string{
4343
"variables",
4444
}
4545

46+
var artifactPath = dyn.MustPathFromString("artifacts")
47+
4648
type resolveVariableReferences struct {
4749
prefixes []string
4850
pattern dyn.Pattern
@@ -54,6 +56,8 @@ type resolveVariableReferences struct {
5456
// includeResources can be used with appropriate pattern to avoid resolving variables
5557
// outside of 'resources'.
5658
includeResources bool
59+
60+
artifactsReferenceUsed bool
5761
}
5862

5963
func ResolveVariableReferencesOnlyResources(prefixes ...string) bundle.Mutator {
@@ -171,6 +175,11 @@ func (m *resolveVariableReferences) Apply(ctx context.Context, b *bundle.Bundle)
171175
break
172176
}
173177
}
178+
179+
if m.artifactsReferenceUsed {
180+
b.Metrics.SetBoolValue("artifacts_reference_used", true)
181+
}
182+
174183
return diags
175184
}
176185

@@ -212,6 +221,11 @@ func (m *resolveVariableReferences) resolveOnce(b *bundle.Bundle, prefixes []dyn
212221
path = newPath
213222
}
214223

224+
// If the path starts with "artifacts", we need to add a metric to track if this reference is used.
225+
if path.HasPrefix(artifactPath) {
226+
m.artifactsReferenceUsed = true
227+
}
228+
215229
// Perform resolution only if the path starts with one of the specified prefixes.
216230
for _, prefix := range prefixes {
217231
if path.HasPrefix(prefix) {

0 commit comments

Comments
 (0)