Skip to content

Commit c9f2d37

Browse files
committed
telemetry(auth): emit metric for createToken
1 parent 3249e0e commit c9f2d37

File tree

3 files changed

+87
-19
lines changed

3 files changed

+87
-19
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ mockitoKotlin = "5.4.1-SNAPSHOT"
2828
mockk = "1.13.17"
2929
nimbus-jose-jwt = "9.40"
3030
node-gradle = "7.0.2"
31-
telemetryGenerator = "1.0.329"
31+
telemetryGenerator = "1.0.338"
3232
testLogger = "4.0.0"
3333
testRetry = "1.5.10"
3434
# test-only; platform provides slf4j transitively at runtime

plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/core/credentials/sso/SsoAccessTokenProvider.kt

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,33 @@ class SsoAccessTokenProvider(
338338
throw ProcessCanceledException(IllegalStateException("Login canceled by user"))
339339
}
340340

341-
val tokenResponse = client.createToken {
342-
it.clientId(registration.clientId)
343-
it.clientSecret(registration.clientSecret)
344-
it.grantType(DEVICE_GRANT_TYPE)
345-
it.deviceCode(authorization.deviceCode)
341+
val startTime = clock.instant()
342+
val tokenResponse = try {
343+
client.createToken {
344+
it.clientId(registration.clientId)
345+
it.clientSecret(registration.clientSecret)
346+
it.grantType(DEVICE_GRANT_TYPE)
347+
it.deviceCode(authorization.deviceCode)
348+
}.also {
349+
val duration = Duration.between(startTime, clock.instant()).toMillis().toDouble()
350+
AuthTelemetry.ssoTokenOperation(
351+
result = Result.Succeeded,
352+
grantType = DEVICE_GRANT_TYPE,
353+
duration = duration
354+
)
355+
LOG.info { "SSO token operation succeeded: grantType=$DEVICE_GRANT_TYPE, duration=${duration}ms" }
356+
}
357+
} catch (e: Exception) {
358+
val duration = Duration.between(startTime, clock.instant()).toMillis().toDouble()
359+
AuthTelemetry.ssoTokenOperation(
360+
result = Result.Failed,
361+
grantType = DEVICE_GRANT_TYPE,
362+
duration = duration,
363+
reason = e::class.simpleName,
364+
reasonDesc = e.message?.let { scrubNames(it) }
365+
)
366+
LOG.warn { "SSO token operation failed: grantType=$DEVICE_GRANT_TYPE, duration=${duration}ms, error=${e::class.simpleName}" }
367+
throw e
346368
}
347369

348370
onPendingToken.tokenRetrieved()
@@ -459,11 +481,33 @@ class SsoAccessTokenProvider(
459481

460482
stageName = RefreshCredentialStage.CREATE_TOKEN
461483
try {
462-
val newToken = client.createToken {
463-
it.clientId(registration.clientId)
464-
it.clientSecret(registration.clientSecret)
465-
it.grantType(REFRESH_GRANT_TYPE)
466-
it.refreshToken(currentToken.refreshToken)
484+
val startTime = clock.instant()
485+
val newToken = try {
486+
client.createToken {
487+
it.clientId(registration.clientId)
488+
it.clientSecret(registration.clientSecret)
489+
it.grantType(REFRESH_GRANT_TYPE)
490+
it.refreshToken(currentToken.refreshToken)
491+
}.also {
492+
val duration = Duration.between(startTime, clock.instant()).toMillis().toDouble()
493+
AuthTelemetry.ssoTokenOperation(
494+
result = Result.Succeeded,
495+
grantType = REFRESH_GRANT_TYPE,
496+
duration = duration
497+
)
498+
LOG.info { "SSO token operation succeeded: grantType=$REFRESH_GRANT_TYPE, duration=${duration}ms" }
499+
}
500+
} catch (e: Exception) {
501+
val duration = Duration.between(startTime, clock.instant()).toMillis().toDouble()
502+
AuthTelemetry.ssoTokenOperation(
503+
result = Result.Failed,
504+
grantType = REFRESH_GRANT_TYPE,
505+
duration = duration,
506+
reason = e::class.simpleName,
507+
reasonDesc = e.message?.let { scrubNames(it) }
508+
)
509+
LOG.warn { "SSO token operation failed: grantType=$REFRESH_GRANT_TYPE, duration=${duration}ms, error=${e::class.simpleName}" }
510+
throw e
467511
}
468512

469513
stageName = RefreshCredentialStage.GET_TOKEN_DETAILS

plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/core/credentials/sso/pkce/ToolkitOAuthService.kt

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@ import software.aws.toolkits.jetbrains.core.credentials.sso.PKCEAuthorizationGra
3232
import software.aws.toolkits.jetbrains.core.credentials.sso.PKCEClientRegistration
3333
import software.aws.toolkits.jetbrains.core.credentials.sso.bearer.buildUnmanagedSsoOidcClient
3434
import software.aws.toolkits.jetbrains.core.gettingstarted.editor.SourceOfEntry
35+
import software.aws.toolkits.jetbrains.services.telemetry.scrubNames
3536
import software.aws.toolkits.resources.AwsCoreBundle
37+
import software.aws.toolkits.telemetry.AuthTelemetry
3638
import software.aws.toolkits.telemetry.AuthType
3739
import software.aws.toolkits.telemetry.AwsTelemetry
3840
import software.aws.toolkits.telemetry.MetricResult
41+
import software.aws.toolkits.telemetry.Result
3942
import java.math.BigInteger
4043
import java.time.Instant
4144
import java.util.Base64
@@ -149,15 +152,36 @@ internal class ToolkitOauthCredentialsAcquirer(
149152
private val redirectUri: String,
150153
) : OAuthCredentialsAcquirer<AccessToken> {
151154
override fun acquireCredentials(code: String): OAuthCredentialsAcquirer.AcquireCredentialsResult<AccessToken> {
152-
val token = buildUnmanagedSsoOidcClient(registration.region).use { client ->
153-
client.createToken {
154-
it.clientId(registration.clientId)
155-
it.clientSecret(registration.clientSecret)
156-
it.grantType("authorization_code")
157-
it.redirectUri(redirectUri)
158-
it.codeVerifier(codeVerifier)
159-
it.code(code)
155+
val grantType = "authorization_code"
156+
val startTime = Instant.now()
157+
val token = try {
158+
buildUnmanagedSsoOidcClient(registration.region).use { client ->
159+
client.createToken {
160+
it.clientId(registration.clientId)
161+
it.clientSecret(registration.clientSecret)
162+
it.grantType(grantType)
163+
it.redirectUri(redirectUri)
164+
it.codeVerifier(codeVerifier)
165+
it.code(code)
166+
}.also {
167+
val duration = java.time.Duration.between(startTime, Instant.now()).toMillis().toDouble()
168+
AuthTelemetry.ssoTokenOperation(
169+
result = Result.Succeeded,
170+
grantType = grantType,
171+
duration = duration
172+
)
173+
}
160174
}
175+
} catch (e: Exception) {
176+
val duration = java.time.Duration.between(startTime, Instant.now()).toMillis().toDouble()
177+
AuthTelemetry.ssoTokenOperation(
178+
result = Result.Failed,
179+
grantType = grantType,
180+
duration = duration,
181+
reason = e::class.simpleName,
182+
reasonDesc = e.message?.let { scrubNames(it) }
183+
)
184+
throw e
161185
}
162186

163187
return OAuthCredentialsAcquirer.AcquireCredentialsResult.Success(

0 commit comments

Comments
 (0)