@@ -50,7 +50,8 @@ Examples:
5050 if pkg == nil {
5151 log .Fatal ("build needs a package" )
5252 }
53- opts , localCache := getBuildOpts (cmd )
53+ opts , localCache , shutdown := getBuildOpts (cmd )
54+ defer shutdown ()
5455
5556 var (
5657 watch , _ = cmd .Flags ().GetBool ("watch" )
@@ -217,7 +218,7 @@ func addBuildFlags(cmd *cobra.Command) {
217218 cmd .Flags ().String ("trace-state" , os .Getenv ("TRACESTATE" ), "W3C Trace Context tracestate header for distributed tracing (defaults to $TRACESTATE)" )
218219}
219220
220- func getBuildOpts (cmd * cobra.Command ) ([]leeway.BuildOption , cache.LocalCache ) {
221+ func getBuildOpts (cmd * cobra.Command ) ([]leeway.BuildOption , cache.LocalCache , func () ) {
221222 // Track if user explicitly set LEEWAY_DOCKER_EXPORT_TO_CACHE before workspace loading.
222223 // This allows us to distinguish:
223224 // - User set explicitly: High priority (overrides package config)
@@ -320,9 +321,15 @@ func getBuildOpts(cmd *cobra.Command) ([]leeway.BuildOption, cache.LocalCache) {
320321
321322 // Initialize OpenTelemetry reporter if endpoint is configured
322323 var tracerProvider * sdktrace.TracerProvider
324+ var otelShutdown func ()
323325 if otelEndpoint , err := cmd .Flags ().GetString ("otel-endpoint" ); err != nil {
324326 log .Fatal (err )
325327 } else if otelEndpoint != "" {
328+ // Set environment variable for InitTracer if not already set
329+ if os .Getenv ("OTEL_EXPORTER_OTLP_ENDPOINT" ) == "" {
330+ os .Setenv ("OTEL_EXPORTER_OTLP_ENDPOINT" , otelEndpoint )
331+ }
332+
326333 // Initialize tracer
327334 tp , err := telemetry .InitTracer (context .Background ())
328335 if err != nil {
@@ -352,14 +359,14 @@ func getBuildOpts(cmd *cobra.Command) ([]leeway.BuildOption, cache.LocalCache) {
352359 tracer := otel .Tracer ("leeway" )
353360 reporter = append (reporter , leeway .NewOTelReporter (tracer , parentCtx ))
354361
355- // Register shutdown handler
356- defer func () {
362+ // Create shutdown function
363+ otelShutdown = func () {
357364 shutdownCtx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
358365 defer cancel ()
359366 if err := telemetry .Shutdown (shutdownCtx , tracerProvider ); err != nil {
360367 log .WithError (err ).Warn ("failed to shutdown tracer provider" )
361368 }
362- }()
369+ }
363370 }
364371 }
365372
@@ -425,6 +432,11 @@ func getBuildOpts(cmd *cobra.Command) ([]leeway.BuildOption, cache.LocalCache) {
425432 dockerExportSet = true
426433 }
427434
435+ // Create a no-op shutdown function if otelShutdown is nil
436+ if otelShutdown == nil {
437+ otelShutdown = func () {}
438+ }
439+
428440 return []leeway.BuildOption {
429441 leeway .WithLocalCache (localCache ),
430442 leeway .WithRemoteCache (remoteCache ),
@@ -442,7 +454,7 @@ func getBuildOpts(cmd *cobra.Command) ([]leeway.BuildOption, cache.LocalCache) {
442454 leeway .WithInFlightChecksums (inFlightChecksums ),
443455 leeway .WithDockerExportToCache (dockerExportToCache , dockerExportSet ),
444456 leeway .WithDockerExportEnv (dockerExportEnvValue , dockerExportEnvSet ),
445- }, localCache
457+ }, localCache , otelShutdown
446458}
447459
448460type pushOnlyRemoteCache struct {
0 commit comments