orders icon indicating copy to clipboard operation
orders copied to clipboard

Can you help me with Jaeger tracing?

Open Jacopo-Inglese opened this issue 3 years ago • 1 comments

Hi everybody, i am an informatic student who is trying to use jaeger to trace the interaction between the two microservices "ORDERS" and "SHIPPING" . This is the code i put in AsyncGetService inside the Order repository : i put a change in "postResource" which is the point of connection with Shipping.


@Async public <T, B> Future<T> postResource(URI uri, B body, ParameterizedTypeReference<T> returnType) { Span span = tracer.buildSpan("Sono Orders").start();

	 try (Scope scope = tracer.scopeManager().activate(span)) {

         Span activeSpan = tracer.activeSpan();
  
         Tags.SPAN_KIND.set(activeSpan, Tags.SPAN_KIND_CLIENT);
         Tags.HTTP_METHOD.set(activeSpan, "POST");
         Tags.HTTP_URL.set(activeSpan, uri.toString());
         
	 RequestEntity<B> request = RequestEntity.post(uri).contentType(MediaType.APPLICATION_JSON).accept(MediaType
            .APPLICATION_JSON).body(body); 

	 TextMapAdapter adapter = new  TextMapAdapter(request.getHeaders().toSingleValueMap());
	 System.out.println("HEADERS "+request.getHeaders().toSingleValueMap().toString());

	 Tags.COMPONENT.set(activeSpan,  request.getHeaders().toSingleValueMap().toString());

    tracer.inject(activeSpan.context(), Format.Builtin.HTTP_HEADERS, adapter); //INJECTION
          
    LOG.debug("Requesting: " + request.toString());
    T responseBody = restProxyTemplate.getRestTemplate().exchange(request, returnType).getBody();
    Tags.HTTP_STATUS.set(activeSpan, responseBody.hashCode());
    
    LOG.debug("Received: " + responseBody);
    return new AsyncResult<>(responseBody);
	  }
	  finally {
          span.finish();
      }
}
}
-------------------------------------------------------------------------------

In ShippingController inside the "Shipping" repository i change the other method linked with Order , which is "postShipping" :


@ResponseStatus(HttpStatus.CREATED) @RequestMapping(value = "/shipping", method = RequestMethod.POST) public @ResponseBody Shipment postShipping(@RequestBody Shipment shipment, @RequestHeader Map<String, String> headers) {

	Tracer.SpanBuilder spanBuilder;   
	System.out.println("HEADERS RICEVUTI\n"+headers.toString());

	TextMapAdapter tx =  new TextMapAdapter(headers);
	 
 
	try {
           SpanContext parentSpanCtx = tracer.extract(Format.Builtin.HTTP_HEADERS, tx);
           if (parentSpanCtx == null) {
               spanBuilder = tracer.buildSpan("Span VUOTO").asChildOf(parentSpanCtx);
           } else {
               spanBuilder = tracer.buildSpan("Span FIGLIO").asChildOf(parentSpanCtx);
           }
       } catch (IllegalArgumentException e) {
           spanBuilder = tracer.buildSpan("Span sbagliato");
       }
	Span span =spanBuilder.start();
	System.out.println("Adding shipment to queue...");
    try (Scope scope = tracer.scopeManager().activate(span)) {
	try  {
        rabbitTemplate.convertAndSend("shipping-task", shipment);
    
	} catch (Exception e) {
        System.out.println("Unable to add to queue (the queue is probably down). Accepting anyway. Don't do this " +
                "for real!");
    }   
    span.log(ImmutableMap.of("event", "shipment", "value",shipment ));

	return shipment;
    }
	finally {
        span.finish();
    }
 
}

The problem is that there are Async invocations and i think that for this reason in jaeger i have the 2 span separated and not a son span (from postShipping -> Shipping ) which is linked to the father one (from postResource -> Orders). I cannot use a classic RequestBuilderCarrier because my request is a RequestEntity<B> , and i cannot use TextMapExtractAdapter/TextMapInjectAdapter. If you can help me i really thank you !

Jacopo-Inglese avatar Oct 29 '21 12:10 Jacopo-Inglese

Hi @Jacopo-Inglese , Could you tell me how you did to activate the trace in the orders microservices? I can't get the order traces to show up in the Zipkin. Thank you.

jnobre avatar Jan 28 '23 21:01 jnobre