Skip to content

Commit 13b550e

Browse files
Merge pull request #270598 from dlepow/tr2
[APIM] v2 tracing
2 parents 7b20951 + a95621e commit 13b550e

7 files changed

+72
-55
lines changed

articles/api-management/api-management-debug-policies.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ This article describes how to debug API Management policies using the [Azure API
2626

2727
## Restrictions and limitations
2828

29-
* This feature uses the built-in (service-level) all-access subscription (display name "Built-in all-access subscription") for debugging. The [**Allow tracing**](api-management-howto-api-inspector.md#verify-allow-tracing-setting) setting must be enabled in this subscription.
29+
* This feature uses the built-in (service-level) all-access subscription (display name "Built-in all-access subscription") for debugging.
3030

3131
[!INCLUDE [api-management-tracing-alert](../../includes/api-management-tracing-alert.md)]
3232

articles/api-management/api-management-howto-api-inspector.md

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ services: api-management
55
author: dlepow
66
ms.service: api-management
77
ms.topic: tutorial
8-
ms.date: 03/26/2024
8+
ms.date: 05/05/2024
99
ms.author: danlep
1010
ms.custom: devdivchpfy22
1111
---
@@ -19,8 +19,9 @@ This tutorial describes how to inspect (trace) request processing in Azure API M
1919
In this tutorial, you learn how to:
2020

2121
> [!div class="checklist"]
22-
> * Trace an example call
22+
> * Trace an example call in the test console
2323
> * Review request processing steps
24+
> * Enable tracing for an API
2425
2526
:::image type="content" source="media/api-management-howto-api-inspector/api-inspector-002.png" alt-text="Screenshot showing the API inspector." lightbox="media/api-management-howto-api-inspector/api-inspector-002.png":::
2627

@@ -32,18 +33,10 @@ In this tutorial, you learn how to:
3233
+ Complete the following quickstart: [Create an Azure API Management instance](get-started-create-service-instance.md).
3334
+ Complete the following tutorial: [Import and publish your first API](import-and-publish.md).
3435

35-
## Verify allow tracing setting
36-
37-
To trace request processing, you must enable the **Allow tracing** setting for the subscription used to debug your API. To check in the portal:
38-
39-
1. Navigate to your API Management instance and select **Subscriptions** to review the settings.
40-
41-
:::image type="content" source="media/api-management-howto-api-inspector/allow-tracing-1.png" alt-text="Screenshot showing how to allow tracing for subscription." lightbox="media/api-management-howto-api-inspector/allow-tracing-1.png":::
42-
1. If tracing isn't enabled for the subscription you're using, select the subscription and enable **Allow tracing**.
4336

4437
[!INCLUDE [api-management-tracing-alert](../../includes/api-management-tracing-alert.md)]
4538

46-
## Trace a call
39+
## Trace a call in the portal
4740

4841
1. Sign in to the [Azure portal](https://portal.azure.com), and navigate to your API Management instance.
4942
1. Select **APIs**.
@@ -56,10 +49,6 @@ To trace request processing, you must enable the **Allow tracing** setting for t
5649
5750
1. Select **Trace**.
5851

59-
* If your subscription doesn't already allow tracing, you're prompted to enable it if you want to trace the call.
60-
* You can also choose to send the request without tracing.
61-
62-
:::image type="content" source="media/api-management-howto-api-inspector/06-debug-your-apis-01-trace-call-1.png" alt-text="Screenshot showing configure API tracing." lightbox="media/api-management-howto-api-inspector/06-debug-your-apis-01-trace-call-1.png":::
6352

6453
## Review trace information
6554

@@ -79,18 +68,66 @@ To trace request processing, you must enable the **Allow tracing** setting for t
7968
> [!TIP]
8069
> Each step also shows the elapsed time since the request is received by API Management.
8170
82-
1. On the **Message** tab, the **ocp-apim-trace-location** header shows the location of the trace data stored in Azure blob storage. If needed, go to this location to retrieve the trace. Trace data can be accessed for up to 24 hours.
8371

84-
:::image type="content" source="media/api-management-howto-api-inspector/response-message-1.png" alt-text="Trace location in Azure Storage":::
72+
## Enable tracing for an API
73+
74+
You can enable tracing for an API when making requests to API Management using `curl`, a REST client such as Visual Studio Code with the REST Client extension, or a client app.
75+
76+
Enable tracing by the following steps using calls to the API Management REST API.
77+
78+
> [!NOTE]
79+
> The following steps require API Management REST API version 2023-05-01-preview or later. You must be assigned the Contributor or higher role on the API Management instance to call the REST API.
80+
81+
1. Obtain trace credentials by calling the [List debug credentials](/rest/api/apimanagement/gateway/list-debug-credentials) API. Pass the gateway ID in the URI, or use "managed" for the instance's managed gateway in the cloud. For example, to obtain trace credentials for the managed gateway, use a call similar to the following:
82+
83+
```http
84+
POST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/gateways/managed/listDebugCredentials?api-version=2023-05-01-preview
85+
```
86+
87+
In the request body, pass the full resource ID of the API that you want to trace, and specify `purposes` as `tracing`. By default the token credential returned in the response expires after 1 hour, but you can specify a different value in the payload.
88+
89+
```json
90+
{
91+
"credentialsExpireAfter": PT1H,
92+
"apiId": "<API resource ID>",
93+
"purposes: ["tracing"]
94+
}
95+
```
96+
97+
The token credential is returned in the response, similar to the following:
98+
99+
```json
100+
{
101+
"token": "aid=api-name&p=tracing&ex=......."
102+
}
103+
```
104+
105+
1. To enable tracing for a request to the API Management gateway, send the token value in an `Apim-Debug-Authorization` header. For example, to trace a call to the demo conference API, use a call similar to the following:
106+
107+
```bash
108+
curl -v GET https://apim-hello-world.azure-api.net/conference/speakers HTTP/1.1 -H "Ocp-Apim-Subscription-Key: <subscription-key>" -H "Apim-Debug-Authorization: aid=api-name&p=tracing&ex=......."
109+
```
110+
1. Depending on the token, the response contains different headers:
111+
* If the token is valid, the response includes an `Apim-Trace-Id` header whose value is the trace ID.
112+
* If the token is expired, the response includes an `Apim-Debug-Authorization-Expired` header with information about expiration date.
113+
* If the token was obtained for wrong API, the response includes an `Apim-Debug-Authorization-WrongAPI` header with an error message.
114+
115+
1. To retrieve the trace, pass the trace ID obtained in the previous step to the [List trace](/rest/api/apimanagement/gateway/list-trace) API for the gateway. For example, to retrieve the trace for the managed gateway, use a call similar to the following:
85116
86-
## Enable tracing using Ocp-Apim-Trace header
117+
```http
118+
POST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/gateways/managed/listTrace?api-version=2023-05-01-preview
119+
```
87120
88-
When making requests to API Management using `curl`, a REST client such as Postman, or a client app, enable tracing by adding the following request headers:
121+
In the request body, pass the trace ID obtained in the previous step.
89122
90-
* **Ocp-Apim-Trace** - set value to `true`
91-
* **Ocp-Apim-Subscription-Key** - set value to the key for a tracing-enabled subscription that allows access to the API
123+
```json
124+
{
125+
"traceId": "<trace ID>"
126+
}
127+
```
128+
129+
The response body contains the trace data for the previous API request to the gateway. The trace is similar to the trace you can see by tracing a call in the portal's test console.
92130
93-
The response includes the **Ocp-Apim-Trace-Location** header, with a URL to the location of the trace data in Azure blob storage.
94131
95132
For information about customizing trace information, see the [trace](trace-policy.md) policy.
96133
@@ -101,6 +138,7 @@ In this tutorial, you learned how to:
101138
> [!div class="checklist"]
102139
> * Trace an example call
103140
> * Review request processing steps
141+
> * Enable tracing for an API
104142
105143
Advance to the next tutorial:
106144

articles/api-management/api-management-howto-create-subscriptions.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ When you publish APIs through Azure API Management, it's easy and common to secu
1717

1818
This article walks through the steps for creating subscriptions in the Azure portal.
1919

20+
> [!IMPORTANT]
21+
> The **Allow tracing** setting in subscriptions to enable debug traces is deprecated. To improve security, tracing can now be enabled for specific API requests to API Management. [Learn more](api-management-howto-api-inspector.md#enable-tracing-for-an-api)
22+
2023
## Prerequisites
2124

2225
To take the steps in this article, the prerequisites are as follows:
@@ -31,12 +34,6 @@ To take the steps in this article, the prerequisites are as follows:
3134
1. Navigate to your API Management instance in the [Azure portal](https://portal.azure.com).
3235
1. In the left menu, under **APIs**, select **Subscriptions** > **Add subscription**.
3336
1. Provide a **Name** and optional **Display name** of the subscription.
34-
1. Optionally, select **Allow tracing** to enable tracing for debugging and troubleshooting APIs. [Learn more](api-management-howto-api-inspector.md)
35-
36-
[!INCLUDE [api-management-tracing-alert](../../includes/api-management-tracing-alert.md)]
37-
38-
[!INCLUDE [api-management-availability-tracing-v2-tiers](../../includes/api-management-availability-tracing-v2-tiers.md)]
39-
4037
1. Select a **Scope** of the subscription from the dropdown list. [Learn more](api-management-subscriptions.md#scope-of-subscriptions)
4138
1. Optionally, choose if the subscription should be associated with a **User** and whether to send a notification for use with the developer portal.
4239
1. Select **Create**.

articles/api-management/trace-policy.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ms.author: danlep
1515

1616
The `trace` policy adds a custom trace into the request tracing output in the test console, Application Insights telemetries, and/or resource logs.
1717

18-
- The policy adds a custom trace to the [request tracing](./api-management-howto-api-inspector.md) output in the test console when tracing is triggered, that is, `Ocp-Apim-Trace` request header is present and set to `true` and `Ocp-Apim-Subscription-Key` request header is present and holds a valid key that allows tracing.
18+
- The policy adds a custom trace to the [request tracing](./api-management-howto-api-inspector.md) output in the test console when tracing is triggered.
1919
- The policy creates a [Trace](../azure-monitor/app/data-model-complete.md#trace) telemetry in Application Insights, when [Application Insights integration](./api-management-howto-app-insights.md) is enabled and the `severity` specified in the policy is equal to or greater than the `verbosity` specified in the [diagnostic setting](./diagnostic-logs-reference.md).
2020
- The policy adds a property in the log entry when [resource logs](./api-management-howto-use-azure-monitor.md#resource-logs) are enabled and the severity level specified in the policy is at or higher than the verbosity level specified in the [diagnostic setting](./diagnostic-logs-reference.md).
2121
- The policy is not affected by Application Insights sampling. All invocations of the policy will be logged.

articles/api-management/visual-studio-code-tutorial.md

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ You need a subscription key for your API Management instance to test the importe
107107
1. In the Explorer pane, expand the **Operations** node under the *demo-conference-api* that you imported.
108108
1. Select an operation such as *GetSpeakers*, and then right-click the operation and select **Test Operation**.
109109
1. In the editor window, next to **Ocp-Apim-Subscription-Key**, replace `{{SubscriptionKey}}` with the subscription key that you copied.
110-
1. Next to `Ocp-Apim-Trace`, enter `false`. This setting disables request tracing.
111110
1. Select **Send request**.
112111

113112
:::image type="content" source="media/visual-studio-code-tutorial/test-api.png" alt-text="Screenshot of sending API request from Visual Studio Code.":::
@@ -126,24 +125,7 @@ Notice the following details in the response:
126125

127126
Optionally, you can get detailed request tracing information to help you debug and troubleshoot the API.
128127

129-
To trace request processing, first enable the **Allow tracing** setting for the subscription used to debug your API. For steps to enable this setting using the portal, see [Verify allow tracing setting](api-management-howto-api-inspector.md#verify-allow-tracing-setting). To limit unintended disclosure of sensitive information, tracing is allowed for only 1 hour.
130-
131-
After allowing tracing with your subscription, follow these steps:
132-
133-
1. In the Explorer pane, expand the **Operations** node under the *demo-conference-api* that you imported.
134-
1. Select an operation such as *GetSpeakers*, and then right-click the operation and select **Test Operation**.
135-
1. In the editor window, next to **Ocp-Apim-Subscription-Key**, replace `{{SubscriptionKey}}` with the subscription key that you want to use.
136-
1. Next to `Ocp-Apim-Trace`, enter `true`. This setting enables tracing for this request.
137-
1. Select **Send request**.
138-
139-
When the request succeeds, the backend response includes an **Ocp-APIM-Trace-Location** header.
140-
141-
:::image type="content" source="media/visual-studio-code-tutorial/test-api-tracing.png" alt-text="Screenshot of tracing location in the API test response in Visual Studio Code.":::
142-
143-
Select the link next to **Ocp-APIM-Trace-Location** to see Inbound, Backend, and Outbound trace information. The trace information helps you determine where problems occur after the request is made.
144-
145-
> [!TIP]
146-
> When you test API operations, the API Management extension allows optional [policy debugging](api-management-debug-policies.md) (available only in the Developer service tier).
128+
For steps to enable tracing for an API, see [Enable tracing for an API](api-management-howto-api-inspector.md#enable-tracing-for-an-api). To limit unintended disclosure of sensitive information, tracing by default is allowed for only 1 hour.
147129

148130
## Clean up resources
149131

includes/api-management-availability-tracing-v2-tiers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ ms.author: danlep
77
---
88

99
> [!NOTE]
10-
> Currently, API request tracing isn't supported in the Basic v2 and Standard v2 tiers.
10+
> Currently, tracing of API requests isn't supported in the Basic v2 and Standard v2 tiers.

includes/api-management-tracing-alert.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
author: dlepow
33
ms.service: api-management
44
ms.topic: include
5-
ms.date: 08/03/2022
5+
ms.date: 05/05/2024
66
ms.author: danlep
77
---
8-
> [!WARNING]
9-
> * Only allow tracing on subscriptions intended for debugging purposes. Sharing subscription keys with tracing allowed with unauthorized users could lead to disclosure of sensitive information contained in tracing logs such as keys, access tokens, passwords, internal hostnames, and IP addresses.
10-
> * In the test console, API Management automatically disables tracing 1 hour after it's enabled on a subscription.
11-
8+
> [!IMPORTANT]
9+
> * API Management request tracing can no longer be enabled by setting the **Ocp-Apim-Trace** header in a request and using the value of the **Ocp-Apim-Trace-Location** header in the response to retrieve the trace.
10+
> * To improve security, tracing is now enabled at the level of an individual API by obtaining a time-limited token using the API Management REST API, and passing the token in a request to the gateway. For details, see later in this tutorial.
11+
> * Take care when enabling tracing, as it can expose sensitive information in the trace data. Ensure that you have appropriate security measures in place to protect the trace data.

0 commit comments

Comments
 (0)