Skip to content

Commit

Permalink
fix: remove telementry hooks
Browse files Browse the repository at this point in the history
Signed-off-by: Gamunu Balagalla <[email protected]>
  • Loading branch information
gamunu committed Jul 17, 2024
1 parent e595118 commit 8b0d9cc
Show file tree
Hide file tree
Showing 11 changed files with 10 additions and 237 deletions.
41 changes: 0 additions & 41 deletions .github/workflows/add-copyright-headers.yml

This file was deleted.

1 change: 1 addition & 0 deletions .github/workflows/build-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
build:
runs-on: ubuntu-latest
needs: [generate-provider-schemas]
strategy:
matrix:
include:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:

build:
runs-on: ubuntu-latest
needs: [generate-provider-schemas]
strategy:
matrix:
include:
Expand Down
36 changes: 0 additions & 36 deletions .github/workflows/check-changelog.yml

This file was deleted.

2 changes: 0 additions & 2 deletions internal/features/modules/hooks/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ package hooks
import (
"log"

"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
"github.com/hashicorp/terraform-ls/internal/features/modules/state"
"github.com/hashicorp/terraform-ls/internal/registry"
)

type Hooks struct {
ModStore *state.ModuleStore
RegistryClient registry.Client
AlgoliaClient *search.Client
Logger *log.Logger
}
50 changes: 2 additions & 48 deletions internal/features/modules/hooks/module_source_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,16 @@ package hooks

import (
"context"
"strings"

"github.com/algolia/algoliasearch-client-go/v3/algolia/opt"
"github.com/hashicorp/hcl-lang/decoder"
"github.com/hashicorp/hcl-lang/lang"
"github.com/zclconf/go-cty/cty"
"strings"
)

type RegistryModule struct {
FullName string `json:"full-name"`
Description string `json:"description"`
}

const algoliaModuleIndex = "tf-registry:prod:modules"

func (h *Hooks) fetchModulesFromAlgolia(ctx context.Context, term string) ([]RegistryModule, error) {
modules := make([]RegistryModule, 0)

index := h.AlgoliaClient.InitIndex(algoliaModuleIndex)
params := []interface{}{
ctx, // transport.Request will magically extract the context from here
opt.AttributesToRetrieve("full-name", "description"),
opt.HitsPerPage(10),
}

res, err := index.Search(term, params...)
if err != nil {
return modules, err
}

err = res.UnmarshalHits(&modules)
if err != nil {
return modules, err

}

return modules, nil
}

func (h *Hooks) RegistryModuleSources(ctx context.Context, value cty.Value) ([]decoder.Candidate, error) {
candidates := make([]decoder.Candidate, 0)
prefix := value.AsString()
Expand All @@ -54,24 +25,7 @@ func (h *Hooks) RegistryModuleSources(ctx context.Context, value cty.Value) ([]d
return candidates, nil
}

if h.AlgoliaClient == nil {
return candidates, nil
}

modules, err := h.fetchModulesFromAlgolia(ctx, prefix)
if err != nil {
h.Logger.Printf("Error fetching modules from Algolia: %#v", err)
return candidates, err
}

for _, mod := range modules {
c := decoder.ExpressionCompletionCandidate(decoder.ExpressionCandidate{
Value: cty.StringVal(mod.FullName),
Detail: "registry",
Description: lang.PlainText(mod.Description),
})
candidates = append(candidates, c)
}
//TODO: figure out how we can do module autocompletion

return candidates, nil
}
42 changes: 6 additions & 36 deletions internal/features/modules/hooks/module_source_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,28 +91,9 @@ func TestHooks_RegistryModuleSources(t *testing.T) {
t.Fatal(err)
}

searchClient := buildSearchClientMock(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/1/indexes/tf-registry%3Aprod%3Amodules/query" {
b, _ := io.ReadAll(r.Body)

if strings.Contains(string(b), "query=aws") {
w.Write([]byte(responseAWS))
return
} else if strings.Contains(string(b), "query=err") {
http.Error(w, responseErr, http.StatusForbidden)
return
}

w.Write([]byte(responseEmpty))
return
}
http.Error(w, fmt.Sprintf("unexpected request: %q", r.RequestURI), 400)
}))

h := &Hooks{
ModStore: store,
AlgoliaClient: searchClient,
Logger: log.New(io.Discard, "", 0),
ModStore: store,
Logger: log.New(io.Discard, "", 0),
}

tests := []struct {
Expand Down Expand Up @@ -186,15 +167,9 @@ func TestHooks_RegistryModuleSourcesCtxCancel(t *testing.T) {
t.Fatal(err)
}

searchClient := buildSearchClientMock(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(500 * time.Millisecond)
http.Error(w, fmt.Sprintf("unexpected request: %q", r.RequestURI), 400)
}))

