mirror of
https://github.com/opentffoundation/opentf.git
synced 2025-12-19 17:59:05 -05:00
[OpenTelemetry] Add traces to init command (#2665)
Signed-off-by: James Humphries <james@james-humphries.co.uk> Signed-off-by: Christian Mesh <christianmesh1@gmail.com> Co-authored-by: Christian Mesh <christianmesh1@gmail.com>
This commit is contained in:
@@ -15,13 +15,14 @@ import (
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/apparentlymart/go-shquot/shquot"
|
||||
"github.com/hashicorp/go-plugin"
|
||||
"github.com/hashicorp/terraform-svchost/disco"
|
||||
"github.com/mattn/go-shellwords"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/mitchellh/colorstring"
|
||||
|
||||
"github.com/opentofu/opentofu/internal/addrs"
|
||||
"github.com/opentofu/opentofu/internal/command/cliconfig"
|
||||
"github.com/opentofu/opentofu/internal/command/format"
|
||||
@@ -29,8 +30,8 @@ import (
|
||||
"github.com/opentofu/opentofu/internal/httpclient"
|
||||
"github.com/opentofu/opentofu/internal/logging"
|
||||
"github.com/opentofu/opentofu/internal/terminal"
|
||||
"github.com/opentofu/opentofu/internal/tracing"
|
||||
"github.com/opentofu/opentofu/version"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
backendInit "github.com/opentofu/opentofu/internal/backend/init"
|
||||
)
|
||||
@@ -69,25 +70,22 @@ func main() {
|
||||
func realMain() int {
|
||||
defer logging.PanicHandler()
|
||||
|
||||
var err error
|
||||
|
||||
err = openTelemetryInit()
|
||||
err := tracing.OpenTelemetryInit()
|
||||
if err != nil {
|
||||
// openTelemetryInit can only fail if OpenTofu was run with an
|
||||
// explicit environment variable to enable telemetry collection,
|
||||
// so in typical use we cannot get here.
|
||||
Ui.Error(fmt.Sprintf("Could not initialize telemetry: %s", err))
|
||||
Ui.Error(fmt.Sprintf("Unset environment variable %s if you don't intend to collect telemetry from OpenTofu.", openTelemetryExporterEnvVar))
|
||||
Ui.Error(fmt.Sprintf("Unset environment variable %s if you don't intend to collect telemetry from OpenTofu.", tracing.OTELExporterEnvVar))
|
||||
|
||||
return 1
|
||||
}
|
||||
var ctx context.Context
|
||||
var otelSpan trace.Span
|
||||
{
|
||||
// At minimum we emit a span covering the entire command execution.
|
||||
_, displayArgs := shquot.POSIXShellSplit(os.Args)
|
||||
ctx, otelSpan = tracer.Start(context.Background(), fmt.Sprintf("tofu %s", displayArgs))
|
||||
defer otelSpan.End()
|
||||
}
|
||||
defer tracing.ForceFlush(5 * time.Second)
|
||||
ctx := context.Background()
|
||||
|
||||
// At minimum, we emit a span covering the entire command execution.
|
||||
ctx, span := tracing.Tracer().Start(ctx, "tofu")
|
||||
defer span.End()
|
||||
|
||||
tmpLogPath := os.Getenv(envTmpLogPath)
|
||||
if tmpLogPath != "" {
|
||||
@@ -102,9 +100,7 @@ func realMain() int {
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf(
|
||||
"[INFO] OpenTofu version: %s %s",
|
||||
Version, VersionPrerelease)
|
||||
log.Printf("[INFO] OpenTofu version: %s %s", Version, VersionPrerelease)
|
||||
for _, depMod := range version.InterestingDependencies() {
|
||||
log.Printf("[DEBUG] using %s %s", depMod.Path, depMod.Version)
|
||||
}
|
||||
@@ -117,6 +113,7 @@ func realMain() int {
|
||||
streams, err := terminal.Init()
|
||||
if err != nil {
|
||||
Ui.Error(fmt.Sprintf("Failed to configure the terminal: %s", err))
|
||||
|
||||
return 1
|
||||
}
|
||||
if streams.Stdout.IsTerminal() {
|
||||
@@ -140,7 +137,7 @@ func realMain() int {
|
||||
// path in the TERRAFORM_CONFIG_FILE environment variable (though probably
|
||||
// ill-advised) will be resolved relative to the true working directory,
|
||||
// not the overridden one.
|
||||
config, diags := cliconfig.LoadConfig()
|
||||
config, diags := cliconfig.LoadConfig(ctx)
|
||||
|
||||
if len(diags) > 0 {
|
||||
// Since we haven't instantiated a command.Meta yet, we need to do
|
||||
|
||||
@@ -1,93 +0,0 @@
|
||||
// Copyright (c) The OpenTofu Authors
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
// Copyright (c) 2023 HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"go.opentelemetry.io/contrib/exporters/autoexport"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
"go.opentelemetry.io/otel/sdk/resource"
|
||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"github.com/opentofu/opentofu/version"
|
||||
)
|
||||
|
||||
// If this environment variable is set to "otlp" when running OpenTofu CLI
|
||||
// then we'll enable an experimental OTLP trace exporter.
|
||||
//
|
||||
// BEWARE! This is not a committed external interface.
|
||||
//
|
||||
// Everything about this is experimental and subject to change in future
|
||||
// releases. Do not depend on anything about the structure of this output.
|
||||
// This mechanism might be removed altogether if a different strategy seems
|
||||
// better based on experience with this experiment.
|
||||
const openTelemetryExporterEnvVar = "OTEL_TRACES_EXPORTER"
|
||||
|
||||
// tracer is the OpenTelemetry tracer to use for traces in package main only.
|
||||
var tracer trace.Tracer
|
||||
|
||||
func init() {
|
||||
tracer = otel.Tracer("github.com/opentofu/opentofu")
|
||||
}
|
||||
|
||||
// openTelemetryInit initializes the optional OpenTelemetry exporter.
|
||||
//
|
||||
// By default we don't export telemetry information at all, since OpenTofu is
|
||||
// a CLI tool and so we don't assume we're running in an environment with
|
||||
// a telemetry collector available.
|
||||
//
|
||||
// However, for those running OpenTofu in automation we allow setting
|
||||
// the standard OpenTelemetry environment variable OTEL_TRACES_EXPORTER=otlp
|
||||
// to enable an OTLP exporter, which is in turn configured by all of the
|
||||
// standard OTLP exporter environment variables:
|
||||
//
|
||||
// https://opentelemetry.io/docs/specs/otel/protocol/exporter/#configuration-options
|
||||
//
|
||||
// We don't currently support any other telemetry export protocols, because
|
||||
// OTLP has emerged as a de-facto standard and each other exporter we support
|
||||
// means another relatively-heavy external dependency. OTLP happens to use
|
||||
// protocol buffers and gRPC, which OpenTofu would depend on for other reasons
|
||||
// anyway.
|
||||
func openTelemetryInit() error {
|
||||
// We'll check the environment variable ourselves first, because the
|
||||
// "autoexport" helper we're about to use is built under the assumption
|
||||
// that exporting should always be enabled and so will expect to find
|
||||
// an OTLP server on localhost if no environment variables are set at all.
|
||||
if os.Getenv(openTelemetryExporterEnvVar) != "otlp" {
|
||||
return nil // By default we just discard all telemetry calls
|
||||
}
|
||||
|
||||
otelResource := resource.NewWithAttributes(
|
||||
semconv.SchemaURL,
|
||||
semconv.ServiceNameKey.String("OpenTofu CLI"),
|
||||
semconv.ServiceVersionKey.String(version.Version),
|
||||
)
|
||||
|
||||
// If the environment variable was set to explicitly enable telemetry
|
||||
// then we'll enable it, using the "autoexport" library to automatically
|
||||
// handle the details based on the other OpenTelemetry standard environment
|
||||
// variables.
|
||||
exp, err := autoexport.NewSpanExporter(context.Background())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sp := sdktrace.NewSimpleSpanProcessor(exp)
|
||||
provider := sdktrace.NewTracerProvider(
|
||||
sdktrace.WithSpanProcessor(sp),
|
||||
sdktrace.WithResource(otelResource),
|
||||
)
|
||||
otel.SetTracerProvider(provider)
|
||||
|
||||
pgtr := propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})
|
||||
otel.SetTextMapPropagator(pgtr)
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user