通过Jaeger上报Java应用数据
示例:
- 手动埋点
- 通过Spring Cloud组件埋点
- 通过gRPC组件埋点
https://help.aliyun.com/document_detail/90501.html
手动埋点Demo
package com.alibaba.apm;
import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.util.GlobalTracer;
import static java.lang.System.exit;
public class OpenTracingDemo {
static {
io.jaegertracing.Configuration config = new io.jaegertracing.Configuration("manalDemo");
io.jaegertracing.Configuration.SenderConfiguration sender = new io.jaegertracing.Configuration.SenderConfiguration();
/**
* 从https://tracing.console.aliyun.com/ -> 集群配置 -》接入点信息 获取jaeger的网关(Endpoint)
* 运行前,请设置当前用户的对应的网关
*/
sender.withEndpoint("http://tracing-analysis-dc-sh.aliyuncs.com/adapt_123@3456_222@444/api/traces");
config.withSampler(new io.jaegertracing.Configuration.SamplerConfiguration().withType("const").withParam(1));
config.withReporter(new io.jaegertracing.Configuration.ReporterConfiguration().withSender(sender).withMaxQueueSize(10000));
GlobalTracer.registerIfAbsent(config.getTracer());
}
public static void main(String[] args) {
System.out.println("begin tracing");
OpenTracingDemo test = new OpenTracingDemo();
test.firstBiz();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// quite
}
exit(0);
}
private void firstBiz() {
Tracer tracer = GlobalTracer.get();
// 创建span
Span span = tracer.buildSpan("parentSpan").withTag("myTag", "spanFrist").start();
try (Scope ignored = tracer.activateSpan(span)) {
tracer.activeSpan().setTag("methodName", "testTracing");
// .... 业务逻辑
secondBiz();
} catch (Exception e) {
TracingHelper.onError(e, span);
throw e;
} finally {
span.finish();
}
}
private void secondBiz() {
Tracer tracer = GlobalTracer.get();
Span childSpan = tracer.buildSpan("childSpan").withTag("myTag", "spanSecond").start();
try (Scope ignored = tracer.activateSpan(childSpan)) {
tracer.activeSpan().setTag("methodName", "testCall");
//... 业务逻辑
// mock exception
//throw new RuntimeException("TestRuntimeException");
} catch (Exception e) {
TracingHelper.onError(e, childSpan);
throw e;
} finally {
childSpan.finish();
}
}
}
TracingHelper
package com.alibaba.apm;
import io.opentracing.Span;
import io.opentracing.tag.Tags;
import java.util.HashMap;
import java.util.Map;
public class TracingHelper {
public static void onError(Throwable throwable, Span span) {
Tags.ERROR.set(span, Boolean.TRUE);
if (throwable != null) {
span.log(errorLogs(throwable));
}
}
private static Map<String, Object> errorLogs(Throwable throwable) {
Map<String, Object> errorLogs = new HashMap<>(2);
errorLogs.put("event", Tags.ERROR.getKey());
errorLogs.put("error.object", throwable);
errorLogs.put("error.kind", throwable.getClass().getName());
return errorLogs;
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.alibaba.apm</groupId>
<artifactId>jaegerDemo</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>com.alibaba.apm</groupId>
<artifactId>jaegerManualDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<log4j.version>2.11.1</log4j.version>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>io.jaegertracing</groupId>
<artifactId>jaeger-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jul</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
</plugins>
</build>
</project>