Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import com.microsoft.identity.common.java.flighting.CommonFlight;
import com.microsoft.identity.common.java.flighting.CommonFlightsManager;
import com.microsoft.identity.common.java.opentelemetry.AttributeName;
import com.microsoft.identity.common.java.opentelemetry.BaggageExtension;
import com.microsoft.identity.common.java.opentelemetry.OTelUtility;
import com.microsoft.identity.common.java.opentelemetry.SpanExtension;
import com.microsoft.identity.common.java.opentelemetry.SpanName;
Expand All @@ -86,7 +87,9 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.security.Principal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.TimeUnit;
Expand All @@ -102,6 +105,7 @@
import static com.microsoft.identity.common.java.exception.ClientException.UNKNOWN_ERROR;
import static com.microsoft.identity.common.java.flighting.CommonFlight.ENABLE_PLAYSTORE_URL_LAUNCH;

import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.StatusCode;
Expand Down Expand Up @@ -579,9 +583,7 @@ private void processSwitchBrowserRequest(@NonNull final String url) {
protected void processWebsiteRequest(@NonNull final WebView view, @NonNull final String url) {
final String methodTag = TAG + ":processWebsiteRequest";
view.stopLoading();
final SpanContext spanContext = getActivity() instanceof AuthorizationActivity ? ((AuthorizationActivity) getActivity()).getSpanContext() : null;
final Span span = spanContext != null ?
OTelUtility.createSpanFromParent(SpanName.ProcessWebsiteRequest.name(), spanContext) : OTelUtility.createSpan(SpanName.ProcessWebsiteRequest.name());
final Span span = createSpanWithAttributesFromParent(SpanName.ProcessWebsiteRequest.name());
span.setAttribute(AttributeName.is_in_web_cp_flow.name(), mInWebCpFlow);
try (final Scope scope = SpanExtension.makeCurrentSpan(span)) {
if (isDeviceCaRequest(url)) {
Expand Down Expand Up @@ -692,9 +694,7 @@ private boolean shouldLaunchCompanyPortal() {
@VisibleForTesting
protected void loadDeviceCaUrl(@NonNull final String originalUrl, @NonNull final WebView view) {
final String methodTag = TAG + ":loadDeviceCaUrl";
final SpanContext spanContext = getActivity() instanceof AuthorizationActivity ? ((AuthorizationActivity) getActivity()).getSpanContext() : null;
final Span span = spanContext != null ?
OTelUtility.createSpanFromParent(SpanName.ProcessWebCpRedirects.name(), spanContext) : OTelUtility.createSpan(SpanName.ProcessWebCpRedirects.name());
final Span span = createSpanWithAttributesFromParent(SpanName.ProcessWebCpRedirects.name());
try (final Scope scope = SpanExtension.makeCurrentSpan(span)) {
if (isWebCpInWebviewFeatureEnabled(originalUrl)) {
Logger.info(methodTag, "Loading device CA request in WebView.");
Expand Down Expand Up @@ -739,7 +739,7 @@ protected boolean isWebCpInWebviewFeatureEnabled(@NonNull final String originalU
final int waitForFlightsTimeOut = CommonFlightsManager.INSTANCE.getFlightsProvider().getIntValue(CommonFlight.WEB_CP_WAIT_TIMEOUT_FOR_FLIGHTS);
final boolean isWebCpFlightEnabled = CommonFlightsManager.INSTANCE.getFlightsProviderForTenant(homeTenantId, waitForFlightsTimeOut).isFlightEnabled(CommonFlight.ENABLE_WEB_CP_IN_WEBVIEW);
SpanExtension.current().setAttribute(AttributeName.web_cp_flight_get_time.name(), (System.currentTimeMillis() - webCpGetFlightStartTime));

SpanExtension.current().setAttribute(AttributeName.tenant_id.name(), homeTenantId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems you are adding tenant id here, which is good

if (isWebCpFlightEnabled) {
// Directly enabled via flight rollout.
Logger.info(methodTag, "WebCP in WebView feature is enabled.");
Expand Down Expand Up @@ -767,11 +767,8 @@ private String getHomeTenantIdFromUrl(@NonNull final String url) {
// This is a special case where the enrollment is not done in the WebView, but rather in the browser.
private void processWebCpEnrollmentUrl(@NonNull final WebView view, @NonNull final String url) {
final String methodTag = TAG + ":processWebCpEnrollmentUrl";
final SpanContext spanContext = getActivity() instanceof AuthorizationActivity ? ((AuthorizationActivity) getActivity()).getSpanContext() : null;
final Span span = spanContext != null ?
OTelUtility.createSpanFromParent(SpanName.ProcessWebCpRedirects.name(), spanContext) : OTelUtility.createSpan(SpanName.ProcessWebCpRedirects.name());
final Span span = createSpanWithAttributesFromParent(SpanName.ProcessWebCpEnrollmentRedirect.name());
try (final Scope scope = SpanExtension.makeCurrentSpan(span)) {
span.setAttribute(AttributeName.is_webcp_enrollment_request.name(), true);
view.stopLoading();
Logger.info(methodTag, "Loading WebCP enrollment url in browser.");
// This is a WebCP enrollment URL, so we need to open it in the browser (it does not work in WebView as google enrollment is enforced to be done in browser).
Expand Down Expand Up @@ -1064,10 +1061,7 @@ private void processNonceAndReAttachHeaders(@NonNull final WebView view, @NonNul
private void processWebCpAuthorize(@NonNull final WebView view, @NonNull final String url) {
final String methodTag = TAG + ":processWebCPAuthorize";
Logger.info(methodTag, "Processing WebCP authorize request.");
final SpanContext spanContext = getActivity() instanceof AuthorizationActivity ? ((AuthorizationActivity) getActivity()).getSpanContext() : null;
final Span span = spanContext != null ?
OTelUtility.createSpanFromParent(SpanName.ProcessWebCpRedirects.name(), spanContext) : OTelUtility.createSpan(SpanName.ProcessWebCpRedirects.name());
span.setAttribute(AttributeName.is_webcp_authorize_request.name(), true);
final Span span = createSpanWithAttributesFromParent(SpanName.ProcessWebCpAuthorizeUrlRedirect.name());
final ReAttachPrtHeaderHandler reAttachPrtHeaderHandler = new ReAttachPrtHeaderHandler(view, mRequestHeaders, span);
reAttachPrtHeader(url, reAttachPrtHeaderHandler, view, methodTag, span);
}
Expand Down Expand Up @@ -1209,4 +1203,33 @@ private String getBrokerAppPackageNameFromUrl(@NonNull final String url) {
return "";
}

/**
* Create a span with parent span context if available.
* @param spanName Name of the span to be created.
* @return Created {@link Span}
*/
private Span createSpanWithAttributesFromParent(@NonNull final String spanName) {
final SpanContext spanContext = getActivity() instanceof AuthorizationActivity ? ((AuthorizationActivity) getActivity()).getSpanContext() : null;
final Span span = spanContext != null ?
OTelUtility.createSpanFromParent(spanName, spanContext) : OTelUtility.createSpan(spanName);
if (mUtid != null) {
span.setAttribute(AttributeName.tenant_id.name(), mUtid);
}
// Populate some of the parent span's attributes to current span.
final Context oTelContext = getActivity() instanceof AuthorizationActivity ? ((AuthorizationActivity) getActivity()).getOtelContext() : null;
if (oTelContext != null) {
final Baggage baggage = BaggageExtension.fromContext(oTelContext);
final List<AttributeName> parentAttributeNames = Arrays.asList(
AttributeName.correlation_id,
AttributeName.calling_package_name
);
for (AttributeName attributeName : parentAttributeNames) {
final String value = baggage.getEntryValue(attributeName.name());
if (value != null) {
span.setAttribute(attributeName.name(), value);
}
}
}
return span;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -413,16 +413,6 @@ public enum AttributeName {
*/
current_broker_package_name,

/**
* Records if the request is a webcp authorize request.
*/
is_webcp_authorize_request,

/**
* Records if the request is a webcp enrollment request.
*/
is_webcp_enrollment_request,

/**
* Records if the webcp is enabled in webview.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,7 @@ public enum SpanName {
ProcessWebCpRedirects,

ProvisionResourceAccount,
ProcessWebsiteRequest
ProcessWebsiteRequest,
ProcessWebCpEnrollmentRedirect,
ProcessWebCpAuthorizeUrlRedirect
}
Loading