Files
coder/coderd/provisionerdserver/scriptorderusage.go
T
George K 6d34557d74 feat: add script ordering adoption telemetry (#30860)
Count `coder_script_order` declarations and literal `rule` blocks in
coderd from the existing START plan in
`CompletedJob.TemplateImport.Plan`,
including child modules, without multiplying counts by resource or
module instances. Persist these counts per template version on
successful import after START plan graph validation, without changing
the provisioner API.

Report a deployment-wide `script_order_summary` with the number of
templates using script ordering and total declaration and rule
counts. Include only active, successfully imported versions of
non-deleted templates so repeated imports do not inflate
adoption. Elect one reporting replica per snapshot period and report
explicit zeros when no templates use ordering.

The summary contains only aggregate counts, without template
identifiers or rule contents. Dynamic rule blocks are not counted
because Terraform omits them from configuration JSON.

Configuration-decoding failures are logged and skip updating the
counts without independently failing the import. Script-order summary
collection failures are logged and omit that summary while allowing
the rest of the snapshot to proceed.

Refs: https://linear.app/codercom/issue/PLAT-551
2026-10-15 15:42:33 -07:00

56 lines
1.5 KiB
Go

package provisionerdserver
import (
"encoding/json"
tfjson "github.com/hashicorp/terraform-json"
"golang.org/x/xerrors"
)
type scriptOrderUsage struct {
dataSourceCount int32
ruleCount int32
}
// countScriptOrderUsage counts declarations in the supplied plan configuration,
// so count, for_each, and module instance counts do not inflate usage.
func countScriptOrderUsage(planJSON []byte) (scriptOrderUsage, error) {
var usage scriptOrderUsage
if len(planJSON) == 0 {
return usage, nil
}
var plan struct {
Config *tfjson.Config `json:"configuration"`
}
if err := json.Unmarshal(planJSON, &plan); err != nil {
return usage, xerrors.Errorf("decode plan configuration: %w", err)
}
if plan.Config != nil {
countScriptOrderUsageInModule(plan.Config.RootModule, &usage)
}
return usage, nil
}
func countScriptOrderUsageInModule(module *tfjson.ConfigModule, usage *scriptOrderUsage) {
if module == nil {
return
}
for _, r := range module.Resources {
if r == nil || r.Mode != tfjson.DataResourceMode || r.Type != "coder_script_order" {
continue
}
usage.dataSourceCount++
// Terraform's configuration JSON omits dynamic blocks, so only literal
// rule blocks contribute to this count.
if rules := r.Expressions["rule"]; rules != nil {
// #nosec G115 - Terraform rule counts are expected to fit in int32.
usage.ruleCount += int32(len(rules.NestedBlocks))
}
}
for _, call := range module.ModuleCalls {
if call != nil {
countScriptOrderUsageInModule(call.Module, usage)
}
}
}