Skip to content
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

Add TFC usage detection #1208

Merged
merged 2 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ require (
github.com/hashicorp/go-uuid v1.0.3
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/hc-install v0.5.0
github.com/hashicorp/hcl-lang v0.0.0-20230213134107-bb2205f5b041
github.com/hashicorp/hcl-lang v0.0.0-20230310081458-97626944945e
github.com/hashicorp/hcl/v2 v2.16.2
github.com/hashicorp/terraform-exec v0.18.1
github.com/hashicorp/terraform-json v0.16.0
github.com/hashicorp/terraform-registry-address v0.1.0
github.com/hashicorp/terraform-schema v0.0.0-20230215154105-62b19298872f
github.com/hashicorp/terraform-schema v0.0.0-20230310150226-9019fdc7a59a
github.com/mh-cbon/go-fmt-fail v0.0.0-20160815164508-67765b3fbcb5
github.com/mitchellh/cli v1.1.5
github.com/mitchellh/go-homedir v1.1.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ github.com/hashicorp/hc-install v0.5.0 h1:D9bl4KayIYKEeJ4vUDe9L5huqxZXczKaykSRcm
github.com/hashicorp/hc-install v0.5.0/go.mod h1:JyzMfbzfSBSjoDCRPna1vi/24BEDxFaCPfdHtM5SCdo=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/hcl-lang v0.0.0-20230213134107-bb2205f5b041 h1:GAhIS/EmlsuNzRTesYncXPNDjca8i5dJzU50WzkLIEc=
github.com/hashicorp/hcl-lang v0.0.0-20230213134107-bb2205f5b041/go.mod h1:alGNoXhoyCKUDHKM6iK88G5bgPym9agemDf6AEB26j0=
github.com/hashicorp/hcl-lang v0.0.0-20230310081458-97626944945e h1:3VRs8PujogY3D+hKc9ChpKK7rP+y/WTaDAKjORrOxk8=
github.com/hashicorp/hcl-lang v0.0.0-20230310081458-97626944945e/go.mod h1:m+ZB0CmTRHSo14j2INkDA+QZdzGBJRYTrOzf+4Xuj1k=
github.com/hashicorp/hcl/v2 v2.16.2 h1:mpkHZh/Tv+xet3sy3F9Ld4FyI2tUpWe9x3XtPx9f1a0=
github.com/hashicorp/hcl/v2 v2.16.2/go.mod h1:JRmR89jycNkrrqnMmvPDMd56n1rQJ2Q6KocSLCMCXng=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
Expand All @@ -220,8 +220,8 @@ github.com/hashicorp/terraform-json v0.16.0 h1:UKkeWRWb23do5LNAFlh/K3N0ymn1qTOO8
github.com/hashicorp/terraform-json v0.16.0/go.mod h1:v0Ufk9jJnk6tcIZvScHvetlKfiNTC+WS21mnXIlc0B0=
github.com/hashicorp/terraform-registry-address v0.1.0 h1:W6JkV9wbum+m516rCl5/NjKxCyTVaaUBbzYcMzBDO3U=
github.com/hashicorp/terraform-registry-address v0.1.0/go.mod h1:EnyO2jYO6j29DTHbJcm00E5nQTFeTtyZH3H5ycydQ5A=
github.com/hashicorp/terraform-schema v0.0.0-20230215154105-62b19298872f h1:QpboEFKLwIvvp0D6UWWzAx4/j1nCj1snfTRh3PgRq4A=
github.com/hashicorp/terraform-schema v0.0.0-20230215154105-62b19298872f/go.mod h1:JSXoOylwIc+IbKDP+QLygHz5yBEgK1B/zKTp6YGSSyg=
github.com/hashicorp/terraform-schema v0.0.0-20230310150226-9019fdc7a59a h1:6pAhW/C4H5QEr3mOnikXgmw803+/UB8/z0nyWVTqZGQ=
github.com/hashicorp/terraform-schema v0.0.0-20230310150226-9019fdc7a59a/go.mod h1:tzE93Y7+GD50yiIiVq+nmS+TdGtCRq3XnyLR9UXv5z0=
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 h1:HKLsbzeOsfXmKNpr3GiT18XAblV0BjCbzL8KQAMZGa0=
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734/go.mod h1:kNDNcF7sN4DocDLBkQYz73HGKwN1ANB1blq4lIYLYvg=
github.com/hexops/autogold v1.3.1 h1:YgxF9OHWbEIUjhDbpnLhgVsjUDsiHDTyDfy2lrfdlzo=
Expand Down
19 changes: 19 additions & 0 deletions internal/langserver/handlers/hooks_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ func moduleTelemetryData(mod *state.Module, ch state.ModuleChanges, store *state
if len(mod.Meta.CoreRequirements) > 0 {
properties["tfRequirements"] = mod.Meta.CoreRequirements.String()
}
if mod.Meta.Cloud != nil {
properties["cloud"] = true

hostname := mod.Meta.Cloud.Hostname

// https://developer.hashicorp.com/terraform/language/settings/terraform-cloud#usage-example
// Required for Terraform Enterprise;
// Defaults to app.terraform.io for Terraform Cloud
if hostname == "" {
hostname = "app.terraform.io"
}

// anonymize any non-default hostnames
if hostname != "app.terraform.io" {
hostname = "custom-hostname"
}
Comment on lines +53 to +62
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks slightly inconsistent with how we check & report hostnames below for backends.

I don't have too strong opinion on whether we should be reporting the difference between default and empty (implied default) hostname, but I do think we should approach both the same way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to https://developer.hashicorp.com/terraform/cli/cloud/settings#hostname, hostname is optional, and is an empty string if not specified. Using the same logic as backend would result is us sending an empty string in telemetry, which defeats the purpose in doing this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would assume that telemetry metrics with empty string still get sent through? As in - we'd still be able to see any cloud blocks parsed, the only difficulty would be treating empty strings as app.terraform.io when processing the data.

To put it differently - I'm happy with your implementation - which implies that we do no further interpretation of empty strings in AppInsights, but in that case I think we should do the same for the remote backend below, otherwise we end up comparing apples to pears.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sending empty data is allowed.

If the default is known, I don't see the need for sending empty strings and then having to do conversion when reporting.

I'll open a ticket for backend reporting.


properties["cloud.hostname"] = hostname
}
if mod.Meta.Backend != nil {
properties["backend"] = mod.Meta.Backend.Type
if data, ok := mod.Meta.Backend.Data.(*backend.Remote); ok {
Expand Down
7 changes: 7 additions & 0 deletions internal/state/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/hcl-lang/reference"
"github.com/hashicorp/hcl/v2"
tfaddr "github.com/hashicorp/terraform-registry-address"
"github.com/hashicorp/terraform-schema/backend"
tfmod "github.com/hashicorp/terraform-schema/module"
"github.com/hashicorp/terraform-schema/registry"

Expand All @@ -20,6 +21,7 @@ import (
type ModuleMetadata struct {
CoreRequirements version.Constraints
Backend *tfmod.Backend
Cloud *backend.Cloud
ProviderReferences map[tfmod.ProviderRef]tfaddr.Provider
ProviderRequirements tfmod.ProviderRequirements
Variables map[string]tfmod.Variable
Expand All @@ -35,6 +37,10 @@ func (mm ModuleMetadata) Copy() ModuleMetadata {
Filenames: mm.Filenames,
}

if mm.Cloud != nil {
newMm.Cloud = mm.Cloud
}

if mm.Backend != nil {
newMm.Backend = &tfmod.Backend{
Type: mm.Backend.Type,
Expand Down Expand Up @@ -924,6 +930,7 @@ func (s *ModuleStore) UpdateMetadata(path string, meta *tfmod.Meta, mErr error)
mod := oldMod.Copy()
mod.Meta = ModuleMetadata{
CoreRequirements: meta.CoreRequirements,
Cloud: meta.Cloud,
Backend: meta.Backend,
ProviderReferences: meta.ProviderReferences,
ProviderRequirements: meta.ProviderRequirements,
Expand Down
10 changes: 10 additions & 0 deletions internal/state/module_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type ModuleChanges struct {

CoreRequirements bool
Backend bool
Cloud bool
ProviderRequirements bool
TerraformVersion bool
InstalledProviders bool
Expand Down Expand Up @@ -77,6 +78,9 @@ func (s *ModuleStore) queueModuleChange(txn *memdb.Txn, oldMod, newMod *Module)
if len(newMod.Meta.CoreRequirements) > 0 {
cb.Changes.CoreRequirements = true
}
if newMod.Meta.Cloud != nil {
cb.Changes.Cloud = true
}
if newMod.Meta.Backend != nil {
cb.Changes.Backend = true
}
Expand All @@ -96,6 +100,9 @@ func (s *ModuleStore) queueModuleChange(txn *memdb.Txn, oldMod, newMod *Module)
if len(oldMod.Meta.CoreRequirements) > 0 {
cb.Changes.CoreRequirements = true
}
if oldMod.Meta.Cloud != nil {
cb.Changes.Cloud = true
}
if oldMod.Meta.Backend != nil {
cb.Changes.Backend = true
}
Expand All @@ -116,6 +123,9 @@ func (s *ModuleStore) queueModuleChange(txn *memdb.Txn, oldMod, newMod *Module)
if !oldMod.Meta.Backend.Equals(newMod.Meta.Backend) {
cb.Changes.Backend = true
}
if !oldMod.Meta.Cloud.Equals(newMod.Meta.Cloud) {
cb.Changes.Cloud = true
}
if !oldMod.Meta.ProviderRequirements.Equals(newMod.Meta.ProviderRequirements) {
cb.Changes.ProviderRequirements = true
}
Expand Down