|
66 | 66 | import com.microsoft.identity.common.java.flighting.CommonFlight; |
67 | 67 | import com.microsoft.identity.common.java.flighting.CommonFlightsManager; |
68 | 68 | import com.microsoft.identity.common.java.opentelemetry.AttributeName; |
| 69 | +import com.microsoft.identity.common.java.opentelemetry.BaggageExtension; |
69 | 70 | import com.microsoft.identity.common.java.opentelemetry.OTelUtility; |
70 | 71 | import com.microsoft.identity.common.java.opentelemetry.SpanExtension; |
71 | 72 | import com.microsoft.identity.common.java.opentelemetry.SpanName; |
|
86 | 87 | import java.net.URISyntaxException; |
87 | 88 | import java.net.URL; |
88 | 89 | import java.security.Principal; |
| 90 | +import java.util.Arrays; |
89 | 91 | import java.util.HashMap; |
| 92 | +import java.util.List; |
90 | 93 | import java.util.Locale; |
91 | 94 | import java.util.Map; |
92 | 95 | import java.util.concurrent.TimeUnit; |
|
102 | 105 | import static com.microsoft.identity.common.java.exception.ClientException.UNKNOWN_ERROR; |
103 | 106 | import static com.microsoft.identity.common.java.flighting.CommonFlight.ENABLE_PLAYSTORE_URL_LAUNCH; |
104 | 107 |
|
| 108 | +import io.opentelemetry.api.baggage.Baggage; |
105 | 109 | import io.opentelemetry.api.trace.Span; |
106 | 110 | import io.opentelemetry.api.trace.SpanContext; |
107 | 111 | import io.opentelemetry.api.trace.StatusCode; |
@@ -579,9 +583,7 @@ private void processSwitchBrowserRequest(@NonNull final String url) { |
579 | 583 | protected void processWebsiteRequest(@NonNull final WebView view, @NonNull final String url) { |
580 | 584 | final String methodTag = TAG + ":processWebsiteRequest"; |
581 | 585 | view.stopLoading(); |
582 | | - final SpanContext spanContext = getActivity() instanceof AuthorizationActivity ? ((AuthorizationActivity) getActivity()).getSpanContext() : null; |
583 | | - final Span span = spanContext != null ? |
584 | | - OTelUtility.createSpanFromParent(SpanName.ProcessWebsiteRequest.name(), spanContext) : OTelUtility.createSpan(SpanName.ProcessWebsiteRequest.name()); |
| 586 | + final Span span = createSpanWithAttributesFromParent(SpanName.ProcessWebsiteRequest.name()); |
585 | 587 | span.setAttribute(AttributeName.is_in_web_cp_flow.name(), mInWebCpFlow); |
586 | 588 | try (final Scope scope = SpanExtension.makeCurrentSpan(span)) { |
587 | 589 | if (isDeviceCaRequest(url)) { |
@@ -692,9 +694,7 @@ private boolean shouldLaunchCompanyPortal() { |
692 | 694 | @VisibleForTesting |
693 | 695 | protected void loadDeviceCaUrl(@NonNull final String originalUrl, @NonNull final WebView view) { |
694 | 696 | final String methodTag = TAG + ":loadDeviceCaUrl"; |
695 | | - final SpanContext spanContext = getActivity() instanceof AuthorizationActivity ? ((AuthorizationActivity) getActivity()).getSpanContext() : null; |
696 | | - final Span span = spanContext != null ? |
697 | | - OTelUtility.createSpanFromParent(SpanName.ProcessWebCpRedirects.name(), spanContext) : OTelUtility.createSpan(SpanName.ProcessWebCpRedirects.name()); |
| 697 | + final Span span = createSpanWithAttributesFromParent(SpanName.ProcessWebCpRedirects.name()); |
698 | 698 | try (final Scope scope = SpanExtension.makeCurrentSpan(span)) { |
699 | 699 | if (isWebCpInWebviewFeatureEnabled(originalUrl)) { |
700 | 700 | Logger.info(methodTag, "Loading device CA request in WebView."); |
@@ -739,7 +739,7 @@ protected boolean isWebCpInWebviewFeatureEnabled(@NonNull final String originalU |
739 | 739 | final int waitForFlightsTimeOut = CommonFlightsManager.INSTANCE.getFlightsProvider().getIntValue(CommonFlight.WEB_CP_WAIT_TIMEOUT_FOR_FLIGHTS); |
740 | 740 | final boolean isWebCpFlightEnabled = CommonFlightsManager.INSTANCE.getFlightsProviderForTenant(homeTenantId, waitForFlightsTimeOut).isFlightEnabled(CommonFlight.ENABLE_WEB_CP_IN_WEBVIEW); |
741 | 741 | SpanExtension.current().setAttribute(AttributeName.web_cp_flight_get_time.name(), (System.currentTimeMillis() - webCpGetFlightStartTime)); |
742 | | - |
| 742 | + SpanExtension.current().setAttribute(AttributeName.tenant_id.name(), homeTenantId); |
743 | 743 | if (isWebCpFlightEnabled) { |
744 | 744 | // Directly enabled via flight rollout. |
745 | 745 | Logger.info(methodTag, "WebCP in WebView feature is enabled."); |
@@ -767,11 +767,8 @@ private String getHomeTenantIdFromUrl(@NonNull final String url) { |
767 | 767 | // This is a special case where the enrollment is not done in the WebView, but rather in the browser. |
768 | 768 | private void processWebCpEnrollmentUrl(@NonNull final WebView view, @NonNull final String url) { |
769 | 769 | final String methodTag = TAG + ":processWebCpEnrollmentUrl"; |
770 | | - final SpanContext spanContext = getActivity() instanceof AuthorizationActivity ? ((AuthorizationActivity) getActivity()).getSpanContext() : null; |
771 | | - final Span span = spanContext != null ? |
772 | | - OTelUtility.createSpanFromParent(SpanName.ProcessWebCpRedirects.name(), spanContext) : OTelUtility.createSpan(SpanName.ProcessWebCpRedirects.name()); |
| 770 | + final Span span = createSpanWithAttributesFromParent(SpanName.ProcessWebCpEnrollmentRedirect.name()); |
773 | 771 | try (final Scope scope = SpanExtension.makeCurrentSpan(span)) { |
774 | | - span.setAttribute(AttributeName.is_webcp_enrollment_request.name(), true); |
775 | 772 | view.stopLoading(); |
776 | 773 | Logger.info(methodTag, "Loading WebCP enrollment url in browser."); |
777 | 774 | // 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). |
@@ -1064,10 +1061,7 @@ private void processNonceAndReAttachHeaders(@NonNull final WebView view, @NonNul |
1064 | 1061 | private void processWebCpAuthorize(@NonNull final WebView view, @NonNull final String url) { |
1065 | 1062 | final String methodTag = TAG + ":processWebCPAuthorize"; |
1066 | 1063 | Logger.info(methodTag, "Processing WebCP authorize request."); |
1067 | | - final SpanContext spanContext = getActivity() instanceof AuthorizationActivity ? ((AuthorizationActivity) getActivity()).getSpanContext() : null; |
1068 | | - final Span span = spanContext != null ? |
1069 | | - OTelUtility.createSpanFromParent(SpanName.ProcessWebCpRedirects.name(), spanContext) : OTelUtility.createSpan(SpanName.ProcessWebCpRedirects.name()); |
1070 | | - span.setAttribute(AttributeName.is_webcp_authorize_request.name(), true); |
| 1064 | + final Span span = createSpanWithAttributesFromParent(SpanName.ProcessWebCpAuthorizeUrlRedirect.name()); |
1071 | 1065 | final ReAttachPrtHeaderHandler reAttachPrtHeaderHandler = new ReAttachPrtHeaderHandler(view, mRequestHeaders, span); |
1072 | 1066 | reAttachPrtHeader(url, reAttachPrtHeaderHandler, view, methodTag, span); |
1073 | 1067 | } |
@@ -1209,4 +1203,33 @@ private String getBrokerAppPackageNameFromUrl(@NonNull final String url) { |
1209 | 1203 | return ""; |
1210 | 1204 | } |
1211 | 1205 |
|
| 1206 | + /** |
| 1207 | + * Create a span with parent span context if available. |
| 1208 | + * @param spanName Name of the span to be created. |
| 1209 | + * @return Created {@link Span} |
| 1210 | + */ |
| 1211 | + private Span createSpanWithAttributesFromParent(@NonNull final String spanName) { |
| 1212 | + final SpanContext spanContext = getActivity() instanceof AuthorizationActivity ? ((AuthorizationActivity) getActivity()).getSpanContext() : null; |
| 1213 | + final Span span = spanContext != null ? |
| 1214 | + OTelUtility.createSpanFromParent(spanName, spanContext) : OTelUtility.createSpan(spanName); |
| 1215 | + if (mUtid != null) { |
| 1216 | + span.setAttribute(AttributeName.tenant_id.name(), mUtid); |
| 1217 | + } |
| 1218 | + // Populate some of the parent span's attributes to current span. |
| 1219 | + final Context oTelContext = getActivity() instanceof AuthorizationActivity ? ((AuthorizationActivity) getActivity()).getOtelContext() : null; |
| 1220 | + if (oTelContext != null) { |
| 1221 | + final Baggage baggage = BaggageExtension.fromContext(oTelContext); |
| 1222 | + final List<AttributeName> parentAttributeNames = Arrays.asList( |
| 1223 | + AttributeName.correlation_id, |
| 1224 | + AttributeName.calling_package_name |
| 1225 | + ); |
| 1226 | + for (AttributeName attributeName : parentAttributeNames) { |
| 1227 | + final String value = baggage.getEntryValue(attributeName.name()); |
| 1228 | + if (value != null) { |
| 1229 | + span.setAttribute(attributeName.name(), value); |
| 1230 | + } |
| 1231 | + } |
| 1232 | + } |
| 1233 | + return span; |
| 1234 | + } |
1212 | 1235 | } |
0 commit comments