Kaganapan
Mar 17, 9 PM - Mar 21, 10 AM
Sumali sa serye ng meetup upang bumuo ng mga scalable AI solusyon batay sa mga kaso ng paggamit ng tunay na mundo sa mga kapwa developer at eksperto.
Magparehistro naHindi na suportado ang browser na ito.
Mag-upgrade sa Microsoft Edge para samantalahin ang mga pinakabagong tampok, update sa seguridad, at teknikal na suporta.
APPLIES TO: All API Management tiers
This article describes how to log API Management events using Azure Event Hubs.
Azure Event Hubs is a highly scalable data ingress service that can ingest millions of events per second so that you can process and analyze the massive amounts of data produced by your connected devices and applications. Event Hubs acts as the "front door" for an event pipeline, and once data is collected into an event hub, it can be transformed and stored using any real-time analytics provider or batching/storage adapters. Event Hubs decouples the production of a stream of events from the consumption of those events, so that event consumers can access the events on their own schedule.
Note
Currently, this feature isn't available in workspaces.
Note
The Event Hubs resource can be in a different subscription or even a different tenant than the API Management resource
To log events to the event hub, you need to configure credentials for access from API Management. API Management supports either of the two following access mechanisms:
Note
Where possible, Microsoft recommends using managed identity credentials for enhanced security.
Enable a system-assigned or user-assigned managed identity for API Management in your API Management instance.
Assign the identity the Azure Event Hubs Data sender role, scoped to the Event Hubs namespace or to the event hub used for logging. To assign the role, use the Azure portal or other Azure tools.
To create an Event Hubs connection string, see Get an Event Hubs connection string.
The next step is to configure a logger in your API Management service so that it can log events to the event hub.
Create and manage API Management loggers by using the API Management REST API directly or by using tools including Azure PowerShell, a Bicep template, or an Azure Resource Management template.
You can configure an API Management logger to an event hub using either system-assigned or user-assigned managed identity credentials.
For prerequisites, see Configure API Management managed identity.
Use the API Management Logger - Create or Update REST API with the following request body.
{
"properties": {
"loggerType": "azureEventHub",
"description": "Event Hub logger with system-assigned managed identity",
"credentials": {
"endpointAddress":"<EventHubsNamespace>.servicebus.windows.net",
"identityClientId":"SystemAssigned",
"name":"<EventHubName>"
}
}
}
For prerequisites, see Configure API Management managed identity.
Use the API Management Logger - Create or Update REST API with the following request body.
{
"properties": {
"loggerType": "azureEventHub",
"description": "Event Hub logger with user-assigned managed identity",
"credentials": {
"endpointAddress":"<EventHubsNamespace>.servicebus.windows.net",
"identityClientId":"<ClientID>",
"name":"<EventHubName>"
}
}
}
For prerequisites, see Configure Event Hubs connection string.
Note
Where possible, Microsoft recommends configuring the logger with managed identity credentials. See Configure logger with managed identity credentials, earlier in this article.
The following example uses the New-AzApiManagementLogger cmdlet to create a logger to an event hub by configuring a connection string.
# API Management service-specific details
$apimServiceName = "apim-hello-world"
$resourceGroupName = "myResourceGroup"
# Create logger
$context = New-AzApiManagementContext -ResourceGroupName $resourceGroupName -ServiceName $apimServiceName
New-AzApiManagementLogger -Context $context -LoggerId "ContosoLogger1" -Name "ApimEventHub" -ConnectionString "Endpoint=sb://<EventHubsNamespace>.servicebus.windows.net/;SharedAccessKeyName=<KeyName>;SharedAccessKey=<key>" -Description "Event hub logger with connection string"
Once your logger is configured in API Management, you can configure your log-to-eventhub policy to log the desired events. For example, use the log-to-eventhub
policy in the inbound policy section to log requests, or in the outbound policy section to log responses.
Browse to your API Management instance.
Select APIs, and then select the API to which you want to add the policy. In this example, we're adding a policy to the Echo API in the Unlimited product.
Select All operations.
On the top of the screen, select the Design tab.
In the Inbound processing or Outbound processing window, select the </>
(code editor) icon. For more information, see How to set or edit policies.
Position your cursor in the inbound
or outbound
policy section.
In the window on the right, select Advanced policies > Log to EventHub. This inserts the log-to-eventhub
policy statement template.
<log-to-eventhub logger-id="logger-id">
@{
return new JObject(
new JProperty("EventTime", DateTime.UtcNow.ToString()),
new JProperty("ServiceName", context.Deployment.ServiceName),
new JProperty("RequestId", context.RequestId),
new JProperty("RequestIp", context.Request.IpAddress),
new JProperty("OperationName", context.Operation.Name)
).ToString();
}
</log-to-eventhub>
logger-id
with the name of the logger that you created in the previous step.log-to-eventhub
element. In this example, a string in JSON format containing the date and time, service name, request ID, request IP address, and operation name is logged.Select Save to save the updated policy configuration. As soon as it's saved, the policy is active and events are logged to the designated event hub.
Note
The maximum supported message size that can be sent to an event hub from this API Management policy is 200 kilobytes (KB). If a message that is sent to an event hub is larger than 200 KB, it will be automatically truncated, and the truncated message will be transferred to the event hub. For larger messages, consider using Azure Storage with Azure API Management as a workaround to bypass the 200KB limit. More details can be found in this article.
You can preview the log in Event Hubs by using Azure Stream Analytics queries.
Kaganapan
Mar 17, 9 PM - Mar 21, 10 AM
Sumali sa serye ng meetup upang bumuo ng mga scalable AI solusyon batay sa mga kaso ng paggamit ng tunay na mundo sa mga kapwa developer at eksperto.
Magparehistro naPagsasanay
Module
Enable reliable messaging for Big Data applications using Azure Event Hubs - Training
Learn to use Azure Event Hubs to reliably process high-volume data streams to enable you to code applications to send and receive messages through the hub.
Sertipikasyon
Microsoft Certified: Azure Cosmos DB Developer Specialty - Certifications
Write efficient queries, create indexing policies, manage, and provision resources in the SQL API and SDK with Microsoft Azure Cosmos DB.