Created
December 7, 2021 07:07
-
-
Save moarychan/78cd8312085335230722493c3d037714 to your computer and use it in GitHub Desktop.
Demonstrates how to make the spring-cloud-azure-trace-sleuth enabled
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) Microsoft Corporation. All rights reserved. | |
// Licensed under the MIT License. | |
package com.azure.spring.cloud.autoconfigure.trace.sleuth; | |
import com.azure.spring.cloud.autoconfigure.storage.blob.properties.AzureStorageBlobProperties; | |
import com.azure.spring.core.factory.AbstractAzureHttpClientBuilderFactory; | |
import com.azure.spring.core.trace.sleuth.AzureHttpClientBuilderFactoryBeanPostProcessor; | |
import com.azure.spring.service.storage.blob.BlobServiceClientBuilderFactory; | |
import com.azure.spring.tracing.sleuth.SleuthHttpPolicy; | |
import com.azure.storage.blob.BlobContainerClient; | |
import com.azure.storage.blob.BlobServiceClient; | |
import com.azure.storage.blob.BlobServiceClientBuilder; | |
import org.springframework.cloud.sleuth.Tracer; | |
import org.springframework.cloud.sleuth.propagation.Propagator; | |
/** | |
* Spring Cloud Azure Autoconfiguration will combine the Sleuth API to associate a HTTP pipeline policy to | |
* each HTTP-based builder factory. The HTTP-based builder factor is all builder factories that inherit | |
* {@link AbstractAzureHttpClientBuilderFactory} class. The HTTP pipeline policy will be applied to each Azure | |
* SDK business method. It completes the association with the Sleuth context, ensuring that the span created | |
* in the Azure SDK business method and the span created by Sleuth Context form a parent-child relationship. | |
*/ | |
public class AzureSleuthUsages { | |
public static void main(String[] args) { | |
// bellow code demonstrates how to make the spring-cloud-azure-trace-sleuth enabled for spring-cloud-azure-starter-storage-blob | |
// add spring-cloud-azure-starter-storage-blob, spring-cloud-starter-sleuth, spring-cloud-azure-trace-sleuth dependencies | |
// Create Spring Cloud Sleuth beans, Tracer, Propagator | |
// Create Spring Cloud Azure Sleuth beans, HttpPipelinePolicy, AzureHttpClientBuilderFactoryBeanPostProcessor | |
Tracer tracer = null; | |
Propagator propagator = null; | |
SleuthHttpPolicy sleuthHttpPolicy = new SleuthHttpPolicy(tracer, propagator); | |
AzureHttpClientBuilderFactoryBeanPostProcessor postProcessor = new AzureHttpClientBuilderFactoryBeanPostProcessor(); | |
// AzureHttpClientBuilderFactoryBeanPostProcessor will bring an extra policy HttpPipelinePolicy to BlobServiceClientBuilderFactory, | |
// the HttpPipelinePolicy will use the parent span to create child span when using blob service client to access the Storage Blob Service. | |
AzureStorageBlobProperties properties = new AzureStorageBlobProperties(); | |
BlobServiceClientBuilderFactory factory = new BlobServiceClientBuilderFactory(properties); | |
// BlobServiceClientBuilder got the extra HTTP pipeline policy sleuthHttpPolicy. | |
postProcessor.postProcessAfterInitialization(factory, "blobServiceClientBuilderFactory"); | |
BlobServiceClientBuilder blobServiceClientBuilder = factory.build(); | |
BlobServiceClient blobServiceClient = blobServiceClientBuilder.buildClient(); | |
// this blob service client will invoke the method com.azure.spring.tracing.sleuth.SleuthHttpPolicy.process method first, | |
// then do blob container creation, finally handle the response with com.azure.spring.tracing.sleuth.SleuthHttpPolicy.handleResponse method. | |
BlobContainerClient blobContainer = blobServiceClient.createBlobContainer("test"); | |
// In the Sleuth context, there will be the complete trace information, including creating a Blob Container. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment