Java调用跟踪系统_Tracer:在分布式系统中的调用跟踪和日志相关

Tracer: Distributed system tracing

highway.jpg

68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7a616c616e646f2f7472616365722f6d61737465722e73766768747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f7a616c616e646f2f7472616365722f6d61737465722e73766768747470733a2f2f6a617661646f632d656d626c656d2e7268636c6f75642e636f6d2f646f632f6f72672e7a616c616e646f2f7472616365722d636f72652f62616467652e73766768747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f7a616c616e646f2f7472616365722e73766768747470733a2f2f696d672e736869656c64732e696f2f6d6176656e2d63656e7472616c2f762f6f72672e7a616c616e646f2f7472616365722d706172656e742e73766768747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667

Tracer noun, /ˈtɹeɪsɚ/: A round of ammunition that contains a flammable substance that produces a visible trail when fired in the dark.

Tracer is a library that manages custom trace identifiers and carries them through distributed systems. Traditionally traces are transported as custom HTTP headers, usually generated by clients on the very first request. Traces are added to any subsequent requests and responses, especially to transitive dependencies. Having a consistent trace across different services in a system allows to correlate requests and responses beyond the traditional single client-server communication.

This library historically originates from a closed-source implementation called Flow-ID. The goal was to create a clean open source version in which we could get rid of all the drawbacks of the old implementation, e.g. strong-coupling to internal libraries, single hard-coded header and limited testability.

Status: Under development and used in production

Features

Tracing of HTTP requests and responses

Customization by having a pluggable trace format and lifecycle listeners for easy integration

Support for Servlet containers, Apache’s HTTP client, Hystrix, JUnit, AspectJ and (via its elegant API) several other frameworks

Convenient Spring Boot Auto Configuration

Sensible defaults

Dependencies

Java 8

Any build tool using Maven Central, or direct download

Servlet Container (optional)

Apache HTTP Client (optional)

Hystrix (optional)

AspectJ (optional)

JUnit (optional)

Spring Boot (optional)

Installation

Selectively add the following dependencies to your project:

org.zalando

tracer-core

${tracer.version}

org.zalando

tracer-servlet

${tracer.version}

org.zalando

tracer-httpclient

${tracer.version}

org.zalando

tracer-hystrix

${tracer.version}

org.zalando

tracer-aspectj

${tracer.version}

org.zalando

tracer-junit

${tracer.version}

test

org.zalando

tracer-spring-boot-starter

${tracer.version}

Usage

After adding the dependency, create a Tracer and specify the name of the traces you want to manage:

Tracer tracer = Tracer.create("X-Trace-ID");

If you need access to the current trace's value, call getValue() on it:

Trace trace = tracer.get("X-Trace-ID"); // this is a live-view that can be a shared as a singleton

entity.setLastModifiedBy(trace.getValue());

By default there can only be one trace active at a time. Sometimes it can be useful to stack traces:

Tracer tracer = Tracer.builder()

.stacked(true)

.trace("X-Trace-ID")

.build();

Generators

When starting a new trace, Tracer will create trace value by means of pre-configured generator. You can override this on a per-trace level by adding the following to your setup:

Tracer tracer = Tracer.builder()

.trace("X-Trace-ID", new CustomGenerator())

.build();

There are several generator implementations included.

UUIDGenerator

This is the default generator implementation. It creates 36 characters long, random-based UUID string as a trace value.

FlowIDGenerator

This generator was created for historical reasons. It basically renders a UUID as a base64-encoded byte array, e.g. REcCvlqMSReeo7adheiYFA. The length of generated value is 22 characters.

PhraseGenerator

Phrase generator provides over 10^9 different phrases like:

tender_goodall_likes_evil_panini

nostalgic_boyd_helps_agitated_noyce

pensive_allen_tells_fervent_einstein

The generated phrase is 22 to 61 character long.

Listeners

For some use cases, e.g. integration with other frameworks and libraries, it might be useful to register a listener that gets notified every time a trace is either started or stopped.

Tracer tracer = Tracer.builder()

.trace("X-Trace-ID")

.listener(new CustomTraceListener())

.build();

Tracer comes with a very useful listener by default, the MDCTraceListener:

Tracer tracer = Tracer.builder()

.trace("X-Trace-ID")

.listener(new MDCTraceListener())

.build();

It allows you to add the trace id to every log line:

Another built-in listener is the LoggingTraceListener which logs the start and end of every trace.

Servlet

On the server side is a single filter that you must be register in your filter chain. Make sure it runs very early — otherwise you might miss some crucial information when debugging.

You have to register the TracerFilter as a Filter in your filter chain:

context.addFilter("TracerFilter", new TracerFilter(tracer))

.addMappingForUrlPatterns(EnumSet.of(REQUEST, ASYNC, ERROR), true, "/*");

Apache HTTP Client

Many client-side HTTP libraries on the JVM use the Apache HTTPClient, which is why Tracer comes with a request interceptor:

