Skip to content

Commit

Permalink
OTEL instrumentation (hashicorp#1327)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko authored Jul 28, 2023
1 parent cf5f93a commit b9eabdc
Show file tree
Hide file tree
Showing 31 changed files with 517 additions and 165 deletions.
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,17 @@ require (
github.com/zclconf/go-cty v1.13.2
github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b
go.bobheadxi.dev/gobenchdata v1.3.1
go.opentelemetry.io/otel/trace v1.16.0
golang.org/x/tools v0.11.0
)

require (
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
)

require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.1 // indirect
Expand Down Expand Up @@ -86,6 +94,9 @@ require (
github.com/stretchr/objx v0.5.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
go.bobheadxi.dev/streamline v1.2.1 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.42.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.42.0
go.opentelemetry.io/otel v1.16.0
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.12.0 // indirect
Expand Down
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk=
github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
Expand All @@ -112,6 +114,11 @@ github.com/go-git/go-git/v5 v5.6.1 h1:q4ZRqQl4pR/ZJHc1L5CFjGA1a10u76aV1iC+nh+bHs
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
Expand Down Expand Up @@ -362,6 +369,16 @@ go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk=
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.42.0 h1:0vzgiFDsCh/jxRCR1xcRrtMoeCu2itXz/PsXst5P8rI=
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.42.0/go.mod h1:y0vOY2OKFMOTvwxKfurStPayUUKGHlNeVqNneHmFXr0=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.42.0 h1:pginetY7+onl4qN1vl0xW/V/v6OBZ0vVdH+esuJgvmM=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.42.0/go.mod h1:XiYsayHc36K3EByOO6nbAXnAWbrUxdjUROCEeeROOH8=
go.opentelemetry.io/otel v1.16.0 h1:Z7GVAX/UkAXPKsy94IU+i6thsQS4nb7LviLpnaNeW8s=
go.opentelemetry.io/otel v1.16.0/go.mod h1:vl0h9NUa1D5s1nv3A5vZOYWn8av4K8Ml6JDeHrT/bx4=
go.opentelemetry.io/otel/metric v1.16.0 h1:RbrpwVG1Hfv85LgnZ7+txXioPDoh6EdbZHo26Q3hqOo=
go.opentelemetry.io/otel/metric v1.16.0/go.mod h1:QE47cpOmkwipPiefDwo2wDzwJrlfxxNYodqc4xnGCo4=
go.opentelemetry.io/otel/trace v1.16.0 h1:8JRpaObFoW0pxuVPapkgH8UhHQj+bJW8jJsCZEu5MQs=
go.opentelemetry.io/otel/trace v1.16.0/go.mod h1:Yt9vYq1SdNz3xdjZZK7wcXv1qv2pwLkqr2QVwea0ef0=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/inspect_module_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (c *InspectModuleCommand) inspect(rootPath string) error {
c.logger, os.Interrupt, syscall.SIGTERM)
defer cancel()

err = ss.WalkerPaths.EnqueueDir(dir)
err = ss.WalkerPaths.EnqueueDir(ctx, dir)
if err != nil {
return err
}
Expand Down
37 changes: 36 additions & 1 deletion internal/cmd/serve_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import (
"github.com/hashicorp/terraform-ls/internal/logging"
"github.com/hashicorp/terraform-ls/internal/pathtpl"
"github.com/mitchellh/cli"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
"go.opentelemetry.io/otel/trace"
)

type ServeCommand struct {
Expand Down Expand Up @@ -108,6 +112,26 @@ func (c *ServeCommand) Run(args []string) int {
ctx = algolia.WithCredentials(ctx, c.AlgoliaAppID, c.AlgoliaAPIKey)
}

var err error
shutdownFunc := func(context.Context) error { return nil }

// TODO: Currently unused until we decide how/where to export data
tp := trace.NewNoopTracerProvider()
otel.SetTracerProvider(tp)

if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to init telemetry: %s", err))
return 1
}
defer func() {
ctx := context.Background()
err := shutdownFunc(ctx)
if err != nil {
logger.Printf("failed to shutdown telemetry: %s", err)
return
}
}()

srv := langserver.NewLangServer(ctx, handlers.NewSession)
srv.SetLogger(logger)

Expand All @@ -120,7 +144,7 @@ func (c *ServeCommand) Run(args []string) int {
return 0
}

err := srv.StartAndWait(os.Stdin, os.Stdout)
err = srv.StartAndWait(os.Stdin, os.Stdout)
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to start server: %s", err))
return 1
Expand All @@ -129,6 +153,17 @@ func (c *ServeCommand) Run(args []string) int {
return 0
}

func (c *ServeCommand) otelResourceAttributes() []attribute.KeyValue {
return []attribute.KeyValue{
semconv.ServiceName("terraform-ls"),
semconv.ServiceVersion(c.Version),
attribute.Int("process.pid", os.Getpid()),
attribute.Int("runtime.NumCPU", runtime.NumCPU()),
attribute.Int("port", c.port),
attribute.Int("reqConcurrency", c.reqConcurrency),
}
}

type stopFunc func() error

func writeCpuProfileInto(rawPath string) (stopFunc, error) {
Expand Down
22 changes: 11 additions & 11 deletions internal/indexer/document_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
op "github.com/hashicorp/terraform-ls/internal/terraform/module/operation"
)

func (idx *Indexer) DocumentChanged(modHandle document.DirHandle) (job.IDs, error) {
func (idx *Indexer) DocumentChanged(ctx context.Context, modHandle document.DirHandle) (job.IDs, error) {
ids := make(job.IDs, 0)

parseId, err := idx.jobStore.EnqueueJob(job.Job{
parseId, err := idx.jobStore.EnqueueJob(ctx, job.Job{
Dir: modHandle,
Func: func(ctx context.Context) error {
return module.ParseModuleConfiguration(ctx, idx.fs, idx.modStore, modHandle.Path())
Expand All @@ -29,13 +29,13 @@ func (idx *Indexer) DocumentChanged(modHandle document.DirHandle) (job.IDs, erro
}
ids = append(ids, parseId)

modIds, err := idx.decodeModule(modHandle, job.IDs{parseId}, true)
modIds, err := idx.decodeModule(ctx, modHandle, job.IDs{parseId}, true)
if err != nil {
return ids, err
}
ids = append(ids, modIds...)

parseVarsId, err := idx.jobStore.EnqueueJob(job.Job{
parseVarsId, err := idx.jobStore.EnqueueJob(ctx, job.Job{
Dir: modHandle,
Func: func(ctx context.Context) error {
return module.ParseVariables(ctx, idx.fs, idx.modStore, modHandle.Path())
Expand All @@ -48,7 +48,7 @@ func (idx *Indexer) DocumentChanged(modHandle document.DirHandle) (job.IDs, erro
}
ids = append(ids, parseVarsId)

varsRefsId, err := idx.jobStore.EnqueueJob(job.Job{
varsRefsId, err := idx.jobStore.EnqueueJob(ctx, job.Job{
Dir: modHandle,
Func: func(ctx context.Context) error {
return module.DecodeVarsReferences(ctx, idx.modStore, idx.schemaStore, modHandle.Path())
Expand All @@ -65,10 +65,10 @@ func (idx *Indexer) DocumentChanged(modHandle document.DirHandle) (job.IDs, erro
return ids, nil
}

func (idx *Indexer) decodeModule(modHandle document.DirHandle, dependsOn job.IDs, ignoreState bool) (job.IDs, error) {
func (idx *Indexer) decodeModule(ctx context.Context, modHandle document.DirHandle, dependsOn job.IDs, ignoreState bool) (job.IDs, error) {
ids := make(job.IDs, 0)

metaId, err := idx.jobStore.EnqueueJob(job.Job{
metaId, err := idx.jobStore.EnqueueJob(ctx, job.Job{
Dir: modHandle,
Func: func(ctx context.Context) error {
return module.LoadModuleMetadata(ctx, idx.modStore, modHandle.Path())
Expand All @@ -82,7 +82,7 @@ func (idx *Indexer) decodeModule(modHandle document.DirHandle, dependsOn job.IDs
}
ids = append(ids, metaId)

eSchemaId, err := idx.jobStore.EnqueueJob(job.Job{
eSchemaId, err := idx.jobStore.EnqueueJob(ctx, job.Job{
Dir: modHandle,
Func: func(ctx context.Context) error {
return module.PreloadEmbeddedSchema(ctx, idx.logger, schemas.FS, idx.modStore, idx.schemaStore, modHandle.Path())
Expand All @@ -96,7 +96,7 @@ func (idx *Indexer) decodeModule(modHandle document.DirHandle, dependsOn job.IDs
}
ids = append(ids, eSchemaId)

refTargetsId, err := idx.jobStore.EnqueueJob(job.Job{
refTargetsId, err := idx.jobStore.EnqueueJob(ctx, job.Job{
Dir: modHandle,
Func: func(ctx context.Context) error {
return module.DecodeReferenceTargets(ctx, idx.modStore, idx.schemaStore, modHandle.Path())
Expand All @@ -110,7 +110,7 @@ func (idx *Indexer) decodeModule(modHandle document.DirHandle, dependsOn job.IDs
}
ids = append(ids, refTargetsId)

refOriginsId, err := idx.jobStore.EnqueueJob(job.Job{
refOriginsId, err := idx.jobStore.EnqueueJob(ctx, job.Job{
Dir: modHandle,
Func: func(ctx context.Context) error {
return module.DecodeReferenceOrigins(ctx, idx.modStore, idx.schemaStore, modHandle.Path())
Expand All @@ -124,7 +124,7 @@ func (idx *Indexer) decodeModule(modHandle document.DirHandle, dependsOn job.IDs
}
ids = append(ids, refOriginsId)

registryId, err := idx.jobStore.EnqueueJob(job.Job{
registryId, err := idx.jobStore.EnqueueJob(ctx, job.Job{
Dir: modHandle,
Func: func(ctx context.Context) error {
return module.GetModuleDataFromRegistry(ctx, idx.registryClient,
Expand Down
12 changes: 6 additions & 6 deletions internal/indexer/document_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
op "github.com/hashicorp/terraform-ls/internal/terraform/module/operation"
)

func (idx *Indexer) DocumentOpened(modHandle document.DirHandle) (job.IDs, error) {
func (idx *Indexer) DocumentOpened(ctx context.Context, modHandle document.DirHandle) (job.IDs, error) {
mod, err := idx.modStore.ModuleByPath(modHandle.Path())
if err != nil {
return nil, err
Expand All @@ -24,7 +24,7 @@ func (idx *Indexer) DocumentOpened(modHandle document.DirHandle) (job.IDs, error
var errs *multierror.Error

if mod.TerraformVersionState == op.OpStateUnknown {
_, err := idx.jobStore.EnqueueJob(job.Job{
_, err := idx.jobStore.EnqueueJob(ctx, job.Job{
Dir: modHandle,
Func: func(ctx context.Context) error {
ctx = exec.WithExecutorFactory(ctx, idx.tfExecFactory)
Expand All @@ -40,7 +40,7 @@ func (idx *Indexer) DocumentOpened(modHandle document.DirHandle) (job.IDs, error
// to avoid delays when documents of new modules are open.
}

parseId, err := idx.jobStore.EnqueueJob(job.Job{
parseId, err := idx.jobStore.EnqueueJob(ctx, job.Job{
Dir: modHandle,
Func: func(ctx context.Context) error {
return module.ParseModuleConfiguration(ctx, idx.fs, idx.modStore, modHandle.Path())
Expand All @@ -53,13 +53,13 @@ func (idx *Indexer) DocumentOpened(modHandle document.DirHandle) (job.IDs, error
}
ids = append(ids, parseId)

modIds, err := idx.decodeModule(modHandle, job.IDs{parseId}, true)
modIds, err := idx.decodeModule(ctx, modHandle, job.IDs{parseId}, true)
if err != nil {
return ids, err
}
ids = append(ids, modIds...)

parseVarsId, err := idx.jobStore.EnqueueJob(job.Job{
parseVarsId, err := idx.jobStore.EnqueueJob(ctx, job.Job{
Dir: modHandle,
Func: func(ctx context.Context) error {
return module.ParseVariables(ctx, idx.fs, idx.modStore, modHandle.Path())
Expand All @@ -72,7 +72,7 @@ func (idx *Indexer) DocumentOpened(modHandle document.DirHandle) (job.IDs, error
}
ids = append(ids, parseVarsId)

varsRefsId, err := idx.jobStore.EnqueueJob(job.Job{
varsRefsId, err := idx.jobStore.EnqueueJob(ctx, job.Job{
Dir: modHandle,
Func: func(ctx context.Context) error {
return module.DecodeVarsReferences(ctx, idx.modStore, idx.schemaStore, modHandle.Path())
Expand Down
20 changes: 10 additions & 10 deletions internal/indexer/module_calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
op "github.com/hashicorp/terraform-ls/internal/terraform/module/operation"
)

func (idx *Indexer) decodeInstalledModuleCalls(modHandle document.DirHandle, ignoreState bool) (job.IDs, error) {
func (idx *Indexer) decodeInstalledModuleCalls(ctx context.Context, modHandle document.DirHandle, ignoreState bool) (job.IDs, error) {
jobIds := make(job.IDs, 0)

moduleCalls, err := idx.modStore.ModuleCalls(modHandle.Path())
Expand Down Expand Up @@ -44,7 +44,7 @@ func (idx *Indexer) decodeInstalledModuleCalls(modHandle document.DirHandle, ign

refCollectionDeps := make(job.IDs, 0)

parseId, err := idx.jobStore.EnqueueJob(job.Job{
parseId, err := idx.jobStore.EnqueueJob(ctx, job.Job{
Dir: mcHandle,
Func: func(ctx context.Context) error {
return module.ParseModuleConfiguration(ctx, idx.fs, idx.modStore, mcPath)
Expand All @@ -61,7 +61,7 @@ func (idx *Indexer) decodeInstalledModuleCalls(modHandle document.DirHandle, ign

var metaId job.ID
if parseId != "" {
metaId, err = idx.jobStore.EnqueueJob(job.Job{
metaId, err = idx.jobStore.EnqueueJob(ctx, job.Job{
Dir: mcHandle,
Type: op.OpTypeLoadModuleMetadata.String(),
Func: func(ctx context.Context) error {
Expand All @@ -77,7 +77,7 @@ func (idx *Indexer) decodeInstalledModuleCalls(modHandle document.DirHandle, ign
refCollectionDeps = append(refCollectionDeps, metaId)
}

eSchemaId, err := idx.jobStore.EnqueueJob(job.Job{
eSchemaId, err := idx.jobStore.EnqueueJob(ctx, job.Job{
Dir: mcHandle,
Func: func(ctx context.Context) error {
return module.PreloadEmbeddedSchema(ctx, idx.logger, schemas.FS, idx.modStore, idx.schemaStore, mcPath)
Expand All @@ -95,15 +95,15 @@ func (idx *Indexer) decodeInstalledModuleCalls(modHandle document.DirHandle, ign
}

if parseId != "" {
ids, err := idx.collectReferences(mcHandle, refCollectionDeps, ignoreState)
ids, err := idx.collectReferences(ctx, mcHandle, refCollectionDeps, ignoreState)
if err != nil {
multierror.Append(errs, err)
} else {
jobIds = append(jobIds, ids...)
}
}

varsParseId, err := idx.jobStore.EnqueueJob(job.Job{
varsParseId, err := idx.jobStore.EnqueueJob(ctx, job.Job{
Dir: mcHandle,
Func: func(ctx context.Context) error {
return module.ParseVariables(ctx, idx.fs, idx.modStore, mcPath)
Expand All @@ -118,7 +118,7 @@ func (idx *Indexer) decodeInstalledModuleCalls(modHandle document.DirHandle, ign
}

if varsParseId != "" {
varsRefId, err := idx.jobStore.EnqueueJob(job.Job{
varsRefId, err := idx.jobStore.EnqueueJob(ctx, job.Job{
Dir: mcHandle,
Func: func(ctx context.Context) error {
return module.DecodeVarsReferences(ctx, idx.modStore, idx.schemaStore, mcPath)
Expand All @@ -138,12 +138,12 @@ func (idx *Indexer) decodeInstalledModuleCalls(modHandle document.DirHandle, ign
return jobIds, errs.ErrorOrNil()
}

func (idx *Indexer) collectReferences(modHandle document.DirHandle, dependsOn job.IDs, ignoreState bool) (job.IDs, error) {
func (idx *Indexer) collectReferences(ctx context.Context, modHandle document.DirHandle, dependsOn job.IDs, ignoreState bool) (job.IDs, error) {
ids := make(job.IDs, 0)

var errs *multierror.Error

id, err := idx.jobStore.EnqueueJob(job.Job{
id, err := idx.jobStore.EnqueueJob(ctx, job.Job{
Dir: modHandle,
Func: func(ctx context.Context) error {
return module.DecodeReferenceTargets(ctx, idx.modStore, idx.schemaStore, modHandle.Path())
Expand All @@ -158,7 +158,7 @@ func (idx *Indexer) collectReferences(modHandle document.DirHandle, dependsOn jo
ids = append(ids, id)
}

id, err = idx.jobStore.EnqueueJob(job.Job{
id, err = idx.jobStore.EnqueueJob(ctx, job.Job{
Dir: modHandle,
Func: func(ctx context.Context) error {
return module.DecodeReferenceOrigins(ctx, idx.modStore, idx.schemaStore, modHandle.Path())
Expand Down
Loading

0 comments on commit b9eabdc

Please sign in to comment.