|
1 | 1 | package kamon.armeria.instrumentation;
|
2 | 2 |
|
3 | 3 | import com.linecorp.armeria.common.HttpRequest;
|
| 4 | +import com.linecorp.armeria.server.RequestConverter; |
4 | 5 | import com.linecorp.armeria.server.ServiceRequestContext;
|
5 | 6 | import io.netty.util.AttributeKey;
|
6 | 7 | import kamon.Kamon;
|
7 | 8 | import kamon.context.Storage;
|
| 9 | +import kamon.instrumentation.http.HttpMessage; |
| 10 | +import kamon.instrumentation.http.HttpServerInstrumentation; |
8 | 11 | import kamon.trace.Span;
|
9 | 12 | import kamon.trace.SpanPropagation.B3;
|
10 | 13 | import kanela.agent.libs.net.bytebuddy.asm.Advice;
|
| 14 | +import scala.Int; |
| 15 | +import scala.Option; |
11 | 16 |
|
12 |
| -// why is this good? |
13 |
| -// i mean, the serve method could still spawn some shit off |
14 |
| -// and go to another thread? |
15 |
| -// ok, so this is about context propagation, right? |
16 |
| -// when I store the context, how do other threads know about that? |
17 |
| -// how does it FORWARD THAT INFORMATION to other threads? |
18 |
| -// the information that yeah, there's a current trace in progress |
19 |
| -// and you're a part of it |
| 17 | +import java.util.Map; |
20 | 18 |
|
21 | 19 | public class ArmeriaHttpServiceInstrumentation {
|
22 | 20 | @Advice.OnMethodEnter()
|
23 |
| - private static void enter( |
24 |
| - @Advice.Local("scope") Storage.Scope scope, |
25 |
| - @Advice.Local("span") Span span, |
26 |
| - @Advice.This Object service, |
| 21 | + private static Storage.Scope enter( |
27 | 22 | @Advice.Argument(0) ServiceRequestContext context,
|
28 | 23 | @Advice.Argument(1) HttpRequest request) {
|
29 |
| - // what can I do with the context? |
30 |
| - // What can i extract out of it? |
31 | 24 |
|
| 25 | + // make sure that this _is_ truly available here |
| 26 | + long startTime = context.log().partial().requestStartTimeNanos(); |
32 | 27 |
|
33 |
| - // I add a header here with the traceID right? |
34 |
| - // I mean not here, but at the beginning of the pipeline |
35 |
| - // is toString needed here? |
36 |
| - // the the elastic-armeria shit without is too |
37 |
| -// context.log().context().setAttr(AttributeKey.newInstance("Kamon.Context"), Kamon.currentContext()); |
38 |
| -// System.out.println("Context:"); |
39 |
| -// System.out.println("Context:"); |
40 |
| -// System.out.println(context); |
41 |
| -// System.out.println("Request"); |
42 |
| -// System.out.println("Request"); |
43 |
| -// System.out.println(request); |
44 |
| -// System.out.println("Service??"); |
45 |
| -// System.out.println("Service??"); |
46 |
| -// System.out.println(service); |
47 |
| -// System.out.println(service.getClass().getEnclosingClass().getSimpleName()); |
48 |
| -// System.out.println("Context again"); |
49 |
| -// System.out.println("Context again"); |
50 |
| -// System.out.println(context.log().context()); |
| 28 | + // figure out how to get the host and the port |
| 29 | + // also, this handler will need to end this span at the end of the pipeline |
| 30 | + // meaning, we need to propagate it there somehow |
| 31 | + HttpServerInstrumentation httpServerInstrumentation = HttpServerInstrumentation.from(Kamon.config().getConfig("kamon.instrumentation.armeria.http-server"), |
| 32 | + "armeria.http.server", "0.0.0.0", 1234); |
| 33 | + HttpServerInstrumentation.RequestHandler handler = httpServerInstrumentation |
| 34 | + .createHandler(RequestConverter.toRequest(request, "whatever", 1234), true); |
| 35 | +// context.log().whenComplete().thenAccept(requestLog -> { |
| 36 | +// // shit |
| 37 | +// handler.responseSent(); |
| 38 | +// // this takes a time |
| 39 | +// // Kamon.Clock to convert it |
| 40 | +// // but which time to use? |
| 41 | +//// handler.span().finish(); |
| 42 | +// }); |
| 43 | + // add a decorator |
| 44 | + // which has acces to this same context |
| 45 | + // context.attr, modify response |
| 46 | +// context.respo |
| 47 | + context.setAttr(AttributeKey.newInstance("myHandler"), handler); |
51 | 48 |
|
52 |
| - // this is wrong |
53 |
| - // i should be starting a span here |
54 |
| - // what tags do I add to it? |
55 |
| - span = Kamon.serverSpanBuilder("some.smart.operation.name", "armeria???").start(); |
56 |
| - // parentSpan? |
57 |
| - // wrong context to store? |
58 |
| - scope = Kamon.storeContext(Kamon.currentContext()); |
| 49 | + // figure out a nice name |
| 50 | + handler.span().name("CoolName").takeSamplingDecision(); |
| 51 | + return Kamon.storeContext(handler.context()); |
59 | 52 | }
|
60 | 53 |
|
61 | 54 | @Advice.OnMethodExit()
|
62 |
| - private static void exit(@Advice.Local("scope") Storage.Scope scope, |
63 |
| - @Advice.Local("span") Span span, |
64 |
| - @Advice.This Object service, |
65 |
| - @Advice.Argument(0) ServiceRequestContext context, |
66 |
| - @Advice.Argument(1) HttpRequest request) { |
67 |
| - // how do i tell here if the thing finished correctly |
68 |
| - // or if I should fail the span? |
69 |
| - System.out.println("Context:"); |
70 |
| - System.out.println("Context:"); |
71 |
| - System.out.println(context); |
72 |
| - System.out.println("Request"); |
73 |
| - System.out.println("Request"); |
74 |
| - System.out.println(request); |
75 |
| - System.out.println("Service??"); |
76 |
| - System.out.println("Service??"); |
77 |
| - System.out.println(service); |
78 |
| - System.out.println(service.getClass().getEnclosingClass().getSimpleName()); |
79 |
| - span.finish(); |
| 55 | + // Advice.Enter will bind the return from the enter to the exit :O |
| 56 | + // :O :O :O :O :O |
| 57 | + private static void exit(@Advice.Enter Storage.Scope scope) { |
80 | 58 | scope.close();
|
81 | 59 | }
|
82 | 60 | }
|
| 61 | + |
| 62 | + |
0 commit comments