应用服务网格 ASM-流量监控:如何使用Istio调用链埋点

时间:2024-07-18 20:19:29

如何使用Istio调用链埋点

Header信息包括如下内容,更多关于调用链的信息请参见https://istio.io/docs/tasks/telemetry/distributed-tracing/

  • x-request-id
  • x-b3-traceid
  • x-b3-spanid
  • x-b3-parentspanid
  • x-b3-sampled
  • x-b3-flags
  • x-ot-span-context

下面以一个典型的例子来说明如何在接收和发送请求时,通过修改代码来传递调用链相关Header信息。

  • Python代码示例
    服务接收端在处理请求时,从请求端解析相关Header。在调用后端请求时,传递Trace相关的header。
    def getForwardHeaders(request):
        headers = {}
    
    if 'user' in session:
        headers['end-user'] = session['user']
    
    incoming_headers = ['x-request-id',
        'x-b3-traceid',
        'x-b3-spanid',
        'x-b3-parentspanid',
        'x-b3-sampled',
        'x-b3-flags',
        'x-ot-span-context'
    ]
    
    return headers
    
    
    @ app.route('/productpage')
    def front():
        product_id = 0# TODO: replace
    default value
    headers = getForwardHeaders(request)
    user = session.get('user', '')
    product = getProduct(product_id)
    detailsStatus, details = getProductDetails(product_id, headers)
    reviewsStatus, reviews = getProductReviews(product_id, headers)
    return render_template(
        'productpage.html',
        detailsStatus = detailsStatus,
        reviewsStatus = reviewsStatus,
        product = product,
        details = details,
        reviews = reviews,
        user = user)
  • Java代码示例

    在java Rest接口上除了解析一般业务参数外,需要从header中解析trace相关信息。同样在调用下一个服务时传递该header信息。

    @GET
    @Path("/reviews/{productId}")
    public Response bookReviewsById(@PathParam("productId") int productId,
    @HeaderParam("end-user") String user,
    @HeaderParam("x-request-id") String xreq,
    @HeaderParam("x-b3-traceid") String xtraceid,
    @HeaderParam("x-b3-spanid") String xspanid,
    @HeaderParam("x-b3-parentspanid") String xparentspanid,
    @HeaderParam("x-b3-sampled") String xsampled,
    @HeaderParam("x-b3-flags") String xflags,
    @HeaderParam("x-ot-span-context") String xotspan) {
    	int starsReviewer1 = -1;
    	int starsReviewer2 = -1;
    	if (ratings_enabled) {
    		JsonObject ratingsResponse = getRatings(Integer.toString(productId), user, xreq, xtraceid, xspanid, xparentspanid, xsampled, xflags, xotspan);
    		if (ratingsResponse != null) {
    			if (ratingsResponse.containsKey("ratings")) {
    				JsonObject ratings = ratingsResponse.getJsonObject("ratings");
    				if (ratings.containsKey("Reviewer1")){
    					starsReviewer1 = ratings.getint("Reviewer1");
    				}
    				if (ratings.containsKey("Reviewer2")){
    					starsReviewer2 = ratings.getint("Reviewer2");
    				}
    			}
    		}
    	}
    	String jsonResStr = getJsonResponse(Integer.toString(productId), starsReviewer1, starsReviewer2);
    	return Response.ok().type(MediaType.APPLICATION_JSON).entity(jsonResStr).build();
    }
    }
support.huaweicloud.com/usermanual-asm/asm_01_0070.html