mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-28 01:41:48 -06:00
7bee77bdd3
Several times over the years we've considered adding tracing instrumentation to Terraform, since even when running in isolation as a CLI program it has a "distributed system-like" structure, with lots of concurrent internal work and also some work delegated to provider plugins that are essentially temporarily-running microservices. However, it's always felt a bit overwhelming to do it because much of Terraform predates the Go context.Context idiom and so it's tough to get a clean chain of context.Context values all the way down the stack without disturbing a lot of existing APIs. This commit aims to just get that process started by establishing how a context can propagate from "package main" into the command package, focusing initially on "terraform init" and some other commands that share some underlying functions with that command. OpenTelemetry has emerged as a de-facto industry standard and so this uses its API directly, without any attempt to hide it behind an abstraction. The OpenTelemetry API is itself already an adapter layer, so we should be able to swap in any backend that uses comparable concepts. For now we just discard the tracing reports by default, and allow users to opt in to delivering traces over OTLP by setting an environment variable when running Terraform (the environment variable was established in an earlier commit, so this commit builds on that.) When tracing collection is enabled, every Terraform CLI run will generate at least one overall span representing the command that was run. Some commands might also create child spans, but most currently do not.
95 lines
2.4 KiB
Go
95 lines
2.4 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"path/filepath"
|
|
|
|
"github.com/hashicorp/go-checkpoint"
|
|
"github.com/hashicorp/terraform/internal/command"
|
|
"github.com/hashicorp/terraform/internal/command/cliconfig"
|
|
"go.opentelemetry.io/otel/codes"
|
|
)
|
|
|
|
func init() {
|
|
checkpointResult = make(chan *checkpoint.CheckResponse, 1)
|
|
}
|
|
|
|
var checkpointResult chan *checkpoint.CheckResponse
|
|
|
|
// runCheckpoint runs a HashiCorp Checkpoint request. You can read about
|
|
// Checkpoint here: https://github.com/hashicorp/go-checkpoint.
|
|
func runCheckpoint(ctx context.Context, c *cliconfig.Config) {
|
|
// If the user doesn't want checkpoint at all, then return.
|
|
if c.DisableCheckpoint {
|
|
log.Printf("[INFO] Checkpoint disabled. Not running.")
|
|
checkpointResult <- nil
|
|
return
|
|
}
|
|
|
|
ctx, span := tracer.Start(ctx, "HashiCorp Checkpoint")
|
|
_ = ctx // prevent staticcheck from complaining to avoid a maintenence hazard of having the wrong ctx in scope here
|
|
defer span.End()
|
|
|
|
configDir, err := cliconfig.ConfigDir()
|
|
if err != nil {
|
|
log.Printf("[ERR] Checkpoint setup error: %s", err)
|
|
checkpointResult <- nil
|
|
return
|
|
}
|
|
|
|
version := Version
|
|
if VersionPrerelease != "" {
|
|
version += fmt.Sprintf("-%s", VersionPrerelease)
|
|
}
|
|
|
|
signaturePath := filepath.Join(configDir, "checkpoint_signature")
|
|
if c.DisableCheckpointSignature {
|
|
log.Printf("[INFO] Checkpoint signature disabled")
|
|
signaturePath = ""
|
|
}
|
|
|
|
resp, err := checkpoint.Check(&checkpoint.CheckParams{
|
|
Product: "terraform",
|
|
Version: version,
|
|
SignatureFile: signaturePath,
|
|
CacheFile: filepath.Join(configDir, "checkpoint_cache"),
|
|
})
|
|
if err != nil {
|
|
log.Printf("[ERR] Checkpoint error: %s", err)
|
|
span.SetStatus(codes.Error, err.Error())
|
|
resp = nil
|
|
} else {
|
|
span.SetStatus(codes.Ok, "checkpoint request succeeded")
|
|
}
|
|
|
|
checkpointResult <- resp
|
|
}
|
|
|
|
// commandVersionCheck implements command.VersionCheckFunc and is used
|
|
// as the version checker.
|
|
func commandVersionCheck() (command.VersionCheckInfo, error) {
|
|
// Wait for the result to come through
|
|
info := <-checkpointResult
|
|
if info == nil {
|
|
var zero command.VersionCheckInfo
|
|
return zero, nil
|
|
}
|
|
|
|
// Build the alerts that we may have received about our version
|
|
alerts := make([]string, len(info.Alerts))
|
|
for i, a := range info.Alerts {
|
|
alerts[i] = a.Message
|
|
}
|
|
|
|
return command.VersionCheckInfo{
|
|
Outdated: info.Outdated,
|
|
Latest: info.CurrentVersion,
|
|
Alerts: alerts,
|
|
}, nil
|
|
}
|