rpc: use "+" concatination in hot path RPC rate limit metrics. (#16923)

This commit is contained in:
James Rasell 2023-04-18 13:41:34 +01:00 committed by GitHub
parent 37ddd2dd86
commit 367cfa6d93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 4 deletions

View file

@ -515,15 +515,15 @@ func (ai *AuthenticatedIdentity) String() string {
return "unauthenticated" return "unauthenticated"
} }
if ai.ACLToken != nil { if ai.ACLToken != nil {
return fmt.Sprintf("token:%s", ai.ACLToken.AccessorID) return "token:" + ai.ACLToken.AccessorID
} }
if ai.Claims != nil { if ai.Claims != nil {
return fmt.Sprintf("alloc:%s", ai.Claims.AllocationID) return "alloc:" + ai.Claims.AllocationID
} }
if ai.ClientID != "" { if ai.ClientID != "" {
return fmt.Sprintf("client:%s", ai.ClientID) return "client:" + ai.ClientID
} }
return fmt.Sprintf("%s:%s", ai.TLSName, ai.RemoteIP.String()) return ai.TLSName + ":" + ai.RemoteIP.String()
} }
func (ai *AuthenticatedIdentity) IsExpired(now time.Time) bool { func (ai *AuthenticatedIdentity) IsExpired(now time.Time) bool {

View file

@ -5,6 +5,7 @@ package structs
import ( import (
"fmt" "fmt"
"net"
"os" "os"
"reflect" "reflect"
"strings" "strings"
@ -22,6 +23,62 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func TestAuthenticatedIdentity_String(t *testing.T) {
ci.Parallel(t)
testCases := []struct {
name string
inputAuthenticatedIdentity *AuthenticatedIdentity
expectedOutput string
}{
{
name: "nil",
inputAuthenticatedIdentity: nil,
expectedOutput: "unauthenticated",
},
{
name: "ACL token",
inputAuthenticatedIdentity: &AuthenticatedIdentity{
ACLToken: &ACLToken{
AccessorID: "my-testing-accessor-id",
},
},
expectedOutput: "token:my-testing-accessor-id",
},
{
name: "alloc claim",
inputAuthenticatedIdentity: &AuthenticatedIdentity{
Claims: &IdentityClaims{
AllocationID: "my-testing-alloc-id",
},
},
expectedOutput: "alloc:my-testing-alloc-id",
},
{
name: "client",
inputAuthenticatedIdentity: &AuthenticatedIdentity{
ClientID: "my-testing-client-id",
},
expectedOutput: "client:my-testing-client-id",
},
{
name: "tls remote IP",
inputAuthenticatedIdentity: &AuthenticatedIdentity{
TLSName: "my-testing-tls-name",
RemoteIP: net.IPv4(192, 168, 135, 232),
},
expectedOutput: "my-testing-tls-name:192.168.135.232",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actualOutput := tc.inputAuthenticatedIdentity.String()
must.Eq(t, tc.expectedOutput, actualOutput)
})
}
}
func TestJob_Validate(t *testing.T) { func TestJob_Validate(t *testing.T) {
ci.Parallel(t) ci.Parallel(t)