-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add auth_addConnection telemetry provider #244
Draft
taldekar
wants to merge
1
commit into
main
Choose a base branch
from
taldekar/AuthTelemetryProviderDesign
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
166 changes: 166 additions & 0 deletions
166
...c/software/aws/toolkits/eclipse/amazonq/telemetry/AuthAddConnectionTelemetryProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package software.aws.toolkits.eclipse.amazonq.telemetry; | ||
|
||
import java.time.Instant; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import software.amazon.awssdk.services.toolkittelemetry.model.MetricDatum; | ||
import software.aws.toolkits.eclipse.amazonq.plugin.Activator; | ||
import software.aws.toolkits.telemetry.AuthTelemetry; | ||
import software.aws.toolkits.telemetry.TelemetryDefinitions; | ||
|
||
public final class AuthAddConnectionTelemetryProvider { | ||
|
||
private static AuthTelemetry.AddConnectionEventBuilder addConnectionBuilder; | ||
private static CompletableFuture<Void> timeoutFuture; | ||
private static long timeoutDuration = 2; | ||
private static TimeUnit timeoutUnit = TimeUnit.MINUTES; | ||
private static final AtomicBoolean IS_AUTHENTICATION_SESSION_ACTIVE = new AtomicBoolean(false); | ||
private static int attemptsCount; | ||
|
||
public enum InputField { | ||
|
||
AUTH_ENABLED_FEATURES("AuthEnabledFeatures"), AUTH_SCOPES("AuthScopes"), | ||
CREDENTIAL_SOURCE_ID("CredentialSourceId"), CREDENTIAL_START_URL("CredentialStartUrl"), FEATURE_ID("FeatureId"), | ||
INVALID_INPUT_FIELDS("InvalidInputFields"), PASSIVE("Passive"), SOURCE("Source"), | ||
SSO_REGISTRATION_CLIENT_ID("SsoRegistrationClientId"), RESULT("Result"), IS_AGGREGATED("IsAggregated"), | ||
IS_REAUTH("IsReAuth"), VALUE("Value"); | ||
|
||
private final String fieldId; | ||
|
||
InputField(final String fieldId) { | ||
this.fieldId = fieldId; | ||
} | ||
|
||
public String getFieldId() { | ||
return fieldId; | ||
} | ||
|
||
public static InputField from(final String fieldId) { | ||
for (InputField field : values()) { | ||
if (field.getFieldId().equals(fieldId)) { | ||
return field; | ||
} | ||
} | ||
throw new IllegalArgumentException("Unknown field ID: " + fieldId); | ||
} | ||
|
||
} | ||
|
||
private AuthAddConnectionTelemetryProvider() { | ||
// prevent initialization | ||
} | ||
|
||
public static void cancel() { | ||
updateField(InputField.RESULT, "Cancelled"); | ||
IS_AUTHENTICATION_SESSION_ACTIVE.set(false); | ||
} | ||
|
||
public static void resetFields() { | ||
addConnectionBuilder = AuthTelemetry.AddConnectionEvent(); | ||
attemptsCount = 0; | ||
IS_AUTHENTICATION_SESSION_ACTIVE.set(true); | ||
} | ||
|
||
public static void setTimeout(final long duration, final TimeUnit unit) { | ||
timeoutDuration = duration; | ||
timeoutUnit = unit; | ||
} | ||
|
||
private static void resetTimeout() { | ||
if (timeoutFuture != null) { | ||
timeoutFuture.cancel(false); | ||
} | ||
|
||
timeoutFuture = new CompletableFuture<Void>().orTimeout(timeoutDuration, timeoutUnit) | ||
.whenComplete((result, exception) -> { | ||
if (exception instanceof TimeoutException) { | ||
if (IS_AUTHENTICATION_SESSION_ACTIVE.get()) { | ||
updateField(InputField.RESULT, "Incomplete"); | ||
} else { | ||
updateField(InputField.RESULT, "Cancelled"); | ||
} | ||
|
||
emitAddConnectionEvent(completed(false)); | ||
} | ||
}); | ||
} | ||
|
||
private static void updateSessionTimeout() { | ||
if (IS_AUTHENTICATION_SESSION_ACTIVE.get()) { | ||
resetTimeout(); // Reset the timeout on each update | ||
} else { | ||
resetFields(); // Start a new session if updating after completion | ||
} | ||
} | ||
|
||
public static void updateField(final InputField inputField, final String value) { | ||
updateSessionTimeout(); | ||
|
||
switch (inputField) { | ||
case AUTH_ENABLED_FEATURES: | ||
addConnectionBuilder.authEnabledFeatures(value); | ||
break; | ||
case AUTH_SCOPES: | ||
addConnectionBuilder.authScopes(value); | ||
break; | ||
case CREDENTIAL_SOURCE_ID: | ||
addConnectionBuilder.credentialSourceId(TelemetryDefinitions.CredentialSourceId.from(value)); | ||
break; | ||
case CREDENTIAL_START_URL: | ||
addConnectionBuilder.credentialStartUrl(value); | ||
break; | ||
case FEATURE_ID: | ||
addConnectionBuilder.featureId(TelemetryDefinitions.FeatureId.from(value)); | ||
break; | ||
case INVALID_INPUT_FIELDS: | ||
addConnectionBuilder.invalidInputFields(value); | ||
break; | ||
case PASSIVE: | ||
addConnectionBuilder.passive(Boolean.parseBoolean(value)); | ||
break; | ||
case SOURCE: | ||
addConnectionBuilder.source(value); | ||
break; | ||
case SSO_REGISTRATION_CLIENT_ID: | ||
addConnectionBuilder.ssoRegistrationClientId(value); | ||
break; | ||
case RESULT: | ||
addConnectionBuilder.result(TelemetryDefinitions.Result.from(value)); | ||
break; | ||
case IS_AGGREGATED: | ||
addConnectionBuilder.isAggregated(Boolean.parseBoolean(value)); | ||
break; | ||
case IS_REAUTH: | ||
addConnectionBuilder.isReAuth(Boolean.parseBoolean(value)); | ||
break; | ||
case VALUE: | ||
addConnectionBuilder.value(Double.parseDouble(value)); | ||
break; | ||
default: | ||
throw new IllegalArgumentException("The input field does not exist"); | ||
} | ||
} | ||
|
||
public static MetricDatum completed(final Boolean success) { | ||
if (!timeoutFuture.isDone()) { | ||
timeoutFuture.complete(null); | ||
addConnectionBuilder.attempts(++attemptsCount); | ||
} | ||
|
||
IS_AUTHENTICATION_SESSION_ACTIVE.set(!success); | ||
updateSessionTimeout(); | ||
|
||
return addConnectionBuilder.createTime(Instant.now()).build(); | ||
} | ||
|
||
public static void emitAddConnectionEvent(final MetricDatum metricDatum) { | ||
Activator.getTelemetryService().emitMetric(metricDatum); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should look to get confirmation if clicking the cancel button during the auth flow should register as an attempt or not.