DefaultHttpClient client = new DefaultHttpClient();

client.addRequestInterceptor(new TracerHttpRequestInterceptor(tracer));

Hystrix

Tracer comes with built-in Hystrix support in form of a custom HystrixConcurrencyStrategy:

final HystrixPlugins plugins = HystrixPlugins.getInstance();

final HystrixConcurrencyStrategy delegate = HystrixConcurrencyStrategyDefault.getInstance(); // or another

plugins.registerConcurrencyStrategy(new TracerConcurrencyStrategy(tracer, delegate));

AspectJ

For background jobs and tests, you can use the built-in aspect:

@Traced

public void performBackgroundJob() {

// do work

}

or you can manage the lifecycle yourself:

tracer.start();

try {

// do work

} finally {

tracer.stop();

}

JUnit

Tracer comes with a special TestRule that manages traces for every test run for you:

@Rule

public final TracerRule tracing = new TracerRule(tracer);

It also supports JSR-330-compliant dependency injection frameworks:

@Inject

@Rule

public final TracerRule tracing;

Spring Boot Starter

Tracer comes with a convenient auto configuration for Spring Boot users that sets up aspect, servlet filter and MDC support automatically with sensible defaults:

Configuration

Description

Default

tracer.stacked

Enables stacking of traces

false

tracer.aspect.enabled

true

tracer.async.enabled

Enables for asynchronous tasks, i.e. @Async

true

tracer.filter.enabled

true

tracer.logging.enabled

false

tracer.logging.category

Changes the category of the LoggingTraceListener

org.zalando.tracer.Tracer

tracer.mdc.enabled

true

tracer.scheduling.enabled

Enables support for Task Scheduling, i.e. @Scheduled

true

tracer.scheduling.pool-size

Configures the thread pool size, i.e. the number of scheduled tasks

# of CPUs

tracer.traces

Configures actual traces, mapping from name to generator type (uuid, flow-id or phrase)

tracer:

aspect.enabled: true

async.enabled: true

filter.enabled: true

logging:

enabled: false

category: org.zalando.tracer.Tracer

mdc.enabled: true

scheduling.enabled: true

traces:

X-Trace-ID: uuid

X-Flow-ID: flow-id

The TracerAutoConfiguration will automatically pick up any TraceListener bound in the application context.

Getting Help with Tracer

If you have questions, concerns, bug reports, etc., please file an issue in this repository's Issue Tracker.

Getting Involved/Contributing

To contribute, simply make a pull request and add a brief description (1-2 sentences) of your addition or change. For more details, check the contribution guidelines.

Alternatives

Tracer, by design, does not provide sampling, metrics or annotations. Neither does it use the semantics of spans as most of the following projects do. If you require any of these, you're highly encouraged to try them.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/348415.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java 垃圾回收手动回收_Java垃圾回收(3)

java 垃圾回收手动回收接下来是我的前两篇垃圾收集博客文章: GC热点概述 。 并行垃圾收集器 。 并发标记扫描 Hotspot中的并行垃圾收集器旨在最大程度地减少应用程序进行垃圾收集所花费的时间,这称为吞吐量 。 对于所有应用程序而言,这并不…

java测试netty_《Netty官方文档》基准测试

原文链接 译者:lijunshuNetty有一个模块叫’netty-microbench’,我们可以用他来执行一系列的微型基准测试。Netty-microbench是基于OpenJDK JMH构件的(HotSpot的推荐基准测试方案)。当你开始netty基准测试时,你不需要额外的依赖。运行基准测…

Java命令行界面(第30部分):观察

这个有关Java命令行参数解析的系列文章由四个月来发表的29篇文章组成,涵盖了28个不同的开放源代码库,可用于解析Java命令行参数。 这篇文章收集了可以从本系列的前29篇文章中得出的一些观点,并提供了一些一般性的考虑,以便在选择2…

java导出excel 科学计数法_基于Java将Excel科学计数法解析成数字

需要注意的是一般的科学表达式是1.8E12 1.8E-12而在Excel中的科学表达式是1.8E12 1.8E-12我写的科学计数法的正则表达式是(-?\d\.?\d*)[Ee]{1}[\-]?[0-9]*导入EXCEL数据时将科学计数法解析成数字,Java代码:import java.text.DecimalFormat;import jav…

java描边_shape描边设置是否显示四周描边

android:width"1pt"/>android:topRightRadius"30pt"android:bottomRightRadius"30pt" />效果图如下:这里设置了左边描边不显示设置虚线:android:shape"line">android:dashGap"3pt"android:d…

java 垃圾回收手动回收_Java垃圾回收(2)

java 垃圾回收手动回收并行清理 今天,我们介绍了并行GC的工作原理。 具体来说,这是在Eden上运行并行Scavenge收集器,在Tenured一代上运行Parallel Mark and Sweep收集器的组合。 您可以通过传递-XX: UseParallelOldGC来获得此选项…

java正则表达式性能_译:Java 中的正则表达式性能概述

