-
Notifications
You must be signed in to change notification settings - Fork 177
stats: Record per resource state file statistics in telemetry #5199
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
Open
shreyas-goenka
wants to merge
1
commit into
main
Choose a base branch
from
shreyas-goenka/state-size-telemetry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+460
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| package phases | ||
|
|
||
| import ( | ||
| "cmp" | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "slices" | ||
|
|
||
| "github.com/databricks/cli/bundle" | ||
| "github.com/databricks/cli/bundle/config/engine" | ||
| "github.com/databricks/cli/bundle/direct/dresources" | ||
| "github.com/databricks/cli/bundle/direct/dstate" | ||
| "github.com/databricks/cli/libs/dyn" | ||
| "github.com/databricks/cli/libs/log" | ||
| "github.com/databricks/cli/libs/telemetry/protos" | ||
| ) | ||
|
|
||
| // collectResourcesMetadata builds a BundleResourcesMetadata for the deploy. | ||
| // | ||
| // State sizes are computed by running each resource's typed config through | ||
| // the direct engine's adapter.PrepareState — the same transformation direct | ||
| // uses to derive the value it persists to resources.json — and marshaling | ||
| // each entry with dstate.SaveState's encoding (MarshalIndent(" ", " ")). | ||
| // The whole-file size is then computed by assembling those entries into a | ||
| // dstate.Database and marshaling it the way DeploymentState.unlockedSave | ||
| // writes it (MarshalIndent("", " ")). So: | ||
| // | ||
| // - Under DATABRICKS_BUNDLE_ENGINE=direct, per-resource sizes equal | ||
| // len(entry.State) on disk byte-for-byte, and state_file_size_bytes | ||
| // matches the resources.json file size to within a few bytes (only | ||
| // Lineage and Serial may differ, which we set to "" / 0 here). | ||
| // - Under =terraform, the same computation runs against the bundle config, | ||
| // producing identical numbers for the same logical bundle. tfstate is | ||
| // never read. | ||
| // | ||
| // Returns nil when the bundle declares no resources. | ||
| func collectResourcesMetadata(ctx context.Context, b *bundle.Bundle) *protos.BundleResourcesMetadata { | ||
| counts, sizesByType, fileSize := collectResourceCountsAndSizes(ctx, b) | ||
| if len(counts) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| types := unionKeys(counts, sizesByType) | ||
| slices.Sort(types) | ||
|
|
||
| resources := make([]protos.ResourceMetadata, 0, len(types)) | ||
| for _, t := range types { | ||
| sizes := sizesByType[t] | ||
| slices.SortFunc(sizes, func(a, b int64) int { return cmp.Compare(a, b) }) | ||
| resources = append(resources, protos.ResourceMetadata{ | ||
| ResourceType: t, | ||
| Count: counts[t], | ||
| StateSizeMaxBytes: statMax(sizes), | ||
| StateSizeMeanBytes: statMean(sizes), | ||
| StateSizeMedianBytes: statMedian(sizes), | ||
| }) | ||
| } | ||
|
|
||
| return &protos.BundleResourcesMetadata{ | ||
| StateEngine: resolveDeployEngine(ctx, b), | ||
| StateFileSizeBytes: fileSize, | ||
| Resources: resources, | ||
| } | ||
| } | ||
|
|
||
| // collectResourceCountsAndSizes walks the bundle config and assembles a | ||
| // dstate.Database with each resource's PrepareState'd value, then marshals | ||
| // that database the way direct writes resources.json. Returns per-type | ||
| // counts, per-type per-resource byte lengths, and the byte length of the | ||
| // whole simulated state file. | ||
| func collectResourceCountsAndSizes(ctx context.Context, b *bundle.Bundle) (map[string]int64, map[string][]int64, int64) { | ||
| counts := make(map[string]int64) | ||
| sizesByType := make(map[string][]int64) | ||
|
|
||
| adapters := getAdapters(ctx, b) | ||
| db := dstate.NewDatabase("", 0) | ||
|
|
||
| pattern := dyn.NewPattern(dyn.Key("resources"), dyn.AnyKey(), dyn.AnyKey()) | ||
| _, err := dyn.MapByPattern(b.Config.Value(), pattern, func(p dyn.Path, v dyn.Value) (dyn.Value, error) { | ||
| if len(p) < 3 { | ||
| return v, nil | ||
| } | ||
| resourceType := p[1].Key() | ||
| counts[resourceType]++ | ||
|
|
||
| stateBytes, err := resourceStateBytes(b, adapters, p, resourceType) | ||
| if err != nil { | ||
| log.Debugf(ctx, "resources-metadata telemetry: %s: %s", p, err) | ||
| return v, nil | ||
| } | ||
| sizesByType[resourceType] = append(sizesByType[resourceType], int64(len(stateBytes))) | ||
| db.State[p.String()] = dstate.ResourceEntry{ | ||
| ID: extractResourceID(v), | ||
| State: stateBytes, | ||
| } | ||
| return v, nil | ||
| }) | ||
| if err != nil { | ||
| log.Debugf(ctx, "resources-metadata telemetry: failed to walk config resources: %s", err) | ||
| } | ||
|
|
||
| var fileSize int64 | ||
| if len(db.State) > 0 { | ||
| raw, mErr := json.MarshalIndent(db, "", " ") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not os.Stat actual state file to get the size? |
||
| if mErr != nil { | ||
| log.Debugf(ctx, "resources-metadata telemetry: failed to marshal database envelope: %s", mErr) | ||
| } else { | ||
| fileSize = int64(len(raw)) | ||
| } | ||
| } | ||
| return counts, sizesByType, fileSize | ||
| } | ||
|
|
||
| // resourceStateBytes derives the bytes direct would store for one resource: | ||
| // GetResourceConfig (typed) → adapter.PrepareState → MarshalIndent with the | ||
| // same prefix/indent direct uses in dstate.SaveState. Falls back to marshaling | ||
| // the typed config when no adapter is registered for the resource type | ||
| // (e.g., a type the direct engine doesn't yet support). | ||
| func resourceStateBytes(b *bundle.Bundle, adapters map[string]*dresources.Adapter, p dyn.Path, resourceType string) ([]byte, error) { | ||
| cfg, err := b.Config.GetResourceConfig(p.String()) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("get config: %w", err) | ||
| } | ||
|
|
||
| target := cfg | ||
| if adapter, ok := adapters[resourceType]; ok { | ||
| state, err := adapter.PrepareState(cfg) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("prepare state: %w", err) | ||
| } | ||
| target = state | ||
| } | ||
|
|
||
| // dstate.SaveState writes resource state with MarshalIndent using these | ||
| // exact prefix/indent arguments; matching them here means each resource's | ||
| // byte length equals len(entry.State) on disk for direct deploys. | ||
| raw, err := json.MarshalIndent(target, " ", " ") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will add some overhead to large bundles (e.g. python-generated one with a lot of jobs). |
||
| if err != nil { | ||
| return nil, fmt.Errorf("marshal: %w", err) | ||
| } | ||
| return raw, nil | ||
| } | ||
|
|
||
| // extractResourceID returns the resource's ID string from its dyn.Value entry, | ||
| // or "" if not yet set. Each resources.<type>.<name> entry has an "id" field | ||
| // populated post-deploy (via BaseResource.ID). | ||
| func extractResourceID(v dyn.Value) string { | ||
| idVal, err := dyn.Get(v, "id") | ||
| if err != nil || idVal.Kind() != dyn.KindString { | ||
| return "" | ||
| } | ||
| return idVal.MustString() | ||
| } | ||
|
|
||
| // getAdapters returns adapters initialized for PrepareState. If the bundle | ||
| // already has them initialized (direct engine path), reuse them. Otherwise, | ||
| // build a fresh set with a nil workspace client — PrepareState is a pure | ||
| // transformation that doesn't touch the client. | ||
| func getAdapters(ctx context.Context, b *bundle.Bundle) map[string]*dresources.Adapter { | ||
| if b.DeploymentBundle.Adapters != nil { | ||
| return b.DeploymentBundle.Adapters | ||
| } | ||
| adapters, err := dresources.InitAll(nil) | ||
| if err != nil { | ||
| log.Debugf(ctx, "resources-metadata telemetry: failed to init adapters: %s", err) | ||
| return nil | ||
| } | ||
| return adapters | ||
| } | ||
|
|
||
| // resolveDeployEngine returns the effective deploy engine ("direct" or | ||
| // "terraform"). Mirrors cmd/bundle/utils.ResolveEngineSetting but is inlined | ||
| // here to avoid a layering import (bundle/phases must not depend on cmd/). | ||
| func resolveDeployEngine(ctx context.Context, b *bundle.Bundle) string { | ||
| if b.Config.Bundle.Engine != engine.EngineNotSet { | ||
| return string(b.Config.Bundle.Engine.ThisOrDefault()) | ||
| } | ||
| envEngine, _ := engine.FromEnv(ctx) | ||
| return string(envEngine.ThisOrDefault()) | ||
| } | ||
|
|
||
| func unionKeys(a map[string]int64, b map[string][]int64) []string { | ||
| seen := make(map[string]struct{}, len(a)+len(b)) | ||
| for k := range a { | ||
| seen[k] = struct{}{} | ||
| } | ||
| for k := range b { | ||
| seen[k] = struct{}{} | ||
| } | ||
| out := make([]string, 0, len(seen)) | ||
| for k := range seen { | ||
| out = append(out, k) | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| func statMax(sortedSizes []int64) int64 { | ||
| if len(sortedSizes) == 0 { | ||
| return 0 | ||
| } | ||
| return sortedSizes[len(sortedSizes)-1] | ||
| } | ||
|
|
||
| func statMean(sortedSizes []int64) int64 { | ||
| if len(sortedSizes) == 0 { | ||
| return 0 | ||
| } | ||
| var total int64 | ||
| for _, s := range sortedSizes { | ||
| total += s | ||
| } | ||
| return total / int64(len(sortedSizes)) | ||
| } | ||
|
|
||
| func statMedian(sortedSizes []int64) int64 { | ||
| if len(sortedSizes) == 0 { | ||
| return 0 | ||
| } | ||
| return sortedSizes[(len(sortedSizes)-1)/2] | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why walk the config and not the actual state? They might not match 1-1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To capture the state for both terraform and direct deployments. Most customers are still on terraform so this givess us approximate stats for the state sizes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, in that case we should not call StateFileSize, since it has nothing to do with it, we should call it ConfigFileSize.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does try to approximate the state size - by calling
PrepareState: