Skip to content

Commit

Permalink
feat(sentry): Trim high cardinality strings (#26)
Browse files Browse the repository at this point in the history
* feat(sentry): Trim high cardinatlity strings

* feat(sentry): Trim high cardinatlity strings
  • Loading branch information
samcm authored Nov 25, 2022
1 parent 01ed43e commit 3f7c6ae
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
8 changes: 8 additions & 0 deletions pkg/proto/eth/v1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ func BLSSignatureToString(s *phase0.BLSSignature) string {
func BytesToString(b []byte) string {
return fmt.Sprintf("%#x", b)
}

func TrimmedString(s string) string {
if len(s) <= 12 {
return s
}

return s[:5] + "..." + s[len(s)-5:]
}
50 changes: 50 additions & 0 deletions pkg/proto/eth/v1/conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package v1

import "testing"

func TestTrimmedString(t *testing.T) {
tests := []struct {
name string
s string
want string
}{
{
name: "Empty",
s: "",
want: "",
},
{
name: "Short",
s: "abc",
want: "abc",
},
{
name: "Long",
s: "abcdefg",
want: "abcdefg",
},
{
name: "Longer",
s: "abcdefghijk",
want: "abcdefghijk",
},
{
name: "Longer-trimmed",
s: "abcdefghijklmno",
want: "abcde...klmno",
},
{
name: "hex-trimmed",
s: "0xfd3963b996723a6055b3323014c4de94345a7b519b17758b386d6b57a1a16b6d",
want: "0xfd3...16b6d",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := TrimmedString(tt.s); got != tt.want {
t.Errorf("TrimmedString() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion pkg/sentry/event_attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (s *Sentry) handleAttestation(ctx context.Context, event *phase0.Attestatio
Root: xatuethv1.RootAsString(event.Data.Target.Root),
},
},
Signature: fmt.Sprintf("%#x", event.Signature),
Signature: xatuethv1.TrimmedString(fmt.Sprintf("%#x", event.Signature)),
},
},
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/sentry/event_contribution_and_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ func (s *Sentry) handleContributionAndProof(ctx context.Context, event *altair.S
},
Data: &xatu.DecoratedEvent_EthV1ContributionAndProof{
EthV1ContributionAndProof: &xatuethv1.EventContributionAndProof{
Signature: xatuethv1.BLSSignatureToString(&event.Signature),
Signature: xatuethv1.TrimmedString(xatuethv1.BLSSignatureToString(&event.Signature)),
Message: &xatuethv1.ContributionAndProof{
AggregatorIndex: uint64(event.Message.AggregatorIndex),
SelectionProof: xatuethv1.BLSSignatureToString(&event.Message.SelectionProof),
SelectionProof: xatuethv1.TrimmedString(xatuethv1.BLSSignatureToString(&event.Message.SelectionProof)),
Contribution: &xatuethv1.ContributionAndProof_SyncCommitteeContribution{
Slot: uint64(event.Message.Contribution.Slot),
SubcommitteeIndex: event.Message.Contribution.SubcommitteeIndex,
AggregationBits: xatuethv1.BytesToString(event.Message.Contribution.AggregationBits.Bytes()),
Signature: xatuethv1.BLSSignatureToString(&event.Message.Contribution.Signature),
Signature: xatuethv1.TrimmedString(xatuethv1.BLSSignatureToString(&event.Message.Contribution.Signature)),
BeaconBlockRoot: xatuethv1.RootAsString(event.Message.Contribution.BeaconBlockRoot),
},
},
Expand Down

0 comments on commit 3f7c6ae

Please sign in to comment.