close
Skip to content

feat(go/adbc): add context.Context support for uniform OpenTelemetry instrumentation#4009

Merged
zeroshade merged 8 commits into
apache:mainfrom
zeroshade:go-contexts
Feb 26, 2026
Merged

feat(go/adbc): add context.Context support for uniform OpenTelemetry instrumentation#4009
zeroshade merged 8 commits into
apache:mainfrom
zeroshade:go-contexts

Conversation

@zeroshade
Copy link
Copy Markdown
Member

@zeroshade zeroshade commented Feb 20, 2026

Summary

Implements context.Context support for all Go ADBC methods to enable uniform OpenTelemetry instrumentation, cancellation, and deadline propagation (addresses #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:

// 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 #2772.

- 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
Comment thread .gitignore
Comment thread go/adbc/adbc.go Outdated
Comment thread go/adbc/adbc.test Outdated
Comment thread go/adbc/adbc.go
Copy link
Copy Markdown
Member

@lidavidm lidavidm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM but some comments on whether we may want to consolidate some things

Comment thread go/adbc/adbc.go Outdated
Comment thread go/adbc/context_adapters.go
Comment thread go/adbc/adbc.go
// 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)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given we aren't suffixing the other methods, maybe it's ok to break this (experimental) API and call it just NewDatabase now?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Darn, alright.

Comment thread go/adbc/adbc.go Outdated
@zeroshade zeroshade requested a review from lidavidm February 25, 2026 16:54
@lidavidm lidavidm added this to the ADBC Libraries 23 milestone Feb 26, 2026
@zeroshade zeroshade merged commit a8a3454 into apache:main Feb 26, 2026
40 checks passed
kentkwu added a commit to kentkwu/arrow-adbc that referenced this pull request Mar 4, 2026
…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.
Comment thread go/adbc/adbc.go
// 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)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed this but this needs to return DatabaseWithContext

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that runs back into the huge refactor...I guess I'll work around this in driverbase

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

go: all methods should always take a context.Context

2 participants