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测试netty_《Netty官方文档》基准测试

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

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来获得此选项…

OpenHub框架进行的异步通信

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

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

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

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

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

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

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

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

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

在MongoDB和Spring Batch中将XML转换为JSON和原始使用

总览 为什么将XML转换为JSON以在MongoDB中原始使用? 由于MongoDB使用JSON文档存储记录,就像表和行将记录存储在关系数据库中一样,我们自然需要将XML转换为JSON。 某些应用程序可能需要存储原始(未修改的)JSON&#xf…

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

java 垃圾回收手动回收这是有关垃圾收集(GC)的系列文章中的第一篇。 我希望能够涵盖整个系列过程中的理论知识以及热点虚拟机中的所有主要收集器。 这篇文章仅说明什么是垃圾回收以及不同回收器共有的元素。 我为什么要在乎? 您的Java虚拟机…

使用Apache Isis快速进行SEMAT应用程序开发

TL; DR这篇文章谈论我使用Apache Isis创建并部署到此处的OpenShift Online的SEMAT宠物项目: http: //semat.ofbizian.com Apache Isis 作为主要在后端系统上工作的Java开发人员,我讨厌创建用户界面和处理Java脚本。 幸运的是,有J…

java 对象锁定_少锁定Java对象池

java 对象锁定自从我写任何东西以来已经有一段时间了,我一直在忙于我的新工作,其中涉及在性能调优方面做一些有趣的工作。 挑战之一是减少应用程序关键部分的对象创建。 尽管Java随着时间的推移已改进了GC算法,但垃圾回收打ic一直是Java的主…

Oracle JDBC中的PreparedStatement占位符过多

使用Oracle数据库时,导致ORA-01745(“无效的主机/绑定变量名称错误”)错误的原因有多种。 关于错误ORA-01500到ORA-02098的Oracle 9i文档提供了有关ORA-01745的更多详细信息。 它指出,“原因”是“绑定变量或INTO规范中的冒号后跟…

webview加载php文件,HYWebview下载自定义文件教程

车机版 HYWebview升级到1.3了多增加了一个进度条多增加了自定义下载URL功能使用教程。浏览器打开DNS地址:103.44.248.95可见 底部有一个 下载链接 和 提货密码 可以输入!比如 我们去应用宝官网复制下载链接出来:https://download.sj.qq.com/u…

设计模式 原型模式_设计模式:原型

设计模式 原型模式创新设计模式之一是原型设计模式 。 尽管原型是创造模式,但它在概念上与其他模式有所区别。 我的意思是原型在某种意义上创造了自己。 我将在下面解释。 原型模式的所有魔力都基于Java Object的clone()方法。 因此&#x…

Packt和Java Code Geeks提供的$ 5 Java编程书籍!

您好极客! 今天,我们为您带来一些激动人心的消息! Java Code Geeks和Packt联手为您提供广泛的书籍库每周折扣。 对于开发人员来说,Java仍然是最强大的选择之一,它是定义企业和移动设备的语言。 本周,我们…

源码时代php中级项目,0526PHP班中级项目评比圆满落幕

为了充分发掘同学们开发项目的成功经验,全面提升学员的综合素质,锻炼学员的解说与问题处理能力,源代码教育(源码时代)重庆校区进行了PHP就业班的中级项目评比。项目评比分为演讲、质询、点评及投票评分几个环节,每个环节都精彩纷呈…

qt linux 添加库文件路径,linux下qt使用第三方库的那些事

开发库查看工具:$sudo apt-get install pkg-config很多时候我们并不知道自己电脑有没有这个库,所以我们可以使用这个工具来查看自己有哪些工具,或者哪些工具没有。同时,qmake是对这个工具配置支持的,所以我们很多时候很…

xp系统上安装linux系统教程,XP系统如何安装fedora linux双系统?WinXP安装fedora linux双系统的方法...

有位朋友因为想在linux中熟悉下hadoop的配置开发环境,所以就开始于WinXP系统中安装fedora linux双系统,可是操作了很久都没成功。这该如何怎么办呢?接下来,小编就给大家介绍WinXP安装fedora linux双系统的具体方法。1.下载Fedora-…

pae扩展内存 linux,Linux内核-内存管理-PAE(物理地址扩展)

Intel 通过在处理器上把管脚数从 32 增加到 36,以提高处理器的寻址能力,使其达到 2^3664GB,然而线性地址的位数仍然是 32 位,为此,需引入一种新的分页机制。从pentium pro 处理器开始,intel引入一种叫做 PA…