Skip to content

Commit 9ba4bce

Browse files
fix: properly shutdown OpenTelemetry tracer after build completes
Move tracer shutdown from getBuildOpts to build command Run function to ensure spans are flushed after the build completes, not immediately after getBuildOpts returns. - Change getBuildOpts to return shutdown function - Update all callers to handle the new return value - Set OTEL_EXPORTER_OTLP_ENDPOINT from CLI flag before InitTracer Tested with Jaeger and confirmed traces are now properly sent. Co-authored-by: Ona <no-reply@ona.com>
1 parent 18421a6 commit 9ba4bce

File tree

7 files changed

+29
-11
lines changed

7 files changed

+29
-11
lines changed

cmd/build.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

448460
type pushOnlyRemoteCache struct {

cmd/build_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func TestGetBuildOptsWithInFlightChecksums(t *testing.T) {
242242
}
243243

244244
// Test getBuildOpts function
245-
opts, localCache := getBuildOpts(cmd)
245+
opts, localCache, _ := getBuildOpts(cmd)
246246

247247
// We can't directly test the WithInFlightChecksums option since it's internal,
248248
// but we can verify the function doesn't error and returns options

cmd/provenance-assert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func getProvenanceTarget(cmd *cobra.Command, args []string) (bundleFN, pkgFN str
125125
log.Fatal("provenance export requires a package")
126126
}
127127

128-
_, cache := getBuildOpts(cmd)
128+
_, cache, _ := getBuildOpts(cmd)
129129

130130
var ok bool
131131
pkgFN, ok = cache.Location(pkg)

cmd/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Should any of the scripts fail Leeway will exit with an exit code of 1 once all
2727
if script == nil {
2828
return errors.New("run needs a script")
2929
}
30-
opts, _ := getBuildOpts(cmd)
30+
opts, _, _ := getBuildOpts(cmd)
3131
return script.Run(opts...)
3232
})
3333
}

cmd/sbom-export.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ If no package is specified, the workspace's default target is used.`,
3232
}
3333

3434
// Get build options and cache
35-
_, localCache := getBuildOpts(cmd)
35+
_, localCache, _ := getBuildOpts(cmd)
3636

3737
// Get output format and file
3838
format, _ := cmd.Flags().GetString("format")

cmd/sbom-scan.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ If no package is specified, the workspace's default target is used.`,
3030
}
3131

3232
// Get cache
33-
_, localCache := getBuildOpts(cmd)
33+
_, localCache, _ := getBuildOpts(cmd)
3434

3535
// Get output directory
3636
outputDir, _ := cmd.Flags().GetString("output-dir")

leeway-dev.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
# Helper script to run leeway from source with go run
3+
# Usage: ./leeway-dev.sh build :package --otel-endpoint=localhost:4318
4+
5+
cd "$(dirname "$0")"
6+
exec go run . "$@"

0 commit comments

Comments
 (0)