Skip to content

Commit

Permalink
broadcaster introspection: clog update to add keys (livepeer#2842)
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-astra-video authored and eliteprox committed Feb 21, 2024
1 parent cf33339 commit d4a0a0c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
6 changes: 4 additions & 2 deletions clog/clog.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ const (
nonce = "nonce"
seqNo = "seqNo"
orchSessionID = "orchSessionID" // session id generated on orchestrator for broadcaster
ethaddress = "ethaddress"
orchestrator = "orchestrator"
)

// Verbose is a boolean type that implements Infof (like Printf) etc.
type Verbose bool

var stdKeys map[string]bool
var stdKeysOrder = []string{manifestID, sessionID, nonce, seqNo, orchSessionID}
var publicLogKeys = []string{manifestID, sessionID, orchSessionID, ClientIP}
var stdKeysOrder = []string{manifestID, sessionID, nonce, seqNo, orchSessionID, ethaddress, orchestrator}
var publicLogKeys = []string{manifestID, sessionID, orchSessionID, ClientIP, seqNo, ethaddress, orchestrator}

func init() {
stdKeys = make(map[string]bool)
Expand Down
22 changes: 15 additions & 7 deletions clog/clog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ func TestStdKeys(t *testing.T) {
ctx = AddNonce(ctx, 1038)
ctx = AddOrchSessionID(ctx, "orchID")
ctx = AddSeqNo(ctx, 9427)
ctx = AddVal(ctx, "ethaddress", "0x0")
ctx = AddVal(ctx, "orchestrator", "http://127.0.0.1:8935")
ctx = AddVal(ctx, "customKey", "customVal")
msg, _ := formatMessage(ctx, false, false, "testing message num=%d", 452)
assert.Equal("manifestID=manID sessionID=sessionID nonce=1038 seqNo=9427 orchSessionID=orchID customKey=customVal testing message num=452", msg)
assert.Equal("manifestID=manID sessionID=sessionID nonce=1038 seqNo=9427 orchSessionID=orchID ethaddress=0x0 orchestrator=http://127.0.0.1:8935 customKey=customVal testing message num=452", msg)
ctxCloned := Clone(context.Background(), ctx)
ctxCloned = AddManifestID(ctxCloned, "newManifest")
msgCloned, _ := formatMessage(ctxCloned, false, false, "testing message num=%d", 4521)
assert.Equal("manifestID=newManifest sessionID=sessionID nonce=1038 seqNo=9427 orchSessionID=orchID customKey=customVal testing message num=4521", msgCloned)
assert.Equal("manifestID=newManifest sessionID=sessionID nonce=1038 seqNo=9427 orchSessionID=orchID ethaddress=0x0 orchestrator=http://127.0.0.1:8935 customKey=customVal testing message num=4521", msgCloned)
// old context shouldn't change
msg, _ = formatMessage(ctx, false, false, "testing message num=%d", 452)
assert.Equal("manifestID=manID sessionID=sessionID nonce=1038 seqNo=9427 orchSessionID=orchID customKey=customVal testing message num=452", msg)
assert.Equal("manifestID=manID sessionID=sessionID nonce=1038 seqNo=9427 orchSessionID=orchID ethaddress=0x0 orchestrator=http://127.0.0.1:8935 customKey=customVal testing message num=452", msg)
}

func TestLastErr(t *testing.T) {
Expand All @@ -47,9 +49,11 @@ func TestPublicLogs(t *testing.T) {
ctx := AddManifestID(context.Background(), "fooManID")
ctx = AddSessionID(ctx, "fooSessionID")
ctx = AddOrchSessionID(ctx, "fooOrchID")
ctx = AddSeqNo(ctx, 555)
ctx = AddVal(ctx, "ethaddress", "0x0")
ctx = AddVal(ctx, "orchestrator", "http://127.0.0.1:8935")
// These should not be visible:
ctx = AddNonce(ctx, 999)
ctx = AddSeqNo(ctx, 555)
ctx = AddVal(ctx, "foo", "Bar")

publicCtx := PublicCloneCtx(ctx, context.Background(), publicLogKeys)
Expand All @@ -61,16 +65,20 @@ func TestPublicLogs(t *testing.T) {
assert.Equal("fooSessionID", val)
val = GetVal(publicCtx, orchSessionID)
assert.Equal("fooOrchID", val)
val = GetVal(publicCtx, seqNo)
assert.Equal("555", val)
val = GetVal(publicCtx, "ethaddress")
assert.Equal("0x0", val)
val = GetVal(publicCtx, "orchestrator")
assert.Equal("http://127.0.0.1:8935", val)

// Verify random keys cannot be leaked:
val = GetVal(publicCtx, nonce)
assert.Equal("", val)
val = GetVal(publicCtx, seqNo)
assert.Equal("", val)
val = GetVal(publicCtx, "foo")
assert.Equal("", val)

// Verify [PublicLogs] gets pre-pended:
msg, _ := formatMessage(ctx, false, true, "testing message num=%d", 123)
assert.Equal("[PublicLogs] manifestID=fooManID sessionID=fooSessionID nonce=999 seqNo=555 orchSessionID=fooOrchID foo=Bar testing message num=123", msg)
assert.Equal("[PublicLogs] manifestID=fooManID sessionID=fooSessionID nonce=999 seqNo=555 orchSessionID=fooOrchID ethaddress=0x0 orchestrator=http://127.0.0.1:8935 foo=Bar testing message num=123", msg)
}

0 comments on commit d4a0a0c

Please sign in to comment.