Istio采用SideCar模式注入的Enovy代理在某些情况下不能完全解决对项目的无侵入性,比如需要用到Istio的链路追踪功能的时候。需要在代码中手动注入链路追踪需要的header,这样就出现了Istio对业务功能的侵入性。
istio服务网格的调用链跟踪需要依赖在服务之间传递trade和span相关的header来实现,否则无法把不同的服务调用关联到同一个调用链,envoy可以实现流量拦截和指标上报,但是无法协助应用程序传递
header,所以应用程序要是需要进行传递header的埋点。请看下面的例子:
借助opentracing,可以实现半自动化埋点。
通过引入opentracing-spring-zipkin-web-starter可以实现调用链跟踪的自动化埋点。
pom.xml:
<dependency><groupId>io.opentracing.contrib</groupId> <artifactId>opentracing-spring-zipkin-web-starter</artifactId> <version>0.1.4</version>
</dependency>
application.yml:
opentracing: zipkin:enabled: true http-sender:baseUrl: http://zipkin:9411/
关于手动埋点的例子,可以在istio官网的bookinfo的例子中见到。如下:
package application.rest;import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.StringReader;@Path("/")
public class LibertyRestEndpoint extends Application {private final static Boolean ratings_enabled = Boolean.valueOf(System.getenv("ENABLE_RATINGS"));private final static String star_color = System.getenv("STAR_COLOR") == null ? "black" : System.getenv("STAR_COLOR");private final static String services_domain = System.getenv("SERVICES_DOMAIN") == null ? "" : ("." + System.getenv("SERVICES_DOMAIN"));private final static String ratings_hostname = System.getenv("RATINGS_HOSTNAME") == null ? "ratings" : System.getenv("RATINGS_HOSTNAME");private final static String ratings_port = System.getenv("RATINGS_SERVICE_PORT") == null ? "9080" : System.getenv("RATINGS_SERVICE_PORT");private final static String ratings_service = String.format("http://%s%s:%s/ratings", ratings_hostname, services_domain, ratings_port);private final static String pod_hostname = System.getenv("HOSTNAME");private final static String clustername = System.getenv("CLUSTER_NAME");// HTTP headers to propagate for distributed tracing are documented at// https://istio.io/docs/tasks/telemetry/distributed-tracing/overview/#trace-context-propagationprivate final static String[] headers_to_propagate = {// All applications should propagate x-request-id. This header is// included in access log statements and is used for consistent trace// sampling and log sampling decisions in Istio."x-request-id",// Lightstep tracing header. Propagate this if you use lightstep tracing// in Istio (see// https://istio.io/latest/docs/tasks/observability/distributed-tracing/lightstep/)// Note: this should probably be changed to use B3 or W3C TRACE_CONTEXT.// Lightstep recommends using B3 or TRACE_CONTEXT and most application// libraries from lightstep do not support x-ot-span-context."x-ot-span-context",// Datadog tracing header. Propagate these headers if you use Datadog// tracing."x-datadog-trace-id","x-datadog-parent-id","x-datadog-sampling-priority",// W3C Trace Context. Compatible with OpenCensusAgent and Stackdriver Istio// configurations."traceparent","tracestate",// Cloud trace context. Compatible with OpenCensusAgent and Stackdriver Istio// configurations."x-cloud-trace-context",// Grpc binary trace context. Compatible with OpenCensusAgent nad// Stackdriver Istio configurations."grpc-trace-bin",// b3 trace headers. Compatible with Zipkin, OpenCensusAgent, and// Stackdriver Istio configurations. Commented out since they are// propagated by the OpenTracing tracer above."x-b3-traceid","x-b3-spanid","x-b3-parentspanid","x-b3-sampled","x-b3-flags",// SkyWalking trace headers."sw8",// Application-specific headers to forward."end-user","user-agent",// Context and session specific headers"cookie","authorization","jwt",};private String getJsonResponse(String productId, int starsReviewer1, int starsReviewer2) {//省略}private JsonObject getRatings(String productId, HttpHeaders requestHeaders) {//省略}}