|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package persistentqueue // import "go.opentelemetry.io/collector/exporter/exporterhelper/internal/queuebatch/internal/persistentqueue" |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "encoding/binary" |
| 9 | + "errors" |
| 10 | + "fmt" |
| 11 | + "io" |
| 12 | + "math" |
| 13 | + |
| 14 | + "go.opentelemetry.io/otel/propagation" |
| 15 | + |
| 16 | + "go.opentelemetry.io/collector/featuregate" |
| 17 | +) |
| 18 | + |
| 19 | +// persistRequestContextFeatureGate controls whether request context should be preserved in the persistent queue. |
| 20 | +var persistRequestContextFeatureGate = featuregate.GlobalRegistry().MustRegister( |
| 21 | + "exporter.PersistRequestContext", |
| 22 | + featuregate.StageAlpha, |
| 23 | + featuregate.WithRegisterFromVersion("v0.128.0"), |
| 24 | + featuregate.WithRegisterDescription("controls whether context should be stored alongside requests in the persistent queue"), |
| 25 | +) |
| 26 | + |
| 27 | +type Encoding[T any] interface { |
| 28 | + // Marshal is a function that can marshal a request into bytes. |
| 29 | + Marshal(T) ([]byte, error) |
| 30 | + |
| 31 | + // Unmarshal is a function that can unmarshal bytes into a request. |
| 32 | + Unmarshal([]byte) (T, error) |
| 33 | +} |
| 34 | + |
| 35 | +// Encoder provides an interface for marshaling and unmarshaling requests along with their context. |
| 36 | +type Encoder[T any] struct { |
| 37 | + encoding Encoding[T] |
| 38 | +} |
| 39 | + |
| 40 | +func NewEncoder[T any](encoding Encoding[T]) Encoder[T] { |
| 41 | + return Encoder[T]{ |
| 42 | + encoding: encoding, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// requestDataKey is the key used to store request data in bytesMap. |
| 47 | +const requestDataKey = "req" |
| 48 | + |
| 49 | +var tracePropagator = propagation.TraceContext{} |
| 50 | + |
| 51 | +func (re Encoder[T]) Marshal(ctx context.Context, req T) ([]byte, error) { |
| 52 | + if !persistRequestContextFeatureGate.IsEnabled() { |
| 53 | + return re.encoding.Marshal(req) |
| 54 | + } |
| 55 | + |
| 56 | + bm := newBytesMap() |
| 57 | + tracePropagator.Inject(ctx, &bytesMapCarrier{bytesMap: bm}) |
| 58 | + reqBuf, err := re.encoding.Marshal(req) |
| 59 | + if err != nil { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + if err := bm.set(requestDataKey, reqBuf); err != nil { |
| 63 | + return nil, fmt.Errorf("failed to marshal request: %w", err) |
| 64 | + } |
| 65 | + |
| 66 | + return *bm, nil |
| 67 | +} |
| 68 | + |
| 69 | +func (re Encoder[T]) Unmarshal(b []byte) (T, context.Context, error) { |
| 70 | + if !persistRequestContextFeatureGate.IsEnabled() { |
| 71 | + req, err := re.encoding.Unmarshal(b) |
| 72 | + return req, context.Background(), err |
| 73 | + } |
| 74 | + |
| 75 | + bm := bytesMapFromBytes(b) |
| 76 | + if bm == nil { |
| 77 | + // Fall back to unmarshalling of the request alone. |
| 78 | + // This can happen if the data persisted by the version that doesn't support the context unmarshaling. |
| 79 | + req, err := re.encoding.Unmarshal(b) |
| 80 | + return req, context.Background(), err |
| 81 | + } |
| 82 | + ctx := tracePropagator.Extract(context.Background(), &bytesMapCarrier{bytesMap: bm}) |
| 83 | + reqBuf, err := bm.get(requestDataKey) |
| 84 | + var req T |
| 85 | + if err != nil { |
| 86 | + return req, context.Background(), fmt.Errorf("failed to read serialized request data: %w", err) |
| 87 | + } |
| 88 | + req, err = re.encoding.Unmarshal(reqBuf) |
| 89 | + return req, ctx, err |
| 90 | +} |
| 91 | + |
| 92 | +// bytesMap is a slice of bytes that represents a map-like structure for storing key-value pairs. |
| 93 | +// It's optimized for efficient memory usage for low number of key-value pairs with big values. |
| 94 | +// The format is a sequence of key-value pairs encoded as: |
| 95 | +// - 1 byte length of the key |
| 96 | +// - key bytes |
| 97 | +// - 4 byte length of the value |
| 98 | +// - value bytes |
| 99 | +type bytesMap []byte |
| 100 | + |
| 101 | +// prefix bytes to denote the bytesMap serialization: 0x00 magic byte + 0x01 version of the encoder. |
| 102 | +const ( |
| 103 | + magicByte = byte(0x00) |
| 104 | + formatV1Byte = byte(0x01) |
| 105 | + prefixBytesLen = 2 |
| 106 | + initialCapacity = 256 |
| 107 | +) |
| 108 | + |
| 109 | +func newBytesMap() *bytesMap { |
| 110 | + bm := bytesMap(make([]byte, 0, initialCapacity)) |
| 111 | + bm = append(bm, magicByte, formatV1Byte) |
| 112 | + return &bm |
| 113 | +} |
| 114 | + |
| 115 | +// set sets the specified key in the map. Must be called only once for each key. |
| 116 | +func (bm *bytesMap) set(key string, val []byte) error { |
| 117 | + if len(key) > math.MaxUint8 { |
| 118 | + return errors.New("key param is too long") |
| 119 | + } |
| 120 | + valSize := len(val) |
| 121 | + if uint64(valSize) > math.MaxUint32 { |
| 122 | + return fmt.Errorf("value is too large to persist, size %d", valSize) |
| 123 | + } |
| 124 | + |
| 125 | + *bm = append(*bm, byte(len(key))) |
| 126 | + *bm = append(*bm, key...) |
| 127 | + |
| 128 | + var lenBuf [4]byte |
| 129 | + binary.LittleEndian.PutUint32(lenBuf[:], uint32(valSize)) //nolint:gosec // disable G115 |
| 130 | + *bm = append(*bm, lenBuf[:]...) |
| 131 | + *bm = append(*bm, val...) |
| 132 | + |
| 133 | + return nil |
| 134 | +} |
| 135 | + |
| 136 | +// get scans sequentially for the first matching key and returns the value as bytes. |
| 137 | +func (bm *bytesMap) get(k string) ([]byte, error) { |
| 138 | + for i := prefixBytesLen; i < len(*bm); { |
| 139 | + kl := int([]byte(*bm)[i]) |
| 140 | + i++ |
| 141 | + |
| 142 | + if i+kl > len(*bm) { |
| 143 | + return nil, io.ErrUnexpectedEOF |
| 144 | + } |
| 145 | + key := string([]byte(*bm)[i : i+kl]) |
| 146 | + i += kl |
| 147 | + |
| 148 | + if i+4 > len(*bm) { |
| 149 | + return nil, io.ErrUnexpectedEOF |
| 150 | + } |
| 151 | + vLen := binary.LittleEndian.Uint32([]byte(*bm)[i:]) |
| 152 | + i += 4 |
| 153 | + |
| 154 | + if i+int(vLen) > len(*bm) { |
| 155 | + return nil, io.ErrUnexpectedEOF |
| 156 | + } |
| 157 | + val := []byte(*bm)[i : i+int(vLen)] |
| 158 | + i += int(vLen) |
| 159 | + |
| 160 | + if key == k { |
| 161 | + return val, nil |
| 162 | + } |
| 163 | + } |
| 164 | + return nil, nil |
| 165 | +} |
| 166 | + |
| 167 | +// keys returns header names in encounter order. |
| 168 | +func (bm *bytesMap) keys() []string { |
| 169 | + var out []string |
| 170 | + for i := prefixBytesLen; i < len(*bm); { |
| 171 | + kl := int([]byte(*bm)[i]) |
| 172 | + i++ |
| 173 | + |
| 174 | + if i+kl > len(*bm) { |
| 175 | + break // malformed entry |
| 176 | + } |
| 177 | + out = append(out, string([]byte(*bm)[i:i+kl])) |
| 178 | + i += kl |
| 179 | + |
| 180 | + if i+4 > len(*bm) { |
| 181 | + break // malformed entry |
| 182 | + } |
| 183 | + vLen := binary.LittleEndian.Uint32([]byte(*bm)[i:]) |
| 184 | + i += 4 + int(vLen) |
| 185 | + } |
| 186 | + return out |
| 187 | +} |
| 188 | + |
| 189 | +func bytesMapFromBytes(b []byte) *bytesMap { |
| 190 | + if len(b) < prefixBytesLen || b[0] != magicByte || b[1] != formatV1Byte { |
| 191 | + return nil |
| 192 | + } |
| 193 | + return (*bytesMap)(&b) |
| 194 | +} |
| 195 | + |
| 196 | +// bytesMapCarrier implements propagation.TextMapCarrier on top of bytesMap. |
| 197 | +type bytesMapCarrier struct { |
| 198 | + *bytesMap |
| 199 | +} |
| 200 | + |
| 201 | +var _ propagation.TextMapCarrier = (*bytesMapCarrier)(nil) |
| 202 | + |
| 203 | +// Set appends a new string entry; if the key already exists it is left unchanged. |
| 204 | +func (c *bytesMapCarrier) Set(k, v string) { |
| 205 | + _ = c.set(k, []byte(v)) |
| 206 | +} |
| 207 | + |
| 208 | +// Get scans sequentially for the first matching key. |
| 209 | +func (c *bytesMapCarrier) Get(k string) string { |
| 210 | + v, _ := c.get(k) |
| 211 | + return string(v) |
| 212 | +} |
| 213 | + |
| 214 | +// Keys returns header names in encounter order. |
| 215 | +func (c *bytesMapCarrier) Keys() []string { |
| 216 | + return c.keys() |
| 217 | +} |
0 commit comments