Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support HTTP based Azure SDK service tracing by Spring Sleuth implementation #24192

Conversation

moarychan
Copy link
Member

@moarychan moarychan commented Sep 17, 2021

You can find the feature proposal here, this PR will support the service HTTP based, the tracer is delegated to Spring Sleuth, it's available to record the service activities when enabling the Spring Sleuth function.

@ghost ghost added the azure-spring All azure-spring related issues label Sep 17, 2021
@moarychan moarychan changed the title Support Azure tracing by Sleuth implementation Support Azure SDK service tracing by Spring Sleuth implementation Sep 17, 2021
@moarychan moarychan changed the title Support Azure SDK service tracing by Spring Sleuth implementation Support HTTP based Azure SDK service tracing by Spring Sleuth implementation Sep 28, 2021
@check-enforcer
Copy link

This pull request is protected by Check Enforcer.

What is Check Enforcer?

Check Enforcer helps ensure all pull requests are covered by at least one check-run (typically an Azure Pipeline). When all check-runs associated with this pull request pass then Check Enforcer itself will pass.

Why am I getting this message?

You are getting this message because Check Enforcer did not detect any check-runs being associated with this pull request within five minutes. This may indicate that your pull request is not covered by any pipelines and so Check Enforcer is correctly blocking the pull request being merged.

What should I do now?

If the check-enforcer check-run is not passing and all other check-runs associated with this PR are passing (excluding license-cla) then you could try telling Check Enforcer to evaluate your pull request again. You can do this by adding a comment to this pull request as follows:
/check-enforcer evaluate
Typically evaulation only takes a few seconds. If you know that your pull request is not covered by a pipeline and this is expected you can override Check Enforcer using the following command:
/check-enforcer override
Note that using the override command triggers alerts so that follow-up investigations can occur (PRs still need to be approved as normal).

What if I am onboarding a new service?

Often, new services do not have validation pipelines associated with them, in order to bootstrap pipelines for a new service, you can issue the following command as a pull request comment:
/azp run prepare-pipelines
This will run a pipeline that analyzes the source tree and creates the pipelines necessary to build and validate your pull request. Once the pipeline has been created you can trigger the pipeline using the following comment:
/azp run java - [service] - ci

@moarychan
Copy link
Member Author

/azp run java - spring - tests

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

private final Propagator propagator;

// standard attributes with http call information
private static final String HTTP_USER_AGENT = "http.user_agent";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/Azure/azure-sdk/blob/main/docs/tracing/distributed-tracing-conventions.yml

could you also take a look of this doc, let's make the tracing id aligned with the doc

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, which stage will this policy be added into pipeline, before or after retry? will retry requests be tracked?

Copy link
Member Author

@moarychan moarychan Sep 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this policy will be placed after a HttpPipelinePosition.PER_RETRY by default, it uses the default pipeline position,

the retry request will be tracked.


public final class HttpTraceUtil {

private static final String STATUS_100 = "Continue";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to define these stuff by youslef, how about using something existing, like org.springframework.http.HttpStatus

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, the dependency org.springframework:spring-web will be added.

* </ul>
*/
@Immutable
public final class SpanId {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is no such class within spring-cloud-sleuth project?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, not found a suitable class.

@moarychan
Copy link
Member Author

/azp run java - spring - tests

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@moarychan
Copy link
Member Author

/azp run java - spring - tests

@moarychan moarychan marked this pull request as draft October 18, 2021 01:49
@moarychan moarychan force-pushed the feature/azure-trace-sleuth-implementation branch from 2af1c96 to 0ec6837 Compare October 18, 2021 07:23
@@ -194,6 +200,7 @@
<module>spring-cloud-azure-starter-stream-servicebus-queue</module>
<module>spring-cloud-azure-stream-binder-servicebus-topic</module>
<module>spring-cloud-azure-starter-stream-servicebus-topic</module>
<module>spring-cloud-azure-trace-sleuth</module>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: move this after spring-cloud-azure-stream-binder-test

@@ -239,6 +246,7 @@
<module>spring-cloud-azure-starter-stream-servicebus-queue</module>
<module>spring-cloud-azure-stream-binder-servicebus-topic</module>
<module>spring-cloud-azure-starter-stream-servicebus-topic</module>
<module>spring-cloud-azure-trace-sleuth</module>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

/**
* Auto-configuration for an Azure SDK Sleuth {@link Tracer}.
*/
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why adding this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought there would be the following confusing log:
trationDelegate$BeanPostProcessorChecker : Bean 'com.azure.spring.cloud.autoconfigure.trace.sleuth.AzureSleuthAutoConfiguration' of type [com.azure.spring.cloud.autoconfigure.trace.sleuth.AzureSleuthAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying).
After added this @ROLE, it looks good.

Comment on lines 206 to 212
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-autoconfigure</artifactId>
<version>3.0.3</version> <!-- {x-version-update;org.springframework.cloud:spring-cloud-sleuth-autoconfigure;external_dependency} -->
<optional>true</optional>
</dependency>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a @ConditionOnClass org.springframework.cloud.sleuth.autoconfig.SleuthTracerProperties in AzureSleuthAutoConfiguration.

*/
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ SleuthHttpPolicy.class, Tracer.class, SleuthTracerProperties.class })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to depend on the auto-configure of sleuth? Or is it enough if we find a Tracer bean?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it's not required, the sleuth API Tracer class is enough.

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (beanFactory != null && bean instanceof AbstractAzureHttpClientBuilderFactory) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will the beanfactory be null?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to @nullable, it can be remove too.

