In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the example analysis of Spring Cloud Sleuth Span, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Span customization
Thanks to SpanInjector and SpanExtractor, you can customize how spans is created and propagated.
There are currently two built-in ways to pass trace information between processes:
Through Spring Integration
Through HTTP
Span ids is extracted from the Zipkin compatible (B3) header (Message or HTTP header) to start or join an existing trace. Trace information is injected into any outbound request, so the next hop can extract them.
An important change from previous versions of Sleuth is that Sleuth is implementing Open Tracing's TextMap concept. In Sleuth, it is called SpanTextMap. Basically the idea is that any means of communication (such as messages, http requests, etc.) can be abstracted through SpanTextMap. This abstraction defines how to insert data into a carrier and how to retrieve data from there. Thanks for this, if you want to use a new HTTP library that uses FooRequest as the average for sending HTTP requests, you must create an implementation of SpanTextMap that delegates calls to FooRequest to retrieve and insert HTTP headers.
Spring Integration
For Spring Integration, there are two interfaces responsible for creating a Span from Message. These are:
MessagingSpanTextMapExtractor
MessagingSpanTextMapInjector
You can override them by providing your own implementation.
HTTP
For HTTP, there are two interfaces responsible for creating a Span from Message. These are:
HttpSpanExtractor
HttpSpanInjector
You can override them by providing your own implementation.
Example
We assume that instead of the standard Zipkin-compatible trace HTTP header name
For trace id-correlationId
For span id-mySpanId
This is an example of SpanExtractor.
Static class CustomHttpSpanExtractor implements HttpSpanExtractor {@ Override public Span joinTrace (SpanTextMap carrier) {Map map = TextMapUtil.asMap (carrier); long traceId = Span.hexToId (map.get ("correlationid")); long spanId = Span.hexToId (map.get ("myspanid")); / / extract all necessary headers Span.SpanBuilder builder = Span.builder () .traceId (traceId) .spanId (spanId) / / build rest of the Span return builder.build ();}} static class CustomHttpSpanInjector implements HttpSpanInjector {@ Override public void inject (Span span, SpanTextMap carrier) {carrier.put ("correlationId", span.traceIdString ()); carrier.put ("mySpanId", Span.idToHex (span.getSpanId ());}}
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twenty-one
You can register as follows:
BeanHttpSpanInjector customHttpSpanInjector () {return new CustomHttpSpanInjector ();} @ BeanHttpSpanExtractor customHttpSpanExtractor () {return new CustomHttpSpanExtractor ();}
Spring Cloud for security reasons, Sleuth does not add trace / span related headers to the Http response. If you need a title, inject the title into the custom SpanInjector of the Http response, and you can add an Servlet filter that uses this tag in the following ways:
Static class CustomHttpServletResponseSpanInjector extends ZipkinHttpSpanInjector {@ Override public void inject (Span span, SpanTextMap carrier) {super.inject (span, carrier); carrier.put (Span.TRACE_ID_NAME, span.traceIdString ()); carrier.put (Span.SPAN_ID_NAME, Span.idToHex (span.getSpanId ();} static class HttpResponseInjectingTraceFilter extends GenericFilterBean {private final Tracer tracer Private final HttpSpanInjector spanInjector; public HttpResponseInjectingTraceFilter (Tracer tracer, HttpSpanInjector spanInjector) {this.tracer = tracer; this.spanInjector = spanInjector;} @ Override public void doFilter (ServletRequest request, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {HttpServletResponse response = (HttpServletResponse) servletResponse; Span currentSpan = this.tracer.getCurrentSpan () This.spanInjector.inject (currentSpan, new HttpServletResponseTextMap (response)); filterChain.doFilter (request, response);} class HttpServletResponseTextMap implements SpanTextMap {private final HttpServletResponse delegate; HttpServletResponseTextMap (HttpServletResponse delegate) {this.delegate = delegate } @ Override public Iterator iterator () {Map map = new HashMap (); for (String header: this.delegate.getHeaderNames ()) {map.put (header, this.delegate.getHeader (header)) } return map.entrySet () .iterator ();} @ Override public void put (String key, String value) {this.delegate.addHeader (key, value);}
You can register as follows:
@ Bean HttpSpanInjector customHttpServletResponseSpanInjector () {return new CustomHttpServletResponseSpanInjector ();} @ BeanHttpResponseInjectingTraceFilter responseInjectingTraceFilter (Tracer tracer) {return new HttpResponseInjectingTraceFilter (tracer, customHttpServletResponseSpanInjector ());} Custom SA tags in Zipkin
Sometimes you want to create a manual Span that packages a phone to an external service that is not being detected. What you can do is create a span with a peer.service tag that contains the value of the service to be invoked. Below you can see an example of calling Redis, which is wrapped in such a span.
Org.springframework.cloud.sleuth.Span newSpan = tracer.createSpan ("redis"); try {newSpan.tag ("redis.op", "get"); newSpan.tag ("lc", "redis"); newSpan.logEvent (org.springframework.cloud.sleuth.Span.CLIENT_SEND); / / call redis service e.g / / return (SomeObj) redisTemplate.opsForHash (). Get ("MYHASH", someObjKey) } finally {newSpan.tag ("peer.service", "redisService"); newSpan.tag ("peer.ipv4", "1.2.3.4"); newSpan.tag ("peer.port", "1234"); newSpan.logEvent (org.springframework.cloud.sleuth.Span.CLIENT_RECV); tracer.close (newSpan);} important remember not to add peer.service tags and SA tags! You just need to add peer.service. Custom service name
By default, Sleuth assumes that when you send the span to Zipkin, you want the service name of the span to be equal to the spring.application. Name value. This is not always the case. In some cases, you want to provide different service names for all spans in your application. To achieve this, you can override this value (an example of a foo service name) simply by passing the following property to the application:
Spring.zipkin.service.name: foo
one
Host locator
In order to define the host corresponding to a specific span, we need to resolve the hostname and port. The default method is to get it from the server properties. If it is not set for some reason, we are trying to retrieve the hostname from the network interface.
If you enable the discovery client and prefer to retrieve the host address from an instance registered in the service registry, you must set the property (for HTTP-and Stream-based span reports).
Spring.zipkin.locator.discovery.enabled: true
one
Span Data as a message
You can accumulate and send data across the Spring Cloud Stream by using spring-cloud-sleuth-stream jar as a dependency and add a channel Binder implementation (for example, spring-cloud-starter-stream-rabbit) to Kafka for RabbitMQ or spring-cloud-starter-stream-kafka. Accumulate and send range data for {22 /} Stream by taking spring-cloud-sleuth-stream jar as a dependency and adding Binder channel spring-cloud-starter-stream-rabbit for RabbitMQ or spring-cloud-starter-stream-kafka.
Kafka). This will automatically convert your application to the maker of a message of payload type Spans.
Zipkin consumers
There is a special convenience annotation for setting up a message consumer for Span data and pushing it into Zipkin SpanStore. This application
@ SpringBootApplication@EnableZipkinStreamServerpublic class Consumer {public static void main (String [] args) {SpringApplication.run (Consumer.class, args);}}
You will listen to any shipping Span data you provide via Spring Cloud StreamBinder (for example, RabbitMQ contains spring-cloud-starter-stream-rabbit), similar initiators of Redis and Kafka). If you add the following UI dependencies
Io.zipkin.javazipkin-autoconfigure-ui
one
two
Then you will have a Zipkin server, and your application will host UI and API on port 9411.
The default SpanStore is in memory (suitable for demonstration, getting started). For more powerful solutions, you can add MySQL and spring-boot-starter-jdbc to the classpath and enable JDBC SpanStore through configuration, such as:
Spring: rabbitmq: host: ${RABBIT_HOST:localhost} datasource: schema: classpath:/mysql.sql url: jdbc:mysql://$ {MYSQL_HOST:localhost} / test username: root password: root# Switch this on to create the schema on startup: initialize: true continueOnError: true sleuth: enabled: falsezipkin: storage: type: mysql Note @ EnableZipkinStreamServer is also annotated with @ EnableZipkinServer, so the process will also expose standard Zipkin server endpoints to collect spans through HTTP And query in Zipkin Web UI. Customized consumer
You can also use spring-cloud-sleuth-stream and bind to SleuthSink to easily implement custom consumers. Example:
EnableBinding (SleuthSink.class) @ SpringBootApplication (exclude = SleuthStreamAutoConfiguration.class) @ MessageEndpointpublic class Consumer {@ ServiceActivator (inputChannel = SleuthSink.INPUT) public void sink (Spans input) throws Exception {/ /. Process spans}} Note that the example consumer application above explicitly excludes SleuthStreamAutoConfiguration, so it does not send messages to itself, but this is optional (you may actually want to trace messages to the consumer application).
To customize the polling mechanism, you can create a bean of type PollerMetadata whose name is equal to StreamSpanReporter.POLLER. An example of such a configuration can be found here.
@ Configurationpublic static class CustomPollerConfiguration {@ Bean (name = StreamSpanReporter.POLLER) PollerMetadata customPoller () {PollerMetadata poller = new PollerMetadata (); poller.setMaxMessagesPerPoll (500); poller.setTrigger (new PeriodicTrigger (5000L)); return poller;}} Metric
Spring Cloud Sleuth currently registers simple metrics related to spans. It uses Spring Boot's metrics to support
To calculate the number of accepted and deleted spans. The number of accepted spans increases each time it is sent to Zipkin. If an error occurs, the number of deleted spans will increase.
Thank you for reading this article carefully. I hope the article "sample Analysis of Spring Cloud Sleuth Span" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.