Skip to content

Commit 606a977

Browse files
Add clarity to the how-to trace section and included an example .http rest client file
1 parent d13c299 commit 606a977

File tree

1 file changed

+69
-8
lines changed

1 file changed

+69
-8
lines changed

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

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Follow these steps to trace an API request in the test console in the portal. Th
7474

7575
The following high level steps are required to enable tracing for a request to API Management when using `curl`, a REST client such as Visual Studio Code with the REST Client extension, or a client app. Currently these steps must be followed using the [API Management REST API](/rest/api/apimanagement):
7676

77-
1. Obtain a token credential for tracing.
77+
1. Obtain a debug token for tracing.
7878
1. Add the token value in an `Apim-Debug-Authorization` request header to the API Management gateway.
7979
1. Obtain a trace ID in the `Apim-Trace-Id` response header.
8080
1. Retrieve the trace corresponding to the trace ID.
@@ -85,7 +85,7 @@ Detailed steps follow.
8585
> * These 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.
8686
> * For information about authenticating to the REST API, see [Azure REST API reference](/rest/api/azure).
8787
88-
1. **Obtain a token credential** - Call the API Management gateway's [List debug credentials](/rest/api/apimanagement/gateway/list-debug-credentials) API. In the URI, enter "managed" for the instance's managed gateway in the cloud, or the gateway ID for a self-hosted gateway. For example, to obtain trace credentials for the instance's managed gateway, use a request similar to the following:
88+
1. **Obtain a debug token** - Call the API Management gateway's [List debug credentials](/rest/api/apimanagement/gateway/list-debug-credentials) API. In the URI, enter "managed" for the instance's managed gateway in the cloud, or the gateway ID for a self-hosted gateway. For example, to obtain trace credentials for the instance's managed gateway, use a request similar to the following:
8989

9090
```http
9191
POST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/gateways/managed/listDebugCredentials?api-version=2023-05-01-preview
@@ -96,10 +96,17 @@ Detailed steps follow.
9696
```json
9797
{
9898
"credentialsExpireAfter": PT1H,
99-
"apiId": ""/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiName}",
99+
"apiId": ""/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiId}",
100100
"purposes": ["tracing"]
101101
}
102102
```
103+
104+
> NOTE: The `apiId` can only be only be pulled from the full resource ID, not the name displayed in the portal.
105+
106+
Get apiId:
107+
```sh
108+
az apim api list --resource-group <resource-group> --service-name <service-name> -o table
109+
```
103110
104111
The token credential is returned in the response, similar to the following:
105112
@@ -112,18 +119,20 @@ Detailed steps follow.
112119
1. **Add the token value in a request header** - 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 Petstore API that you imported in a previous tutorial, you might use a request similar to the following:
113120
114121
```bash
115-
curl -v https://apim-hello-world.azure-api.net/pet/1 HTTP/1.1 -H "Ocp-Apim-Subscription-Key: <subscription-key>" -H "Apim-Debug-Authorization: aid=api-name&......."
122+
curl -v https://apim-hello-world.azure-api.net/pet/1 HTTP/1.1 \
123+
-H "Ocp-Apim-Subscription-Key: <subscription-key>" \
124+
-H "Apim-Debug-Authorization: aid=api-name&......."
116125
```
117126
118-
1. Depending on the token, the response contains one of the following headers:
119-
* If the token is valid, the response includes an `Apim-Trace-Id` header whose value is the trace ID, similar to the following:
127+
1. **Evaluate the response** - The response can contain one of the following headers depending on the state of the debug token:
128+
* If the debug token is valid, the response includes an `Apim-Trace-Id` header whose value is the trace ID, similar to the following:
120129
121130
```http
122131
Apim-Trace-Id: 0123456789abcdef....
123132
```
124133
125-
* If the token is expired, the response includes an `Apim-Debug-Authorization-Expired` header with information about expiration date.
126-
* If the token was obtained for a different API, the response includes an `Apim-Debug-Authorization-WrongAPI` header with an error message.
134+
* If the debug token is expired, the response includes an `Apim-Debug-Authorization-Expired` header with information about expiration date.
135+
* If the debug token was obtained for a different API, the response includes an `Apim-Debug-Authorization-WrongAPI` header with an error message.
127136
128137
1. **Retrieve the trace** - Pass the trace ID obtained in the previous step to the gateway's [List trace](/rest/api/apimanagement/gateway/list-trace) API. For example, to retrieve the trace for the managed gateway, use a request similar to the following:
129138
@@ -141,6 +150,58 @@ Detailed steps follow.
141150
142151
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.
143152
153+
Below is an example `.http` file for the VSCode http client extension:
154+
155+
```http
156+
@subscriptionId = // Your subscription Id
157+
@resourceGroup = // Your resource group
158+
@apimName = // Your APIM Serviuce name
159+
@clientId = // Client id from an app registration for authentication
160+
@clientSecret = // Client Secret
161+
@externalHost = // The host name of the APP Gateway or the fully qualified gateway URL
162+
@subscriptionKey = // Subscription key
163+
@apiEndPoint = // API URL
164+
@requestBody = // Data to send
165+
@tenantId = // Tenant Id
166+
167+
POST https://login.microsoftonline.com/{{tenandId}}/oauth2/token
168+
content-type: application/x-www-form-urlencoded
169+
170+
grant_type=client_credentials&client_id={{clientId}}&client_secret={{clientSecret}}&resource=https%3A%2F%2Fmanagement.azure.com%2F
171+
172+
###
173+
@authToken = {{login.response.body.$.access_token}}
174+
###
175+
# @name listDebugCredentials
176+
POST https://management.azure.com/subscriptions/{{subscriptionId}}/resourceGroups/{{resourceGroup}}/providers/Microsoft.ApiManagement/service/{{apimName}}/gateways/managed/listDebugCredentials?api-version=2023-05-01-preview
177+
Authorization: Bearer {{authToken}}
178+
Content-Type: application/json
179+
{
180+
"credentialsExpireAfter": "PT1H",
181+
"apiId": "/subscriptions/{{subscriptionId}}/resourceGroups/{{resourceGroup}}/providers/Microsoft.ApiManagement/service/{{apimName}}/apis/{{apiId}}",
182+
"purposes": ["tracing"]
183+
}
184+
185+
###
186+
@debugToken = {{listDebugCredentials.response.body.$.token}}
187+
188+
###
189+
# @name callApi
190+
curl -k -H "Apim-Debug-Authorization: {{debugToken}}" -H 'Host: {{externalHost}}' -H 'Ocp-Apim-Subscription-Key: {{subscriptionKey}}' -H 'Content-Type: application/json' '{{apiEndPoint}}' -d '{{requestBody}}'
191+
192+
###
193+
@traceId = {{callApi.response.headers.Apim-Trace-Id}}
194+
195+
###
196+
# @name getTrace
197+
POST https://management.azure.com/subscriptions/{{subscriptionId}}/resourceGroups/{{resourceGroup}}/providers/Microsoft.ApiManagement/service/{{apimName}}/gateways/managed/listTrace?api-version=2024-06-01-preview
198+
Authorization: Bearer {{authToken}}
199+
Content-Type: application/json
200+
201+
{
202+
"traceId": "{{traceId}}"
203+
}
204+
```
144205

145206
For information about customizing trace information, see the [trace](trace-policy.md) policy.
146207

0 commit comments

Comments
 (0)