Documentation ¶
Overview ¶
Package splunkleveldb provides OpenTelemetry instrumentation for the github.com/syndtr/goleveldb/leveldb package.
Example ¶
package main import ( "context" "log" "github.com/syndtr/goleveldb/leveldb/storage" "go.opentelemetry.io/otel" "github.com/unionai/splunk-otel-go/instrumentation/github.com/syndtr/goleveldb/leveldb/splunkleveldb" ) func main() { // Open a new database backed by memory storage. memstore := storage.NewMemStorage() // Ensure span is used as a parent for all spans the database will create. db, err := splunkleveldb.Open(memstore, nil) if err != nil { // Assume corruptions and attept a recover. db, err = splunkleveldb.Recover(memstore, nil) if err != nil { log.Fatal(err) } } defer func() { if cErr := db.Close(); cErr != nil { log.Fatal(cErr) } }() ctx, span := otel.Tracer("my-inst").Start(context.Background(), "main") defer span.End() // Ensure span is used as a parent for all spans the database will create. db = db.WithContext(ctx) // Write to the database. A span is created to trace this operation. if err = db.Put([]byte("key"), []byte("value"), nil); err != nil { log.Println("failed to write data", err) } // Read back the data. A span is created to trace this operation. data, err := db.Get([]byte("key"), nil) if err != nil { log.Println("failed to read data", err) } else { log.Println("read from database:", data) } // Delete the data for the key. A span is created to trace this operation. if err = db.Delete([]byte("key"), nil); err != nil { log.Println("failed to delete data", err) } }
Output:
Index ¶
- func Version() string
- func WrapIterator(it iterator.Iterator, opts ...Option) iterator.Iterator
- type DB
- func Open(stor storage.Storage, o *opt.Options, opts ...Option) (*DB, error)
- func OpenFile(path string, o *opt.Options, opts ...Option) (*DB, error)
- func Recover(stor storage.Storage, o *opt.Options, opts ...Option) (*DB, error)
- func RecoverFile(path string, o *opt.Options, opts ...Option) (*DB, error)
- func WrapDB(db *leveldb.DB, opts ...Option) *DB
- func (db *DB) CompactRange(r util.Range) error
- func (db *DB) Delete(key []byte, wo *opt.WriteOptions) error
- func (db *DB) Get(key []byte, ro *opt.ReadOptions) (value []byte, err error)
- func (db *DB) GetSnapshot() (*Snapshot, error)
- func (db *DB) Has(key []byte, ro *opt.ReadOptions) (ret bool, err error)
- func (db *DB) NewIterator(slice *util.Range, ro *opt.ReadOptions) iterator.Iterator
- func (db *DB) OpenTransaction() (*Transaction, error)
- func (db *DB) Put(key, value []byte, wo *opt.WriteOptions) error
- func (db *DB) WithContext(ctx context.Context) *DB
- func (db *DB) Write(batch *leveldb.Batch, wo *opt.WriteOptions) error
- type Option
- type Snapshot
- func (snap *Snapshot) Get(key []byte, ro *opt.ReadOptions) (value []byte, err error)
- func (snap *Snapshot) Has(key []byte, ro *opt.ReadOptions) (ret bool, err error)
- func (snap *Snapshot) NewIterator(slice *util.Range, ro *opt.ReadOptions) iterator.Iterator
- func (snap *Snapshot) WithContext(ctx context.Context) *Snapshot
- type Transaction
- func (tr *Transaction) Commit() error
- func (tr *Transaction) Delete(key []byte, wo *opt.WriteOptions) error
- func (tr *Transaction) Discard()
- func (tr *Transaction) Get(key []byte, ro *opt.ReadOptions) (value []byte, err error)
- func (tr *Transaction) Has(key []byte, ro *opt.ReadOptions) (ret bool, err error)
- func (tr *Transaction) NewIterator(slice *util.Range, ro *opt.ReadOptions) iterator.Iterator
- func (tr *Transaction) Put(key, value []byte, wo *opt.WriteOptions) error
- func (tr *Transaction) WithContext(ctx context.Context) *Transaction
- func (tr *Transaction) Write(b *leveldb.Batch, wo *opt.WriteOptions) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type DB ¶
DB wraps a *leveldb.DB, tracing all operations performed.
func Open ¶
Open opens or creates a traced DB for the given storage. The DB will be created if not exist, unless ErrorIfMissing is true. Also, if ErrorIfExist is true and the DB exist Open will returns os.ErrExist error.
Open will return an error with type of ErrCorrupted if corruption detected in the DB. Use errors.IsCorrupted to test whether an error is due to corruption. Corrupted DB can be recovered with Recover function.
The returned DB instance is safe for concurrent use. The DB must be closed after use, by calling Close method.
Example ¶
package main import ( "log" "github.com/syndtr/goleveldb/leveldb/storage" "github.com/unionai/splunk-otel-go/instrumentation/github.com/syndtr/goleveldb/leveldb/splunkleveldb" ) func main() { memstore := storage.NewMemStorage() db, err := splunkleveldb.Open(memstore, nil) if err != nil { // Assume corruptions and attept a recover. db, err = splunkleveldb.Recover(memstore, nil) if err != nil { log.Fatal(err) } } defer func() { if err := db.Close(); err != nil { log.Fatal(err) } }() }
Output:
func OpenFile ¶
OpenFile opens or creates a traced DB for the given path. The DB will be created if not exist, unless ErrorIfMissing is true. Also, if ErrorIfExist is true and the DB exist OpenFile will returns os.ErrExist error.
OpenFile uses standard file-system backed storage implementation as described in the leveldb/storage package.
OpenFile will return an error with type of ErrCorrupted if corruption detected in the DB. Use errors.IsCorrupted to test whether an error is due to corruption. Corrupted DB can be recovered with Recover function.
The returned DB instance is safe for concurrent use. The DB must be closed after use, by calling Close method.
Example ¶
package main import ( "log" "github.com/unionai/splunk-otel-go/instrumentation/github.com/syndtr/goleveldb/leveldb/splunkleveldb" ) func main() { dbPath := "/path/to/db" db, err := splunkleveldb.OpenFile(dbPath, nil) if err != nil { // Assume corruptions and attept a recover. db, err = splunkleveldb.RecoverFile(dbPath, nil) if err != nil { log.Fatal(err) } log.Fatal(err) } defer func() { if err := db.Close(); err != nil { log.Fatal(err) } }() }
Output:
func Recover ¶
Recover recovers and opens a traced DB with missing or corrupted manifest files for the given storage. It will ignore any manifest files, valid or not. The DB must already exist or it will returns an error. Also, Recover will ignore ErrorIfMissing and ErrorIfExist options.
The returned DB instance is safe for concurrent use. The DB must be closed after use, by calling Close method.
func RecoverFile ¶
RecoverFile recovers and opens a traced DB with missing or corrupted manifest files for the given path. It will ignore any manifest files, valid or not. The DB must already exist or it will returns an error. Also, Recover will ignore ErrorIfMissing and ErrorIfExist options.
RecoverFile uses standard file-system backed storage implementation as described in the leveldb/storage package.
The returned DB instance is safe for concurrent use. The DB must be closed after use, by calling Close method.
func (*DB) CompactRange ¶
CompactRange compacts the underlying traced DB for the given key range. In particular, deleted and overwritten versions are discarded, and the data is rearranged to reduce the cost of operations needed to access the data. This operation should typically only be invoked by users who understand the underlying implementation.
A nil Range.Start is treated as a key before all keys in the DB. And a nil Range.Limit is treated as a key after all keys in the DB. Therefore if both is nil then it will compact entire DB.
func (*DB) Delete ¶
func (db *DB) Delete(key []byte, wo *opt.WriteOptions) error
Delete deletes the value for the given key. Delete will not returns error if key doesn't exist. Write merge also applies for Delete, see Write.
It is safe to modify the contents of the arguments after Delete returns but not before.
func (*DB) Get ¶
Get gets the value for the given key. It returns ErrNotFound if the DB does not contains the key.
The returned slice is its own copy, it is safe to modify the contents of the returned slice. It is safe to modify the contents of the argument after Get returns.
func (*DB) GetSnapshot ¶
GetSnapshot returns a latest snapshot of the underlying DB. A snapshot is a frozen snapshot of a DB state at a particular point in time. The content of snapshot are guaranteed to be consistent.
The snapshot must be released after use, by calling Release method.
func (*DB) Has ¶
Has returns true if the DB does contains the given key.
It is safe to modify the contents of the argument after Has returns.
func (*DB) NewIterator ¶
NewIterator returns a traced iterator for the latest snapshot of the underlying DB. The returned iterator is not safe for concurrent use, but it is safe to use multiple iterators concurrently, with each in a dedicated goroutine. It is also safe to use an iterator concurrently with modifying its underlying DB. The resultant key/value pairs are guaranteed to be consistent.
Slice allows slicing the iterator to only contains keys in the given range. A nil Range.Start is treated as a key before all keys in the DB. And a nil Range.Limit is treated as a key after all keys in the DB.
WARNING: Any slice returned by interator (e.g. slice returned by calling Iterator.Key() or Iterator.Key() methods), its content should not be modified unless noted otherwise.
The iterator must be released after use, by calling Release method.
Also read Iterator documentation of the leveldb/iterator package.
func (*DB) OpenTransaction ¶
func (db *DB) OpenTransaction() (*Transaction, error)
OpenTransaction opens an atomic DB transaction. Only one transaction can be opened at a time. Subsequent call to Write and OpenTransaction will be blocked until in-flight transaction is committed or discarded. The returned transaction handle is safe for concurrent use.
Transaction is expensive and can overwhelm compaction, especially if transaction size is small. Use with caution.
The transaction must be closed once done, either by committing or discarding the transaction. Closing the DB will discard open transaction.
func (*DB) Put ¶
func (db *DB) Put(key, value []byte, wo *opt.WriteOptions) error
Put sets the value for the given key. It overwrites any previous value for that key; a DB is not a multi-map. Write merge also applies for Put, see Write.
It is safe to modify the contents of the arguments after Put returns but not before.
func (*DB) WithContext ¶
WithContext returns a new DB that will use ctx. If ctx contains any active spans of a trace, all traced operations of the returned DB will be represented as child spans of that active span.
func (*DB) Write ¶
Write apply the given batch to the DB. The batch records will be applied sequentially. Write might be used concurrently, when used concurrently and batch is small enough, write will try to merge the batches. Set NoWriteMerge option to true to disable write merge.
It is safe to modify the contents of the arguments after Write returns but not before. Write will not modify content of the batch.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option applies options to a configuration.
func WithAttributes ¶
WithAttributes returns an Option that appends attr to the attributes set for every span created with this instrumentation library.
func WithContext ¶
WithContext returns an Option that sets the Context used with this instrumentation library by default. This is used to pass context of any existing trace to the instrumentation.
func WithTracerProvider ¶
func WithTracerProvider(tp trace.TracerProvider) Option
WithTracerProvider returns an Option that sets the TracerProvider used with this instrumentation library.
type Snapshot ¶
Snapshot wraps a leveldb.Snapshot, tracing all operations performed.
func WrapSnapshot ¶
WrapSnapshot returns a traced *Snapshot that wraps a *leveldb.Snapshot.
func (*Snapshot) Get ¶
Get gets the value for the given key. It returns ErrNotFound if the DB does not contains the key.
The caller should not modify the contents of the returned slice, but it is safe to modify the contents of the argument after Get returns.
func (*Snapshot) Has ¶
Has returns true if the DB does contains the given key.
It is safe to modify the contents of the argument after Get returns.
func (*Snapshot) NewIterator ¶
NewIterator returns a traced iterator for the snapshot of the underlying DB. The returned iterator is not safe for concurrent use, but it is safe to use multiple iterators concurrently, with each in a dedicated goroutine. It is also safe to use an iterator concurrently with modifying its underlying DB. The resultant key/value pairs are guaranteed to be consistent.
Slice allows slicing the iterator to only contains keys in the given range. A nil Range.Start is treated as a key before all keys in the DB. And a nil Range.Limit is treated as a key after all keys in the DB.
WARNING: Any slice returned by interator (e.g. slice returned by calling Iterator.Key() or Iterator.Value() methods), its content should not be modified unless noted otherwise.
The iterator must be released after use, by calling Release method. Releasing the snapshot doesn't mean releasing the iterator too, the iterator would be still valid until released.
Also read Iterator documentation of the leveldb/iterator package.
type Transaction ¶
type Transaction struct { *leveldb.Transaction // contains filtered or unexported fields }
Transaction wraps a *leveldb.Transaction, tracing all operations performed.
func WrapTransaction ¶
func WrapTransaction(tr *leveldb.Transaction, opts ...Option) *Transaction
WrapTransaction returns a traced Transaction that wraps a *leveldb.Transaction.
func (*Transaction) Commit ¶
func (tr *Transaction) Commit() error
Commit commits the transaction. If error is not nil, then the transaction is not committed, it can then either be retried or discarded.
Other methods should not be called after transaction has been committed.
func (*Transaction) Delete ¶
func (tr *Transaction) Delete(key []byte, wo *opt.WriteOptions) error
Delete deletes the value for the given key. Please note that the transaction is not compacted until committed, so if you writes 10 same keys, then those 10 same keys are in the transaction.
It is safe to modify the contents of the arguments after Delete returns.
func (*Transaction) Discard ¶
func (tr *Transaction) Discard()
Discard discards the transaction.
Other methods should not be called after transaction has been discarded.
func (*Transaction) Get ¶
func (tr *Transaction) Get(key []byte, ro *opt.ReadOptions) (value []byte, err error)
Get gets the value for the given key. It returns ErrNotFound if the DB does not contains the key.
The returned slice is its own copy, it is safe to modify the contents of the returned slice. It is safe to modify the contents of the argument after Get returns.
func (*Transaction) Has ¶
func (tr *Transaction) Has(key []byte, ro *opt.ReadOptions) (ret bool, err error)
Has returns true if the DB does contains the given key.
It is safe to modify the contents of the argument after Has returns.
func (*Transaction) NewIterator ¶
func (tr *Transaction) NewIterator(slice *util.Range, ro *opt.ReadOptions) iterator.Iterator
NewIterator returns a traced iterator for the latest snapshot of the transaction. The returned iterator is not safe for concurrent use, but it is safe to use multiple iterators concurrently, with each in a dedicated goroutine. It is also safe to use an iterator concurrently while writes to the transaction. The resultant key/value pairs are guaranteed to be consistent.
Slice allows slicing the iterator to only contains keys in the given range. A nil Range.Start is treated as a key before all keys in the DB. And a nil Range.Limit is treated as a key after all keys in the DB.
WARNING: Any slice returned by interator (e.g. slice returned by calling Iterator.Key() or Iterator.Key() methods), its content should not be modified unless noted otherwise.
The iterator must be released after use, by calling Release method.
Also read Iterator documentation of the leveldb/iterator package.
func (*Transaction) Put ¶
func (tr *Transaction) Put(key, value []byte, wo *opt.WriteOptions) error
Put sets the value for the given key. It overwrites any previous value for that key; a DB is not a multi-map. Please note that the transaction is not compacted until committed, so if you writes 10 same keys, then those 10 same keys are in the transaction.
It is safe to modify the contents of the arguments after Put returns.
func (*Transaction) WithContext ¶
func (tr *Transaction) WithContext(ctx context.Context) *Transaction
WithContext returns a new Transaction that will use ctx. If ctx contains any active spans of a trace, all traced operations of the returned DB will be represented as child spans of that active span.
func (*Transaction) Write ¶
func (tr *Transaction) Write(b *leveldb.Batch, wo *opt.WriteOptions) error
Write applies the given batch to the transaction. The batch will be applied sequentially. Please note that the transaction is not compacted until committed, so if you writes 10 same keys, then those 10 same keys are in the transaction.
It is safe to modify the contents of the arguments after Write returns.