尽管Istio代理能够自动发送Span,但它们需要一些信息来将整个跟踪串联在一起。应用程序需要传播适当的HTTP标头,以便在代理发送Span信息时,Span可以正确地关联到单个跟踪中。
为此,应用程序需要收集传入请求中的HTTP标头,并将其传播到任何传出请求。HTTP标头如下所示:
x-request-id x-b3-traceid x-b3-spanid x-b3-parentspanid x-b3-sampled x-b3-flags x-ot-span-context
在Istio官方的示例服务Productpage中,可以看到该productpage服务(Python)从HTTP请求中提取所需的标头,如下所示:
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'
]
for ihdr in incoming_headers:
val = request.headers.get(ihdr)
if val is not None:
headers[ihdr] = val
#print "incoming: "+ihdr+":"+val
return headers
评论应用程序(Java语言实现)则是执行了如下类似的操作:
@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);
在应用程序中进行下游调用时,请确保包含这些标头。
按照前面章节的介绍,现在部署Istio官方示例Bookinfo应用程序,即执行如下命令:
kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml kubectl apply -f samples/bookinfo/networking/bookinfo-gateway.yaml