Skip to content

Commit

Permalink
serverident-key
Browse files Browse the repository at this point in the history
  • Loading branch information
RaduBerinde committed Dec 11, 2024
1 parent 8eb57c3 commit c15aadc
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
10 changes: 5 additions & 5 deletions pkg/base/serverident/server_ident.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,31 @@ import "context"
// an import dependency cycle. See SetSystemTenantID.
var SystemTenantID string

// ServerIdentificationContextKey is the type of a context.Value key
// serverIdentificationContextKey is the type of a context.Value key
// used to carry ServerIdentificationPayload values.
type ServerIdentificationContextKey struct{}
type serverIdentificationContextKey struct{}

// ContextWithServerIdentification returns a context annotated with the provided
// server identity. Use ServerIdentificationFromContext(ctx) to retrieve it from
// the ctx later.
func ContextWithServerIdentification(
ctx context.Context, serverID ServerIdentificationPayload,
) context.Context {
return context.WithValue(ctx, ServerIdentificationContextKey{}, serverID)
return context.WithValue(ctx, serverIdentificationContextKey{}, serverID)
}

// ServerIdentificationFromContext retrieves the server identity put in the
// context by ContextWithServerIdentification.
func ServerIdentificationFromContext(ctx context.Context) ServerIdentificationPayload {
r := ctx.Value(ServerIdentificationContextKey{})
r := ctx.Value(serverIdentificationContextKey{})
if r == nil {
return nil
}
return r.(ServerIdentificationPayload)
}

// ServerIdentificationPayload is the type of a context.Value payload
// associated with a ServerIdentificationContextKey.
// associated with a serverIdentificationContextKey.
type ServerIdentificationPayload interface {
// ServerIdentityString retrieves an identifier corresponding to the
// given retrieval key. If there is no value known for a given key,
Expand Down
4 changes: 2 additions & 2 deletions pkg/server/testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2509,14 +2509,14 @@ func TestingMakeLoggingContexts(
appTenantID roachpb.TenantID,
) (sysContext, appContext context.Context) {
ctxSysTenant := context.Background()
ctxSysTenant = context.WithValue(ctxSysTenant, serverident.ServerIdentificationContextKey{}, &idProvider{
ctxSysTenant = serverident.ContextWithServerIdentification(ctxSysTenant, &idProvider{
tenantID: roachpb.SystemTenantID,
clusterID: &base.ClusterIDContainer{},
serverID: &base.NodeIDContainer{},
tenantName: roachpb.NewTenantNameContainer("system"),
})
ctxAppTenant := context.Background()
ctxAppTenant = context.WithValue(ctxAppTenant, serverident.ServerIdentificationContextKey{}, &idProvider{
ctxAppTenant = serverident.ContextWithServerIdentification(ctxAppTenant, &idProvider{
tenantID: appTenantID,
clusterID: &base.ClusterIDContainer{},
serverID: &base.NodeIDContainer{},
Expand Down
6 changes: 3 additions & 3 deletions pkg/util/log/format_crdb_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,21 @@ func TestFormatCrdbV2(t *testing.T) {

sysCtx := context.Background()
sysIDPayload := testIDPayload{tenantID: "1"}
sysCtx = context.WithValue(sysCtx, serverident.ServerIdentificationContextKey{}, sysIDPayload)
sysCtx = serverident.ContextWithServerIdentification(sysCtx, sysIDPayload)
sysCtx = logtags.AddTag(sysCtx, "noval", nil)
sysCtx = logtags.AddTag(sysCtx, "s", "1")
sysCtx = logtags.AddTag(sysCtx, "long", "2")

tenantIDPayload := testIDPayload{tenantID: "2"}
tenantCtx := context.Background()
tenantCtx = context.WithValue(tenantCtx, serverident.ServerIdentificationContextKey{}, tenantIDPayload)
tenantCtx = serverident.ContextWithServerIdentification(tenantCtx, tenantIDPayload)
tenantCtx = logtags.AddTag(tenantCtx, "noval", nil)
tenantCtx = logtags.AddTag(tenantCtx, "p", "3")
tenantCtx = logtags.AddTag(tenantCtx, "longKey", "456")

namedTenantIDPayload := tenantIDPayload
namedTenantIDPayload.tenantName = "abc"
namedTenantCtx := context.WithValue(tenantCtx, serverident.ServerIdentificationContextKey{}, namedTenantIDPayload)
namedTenantCtx := serverident.ContextWithServerIdentification(tenantCtx, namedTenantIDPayload)

defer func(prev int) { crdbV2LongLineLen.set(prev) }(int(crdbV2LongLineLen))
crdbV2LongLineLen.set(1024)
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/log/format_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestJSONFormats(t *testing.T) {

ctx := context.Background()
sysIDPayload := testIDPayload{tenantID: "1"}
ctx = context.WithValue(ctx, serverident.ServerIdentificationContextKey{}, sysIDPayload)
ctx = serverident.ContextWithServerIdentification(ctx, sysIDPayload)
ctx = logtags.AddTag(ctx, "noval", nil)
ctx = logtags.AddTag(ctx, "s", "1")
ctx = logtags.AddTag(ctx, "long", "2")
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/log/redact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestRedactedLogOutput(t *testing.T) {

ctx := context.Background()
sysIDPayload := testIDPayload{tenantID: "1"}
ctx = context.WithValue(ctx, serverident.ServerIdentificationContextKey{}, sysIDPayload)
ctx = serverident.ContextWithServerIdentification(ctx, sysIDPayload)

Errorf(ctx, "test1 %v end", "hello")
if contains(redactableIndicator, t) {
Expand Down

0 comments on commit c15aadc

Please sign in to comment.