{"record":{"id":"fb672743fe60e3bc","repo":"googleapis/mcp-toolbox","slug":"error-shutting-down-opentelemetry-w","errorCode":null,"errorMessage":"error shutting down OpenTelemetry: %w","messagePattern":"error shutting down OpenTelemetry: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/internal/options.go","lineNumber":119,"sourceCode":"\tctx = util.WithLogger(ctx, logger)\n\topts.Logger = logger\n\n\tctx = util.WithIgnoreUnknownTools(ctx, opts.Cfg.IgnoreUnknownTools)\n\n\tlogger.InfoContext(ctx, fmt.Sprintf(\"Starting MCP Toolbox for Databases version %s\", opts.Cfg.Version))\n\n\t// Set up OpenTelemetry\n\totelShutdown, err := telemetry.SetupOTel(ctx, opts.Cfg.Version, opts.Cfg.TelemetryOTLP, opts.Cfg.TelemetryGCP, opts.Cfg.TelemetryGCPProject, opts.Cfg.TelemetryServiceName)\n\tif err != nil {\n\t\terrMsg := fmt.Errorf(\"error setting up OpenTelemetry: %w\", err)\n\t\tlogger.ErrorContext(ctx, errMsg.Error())\n\t\treturn ctx, nil, errMsg\n\t}\n\n\tshutdownFunc := func(ctx context.Context) error {\n\t\terr := otelShutdown(ctx)\n\t\tif err != nil {\n\t\t\terrMsg := fmt.Errorf(\"error shutting down OpenTelemetry: %w\", err)\n\t\t\tlogger.ErrorContext(ctx, errMsg.Error())\n\t\t\treturn err\n\t\t}\n\t\treturn nil\n\t}\n\n\tinstrumentation, err := telemetry.CreateTelemetryInstrumentation(opts.Cfg.Version)\n\tif err != nil {\n\t\terrMsg := fmt.Errorf(\"unable to create telemetry instrumentation: %w\", err)\n\t\tlogger.ErrorContext(ctx, errMsg.Error())\n\t\treturn ctx, shutdownFunc, errMsg\n\t}\n\n\tctx = util.WithInstrumentation(ctx, instrumentation)\n\n\treturn ctx, shutdownFunc, nil\n}\n","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/cmd/internal/options.go#L101-L137","documentation":"This error is produced by the shutdownFunc closure created in Setup: when the OpenTelemetry shutdown hook (otelShutdown, which flushes and shuts down tracer/meter providers) returns an error, it is wrapped with this message. It occurs at process shutdown, not startup, and typically means telemetry data could not be flushed.","triggerScenarios":"Calling otelShutdown(ctx) during graceful shutdown when an OTLP/GCP exporter fails to flush remaining spans or the provider shutdown returns an error (e.g., shutdown context canceled, exporter network error).","commonSituations":"Fast SIGINT/SIGTERM leaving no time to flush spans to a slow OTLP endpoint, network outage when the process exits, or the shutdown context being canceled before exporters finish.","solutions":["Inspect the wrapped root cause for the failing exporter","Ensure the shutdown context allows enough time for flushing (avoid immediate cancellation)","Check network connectivity to the OTLP collector at exit","Treat as non-fatal if telemetry loss at exit is acceptable — the shutdown error is logged and propagated"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"defer func() {\n    if err := otelShutdown(ctx); err != nil {\n        logger.WarnContext(ctx, \"otel shutdown: %v\", err) // tolerate, don't crash\n    }\n}()","preventionTips":["Allow sufficient time for flushing on shutdown (avoid instant SIGKILL)","Monitor collector availability so spans can flush before exit","Accept and log shutdown flush errors as non-fatal in wrappers"],"tags":["opentelemetry","shutdown","telemetry","go"],"backgroundTag":"telemetry-flush-failure","analyzedSha":"8cc6e09de2ad7b8bffc77751799585a1401a48eb","analyzedAt":"2026-09-05T01:10:36.887Z","contentChangedAt":"2026-09-05T01:10:36.887Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}