| 1 | // Copyright The OpenTelemetry Authors |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | package storage // import "go.opentelemetry.io/collector/extension/xextension/storage" |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "errors" |
| 9 | |
| 10 | "go.opentelemetry.io/collector/component" |
| 11 | "go.opentelemetry.io/collector/extension" |
| 12 | ) |
| 13 | |
| 14 | // Extension is the interface that storage extensions must implement |
| 15 | type Extension interface { |
| 16 | extension.Extension |
| 17 | |
| 18 | // GetClient will create a client for use by the specified component. |
| 19 | // Each component can have multiple storages (e.g. one for each signal), |
| 20 | // which can be identified using storageName parameter. |
| 21 | // The component can use the client to manage state |
| 22 | GetClient(ctx context.Context, kind component.Kind, id component.ID, storageName string) (Client, error) |
| 23 | } |
| 24 | |
| 25 | // Client is the interface that storage clients must implement |
| 26 | // All methods should return error only if a problem occurred. |
| 27 | // This mirrors the behavior of a golang map: |
| 28 | // - Set doesn't error if a key already exists - it just overwrites the value. |
| 29 | // - Get doesn't error if a key is not found - it just returns nil. |
| 30 | // - Delete doesn't error if the key doesn't exist - it just no-ops. |
| 31 | // |
| 32 | // Similarly: |
| 33 | // - Batch doesn't error if any of the above happens for either retrieved or updated keys |
| 34 | // |
| 35 | // This also provides a way to differentiate data operations |
| 36 | // |
| 37 | // [overwrite | not-found | no-op] from "real" problems |
| 38 | type Client interface { |
| 39 | // Get will retrieve data from storage that corresponds to the |
| 40 | // specified key. It should return (nil, nil) if not found |
| 41 | Get(ctx context.Context, key string) ([]byte, error) |
| 42 | |
| 43 | // Set will store data. The data can be retrieved by the same |
| 44 | // component after a process restart, using the same key |
| 45 | Set(ctx context.Context, key string, value []byte) error |
| 46 | |
| 47 | // Delete will delete data associated with the specified key |
| 48 | Delete(ctx context.Context, key string) error |
| 49 | |
| 50 | // Batch handles specified operations in batch. Get operation results are put in-place |
| 51 | Batch(ctx context.Context, ops ...*Operation) error |
| 52 | |
| 53 | // Close will release any resources held by the client |
| 54 | Close(ctx context.Context) error |
| 55 | } |
| 56 | |
| 57 | type OpType int |
| 58 | |
| 59 | const ( |
| 60 | Get OpType = iota |
| 61 | Set |
| 62 | Delete |
| 63 | ) |
| 64 | |
| 65 | type Operation struct { |
| 66 | // Key specifies key which is going to be get/set/deleted |
| 67 | Key string |
| 68 | // Value specifies value that is going to be set or holds result of get operation |
| 69 | Value []byte |
| 70 | // Type describes the operation type |
| 71 | Type OpType |
| 72 | } |
| 73 | |
| 74 | func SetOperation(key string, value []byte) *Operation { |
| 75 | return &Operation{ |
| 76 | Key: key, |
| 77 | Value: value, |
| 78 | Type: Set, |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func GetOperation(key string) *Operation { |
| 83 | return &Operation{ |
| 84 | Key: key, |
| 85 | Type: Get, |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func DeleteOperation(key string) *Operation { |
| 90 | return &Operation{ |
| 91 | Key: key, |
| 92 | Type: Delete, |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | var ErrStorageFull = errors.New("the storage extension has run out of available space") |