概念

Instrumentation 表示对一种特定组件交互的跟踪封装,比如 MQ,HTTP,方法调用等

接口定义

1
2
3
4
5
6
public class Instrumenter<REQUEST, RESPONSE> {

boolean shouldStart(Context parentContext, REQUEST request);
Context start(Context parentContext, REQUEST request);
void end(
Context context, REQUEST request, @Nullable RESPONSE response, @Nullable Throwable error);

其定义了 start 和 end 方法来代替手动创建 Span 的过程

  • request 表示输入端的参数
  • response 表示输出结果,error 表示输出结果是否有异常

构造 Instrumenter

Instrumenter 的要素比较多,其构建时使用 InstrumenterBuilder 进行构造

1
2
3
4
5
6
public class Instrumenter<REQUEST, RESPONSE> {
public static <REQUEST, RESPONSE> InstrumenterBuilder<REQUEST, RESPONSE> builder(
OpenTelemetry openTelemetry,
String instrumentationName,
SpanNameExtractor<? super REQUEST> spanNameExtractor);
}

调用示例

1
2
3
4
5
6
7
8
Instrumenter instrumenter =
Instrumenter.builder(openTelemetry, INSTRUMENTATION_NAME, WithSpanAspect::spanName)
.addAttributesExtractor(
MethodSpanAttributesExtractor.newInstance(
JoinPointRequest::method,
parameterAttributeNamesExtractor,
JoinPointRequest::args))
.newInstrumenter(WithSpanAspect::spanKind);

Extractor

Instrumentation 接口中存在着非常多的 Extractor,其对对象的某一些属性进行抽取,可以理解为内容过滤器,如下定义

  • SpanKeyExtractor
  • SpanKindExtractor
  • SpanLinksExtractor
  • SpanNameExtractor
  • SpanStatusExtractor
  • AttributesExtractor
  • TimeExtractor

AttributesExtractor

  • MethodSpanAttributesExtractor
  • MethodExtractor
  • ParameterAttributeNamesExtractor
  • MethodArgumentsExtractor

AttributeKey

是带有类型的键

接口定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public interface AttributeKey<T> {

String getKey();

AttributeType getType();
}
public enum AttributeType {
STRING,
BOOLEAN,
LONG,
DOUBLE,
STRING_ARRAY,
BOOLEAN_ARRAY,
LONG_ARRAY,
DOUBLE_ARRAY
}
阅读全文 »

背景

当 2 个进程之间进行交互时,span 传递的上下文就会中断,需要有一种机制,在 RPC 期间,保持链路跟踪

思路

在 RPC 调用时,将必要的 Span 上下文数据作为附带信息传递给对方进程,在对方接收时识别附带的信息,并进行解码恢复 Span 上下文环境

阅读全文 »

Direct

当生产者发送消息交换时,Direct 组件提供对任何消费者的直接、同步调用。
此端点可用于连接同一Camel 上下文中的现有路由。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Test
public void testDirect() throws Exception {

CamelContext context=new DefaultCamelContext();

context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:home")
.process(exchange -> {
System.out.println("process:"+exchange.getIn().getBody());
});
}
});
context.start();
context.createProducerTemplate().sendBody("direct:home","hello");
Thread.sleep(2000);
}

ProducerTemplate

ProducerTemplate 接口允许您以各种不同的方式将消息交换发送到端点,从而可以轻松地从 Java 代码使用 Camel Endpoint 实例。

如果您只想向同一个端点发送大量消息,可以使用默认端点配置它;或者您可以指定 Endpoint 或 uri 作为第一个参数。

该 sendBody()方法允许您轻松地将任何对象发送到端点,如下所示:

发送消息

1
2
3
4
5
6
7
8
9
10
11
12
ProducerTemplate template = exchange.getContext().createProducerTemplate();

// send to default endpoint
template.sendBody("<hello>world!</hello>");

// send to a specific queue
template.sendBody("activemq:MyQueue", "<hello>world!</hello>");

// send with a body and header
template.sendBodyAndHeader("activemq:MyQueue",
"<hello>world!</hello>",
"CustomerRating", "Gold");

请求消息

ProducerTemplate 支持消息交换模式被用来控制消息风格来使用(MEP):

  • 发送方法-事件消息(InOnly)
  • 请求方法-请求回复(InOut)
1
2
3
4
5
6
7
Object response = template.requestBody("<hello/>");

// you can type convert the response to what you want such as String
String ret = template.requestBody("<hello/>", String.class);

// or specify the endpoint uri in the method
String ret = template.requestBody("cxf:bean:HelloWorldService", "<hello/>", String.class);
0%