1
1
package main
2
2
3
3
import (
4
+ "context"
4
5
"encoding/json"
5
6
"fmt"
7
+ "github.com/apparentlymart/go-shquot/shquot"
6
8
"github.com/hashicorp/go-plugin"
7
9
svchost "github.com/hashicorp/terraform-svchost"
8
10
"github.com/hashicorp/terraform-svchost/disco"
@@ -23,6 +25,7 @@ import (
23
25
"github.com/hashicorp/terraform/version"
24
26
"github.com/mitchellh/cli"
25
27
"github.com/mitchellh/colorstring"
28
+ "go.opentelemetry.io/otel/trace"
26
29
"log"
27
30
"os"
28
31
"os/signal"
@@ -100,6 +103,24 @@ func RunCli(cArgc C.int, cArgv **C.char, cStdOutFd C.int, cStdErrFd C.int) C.int
100
103
}
101
104
}()
102
105
106
+ err = openTelemetryInit ()
107
+ if err != nil {
108
+ // openTelemetryInit can only fail if Terraform was run with an
109
+ // explicit environment variable to enable telemetry collection,
110
+ // so in typical use we cannot get here.
111
+ Ui .Error (fmt .Sprintf ("Could not initialize telemetry: %s" , err ))
112
+ Ui .Error (fmt .Sprintf ("Unset environment variable %s if you don't intend to collect telemetry from Terraform." , openTelemetryExporterEnvVar ))
113
+ return 1
114
+ }
115
+ var ctx context.Context
116
+ var otelSpan trace.Span
117
+ {
118
+ // At minimum we emit a span covering the entire command execution.
119
+ _ , displayArgs := shquot .POSIXShellSplit (os .Args )
120
+ ctx , otelSpan = tracer .Start (context .Background (), fmt .Sprintf ("terraform %s" , displayArgs ))
121
+ defer otelSpan .End ()
122
+ }
123
+
103
124
tmpLogPath := os .Getenv (envTmpLogPath )
104
125
if tmpLogPath != "" {
105
126
f , err := os .OpenFile (tmpLogPath , os .O_RDWR | os .O_APPEND , 0666 )
@@ -262,7 +283,7 @@ func RunCli(cArgc C.int, cArgv **C.char, cStdOutFd C.int, cStdErrFd C.int) C.int
262
283
commands := NewCommands (meta )
263
284
264
285
// Run checkpoint
265
- go runCheckpoint (config )
286
+ go runCheckpoint (ctx , config )
266
287
267
288
// Make sure we clean up any managed plugins at the end of this
268
289
defer func () {
0 commit comments