feat(go/adbc): add context.Context support for uniform OpenTelemetry instrumentation#4009
Conversation
- Add DatabaseContext, ConnectionContext, StatementContext interfaces - Add extension Context interfaces (PostInitOptions, GetSetOptions, etc.) - Add context_adapters.go with DatabaseContext adapter implementation - Add context_adapters_test.go with tests for DatabaseContext adapter - Update .gitignore to exclude docs/plans Relates to apache#2772
Implement adapter to wrap existing Connection and Statement implementations as ConnectionContext and StatementContext for backward compatibility. Relates to apache#2772
Ensure adapters handle nil inputs correctly and return nil when given nil inputs. Tests verify that AsDatabaseContext, AsConnectionContext, and AsStatementContext all handle nil correctly. Note: Tests for avoiding double-wrapping are not applicable because Database/DatabaseContext, Connection/ConnectionContext, and Statement/StatementContext have conflicting method signatures (Close() vs Close(ctx)), making it impossible for a type to implement both interfaces simultaneously. Relates to apache#2772
- Update package docs to explain Context interfaces - Add IngestStreamContext helper for context-aware ingestion - Add example code demonstrating Context usage Relates to apache#2772
lidavidm
left a comment
There was a problem hiding this comment.
Overall LGTM but some comments on whether we may want to consolidate some things
| // Any connection specific options should be set using SetOptions before | ||
| // calling Open. | ||
| type DriverWithContext interface { | ||
| NewDatabaseWithContext(ctx context.Context, opts map[string]string) (Database, error) |
There was a problem hiding this comment.
Given we aren't suffixing the other methods, maybe it's ok to break this (experimental) API and call it just NewDatabase now?
There was a problem hiding this comment.
Changing this has a knock-on effect of causing us to have to update and cause breaking changes to the bigquery and databricks drivers here which currently implement both NewDatabase and NewDatabaseWithContext. If we change this, we have to update all of those drivers by removing the version without a context. Are we okay with that?
There was a problem hiding this comment.
This ends up being a HUGE refactor having to update EVERY go-based driver in here 😦 I think it's not worth doing, at least not as part of this PR
…instrumentation (apache#4009) ## Summary Implements context.Context support for all Go ADBC methods to enable uniform OpenTelemetry instrumentation, cancellation, and deadline propagation (addresses apache#2772). - Add `DatabaseContext`, `ConnectionContext`, and `StatementContext` interfaces that mirror existing interfaces but require `context.Context` for all methods - Add extension Context interfaces: `PostInitOptionsContext`, `GetSetOptionsContext`, `ConnectionGetStatisticsContext`, `StatementExecuteSchemaContext` - Implement adapter functions (`AsDatabaseContext`, `AsConnectionContext`, `AsStatementContext`) to wrap non-context implementations for backward compatibility - Add `IngestStreamContext` helper for context-aware bulk ingestion - Comprehensive test suite (9 tests, all passing) ## Changes **New Interfaces** (`go/adbc/adbc.go`): - `DatabaseContext` - Database with context support for all operations - `ConnectionContext` - Connection with context support for all operations - `StatementContext` - Statement with context support for all operations - Extension interfaces with context support **Adapter Implementation** (`go/adbc/context_adapters.go`): - Three adapter types that wrap non-context implementations - Proper nil handling and double-wrap prevention - Context pass-through for methods that already accept context - Clear documentation about context limitations for legacy methods **Tests** (`go/adbc/context_adapters_test.go`): - Comprehensive test coverage for all adapters - Tests for error handling, nil handling, and context pass-through - All tests passing with race detector **Documentation & Examples**: - Updated package documentation explaining Context interfaces - Added `IngestStreamContext` helper function - Example code demonstrating adapter usage ## Migration Path **For Applications:** ```go // Wrap existing Database to add context support db := driver.NewDatabase(opts) dbCtx := adbc.AsDatabaseContext(db) ctx := context.Background() conn, err := dbCtx.Open(ctx) ``` **For Driver Implementers:** - Existing drivers work unchanged via adapters - New drivers should implement Context interfaces directly - Gradual migration supported - no breaking changes ## Notes This is the first step toward uniform OpenTelemetry support. Future work includes: - Updating individual drivers to implement Context interfaces natively - Adding context-aware tests to validation suite - Enhanced tracing instrumentation using context propagation Closes apache#2772.
| // Any connection specific options should be set using SetOptions before | ||
| // calling Open. | ||
| type DriverWithContext interface { | ||
| NewDatabaseWithContext(ctx context.Context, opts map[string]string) (Database, error) |
There was a problem hiding this comment.
I missed this but this needs to return DatabaseWithContext
There was a problem hiding this comment.
Ah, that runs back into the huge refactor...I guess I'll work around this in driverbase
Summary
Implements context.Context support for all Go ADBC methods to enable uniform OpenTelemetry instrumentation, cancellation, and deadline propagation (addresses #2772).
DatabaseContext,ConnectionContext, andStatementContextinterfaces that mirror existing interfaces but requirecontext.Contextfor all methodsPostInitOptionsContext,GetSetOptionsContext,ConnectionGetStatisticsContext,StatementExecuteSchemaContextAsDatabaseContext,AsConnectionContext,AsStatementContext) to wrap non-context implementations for backward compatibilityIngestStreamContexthelper for context-aware bulk ingestionChanges
New Interfaces (
go/adbc/adbc.go):DatabaseContext- Database with context support for all operationsConnectionContext- Connection with context support for all operationsStatementContext- Statement with context support for all operationsAdapter Implementation (
go/adbc/context_adapters.go):Tests (
go/adbc/context_adapters_test.go):Documentation & Examples:
IngestStreamContexthelper functionMigration Path
For Applications:
For Driver Implementers:
Notes
This is the first step toward uniform OpenTelemetry support. Future work includes:
Closes #2772.