Add EventGrid distributed tracing#15850
Conversation
| .collectList() | ||
| .flatMap(list -> this.impl.publishCloudEventEventsAsync(this.hostname, list, context)); | ||
| .flatMap(list -> this.impl.publishCloudEventEventsAsync(this.hostname, list, | ||
| context.addData(AZ_TRACING_NAMESPACE_KEY, Constants.EVENT_GRID_TRACING_NAMESPACE_VALUE))); |
There was a problem hiding this comment.
Make sure to check context for null on the calling function to avoid NPE when doing addData
There was a problem hiding this comment.
Does withContext guarantee context to be not null?
There was a problem hiding this comment.
In some cases, we end up calling this from the sync client and hence the context could be null. Need a check there when passing.
| event.addExtensionAttribute(Constants.TRACE_PARENT, Constants.TRACE_PARENT_PLACEHOLDER); | ||
| event.addExtensionAttribute(Constants.TRACE_STATE, Constants.TRACE_STATE_PLACEHOLDER); |
There was a problem hiding this comment.
I put the two placeholder separately. After placeholder replacement, whatever in the request headers will be put into the body eventually. There won't be two in the header has just one.
...ing-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherAsyncClient.java
Outdated
Show resolved
Hide resolved
.../main/java/com/azure/messaging/eventgrid/systemevents/AcsChatMessageEventBaseProperties.java
Show resolved
Hide resolved
| // Please see <a href=https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/azure-services-resource-providers>here</a> | ||
| // for more information on Azure resource provider namespaces. |
There was a problem hiding this comment.
Not sure how this resource provide namespace link is relevant to the constructor.
There was a problem hiding this comment.
It's for a constant, which is copied to Constants.java but I didn't copy this together. Good catch.
| .collectList() | ||
| .flatMap(list -> this.impl.publishEventsAsync(this.hostname, list, context)); | ||
| .flatMap(list -> this.impl.publishEventsAsync(this.hostname, | ||
| list, context.addData(AZ_TRACING_NAMESPACE_KEY, Constants.EVENT_GRID_TRACING_NAMESPACE_VALUE))); |
There was a problem hiding this comment.
Should have a null check for context before using it.
There was a problem hiding this comment.
This internal api is called by a public API, which calls FluxUtil.withContext to create a Context. So I assume the context won't be null.
In debugging, I see it's an empty Context instance instead of null.
There was a problem hiding this comment.
@YijunXieMS - this method is also called from sync client and the user can pass a null context.
User can call sendEventsWithResponse(events, null)
public Response<Void> sendEventsWithResponse(Iterable<EventGridEvent> events, Context context) {
return asyncClient.sendEventsWithResponse(events, context).block();
}|
|
||
| private void addCloudEventTracePlaceHolder(Iterable<CloudEvent> events) { | ||
| if (TracerProxy.isTracingEnabled()) { | ||
| for (CloudEvent event : events) { |
There was a problem hiding this comment.
events can be null since the public APIs don't seem to check. It might be better to have the null check and include an error message to indicate that events cannot be null.
There was a problem hiding this comment.
Added null check like in EventHubs
| import java.nio.charset.StandardCharsets; | ||
|
|
||
|
|
||
| public class CloudEventTracingPipelinePolicy implements HttpPipelinePolicy { |
| } | ||
|
|
||
| static String replaceTracingPlaceHolder(HttpRequest request, ByteBuffer byteBuffer) { | ||
| String bodyString = new String(byteBuffer.array(), StandardCharsets.UTF_8); |
There was a problem hiding this comment.
Can the Flux stream have strings that are split across multiple ByteBuffer boundaries?
There was a problem hiding this comment.
Changed to use a StringBuilder to take everything from the Flux. Good question.
| String bodyString = new String(byteBuffer.array(), StandardCharsets.UTF_8); | ||
| final HttpHeader traceparentHeader = request.getHeaders().get(Constants.TRACE_PARENT); | ||
| final HttpHeader tracestateHeader = request.getHeaders().get(Constants.TRACE_STATE); | ||
| bodyString = bodyString.replace(Constants.TRACE_PARENT_REPLACE, |
There was a problem hiding this comment.
It is not very clear why we are replacing the trace names. It might be good to add some documentation for this method.
There was a problem hiding this comment.
Added Javadoc and some comments.
| .collectList() | ||
| .flatMap(list -> this.impl.publishEventsAsync(this.hostname, list, context)); | ||
| .flatMap(list -> this.impl.publishEventsAsync(this.hostname, | ||
| list, context.addData(AZ_TRACING_NAMESPACE_KEY, Constants.EVENT_GRID_TRACING_NAMESPACE_VALUE))); |
There was a problem hiding this comment.
@YijunXieMS - this method is also called from sync client and the user can pass a null context.
User can call sendEventsWithResponse(events, null)
public Response<Void> sendEventsWithResponse(Iterable<EventGridEvent> events, Context context) {
return asyncClient.sendEventsWithResponse(events, context).block();
}| .collectList() | ||
| .flatMap(list -> this.impl.publishEventsWithResponseAsync(this.hostname, list, context)); | ||
| .flatMap(list -> this.impl.publishEventsWithResponseAsync(this.hostname, list, | ||
| context.addData(AZ_TRACING_NAMESPACE_KEY, Constants.EVENT_GRID_TRACING_NAMESPACE_VALUE))); |
There was a problem hiding this comment.
I think my comment about user passing null for context applies to this method.
.../main/java/com/azure/messaging/eventgrid/implementation/CloudEventTracingPipelinePolicy.java
Show resolved
Hide resolved
|
/azp run java - eventgrid - tests |
|
Azure Pipelines successfully started running 1 pipeline(s). |
EventGridEvent,CloudEventand system events.CloudEvent, If the tracestate/traceparent are not populated in the event, then we want to copy the traceparent/tracestate into the CloudEvent extensions. Closes Distributed Tracing in EventGrid #14851NullPointerExceptionbug forCloudEvent.getExtensionAttributes()andaddExtensionAttributes().AcsChatMessageEventBasePropertiesandAcsChatThreadEventBasePropertiesbecause the swagger file is changed. Closes Make EventGrid ACS chat events "version" property as Long #15848