-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Log request information as transaction metadata
Signed-off-by: Stefano Scafiti <[email protected]>
- Loading branch information
Showing
23 changed files
with
466 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package schema | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
) | ||
|
||
const maxMetadataLen = 256 | ||
|
||
var ( | ||
ErrEmptyMetadataKey = errors.New("metadata key cannot be empty") | ||
ErrEmptyMetadataValue = errors.New("metadata value cannot be empty") | ||
ErrMetadataTooLarge = errors.New("metadata exceeds maximum size") | ||
ErrCorruptedMetadata = errors.New("corrupted metadata") | ||
) | ||
|
||
const ( | ||
UserRequestMetadataKey = "usr" | ||
IpRequestMetadataKey = "ip" | ||
) | ||
|
||
type Metadata map[string]string | ||
|
||
func (m Metadata) Marshal() ([]byte, error) { | ||
if err := m.validate(); err != nil { | ||
return nil, err | ||
} | ||
|
||
var data [maxMetadataLen]byte | ||
|
||
off := 0 | ||
for k, v := range m { | ||
data[off] = byte(len(k) - 1) | ||
data[off+1] = byte(len(v) - 1) | ||
|
||
off += 2 | ||
copy(data[off:], []byte(k)) | ||
off += len(k) | ||
|
||
copy(data[off:], []byte(v)) | ||
off += len(v) | ||
} | ||
return data[:off], nil | ||
} | ||
|
||
func (m Metadata) validate() error { | ||
size := 0 | ||
for k, v := range m { | ||
if len(k) == 0 { | ||
return ErrEmptyMetadataKey | ||
} | ||
|
||
if len(v) == 0 { | ||
return ErrEmptyMetadataValue | ||
} | ||
|
||
size += len(k) + len(v) + 2 | ||
|
||
if size > maxMetadataLen { | ||
return ErrMetadataTooLarge | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (m Metadata) Unmarshal(data []byte) error { | ||
off := 0 | ||
for off <= len(data)-2 { | ||
keySize := int(data[off]) + 1 | ||
valueSize := int(data[off+1]) + 1 | ||
|
||
off += 2 | ||
|
||
if off+keySize+valueSize > len(data) { | ||
return ErrCorruptedMetadata | ||
} | ||
|
||
m[string(data[off:off+keySize])] = string(data[off+keySize : off+keySize+valueSize]) | ||
|
||
off += keySize + valueSize | ||
} | ||
|
||
if off != len(data) { | ||
return ErrCorruptedMetadata | ||
} | ||
return nil | ||
} | ||
|
||
type metadataKey struct{} | ||
|
||
func ContextWithMetadata(ctx context.Context, md Metadata) context.Context { | ||
return context.WithValue(ctx, metadataKey{}, md) | ||
} | ||
|
||
func MetadataFromContext(ctx context.Context) Metadata { | ||
md, ok := ctx.Value(metadataKey{}).(Metadata) | ||
if !ok { | ||
return nil | ||
} | ||
return md | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package schema | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMetadataMarshalUnmarshal(t *testing.T) { | ||
meta := Metadata{ | ||
"user": "default", | ||
"ip": "127.0.0.1:8080", | ||
} | ||
|
||
data, err := meta.Marshal() | ||
require.NoError(t, err) | ||
|
||
t.Run("valid metadata", func(t *testing.T) { | ||
unmarshalled := Metadata{} | ||
err := unmarshalled.Unmarshal(data) | ||
require.NoError(t, err) | ||
require.Equal(t, meta, unmarshalled) | ||
}) | ||
|
||
t.Run("corrupted metadata", func(t *testing.T) { | ||
unmarshalled := Metadata{} | ||
err := unmarshalled.Unmarshal(data[:len(data)/2]) | ||
require.ErrorIs(t, err, ErrCorruptedMetadata) | ||
}) | ||
|
||
t.Run("empty metadata", func(t *testing.T) { | ||
m := Metadata{} | ||
data, err := m.Marshal() | ||
require.NoError(t, err) | ||
require.Empty(t, data) | ||
|
||
unmarshalled := Metadata{} | ||
err = unmarshalled.Unmarshal([]byte{}) | ||
require.NoError(t, err) | ||
require.Empty(t, unmarshalled) | ||
}) | ||
|
||
t.Run("invalid metadata", func(t *testing.T) { | ||
x := make([]byte, 256) | ||
|
||
m := Metadata{"x": string(x)} | ||
_, err := m.Marshal() | ||
require.ErrorIs(t, err, ErrMetadataTooLarge) | ||
|
||
m = Metadata{"": "v"} | ||
_, err = m.Marshal() | ||
require.ErrorIs(t, err, ErrEmptyMetadataKey) | ||
|
||
m = Metadata{"k": ""} | ||
_, err = m.Marshal() | ||
require.ErrorIs(t, err, ErrEmptyMetadataValue) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.