译者:Darren Luo1. 概述在本快速教程中,我们将展示模式匹配引擎是如何工作的。我们还将介绍在 Java 中优化正则表达式的不同方式。有关正则表达式的的使用介绍,请参阅此文。2. 模式匹配引擎java.util.regex 包使用了一种叫做 Nondeterministi…

带注释的控制器– Spring Web / Webflux和测试

Spring Webflux和Spring Web是两个完全不同的Web堆栈。 但是, Spring Webflux继续支持基于注释的编程模型 使用这两个堆栈定义的端点可能看起来相似,但是测试该端点的方式却大不相同,并且编写此端点的用户必须知道哪个堆栈处于活动状态并相应…

jquery解析java对象数组_Javascript / jQuery初学者:将对象推送到数组

Well you are changing the reference of same object通过示例了解它是如何工作的let a {};let b a;a.name xyz;a.name abc;console.log(a.name)console.log(b.name)所以在上面的示例中我们有两个变量a和b . a是一个对象 .每当我们更新名称时,最后一个值将被新的…

java查看jvm对象个数_jmap-查看 jvm 内存对象信息

jmap 概述命令jmap是一个多功能的命令。它可以生成 java 程序的 dump 文件,也可以查看堆内对象示例的统计信息、查看 ClassLoader 的信息以及 finalizer 队列。参数option:选项参数。pid:需要打印配置信息的进程ID。executable:产…

OpenHub框架进行的异步通信

在本系列的前一部分中,我们介绍了OpenHub框架 。 这部分显示了框架最强大的功能之一- 异步消息传递模型 。 当源系统无法等待目标系统的响应时,将使用系统之间的异步通信。 有以下几个原因: 源系统必须尽可能地响应 ,并且不受外…

Java实现C语言select函数_一道面试题目,分别用sql 和java,c++, c语言实现,

引用来自“雨翔河”的评论获取国家假日办的的信息,然后根据假日办提供的信息来搞定。日期的话,哈哈,找个提供日期查询的接口,借用一下来查日期,也搞定了。总之哪里有的抄就抄哪里的。再来一个笨蛋的意见,把…

java注解的反射_Java注解与反射

概要本文主要是总结Java注解与反射的相关知识,加深自己对Java类动态语言的理解,同时为日后学习Spring打下基础。注解:什么是注解Annotation的作用不是程序本身,但是可以对程序作出解释。可以被其他程序(比如:编译器等)…

工厂设计模式和策略设计模式_设计模式:策略

工厂设计模式和策略设计模式这次我想谈谈策略设计模式 。 通过这种方式,我开始撰写有关行为设计模式的文章。 这种模式表示对象之间的某些交互模式,以使代码更灵活且组织得更好。此方法的最本质点是对象之间的松散耦合。 当您的应用程序中有多个实现目的…

java 8 排序反转_Java 8 排序小结

1、概述首先,让我们先定义一个简单的实体类:Datapublic class Human {private String name;private int age;public Human() {super();}public Human(final String name, final int age) {super();this.name name;this.age age;}}2、不使用Lambda表达式…

如何将不带web.xml的Spring应用程序部署到Tomcat

介绍 由于Servlet 3规范不再需要web.xml来配置Web应用程序,因此已通过使用注释代替。 在本文中,我们将研究如何在不使用web.xml情况下将简单的基于Spring的应用程序部署到Tomcat 8.5。*。 创建一个空的应用程序 使用以下命令使用maven webapp原型创建一…

java创建单线程计时器_我们如何在Java中实现计时器线程?

该定时器类计划任务一次或多次给定的时间运行。它也可以作为后台程序线程在后台运行。要将Timer与守护程序线程相关联,有一个带有布尔值的构造函数。计时器以固定的延迟和固定的速率安排任务。在固定的延迟中,如果系统GC延迟了任何执行,则其他…

Python和Java结合的项目实战_[项目实战] Python高级教程项目实战篇 Python和Java结合的项目实战 视频教程 [...

资源介绍课程简介:xa0xa0Python高级教程项目实战篇 Python和Java结合的项目实战 视频教程 教学视频----------------------课程目录Python项目实战篇[初级项目:图片社交电商导购漂流瓶]项目实现功能: 用户注册,登录,登出图片的多种…

java04376_Java - jdbc mybatis

jdbc首先配置maven包org.springframework.bootspring-boot-starter-jdbcorg.mybatis.spring.bootmybatis-spring-boot-starter2.1.0注意pom.xml中配置结点src/main/java**/*.xmlfalsesrc/main/resourcesstatic/*.*templates/*.***/*.xml**/*.yml**/*.propertiesfalse针对不同的…

设计模式 建造者模式_设计模式:建造者

设计模式 建造者模式有时需要在应用程序中创建一个复杂的对象。 一种解决方案是Factory模式,另一种是Builder设计模式。 在某些情况下,您甚至可以结合使用这两种模式。 但是在本文中,我想研究一下Builder设计模式 。 我需要说的第一件事是创造…