h := &Hooks{
ModStore: store,
AlgoliaClient: searchClient,
Logger: log.New(io.Discard, "", 0),
ModStore: store,
Logger: log.New(io.Discard, "", 0),
}

_, err = h.RegistryModuleSources(ctx, cty.StringVal("aws"))
Expand All @@ -220,14 +195,9 @@ func TestHooks_RegistryModuleSourcesIgnore(t *testing.T) {
t.Fatal(err)
}

searchClient := buildSearchClientMock(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, fmt.Sprintf("unexpected request: %q", r.RequestURI), 400)
}))

h := &Hooks{
ModStore: store,
AlgoliaClient: searchClient,
Logger: log.New(io.Discard, "", 0),
ModStore: store,
Logger: log.New(io.Discard, "", 0),
}

tests := []struct {
Expand Down
13 changes: 0 additions & 13 deletions internal/features/modules/modules_feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import (
"io"
"log"

"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl-lang/decoder"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/terraform-ls/internal/algolia"
"github.com/hashicorp/terraform-ls/internal/document"
"github.com/hashicorp/terraform-ls/internal/eventbus"
fdecoder "github.com/hashicorp/terraform-ls/internal/features/modules/decoder"
Expand All @@ -23,7 +21,6 @@ import (
"github.com/hashicorp/terraform-ls/internal/langserver/diagnostics"
"github.com/hashicorp/terraform-ls/internal/registry"
globalState "github.com/hashicorp/terraform-ls/internal/state"
"github.com/hashicorp/terraform-ls/internal/telemetry"
"github.com/hashicorp/terraform-schema/backend"
tfmod "github.com/hashicorp/terraform-schema/module"
)
Expand Down Expand Up @@ -170,11 +167,6 @@ func (f *ModulesFeature) AppendCompletionHooks(srvCtx context.Context, decoderCo
Logger: f.logger,
}

credentials, ok := algolia.CredentialsFromContext(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
Expand Down Expand Up @@ -247,11 +239,6 @@ func (f *ModulesFeature) Telemetry(path string) map[string]interface{} {
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 := f.stateStore.ProviderSchemas.GetProviderID(pAddr)
if err != nil {
Expand Down
39 changes: 0 additions & 39 deletions internal/features/rootmodules/root_modules_feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package rootmodules

import (
"context"
"fmt"
"io"
"log"

Expand All @@ -14,7 +13,6 @@ import (
"github.com/hashicorp/terraform-ls/internal/features/rootmodules/jobs"
"github.com/hashicorp/terraform-ls/internal/features/rootmodules/state"
globalState "github.com/hashicorp/terraform-ls/internal/state"
"github.com/hashicorp/terraform-ls/internal/telemetry"
"github.com/hashicorp/terraform-ls/internal/terraform/exec"
tfaddr "github.com/hashicorp/terraform-registry-address"
tfmod "github.com/hashicorp/terraform-schema/module"
Expand Down Expand Up @@ -152,43 +150,6 @@ func (f *RootModulesFeature) CallersOfModule(modPath string) ([]string, error) {
return f.Store.CallersOfModule(modPath)
}

func (f *RootModulesFeature) Telemetry(path string) map[string]interface{} {
properties := make(map[string]interface{})

record, err := f.Store.RootRecordByPath(path)
if err != nil {
return properties
}

if record.TerraformVersion != nil {
properties["tfVersion"] = record.TerraformVersion.String()
}
if len(record.InstalledProviders) > 0 {
installedProviders := make(map[string]string, 0)
for pAddr, pv := range record.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 := f.stateStore.ProviderSchemas.GetProviderID(pAddr)
if err != nil {
continue
}
addr := fmt.Sprintf("unlisted/%s", id)
installedProviders[addr] = ""
}
properties["installedProviders"] = installedProviders
}

return properties
}

// InstalledModulePath checks the installed modules in the given root module
// for the given normalized source address.
//
Expand Down
21 changes: 0 additions & 21 deletions internal/langserver/handlers/completion_hooks.go

This file was deleted.

1 change: 0 additions & 1 deletion internal/langserver/handlers/hooks_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/hashicorp/terraform-ls/internal/state"
)


func callRefreshClientCommand(clientRequester session.ClientCaller, commandId string) notifier.Hook {
return func(ctx context.Context, changes state.Changes) error {
// TODO: avoid triggering if module calls/providers did not change
Expand Down

0 comments on commit 8b0d9cc

Please sign in to comment.