|
| 1 | +--- |
| 2 | +title: How to use cert+sp emit log to eventhub |
| 3 | +description: Learn to setting up Azure services, particularly focusing on integrating Azure Synapse with Event Hubs and Key Vault. |
| 4 | +author: jejiang |
| 5 | +ms.author: jejiang |
| 6 | +ms.reviewer: whhender |
| 7 | +ms.topic: tutorial |
| 8 | +ms.date: 03/24/2025 |
| 9 | +--- |
| 10 | + |
| 11 | +# How to use certificate and Service Principal emit log to eventhub |
| 12 | + |
| 13 | +This document provides a step-by-step guide for setting up Azure services, particularly focusing on integrating Azure Synapse with Event Hubs and Key Vault. |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +- An Azure subscription. If you don't have one, [create a free account](https://azure.microsoft.com/free/) before you begin. |
| 18 | +- [Synapse Analytics workspace](quickstart-create-workspace.md) |
| 19 | +- If you are new to Azure Event Hubs, read through [Event Hubs overview](/azure/event-hubs/event-hubs-about.md) and [Event Hubs features](/azure/event-hubs/event-hubs-features). |
| 20 | +- [Azure Key Vault](azure/key-vault/general/overview.md) |
| 21 | +- To complete this tutorial's steps, you need to have access to a resource group for which you're assigned the Owner role. Generate a certificate in the Key Vault in this resource group. |
| 22 | + |
| 23 | + |
| 24 | +## Step 1. Create an App Registration (Service Principal) |
| 25 | + |
| 26 | +1. Sign in to the [Azure portal](https://portal.azure.com/) and go to [App registrations](https://ms.portal.azure.com/#view/Microsoft_AAD_RegisteredApps/ApplicationsListBlade). |
| 27 | +2. Create a new app registration (Service Principal) for your Synapse workspace. |
| 28 | + |
| 29 | + :::image type="content" source="media\how-to-use-certsp-emit-log-to-eventhub\create-a-new-app-registration.png" alt-text="Screenshot showing create a new app registration."::: |
| 30 | + |
| 31 | +## Step 2. Generate a Certificate in Key Vault |
| 32 | + |
| 33 | +1. Navigate to Key Vault. |
| 34 | +2. Expand the **Odject**, and select the **Certificates**. |
| 35 | +3. Click on **Generate/Import**. |
| 36 | + |
| 37 | + :::image type="content" source="media\how-to-use-certsp-emit-log-to-eventhub\generate-a-new-certificate.png" alt-text="Screenshot showing generate a new certificate for app."::: |
| 38 | + |
| 39 | +## Step 3. Trust the Certificate in the Application |
| 40 | + |
| 41 | +1. Go to the app created in Step 1 -> **Manage** -> **Manifest**. |
| 42 | +2. Append the certificate details to the manifest file to establish trust. |
| 43 | + |
| 44 | +``` |
| 45 | + "trustedCertificateSubjects": [ |
| 46 | + { |
| 47 | + "authorityId": "00000000-0000-0000-0000-000000000001", |
| 48 | + "subjectName": "Your-Subject-of-Certificate", |
| 49 | + "revokedCertificateIdentifiers": [] |
| 50 | + } |
| 51 | + ] |
| 52 | +``` |
| 53 | + :::image type="content" source="media\trust-the-certificate.png" alt-text="Screenshot showing trust the certificate in the application."::: |
| 54 | + |
| 55 | +## Step 4. Assign Azure Event Hubs Data Sender Role |
| 56 | + |
| 57 | +1. In Event Hub, navigate to Access control (IAM). |
| 58 | +2. Assign the app (Service Principal) with the Azure Event Hubs Data Sender role. |
| 59 | + |
| 60 | + :::image type="content" source="media\how-to-use-certsp-emit-log-to-eventhub\assign-azure-event-hubs-data-sender-role.png" alt-text="Screenshot showing assign azure event hubs data sender role."::: |
| 61 | + |
| 62 | +## Step 5. Create a Linked Service in Synapse |
| 63 | + |
| 64 | +1. In Synapse Analytics workspace, go to **Manage** -> **Linked service**. |
| 65 | +2. Create a new **Linked Service** in Synapse to connect to **Key Vault**. |
| 66 | + |
| 67 | + :::image type="content" source="media\how-to-use-certsp-emit-log-to-eventhub\create-a-linked-service-in-synapse.png" alt-text="Screenshot showing create a linked service in synapse."::: |
| 68 | + |
| 69 | +## Step 6. Assign Reader Role to Linked Service in Key Vault |
| 70 | + |
| 71 | +1. In **Key Vault**, assign the linked service a **Reader** role. |
| 72 | +2. You can find the linked service's managed identity name and object ID under **Edit linked service**. |
| 73 | + |
| 74 | + :::image type="content" source="media\how-to-use-certsp-emit-log-to-eventhub\managed-identity-name-and-object-id.png" alt-text="Screenshot showing managed identity name and object id are in edit linked service."::: |
| 75 | + |
| 76 | +## Step 7. Configure Logging in Synapse Notebook |
| 77 | + |
| 78 | +1. Open your Synapse workspace and create or open a notebook. |
| 79 | +2. In the first code cell, add the configuration code to emit logs to Event Hub. |
| 80 | + |
| 81 | + ``` |
| 82 | + %%configure -f |
| 83 | + { |
| 84 | + "conf": { |
| 85 | + "spark.yarn.user.classpath.first": "true", |
| 86 | + "spark.synapse.diagnostic.emitters": "EventHub", |
| 87 | + "spark.synapse.diagnostic.emitter.EventHub.type": "AzureEventHub", |
| 88 | + "spark.synapse.diagnostic.emitter.EventHub.categories": "DriverLog,ExecutorLog,EventLog,Metrics", |
| 89 | + "spark.synapse.diagnostic.emitter.EventHub.certificate.keyVault": "Your-keyvault-name", |
| 90 | + "spark.synapse.diagnostic.emitter.EventHub.certificate.keyVault.certificateName": "Your-certificate-name", |
| 91 | + "spark.synapse.diagnostic.emitter.EventHub.certificate.keyVault.linkedService": "Your-linkedservice-name", |
| 92 | + "spark.synapse.diagnostic.emitter.EventHub.hostName": "Your-eventhub-hostname", |
| 93 | + "spark.synapse.diagnostic.emitter.EventHub.tenantId": "Your-sp-tenantId", |
| 94 | + "spark.synapse.diagnostic.emitter.EventHub.clientId": "Your-sp-clientid", |
| 95 | + "spark.synapse.diagnostic.emitter.EventHub.entityPath": "Your-eventhub-entitypath" |
| 96 | + }, |
| 97 | + "jars": [ |
| 98 | + "Your-specific-jar-in-blob" |
| 99 | + ] |
| 100 | + } |
| 101 | + ``` |
| 102 | +### Description |
| 103 | +
|
| 104 | +- EventHub.hostName : Event Hubs Namespace - Overview - Host name |
| 105 | +- EventHub.tenantId: App registrations - your app name - Overview - Directory (tenant) ID |
| 106 | +- EventHub.clientId: App registrations - your app name - Overview - Application(client) ID |
| 107 | +- EventHub.entityPath: Event Hubs Instance - Settings - Shared access policies - Find "EntityPath" in Connection string |
| 108 | +
|
| 109 | +## Step 8. Run the Log-Sending Code |
| 110 | +
|
| 111 | +After executing the configuration code in Step 7, run the log-sending code to start emitting logs to Event Hub. |
| 112 | +
|
| 113 | + ``` |
| 114 | + %%spark |
| 115 | + val logger = org.apache.log4j.LogManager.getLogger("com.contoso.LoggerExample") |
| 116 | + logger.info("Hello, info message") |
| 117 | + logger.warn("Hello, warn message") |
| 118 | + logger.error("Hello, error message") |
| 119 | + ``` |
| 120 | +
|
| 121 | +
|
0 commit comments