Skip to content

Commit c2d30e2

Browse files
authored
[ADDED] Sampling in MsgTrace structure (#219)
When an account has a `Trace` field set with a `Destination`, if an incoming message has a `traceparent` header with proper flag indicating that the message should be trace, the `Sampling` integer (value in the range of [1..100]) allows tracing only a random sample of messages. The server will pick a random number below 100 and if that number is lower or equal to this `Sampling` value then the trace will trigger. If above, the trace will not trigger. Signed-off-by: Ivan Kozlovic <[email protected]>
1 parent 38e07c5 commit c2d30e2

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

v2/account_claims.go

+14
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,16 @@ type Account struct {
237237

238238
// MsgTrace holds distributed message tracing configuration
239239
type MsgTrace struct {
240+
// Destination is the subject the server will send message traces to
241+
// if the inbound message contains the "traceparent" header and has
242+
// its sampled field indicating that the trace should be triggered.
240243
Destination Subject `json:"dest,omitempty"`
244+
// Sampling is used to set the probability sampling, that is, the
245+
// server will get a random number between 1 and 100 and trigger
246+
// the trace if the number is lower than this Sampling value.
247+
// The valid range is [1..100]. If the value is not set Validate()
248+
// will set the value to 100.
249+
Sampling int `json:"sampling,omitempty"`
241250
}
242251

243252
// Validate checks if the account is valid, based on the wrapper
@@ -257,6 +266,11 @@ func (a *Account) Validate(acct *AccountClaims, vr *ValidationResults) {
257266
if a.Trace.Destination.HasWildCards() {
258267
vr.AddError("the account Trace.Destination subject %q is not a valid publish subject", a.Trace.Destination)
259268
}
269+
if a.Trace.Sampling < 0 || a.Trace.Sampling > 100 {
270+
vr.AddError("the account Trace.Sampling value '%d' is not valid, should be in the range [1..100]", a.Trace.Sampling)
271+
} else if a.Trace.Sampling == 0 {
272+
a.Trace.Sampling = 100
273+
}
260274
}
261275

262276
if !a.Limits.IsEmpty() && a.Limits.Imports >= 0 && int64(len(a.Imports)) > a.Limits.Imports {

v2/account_claims_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package jwt
1717

1818
import (
1919
"fmt"
20+
"strings"
2021
"testing"
2122
"time"
2223

@@ -910,3 +911,48 @@ func TestAccountClaimsTraceDest(t *testing.T) {
910911
})
911912
}
912913
}
914+
915+
func TestAccountClaimsTraceDestSampling(t *testing.T) {
916+
akp := createAccountNKey(t)
917+
apk := publicKey(akp, t)
918+
919+
account := NewAccountClaims(apk)
920+
for _, test := range []struct {
921+
name string
922+
dest string
923+
sampling int
924+
expectErr string
925+
}{
926+
{"sampling without destination", "", 10, "subject cannot be empty"},
927+
{"sampling negative", "dest", -1, "should be in the range [1..100]"},
928+
{"sampling above 100", "dest", 101, "should be in the range [1..100]"},
929+
{"sampling at 50", "dest", 50, ""},
930+
{"sampling at zero sets to 100", "dest", 0, ""},
931+
} {
932+
t.Run(test.name, func(t *testing.T) {
933+
account.Trace = &MsgTrace{Destination: Subject(test.dest), Sampling: test.sampling}
934+
vr := CreateValidationResults()
935+
account.Validate(vr)
936+
937+
if test.expectErr == "" {
938+
if !vr.IsEmpty() {
939+
t.Fatalf("account validation should not have failed, got %+v", vr.Issues)
940+
}
941+
if test.sampling == 0 {
942+
if account.Trace.Sampling != 100 {
943+
t.Fatalf("account sampling should have been set to 100 got %d", account.Trace.Sampling)
944+
}
945+
} else if test.sampling != account.Trace.Sampling {
946+
t.Fatalf("account sampling should be %d, got %d", test.sampling, account.Trace.Sampling)
947+
}
948+
} else {
949+
if vr.IsEmpty() {
950+
t.Fatal("account validation should have failed")
951+
}
952+
if !strings.Contains(vr.Issues[0].Description, test.expectErr) {
953+
t.Fatalf("account validation should have failed with error %q, got %q", test.expectErr, vr.Issues[0].Description)
954+
}
955+
}
956+
})
957+
}
958+
}

0 commit comments

Comments
 (0)