Skip to content

Commit 6e94e91

Browse files
authored
[Python] Add telemetry (#2713)
## Changes Add telemetry for PythonMutator. It includes: - counts of added and updated resources - counts of declared mutators and resource loaders ## Why It allows to track adoption patterns of `experimental/python` feature. ## Tests Unit test
1 parent 34949a7 commit 6e94e91

File tree

7 files changed

+56
-9
lines changed

7 files changed

+56
-9
lines changed

bundle/bundle.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ const internalFolder = ".internal"
3636
// This struct is used as a communication channel to collect metrics
3737
// from all over the bundle codebase to finally be emitted as telemetry.
3838
type Metrics struct {
39-
ConfigurationFileCount int64
40-
TargetCount int64
41-
DeploymentId uuid.UUID
42-
BoolValues []protos.BoolMapEntry
39+
ConfigurationFileCount int64
40+
TargetCount int64
41+
DeploymentId uuid.UUID
42+
BoolValues []protos.BoolMapEntry
43+
PythonAddedResourcesCount int64
44+
PythonUpdatedResourcesCount int64
4345
}
4446

4547
func (m *Metrics) AddBoolValue(key string, value bool) {

bundle/config/mutator/python/python_mutator.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,13 @@ func (m *pythonMutator) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagno
258258
return newRoot, nil
259259
})
260260

261+
// don't instrument deprecated phases
262+
if m.phase == PythonMutatorPhaseLoadResources || m.phase == PythonMutatorPhaseApplyMutators {
263+
// we can precisely track resources that are added/updated, so sum doesn't double-count
264+
b.Metrics.PythonUpdatedResourcesCount += int64(result.UpdatedResources.Size())
265+
b.Metrics.PythonAddedResourcesCount += int64(result.AddedResources.Size())
266+
}
267+
261268
if err == mutateDiagsHasError {
262269
if !mutateDiags.HasError() {
263270
panic("mutateDiags has no error, but error is expected")

bundle/config/mutator/python/python_mutator_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ workspace: { current_user: { userName: test }}`)
166166
})
167167
assert.NoError(t, err)
168168

169+
assert.Equal(t, int64(2), b.Metrics.PythonAddedResourcesCount)
170+
assert.Equal(t, int64(0), b.Metrics.PythonUpdatedResourcesCount)
171+
169172
assert.Equal(t, 1, len(diags))
170173
assert.Equal(t, "job doesn't have any tasks", diags[0].Summary)
171174
assert.Equal(t, []dyn.Location{
@@ -243,6 +246,9 @@ resources:
243246
return v, nil
244247
})
245248
assert.NoError(t, err)
249+
250+
assert.Equal(t, int64(0), b.Metrics.PythonAddedResourcesCount)
251+
assert.Equal(t, int64(1), b.Metrics.PythonUpdatedResourcesCount)
246252
}
247253

248254
func TestPythonMutator_badOutput(t *testing.T) {

bundle/config/mutator/resourcemutator/resource_key.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ func (r ResourceKeySet) IsEmpty() bool {
3737
return len(r) == 0
3838
}
3939

40+
func (r ResourceKeySet) Size() int {
41+
size := 0
42+
43+
for _, resources := range r {
44+
size += len(resources)
45+
}
46+
47+
return size
48+
}
49+
4050
// AddPattern adds all resource keys that match the pattern.
4151
func (r ResourceKeySet) AddPattern(pattern dyn.Pattern, root dyn.Value) error {
4252
if len(pattern) != 3 {

bundle/config/mutator/resourcemutator/resource_key_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,5 @@ func TestResourceKeySet_AddResourceKey(t *testing.T) {
127127
},
128128
set.ToArray(),
129129
)
130+
assert.Equal(t, 2, set.Size())
130131
}

bundle/phases/deploy.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,11 @@ func logTelemetry(ctx context.Context, b *bundle.Bundle) {
333333
mode = protos.BundleModeProduction
334334
}
335335

336+
experimentalConfig := b.Config.Experimental
337+
if experimentalConfig == nil {
338+
experimentalConfig = &config.Experimental{}
339+
}
340+
336341
telemetry.Log(ctx, protos.DatabricksCliLog{
337342
BundleDeployEvent: &protos.BundleDeployEvent{
338343
BundleUuid: bundleUuid,
@@ -358,11 +363,15 @@ func logTelemetry(ctx context.Context, b *bundle.Bundle) {
358363
ResourceDashboardIDs: dashboardIds,
359364

360365
Experimental: &protos.BundleDeployExperimental{
361-
BundleMode: mode,
362-
ConfigurationFileCount: b.Metrics.ConfigurationFileCount,
363-
TargetCount: b.Metrics.TargetCount,
364-
WorkspaceArtifactPathType: artifactPathType,
365-
BoolValues: b.Metrics.BoolValues,
366+
BundleMode: mode,
367+
ConfigurationFileCount: b.Metrics.ConfigurationFileCount,
368+
TargetCount: b.Metrics.TargetCount,
369+
WorkspaceArtifactPathType: artifactPathType,
370+
BoolValues: b.Metrics.BoolValues,
371+
PythonAddedResourcesCount: b.Metrics.PythonAddedResourcesCount,
372+
PythonUpdatedResourcesCount: b.Metrics.PythonUpdatedResourcesCount,
373+
PythonResourceLoadersCount: int64(len(experimentalConfig.Python.Resources)),
374+
PythonResourceMutatorsCount: int64(len(experimentalConfig.Python.Mutators)),
366375
},
367376
},
368377
})

libs/telemetry/protos/bundle_deploy.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ type BundleDeployExperimental struct {
6868

6969
// Execution time per mutator for a selected subset of mutators.
7070
BundleMutatorExecutionTimeMs []IntMapEntry `json:"bundle_mutator_execution_time_ms,omitempty"`
71+
72+
// Number of resources added by PythonMutator
73+
PythonAddedResourcesCount int64 `json:"python_added_resources_count,omitempty"`
74+
75+
// Number of resources updated by PythonMutator
76+
PythonUpdatedResourcesCount int64 `json:"python_updated_resources_count,omitempty"`
77+
78+
// Number of resource loaders declared at 'python/resources' in databricks.yml
79+
PythonResourceLoadersCount int64 `json:"python_resource_loaders_count,omitempty"`
80+
81+
// Number of resource mutators declared at 'python/mutators' in databricks.yml
82+
PythonResourceMutatorsCount int64 `json:"python_resource_mutators_count,omitempty"`
7183
}
7284

7385
type BoolMapEntry struct {

0 commit comments

Comments
 (0)