通过Jaeger上报Java应用数据

示例:

  • 手动埋点
  • 通过Spring Cloud组件埋点
  • 通过gRPC组件埋点

https://help.aliyun.com/document_detail/90501.html

手动埋点Demo

  1. package com.alibaba.apm;
  2. import io.opentracing.Scope;
  3. import io.opentracing.Span;
  4. import io.opentracing.Tracer;
  5. import io.opentracing.util.GlobalTracer;
  6. import static java.lang.System.exit;
  7. public class OpenTracingDemo {
  8. static {
  9. io.jaegertracing.Configuration config = new io.jaegertracing.Configuration("manalDemo");
  10. io.jaegertracing.Configuration.SenderConfiguration sender = new io.jaegertracing.Configuration.SenderConfiguration();
  11. /**
  12. * 从https://tracing.console.aliyun.com/ -> 集群配置 -》接入点信息 获取jaeger的网关(Endpoint)
  13. * 运行前,请设置当前用户的对应的网关
  14. */
  15. sender.withEndpoint("http://tracing-analysis-dc-sh.aliyuncs.com/adapt_123@3456_222@444/api/traces");
  16. config.withSampler(new io.jaegertracing.Configuration.SamplerConfiguration().withType("const").withParam(1));
  17. config.withReporter(new io.jaegertracing.Configuration.ReporterConfiguration().withSender(sender).withMaxQueueSize(10000));
  18. GlobalTracer.registerIfAbsent(config.getTracer());
  19. }
  20. public static void main(String[] args) {
  21. System.out.println("begin tracing");
  22. OpenTracingDemo test = new OpenTracingDemo();
  23. test.firstBiz();
  24. try {
  25. Thread.sleep(10000);
  26. } catch (InterruptedException e) {
  27. // quite
  28. }
  29. exit(0);
  30. }
  31. private void firstBiz() {
  32. Tracer tracer = GlobalTracer.get();
  33. // 创建span
  34. Span span = tracer.buildSpan("parentSpan").withTag("myTag", "spanFrist").start();
  35. try (Scope ignored = tracer.activateSpan(span)) {
  36. tracer.activeSpan().setTag("methodName", "testTracing");
  37. // .... 业务逻辑
  38. secondBiz();
  39. } catch (Exception e) {
  40. TracingHelper.onError(e, span);
  41. throw e;
  42. } finally {
  43. span.finish();
  44. }
  45. }
  46. private void secondBiz() {
  47. Tracer tracer = GlobalTracer.get();
  48. Span childSpan = tracer.buildSpan("childSpan").withTag("myTag", "spanSecond").start();
  49. try (Scope ignored = tracer.activateSpan(childSpan)) {
  50. tracer.activeSpan().setTag("methodName", "testCall");
  51. //... 业务逻辑
  52. // mock exception
  53. //throw new RuntimeException("TestRuntimeException");
  54. } catch (Exception e) {
  55. TracingHelper.onError(e, childSpan);
  56. throw e;
  57. } finally {
  58. childSpan.finish();
  59. }
  60. }
  61. }

TracingHelper

  1. package com.alibaba.apm;
  2. import io.opentracing.Span;
  3. import io.opentracing.tag.Tags;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. public class TracingHelper {
  7. public static void onError(Throwable throwable, Span span) {
  8. Tags.ERROR.set(span, Boolean.TRUE);
  9. if (throwable != null) {
  10. span.log(errorLogs(throwable));
  11. }
  12. }
  13. private static Map<String, Object> errorLogs(Throwable throwable) {
  14. Map<String, Object> errorLogs = new HashMap<>(2);
  15. errorLogs.put("event", Tags.ERROR.getKey());
  16. errorLogs.put("error.object", throwable);
  17. errorLogs.put("error.kind", throwable.getClass().getName());
  18. return errorLogs;
  19. }
  20. }

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <parent>
  7. <groupId>com.alibaba.apm</groupId>
  8. <artifactId>jaegerDemo</artifactId>
  9. <version>1.0.0-SNAPSHOT</version>
  10. </parent>
  11. <groupId>com.alibaba.apm</groupId>
  12. <artifactId>jaegerManualDemo</artifactId>
  13. <version>1.0-SNAPSHOT</version>
  14. <packaging>jar</packaging>
  15. <properties>
  16. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  17. <log4j.version>2.11.1</log4j.version>
  18. <maven.compiler.source>1.7</maven.compiler.source>
  19. <maven.compiler.target>1.7</maven.compiler.target>
  20. </properties>
  21. <dependencies>
  22. <dependency>
  23. <groupId>io.jaegertracing</groupId>
  24. <artifactId>jaeger-client</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.apache.logging.log4j</groupId>
  28. <artifactId>log4j-core</artifactId>
  29. <version>${log4j.version}</version>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.apache.logging.log4j</groupId>
  33. <artifactId>log4j-jul</artifactId>
  34. <version>${log4j.version}</version>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.apache.logging.log4j</groupId>
  38. <artifactId>log4j-slf4j-impl</artifactId>
  39. <version>${log4j.version}</version>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.apache.logging.log4j</groupId>
  43. <artifactId>log4j-jcl</artifactId>
  44. <version>${log4j.version}</version>
  45. </dependency>
  46. </dependencies>
  47. <build>
  48. <plugins>
  49. <plugin>
  50. <artifactId>maven-compiler-plugin</artifactId>
  51. <version>3.8.0</version>
  52. </plugin>
  53. </plugins>
  54. </build>
  55. </project>