From a25a2d26e85631d108becdd33430678b5afcb3e5 Mon Sep 17 00:00:00 2001 From: Gamunu Balagalla Date: Sun, 3 Mar 2024 21:13:34 +0530 Subject: [PATCH] feat: remove telemetry and agolia --- .github/workflows/build.yml | 4 +- algolia.go | 10 -- internal/algolia/algolia.go | 25 ---- internal/cmd/serve_command.go | 7 - .../langserver/handlers/completion_hooks.go | 7 - internal/langserver/handlers/hooks_module.go | 129 ------------------ internal/langserver/handlers/initialize.go | 65 --------- internal/langserver/handlers/service.go | 16 --- internal/telemetry/noop.go | 23 ---- internal/telemetry/sender.go | 10 -- internal/telemetry/telemetry.go | 50 ------- main.go | 6 +- 12 files changed, 4 insertions(+), 348 deletions(-) delete mode 100644 algolia.go delete mode 100644 internal/algolia/algolia.go delete mode 100644 internal/telemetry/noop.go delete mode 100644 internal/telemetry/sender.go delete mode 100644 internal/telemetry/telemetry.go diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f42ea4beb..d0f1858a7 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -10,7 +10,7 @@ permissions: actions: write env: - PKG_NAME: "terraform-ls" + PKG_NAME: "opentofu-ls" jobs: get-go-version: @@ -146,7 +146,7 @@ jobs: arch: ${{ matrix.goarch }} version: ${{ needs.set-product-version.outputs.product-version }} maintainer: "HashiCorp" - homepage: "https://github.com/hashicorp/terraform-ls" + homepage: "https://github.com/gamunu/opentofu-ls" license: "MPL-2.0" binary: "dist/${{ env.PKG_NAME }}" deb_depends: "openssl" diff --git a/algolia.go b/algolia.go deleted file mode 100644 index 18af714ce..000000000 --- a/algolia.go +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -package main - -// Algolia application ID which should be used for searching Terraform registry modules -var algoliaAppID = "" - -// Algolia API key which should be used for searching Terraform registry modules -var algoliaAPIKey = "" diff --git a/internal/algolia/algolia.go b/internal/algolia/algolia.go deleted file mode 100644 index 57841eda8..000000000 --- a/internal/algolia/algolia.go +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -package algolia - -import "context" - -var credentialsKey struct{} - -type Credentials struct { - AppID string - APIKey string -} - -func WithCredentials(ctx context.Context, appID, apiKey string) context.Context { - return context.WithValue(ctx, credentialsKey, Credentials{ - AppID: appID, - APIKey: apiKey, - }) -} - -func CredentialsFromContext(ctx context.Context) (Credentials, bool) { - credentials, ok := ctx.Value(credentialsKey).(Credentials) - return credentials, ok -} diff --git a/internal/cmd/serve_command.go b/internal/cmd/serve_command.go index e42c58dff..08b944bd8 100644 --- a/internal/cmd/serve_command.go +++ b/internal/cmd/serve_command.go @@ -14,7 +14,6 @@ import ( "strings" "syscall" - "github.com/hashicorp/terraform-ls/internal/algolia" lsctx "github.com/hashicorp/terraform-ls/internal/context" "github.com/hashicorp/terraform-ls/internal/langserver" "github.com/hashicorp/terraform-ls/internal/langserver/handlers" @@ -31,9 +30,6 @@ type ServeCommand struct { Ui cli.Ui Version string - AlgoliaAppID string - AlgoliaAPIKey string - // flags port int logFilePath string @@ -108,9 +104,6 @@ func (c *ServeCommand) Run(args []string) int { logger.Printf("Starting terraform-ls %s", c.Version) ctx = lsctx.WithLanguageServerVersion(ctx, c.Version) - if c.AlgoliaAppID != "" && c.AlgoliaAPIKey != "" { - ctx = algolia.WithCredentials(ctx, c.AlgoliaAppID, c.AlgoliaAPIKey) - } var err error shutdownFunc := func(context.Context) error { return nil } diff --git a/internal/langserver/handlers/completion_hooks.go b/internal/langserver/handlers/completion_hooks.go index 1548818c8..26f7b9e55 100644 --- a/internal/langserver/handlers/completion_hooks.go +++ b/internal/langserver/handlers/completion_hooks.go @@ -4,9 +4,7 @@ package handlers import ( - "github.com/algolia/algoliasearch-client-go/v3/algolia/search" "github.com/hashicorp/hcl-lang/decoder" - "github.com/hashicorp/terraform-ls/internal/algolia" "github.com/hashicorp/terraform-ls/internal/hooks" ) @@ -17,11 +15,6 @@ func (s *service) AppendCompletionHooks(decoderContext decoder.DecoderContext) { Logger: s.logger, } - credentials, ok := algolia.CredentialsFromContext(s.srvCtx) - if ok { - h.AlgoliaClient = search.NewClient(credentials.AppID, credentials.APIKey) - } - decoderContext.CompletionHooks["CompleteLocalModuleSources"] = h.LocalModuleSources decoderContext.CompletionHooks["CompleteRegistryModuleSources"] = h.RegistryModuleSources decoderContext.CompletionHooks["CompleteRegistryModuleVersions"] = h.RegistryModuleVersions diff --git a/internal/langserver/handlers/hooks_module.go b/internal/langserver/handlers/hooks_module.go index a1bb67506..4440b40ee 100644 --- a/internal/langserver/handlers/hooks_module.go +++ b/internal/langserver/handlers/hooks_module.go @@ -11,137 +11,8 @@ import ( "github.com/hashicorp/terraform-ls/internal/langserver/notifier" "github.com/hashicorp/terraform-ls/internal/langserver/session" "github.com/hashicorp/terraform-ls/internal/state" - "github.com/hashicorp/terraform-ls/internal/telemetry" - "github.com/hashicorp/terraform-schema/backend" ) -func sendModuleTelemetry(store *state.StateStore, telemetrySender telemetry.Sender) notifier.Hook { - return func(ctx context.Context, changes state.ModuleChanges) error { - if changes.IsRemoval { - // we ignore removed modules for now - return nil - } - - mod, err := notifier.ModuleFromContext(ctx) - if err != nil { - return err - } - - properties, hasChanged := moduleTelemetryData(mod, changes, store) - if hasChanged { - telemetrySender.SendEvent(ctx, "moduleData", properties) - } - return nil - } -} - -func moduleTelemetryData(mod *state.Module, ch state.ModuleChanges, store *state.StateStore) (map[string]interface{}, bool) { - properties := make(map[string]interface{}) - hasChanged := ch.CoreRequirements || ch.Backend || ch.ProviderRequirements || - ch.TerraformVersion || ch.InstalledProviders - - if !hasChanged { - return properties, false - } - - 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" - } - - 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 { - hostname := data.Hostname - - // https://developer.hashicorp.com/terraform/language/settings/backends/remote#hostname - // 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" - } - - properties["backend.remote.hostname"] = hostname - } - } - if len(mod.Meta.ProviderRequirements) > 0 { - reqs := make(map[string]string, 0) - for pAddr, cons := range mod.Meta.ProviderRequirements { - if telemetry.IsPublicProvider(pAddr) { - reqs[pAddr.String()] = cons.String() - continue - } - - // anonymize any unknown providers or the ones not publicly listed - id, err := store.GetProviderID(pAddr) - if err != nil { - continue - } - addr := fmt.Sprintf("unlisted/%s", id) - reqs[addr] = cons.String() - } - properties["providerRequirements"] = reqs - } - if mod.TerraformVersion != nil { - properties["tfVersion"] = mod.TerraformVersion.String() - } - if len(mod.InstalledProviders) > 0 { - installedProviders := make(map[string]string, 0) - for pAddr, pv := range mod.InstalledProviders { - if telemetry.IsPublicProvider(pAddr) { - versionString := "" - if pv != nil { - versionString = pv.String() - } - installedProviders[pAddr.String()] = versionString - continue - } - - // anonymize any unknown providers or the ones not publicly listed - id, err := store.GetProviderID(pAddr) - if err != nil { - continue - } - addr := fmt.Sprintf("unlisted/%s", id) - installedProviders[addr] = "" - } - properties["installedProviders"] = installedProviders - } - - if !hasChanged { - return nil, false - } - - modId, err := store.GetModuleID(mod.Path) - if err != nil { - return nil, false - } - properties["moduleId"] = modId - - return properties, true -} - func updateDiagnostics(dNotifier *diagnostics.Notifier) notifier.Hook { return func(ctx context.Context, changes state.ModuleChanges) error { if changes.Diagnostics { diff --git a/internal/langserver/handlers/initialize.go b/internal/langserver/handlers/initialize.go index 947dac915..eb1a633f0 100644 --- a/internal/langserver/handlers/initialize.go +++ b/internal/langserver/handlers/initialize.go @@ -32,17 +32,11 @@ func (svc *service) Initialize(ctx context.Context, params lsp.InitializeParams) return serverCaps, err } - properties := getTelemetryProperties(out) - properties["lsVersion"] = serverCaps.ServerInfo.Version - clientCaps := params.Capabilities expClientCaps := lsp.ExperimentalClientCapabilities(clientCaps.Experimental) svc.server = jrpc2.ServerFromContext(ctx) - setupTelemetry(expClientCaps, svc, ctx, properties) - defer svc.telemetry.SendEvent(ctx, "initialize", properties) - if params.ClientInfo.Name != "" { err = ilsp.SetClientName(ctx, params.ClientInfo.Name) if err != nil { @@ -54,19 +48,15 @@ func (svc *service) Initialize(ctx context.Context, params lsp.InitializeParams) if _, ok := expClientCaps.ShowReferencesCommandId(); ok { expServerCaps.ReferenceCountCodeLens = true - properties["experimentalCapabilities.referenceCountCodeLens"] = true } if _, ok := expClientCaps.RefreshModuleProvidersCommandId(); ok { expServerCaps.RefreshModuleProviders = true - properties["experimentalCapabilities.refreshModuleProviders"] = true } if _, ok := expClientCaps.RefreshModuleCallsCommandId(); ok { expServerCaps.RefreshModuleCalls = true - properties["experimentalCapabilities.refreshModuleCalls"] = true } if _, ok := expClientCaps.RefreshTerraformVersionCommandId(); ok { expServerCaps.RefreshTerraformVersion = true - properties["experimentalCapabilities.refreshTerraformVersion"] = true } serverCaps.Capabilities.Experimental = expServerCaps @@ -128,13 +118,6 @@ func (svc *service) Initialize(ctx context.Context, params lsp.InitializeParams) if params.RootURI == "" { svc.singleFileMode = true - properties["root_uri"] = "file" - if properties["options.ignoreSingleFileWarning"] == false { - jrpc2.ServerFromContext(ctx).Notify(ctx, "window/showMessage", &lsp.ShowMessageParams{ - Type: lsp.Warning, - Message: "Some capabilities may be reduced when editing a single file. We recommend opening a directory for full functionality. Use 'ignoreSingleFileWarning' to suppress this warning.", - }) - } } else { rootURI := string(params.RootURI) @@ -143,7 +126,6 @@ func (svc *service) Initialize(ctx context.Context, params lsp.InitializeParams) "This is most likely client bug, please report it.", rootURI) if uri.IsWSLURI(rootURI) { - properties["root_uri"] = "invalid" // For WSL URIs we return additional error data // such that clients (e.g. VS Code) can provide better UX // and nudge users to open in the WSL Remote Extension instead. @@ -151,8 +133,6 @@ func (svc *service) Initialize(ctx context.Context, params lsp.InitializeParams) } if !uri.IsURIValid(rootURI) { - properties["root_uri"] = "invalid" - return serverCaps, invalidUriErr } @@ -180,51 +160,6 @@ func (svc *service) Initialize(ctx context.Context, params lsp.InitializeParams) return serverCaps, err } -func setupTelemetry(expClientCaps lsp.ExpClientCapabilities, svc *service, ctx context.Context, properties map[string]interface{}) { - if tv, ok := expClientCaps.TelemetryVersion(); ok { - svc.logger.Printf("enabling telemetry (version: %d)", tv) - err := svc.setupTelemetry(tv, svc.server) - if err != nil { - svc.logger.Printf("failed to setup telemetry: %s", err) - } - svc.logger.Printf("telemetry enabled (version: %d)", tv) - } -} - -func getTelemetryProperties(out *settings.DecodedOptions) map[string]interface{} { - properties := map[string]interface{}{ - "experimentalCapabilities.referenceCountCodeLens": false, - "options.ignoreSingleFileWarning": false, - "options.rootModulePaths": false, - "options.excludeModulePaths": false, - "options.commandPrefix": false, - "options.indexing.ignoreDirectoryNames": false, - "options.indexing.ignorePaths": false, - "options.experimentalFeatures.validateOnSave": false, - "options.terraform.path": false, - "options.terraform.timeout": "", - "options.terraform.logFilePath": false, - "options.validation.earlyValidation": false, - "root_uri": "dir", - "lsVersion": "", - } - - properties["options.rootModulePaths"] = len(out.Options.XLegacyModulePaths) > 0 - properties["options.excludeModulePaths"] = len(out.Options.XLegacyExcludeModulePaths) > 0 - properties["options.commandPrefix"] = len(out.Options.CommandPrefix) > 0 - properties["options.indexing.ignoreDirectoryNames"] = len(out.Options.Indexing.IgnoreDirectoryNames) > 0 - properties["options.indexing.ignorePaths"] = len(out.Options.Indexing.IgnorePaths) > 0 - properties["options.experimentalFeatures.prefillRequiredFields"] = out.Options.ExperimentalFeatures.PrefillRequiredFields - properties["options.experimentalFeatures.validateOnSave"] = out.Options.ExperimentalFeatures.ValidateOnSave - properties["options.ignoreSingleFileWarning"] = out.Options.IgnoreSingleFileWarning - properties["options.terraform.path"] = len(out.Options.Terraform.Path) > 0 - properties["options.terraform.timeout"] = out.Options.Terraform.Timeout - properties["options.terraform.logFilePath"] = len(out.Options.Terraform.LogFilePath) > 0 - properties["options.validation.earlyValidation"] = out.Options.Validation.EnableEnhancedValidation - - return properties -} - func initializeResult(ctx context.Context) lsp.InitializeResult { serverCaps := lsp.InitializeResult{ Capabilities: lsp.ServerCapabilities{ diff --git a/internal/langserver/handlers/service.go b/internal/langserver/handlers/service.go index be83f3e18..17c203327 100644 --- a/internal/langserver/handlers/service.go +++ b/internal/langserver/handlers/service.go @@ -30,7 +30,6 @@ import ( "github.com/hashicorp/terraform-ls/internal/scheduler" "github.com/hashicorp/terraform-ls/internal/settings" "github.com/hashicorp/terraform-ls/internal/state" - "github.com/hashicorp/terraform-ls/internal/telemetry" "github.com/hashicorp/terraform-ls/internal/terraform/discovery" "github.com/hashicorp/terraform-ls/internal/terraform/exec" "github.com/hashicorp/terraform-ls/internal/walker" @@ -63,7 +62,6 @@ type service struct { tfDiscoFunc discovery.DiscoveryFunc tfExecFactory exec.ExecutorFactory tfExecOpts *exec.ExecutorOpts - telemetry telemetry.Sender decoder *decoder.Decoder stateStore *state.StateStore server session.Server @@ -91,7 +89,6 @@ func NewSession(srvCtx context.Context) session.Session { stopSession: stopSession, tfDiscoFunc: d.LookPath, tfExecFactory: exec.NewExecutor, - telemetry: &telemetry.NoopSender{}, registryClient: registry.NewClient(), } } @@ -112,8 +109,6 @@ func (svc *service) Assigner() (jrpc2.Assigner, error) { return nil, fmt.Errorf("Unable to prepare session: %w", err) } - svc.telemetry = &telemetry.NoopSender{Logger: svc.logger} - cc := &lsp.ClientCapabilities{} rootDir := "" @@ -473,7 +468,6 @@ func (svc *service) configureSessionDependencies(ctx context.Context, cfgOpts *s moduleHooks := []notifier.Hook{ updateDiagnostics(svc.diagsNotifier), - sendModuleTelemetry(svc.stateStore, svc.telemetry), } svc.lowPrioIndexer = scheduler.NewScheduler(svc.stateStore.JobStore, 1, job.LowPriority) @@ -544,16 +538,6 @@ func (svc *service) configureSessionDependencies(ctx context.Context, cfgOpts *s return nil } -func (svc *service) setupTelemetry(version int, notifier session.ClientNotifier) error { - t, err := telemetry.NewSender(version, notifier) - if err != nil { - return err - } - - svc.telemetry = t - return nil -} - func (svc *service) Finish(_ jrpc2.Assigner, status jrpc2.ServerStatus) { if status.Closed || status.Err != nil { svc.logger.Printf("session stopped unexpectedly (err: %v)", status.Err) diff --git a/internal/telemetry/noop.go b/internal/telemetry/noop.go deleted file mode 100644 index 7fa01101d..000000000 --- a/internal/telemetry/noop.go +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -package telemetry - -import ( - "context" - "io/ioutil" - "log" -) - -type NoopSender struct { - Logger *log.Logger -} - -func (t *NoopSender) log() *log.Logger { - if t.Logger != nil { - return t.Logger - } - return log.New(ioutil.Discard, "", 0) -} - -func (t *NoopSender) SendEvent(ctx context.Context, name string, properties map[string]interface{}) {} diff --git a/internal/telemetry/sender.go b/internal/telemetry/sender.go deleted file mode 100644 index 851088b06..000000000 --- a/internal/telemetry/sender.go +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -package telemetry - -import "context" - -type Sender interface { - SendEvent(ctx context.Context, name string, properties map[string]interface{}) -} diff --git a/internal/telemetry/telemetry.go b/internal/telemetry/telemetry.go deleted file mode 100644 index c4bd85406..000000000 --- a/internal/telemetry/telemetry.go +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -package telemetry - -import ( - "context" - "fmt" - - lsp "github.com/hashicorp/terraform-ls/internal/protocol" - tfaddr "github.com/hashicorp/terraform-registry-address" -) - -type Telemetry struct { - version int - notifier Notifier -} - -type Notifier interface { - Notify(ctx context.Context, method string, params interface{}) error -} - -func NewSender(version int, notifier Notifier) (*Telemetry, error) { - if version != lsp.TelemetryFormatVersion { - return nil, fmt.Errorf("unsupported telemetry format version: %d", version) - } - - return &Telemetry{ - version: version, - notifier: notifier, - }, nil -} - -func (t *Telemetry) SendEvent(ctx context.Context, name string, properties map[string]interface{}) { - t.notifier.Notify(ctx, "telemetry/event", lsp.TelemetryEvent{ - Version: t.version, - Name: name, - Properties: properties, - }) -} - -func IsPublicProvider(addr tfaddr.Provider) bool { - if addr.Hostname == tfaddr.DefaultProviderRegistryHost { - return true - } - if addr.IsLegacy() || addr.IsBuiltIn() { - return true - } - return false -} diff --git a/main.go b/main.go index c757a4bc1..dc946af51 100644 --- a/main.go +++ b/main.go @@ -32,10 +32,8 @@ func main() { c.Commands = map[string]cli.CommandFactory{ "serve": func() (cli.Command, error) { return &cmd.ServeCommand{ - Ui: ui, - Version: VersionString(), - AlgoliaAppID: algoliaAppID, - AlgoliaAPIKey: algoliaAPIKey, + Ui: ui, + Version: VersionString(), }, nil }, "version": func() (cli.Command, error) {