Comment on lines +49 to +53
<groupId>com.azure</groupId>
<artifactId>azure-storage-blob</artifactId>
<version>12.14.1</version> <!-- {x-version-update;com.azure:azure-storage-blob;dependency} -->
<scope>test</scope>
</dependency>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why adding this storage blob dependency?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's for test blob service builder

Comment on lines 83 to 96
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.2</version> <!-- {x-version-update;org.apache.maven.plugins:maven-jar-plugin;external_dependency} -->
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this used for?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied it from the core module, will remove it.

Comment on lines 40 to 43
@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why adding this Ordered?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this bean should be the low precedence to register, after testing, it can be removed.

if ((boolean) context.getData(DISABLE_TRACING_KEY).orElse(false)) {
return next.process();
}
// tracer.getBaggage()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need this comment?

@moarychan
Copy link
Member Author

/azp run java - spring - tests

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Moary Chen added 2 commits October 21, 2021 14:26
@moarychan
Copy link
Member Author

/azp run java - spring - tests

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@lmolkova
Copy link
Member

Hey @moarychan and @saragluna I'm doing some changes in #25012 related to Tracer. I believe you can ignore the rest of that PR, as it's OTel-specific, but the important difference is on PARENT_SPAN_KEY:
Span is too narrow and we'd want to propagate more than that: In OTel it's Context that contains span + baggage + possible other markers. Sleuth (brave) doesn't have context and passes baggage + spans separately.

What I propose: let's use the same wider name (e.g. PARENT_TRACE_CONTEXT_KEY) for Otel context or Sleuth spans and always refer to it in the docs, but deprecate PARENT_SPAN_KEY. I'm open to any other names if you have any suggestions.

Copy link
Member

@lmolkova lmolkova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks fine, I left some minor comments.

The only blocker I see is docs: It's important to highlight that Sleuth integration is a solution that applies to existing Sleuth users and has limited functionality (i.e. HTTP requests tracing only). Microsoft supports OpenTelemetry company-wide and we should point users in this direction to provide a better long-term experience to users and avoid any reputational damage.

// run the next policy and handle success and error
return next.process()
.doOnEach(SleuthHttpPolicy::handleResponse)
.contextWrite(Context.of("TRACING_SPAN", span, "REQUEST", request));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: is "REQUEST" needed?

return next.process();
}

Span parentSpan = (Span) context.getData(PARENT_SPAN_KEY).orElse(tracer.currentSpan());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might make sense to move span creation to contextWrite similarly to

.contextWrite(reactor.util.context.Context.of(REACTOR_PARENT_TRACE_CONTEXT_KEY, startSpan(context)));
in #25012

It ensures that span is created when someone really subscribes to this reactor call and not on the hot path.

request.getHeaders().getValue("User-Agent"));
putTagIfNotEmptyOrNull(span, HTTP_METHOD, request.getHttpMethod().toString());
putTagIfNotEmptyOrNull(span, HTTP_URL, request.getUrl().toString());
Optional<Object> tracingNamespace = context.getData(AZ_TRACING_NAMESPACE_KEY);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are Otel conventions. Brave conventions are close, but not the same: https://github.com/openzipkin/brave/blob/master/instrumentation/http/README.md

requestId = response.getHeaderValue(REQUEST_ID);
}

putTagIfNotEmptyOrNull(span, REQUEST_ID, requestId);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this has been changed slightly here to be consistent across all languages 7a5d4ab#diff-74209afca0d9d5d4a8a40b7b0e0e28b0df6529e0e2af5c4fff1b33cc54ff4b24

// Error status, try to parse the error status.
HttpStatus status = HttpStatus.resolve(statusCode);
if (status != null) {
return span.tag("http.status_message", status.getReasonPhrase());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how to modify it, please help provide more specific information.

[Source code][src] | [Package (Maven)][package] | [API reference documentation][refdocs]

## Getting started

Copy link
Member

@lmolkova lmolkova Oct 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Microsoft embraces OpenTelemetry, it's important to highlight here that it's the short term solution targeting existing Sleuth users with manual instrumentation.

I would suggest putting something along the lines of:

"Spring Cloud for Azure Sleuth Trace is the solution for users who manually instrumented their applications with Sleuth and are looking to add observability into HTTP requests made by Azure SDKs.

For everyone else, we recommend using OpenTelemetry as a ubiquitous and portable observability solution for tracing, metrics, and logs supported by most APM tools with extensive integrations across Java ecosystem"

@moarychan
Copy link
Member Author

Hi @lmolkova , Thank you so much for your comments.
The new keys from your PR are fine, but now I can not import using directly, once the azure-core package is released, I will make a sync update.

@lmolkova
Copy link
Member

lmolkova commented Nov 1, 2021

Hi @lmolkova , Thank you so much for your comments. The new keys from your PR are fine, but now I can not import using directly, once the azure-core package is released, I will make a sync update.

no problem, you can keep PARENT_SPAN_KEY for as long as you want - it's deprecated, but stays for a while. And azure-core is backward compatible with it

@moarychan moarychan marked this pull request as ready for review November 17, 2021 01:12
@moarychan
Copy link
Member Author

/azp run java - spring - tests

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@moarychan moarychan merged commit efdde62 into Azure:feature/azure-spring-cloud-4.0 Nov 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
azure-spring All azure-spring related issues azure-spring-keyvault Spring keyvault related issues. azure-spring-storage Spring storage releated issues.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE REQ] Supports Spring Cloud Sleuth for Azure HTTP-based service client
4 participants