Repository: microsoft/promptflow
File: src/promptflow-devkit/promptflow/_sdk/_tracing.py (Python)

Code before:

def _print_tracing_url_from_azure_portal(
    ws_triad: AzureMLWorkspaceTriad,
    collection: str,
    exp: typing.Optional[str] = None,  # pylint: disable=unused-argument
    run: typing.Optional[str] = None,
) -> None:
    # as this there is an if condition for azure extension, we can assume the extension is installed
    from azure.ai.ml import MLClient

    # we have different url for Azure ML workspace and AI project
    # so we need to distinguish them
    ml_client = MLClient(
        credential=get_credentials_for_cli(),
        subscription_id=ws_triad.subscription_id,
        resource_group_name=ws_triad.resource_group_name,
        workspace_name=ws_triad.workspace_name,
    )
    workspace = ml_client.workspaces.get(name=ws_triad.workspace_name)

    url = (
        "https://int.ml.azure.com/{query}?"
        f"wsid=/subscriptions/{ws_triad.subscription_id}"
        f"/resourceGroups/{ws_triad.resource_group_name}"
        "/providers/Microsoft.MachineLearningServices"
        f"/workspaces/{ws_triad.workspace_name}"
        "&flight=PFTrace,PFNewRunDetail"
    )

    if run is None:
        _logger.debug("run is not specified, need to concat `collection_id` for query")
        collection_id = _get_collection_id(collection=collection)
    if AzureWorkspaceKind.is_workspace(workspace):
        _logger.debug(f"{ws_triad.workspace_name!r} is an Azure ML workspace.")
        if run is None:
            query = f"trace/collection/{collection_id}"
        else:
            query = f"prompts/trace/run/{run}"
    elif AzureWorkspaceKind.is_project(workspace):
        _logger.debug(f"{ws_triad.workspace_name!r} is an Azure AI project.")
        if run is None:
            query = f"projecttrace/collection/{collection_id}"
        else:
            query = f"projectflows/trace/run/{run}"
    else:
        _logger.error(f"the workspace type of {ws_triad.workspace_name!r} is not supported.")
        return

    url = url.format(query=query)
    print(f"You can view the traces in cloud from Azure portal: {url}")


Review comment (suggestion):
Considering local-to-cloud run also initilized a pfazure client (which contains a mlclient), feel like we can make trace and upload run share the same pfazure client? Not necessary to do it in this PR tho.

Code after:
    pfs_port: str,
    collection: str,
    exp: typing.Optional[str] = None,  # pylint: disable=unused-argument
    run: typing.Optional[str] = None,
) -> None:
    url = f"http://localhost:{pfs_port}/v1.0/ui/traces/"
    if run is not None:
        url += f"?#run={run}"
    else:
        # collection will not be None
        url += f"?#collection={collection}"
    print(f"You can view the traces from local: {url}")


def _print_tracing_url_from_azure_portal(
    ws_triad: AzureMLWorkspaceTriad,
    collection: str,
    exp: typing.Optional[str] = None,  # pylint: disable=unused-argument
    run: typing.Optional[str] = None,
) -> None:
    # as this there is an if condition for azure extension, we can assume the extension is installed
    from azure.ai.ml import MLClient

    # we have different url for Azure ML workspace and AI project
    # so we need to distinguish them
    ml_client = MLClient(
        credential=get_credentials_for_cli(),
        subscription_id=ws_triad.subscription_id,
        resource_group_name=ws_triad.resource_group_name,
        workspace_name=ws_triad.workspace_name,
    )
    workspace = ml_client.workspaces.get(name=ws_triad.workspace_name)

    url = (
        "https://int.ml.azure.com/{query}?"
        f"wsid=/subscriptions/{ws_triad.subscription_id}"
        f"/resourceGroups/{ws_triad.resource_group_name}"
        "/providers/Microsoft.MachineLearningServices"
        f"/workspaces/{ws_triad.workspace_name}"
        "&flight=PFTrace,PFNewRunDetail"
    )

    if run is None:
        _logger.debug("run is not specified, need to concat `collection_id` for query")
        collection_id = _get_collection_id_for_azure(collection=collection)
    if AzureWorkspaceKind.is_workspace(workspace):
        _logger.debug(f"{ws_triad.workspace_name!r} is an Azure ML workspace.")
        if run is None:
            query = f"trace/collection/{collection_id}"
        else:
            query = f"prompts/trace/run/{run}"