|
| 1 | +package annotatedsample.latest; |
| 2 | + |
| 3 | +import static datadog.trace.api.DDTags.SERVICE_NAME; |
| 4 | +import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan; |
| 5 | +import static io.opentelemetry.api.trace.SpanKind.CLIENT; |
| 6 | +import static io.opentelemetry.api.trace.SpanKind.CONSUMER; |
| 7 | +import static io.opentelemetry.api.trace.SpanKind.INTERNAL; |
| 8 | +import static io.opentelemetry.api.trace.SpanKind.PRODUCER; |
| 9 | +import static io.opentelemetry.api.trace.SpanKind.SERVER; |
| 10 | +import static java.util.concurrent.TimeUnit.SECONDS; |
| 11 | + |
| 12 | +import io.opentelemetry.instrumentation.annotations.SpanAttribute; |
| 13 | +import io.opentelemetry.instrumentation.annotations.WithSpan; |
| 14 | +import java.util.List; |
| 15 | +import java.util.concurrent.Callable; |
| 16 | +import java.util.concurrent.CompletableFuture; |
| 17 | +import java.util.concurrent.CompletionStage; |
| 18 | +import java.util.concurrent.CountDownLatch; |
| 19 | + |
| 20 | +public class TracedMethods { |
| 21 | + @WithSpan |
| 22 | + public static String sayHello() { |
| 23 | + activeSpan().setTag(SERVICE_NAME, "custom-service-name"); |
| 24 | + return "hello!"; |
| 25 | + } |
| 26 | + |
| 27 | + @WithSpan(value = "custom-operation-name") |
| 28 | + public static String sayHelloWithCustomOperationName() { |
| 29 | + activeSpan().setTag(SERVICE_NAME, "custom-service-name"); |
| 30 | + return "hello!"; |
| 31 | + } |
| 32 | + |
| 33 | + @WithSpan(kind = SERVER) |
| 34 | + public static String sayHelloWithServerKind() { |
| 35 | + return "hello!"; |
| 36 | + } |
| 37 | + |
| 38 | + @WithSpan(kind = CLIENT) |
| 39 | + public static String sayHelloWithClientKind() { |
| 40 | + return "hello!"; |
| 41 | + } |
| 42 | + |
| 43 | + @WithSpan(kind = PRODUCER) |
| 44 | + public static String sayHelloWithProducerKind() { |
| 45 | + return "hello!"; |
| 46 | + } |
| 47 | + |
| 48 | + @WithSpan(kind = CONSUMER) |
| 49 | + public static String sayHelloWithConsumerKind() { |
| 50 | + return "hello!"; |
| 51 | + } |
| 52 | + |
| 53 | + @WithSpan(kind = INTERNAL) |
| 54 | + public static String sayHelloWithInternalKind() { |
| 55 | + return "hello!"; |
| 56 | + } |
| 57 | + |
| 58 | + @WithSpan(inheritContext = false) |
| 59 | + public static String sayHelloWithInANewTrace() { |
| 60 | + return "hello!"; |
| 61 | + } |
| 62 | + |
| 63 | + @WithSpan |
| 64 | + public static String sayHelloWithStringAttribute(@SpanAttribute("custom-tag") String param) { |
| 65 | + return "hello!"; |
| 66 | + } |
| 67 | + |
| 68 | + @WithSpan |
| 69 | + public static String sayHelloWithIntAttribute(@SpanAttribute("custom-tag") int param) { |
| 70 | + return "hello!"; |
| 71 | + } |
| 72 | + |
| 73 | + @WithSpan |
| 74 | + public static String sayHelloWithLongAttribute(@SpanAttribute("custom-tag") long param) { |
| 75 | + return "hello!"; |
| 76 | + } |
| 77 | + |
| 78 | + @WithSpan |
| 79 | + public static String sayHelloWithListAttribute(@SpanAttribute("custom-tag") List<?> param) { |
| 80 | + return "hello!"; |
| 81 | + } |
| 82 | + |
| 83 | + @WithSpan |
| 84 | + public static void throwException() { |
| 85 | + throw new RuntimeException("Some exception"); |
| 86 | + } |
| 87 | + |
| 88 | + public static String traceAnonymousInnerClass() { |
| 89 | + return new Callable<String>() { |
| 90 | + @WithSpan |
| 91 | + @Override |
| 92 | + public String call() { |
| 93 | + return "hello!"; |
| 94 | + } |
| 95 | + }.call(); |
| 96 | + } |
| 97 | + |
| 98 | + @WithSpan |
| 99 | + public static CompletableFuture<String> traceAsyncCompletableFuture(CountDownLatch latch) { |
| 100 | + return CompletableFuture.supplyAsync( |
| 101 | + () -> { |
| 102 | + await(latch); |
| 103 | + return "hello!"; |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + @WithSpan |
| 108 | + public static CompletableFuture<String> traceAsyncFailingCompletableFuture( |
| 109 | + CountDownLatch latch, RuntimeException exception) { |
| 110 | + return CompletableFuture.supplyAsync( |
| 111 | + () -> { |
| 112 | + await(latch); |
| 113 | + throw exception; |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + @WithSpan |
| 118 | + public static CompletionStage<String> traceAsyncCompletionStage(CountDownLatch latch) { |
| 119 | + return CompletableFuture.supplyAsync( |
| 120 | + () -> { |
| 121 | + await(latch); |
| 122 | + return "hello!"; |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + @WithSpan |
| 127 | + public static CompletionStage<String> traceAsyncFailingCompletionStage( |
| 128 | + CountDownLatch latch, RuntimeException exception) { |
| 129 | + return CompletableFuture.supplyAsync( |
| 130 | + () -> { |
| 131 | + await(latch); |
| 132 | + throw exception; |
| 133 | + }); |
| 134 | + } |
| 135 | + |
| 136 | + @WithSpan |
| 137 | + public static String sayHelloMeasured() { |
| 138 | + return "hello!"; |
| 139 | + } |
| 140 | + |
| 141 | + private static void await(CountDownLatch latch) { |
| 142 | + try { |
| 143 | + if (!latch.await(5, SECONDS)) { |
| 144 | + throw new IllegalStateException("Latch still locked"); |
| 145 | + } |
| 146 | + } catch (InterruptedException e) { |
| 147 | + throw new RuntimeException(e); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments