opentelemetry-java icon indicating copy to clipboard operation
opentelemetry-java copied to clipboard

Annotations support for non-auto instrumented code

Open chandru-kumar opened this issue 3 years ago • 13 comments

Just a question - Do we have any example to create span for a method with Annotations.? I see @WithSpan in extension for auto instrumenting..But i dont see any annotations for adding spans in code.

Will we support for non-autoinstrumented solutions? If not, do we have any references to create custom annotations to create spans for methods using annotations.

chandru-kumar avatar Mar 17 '21 10:03 chandru-kumar

We've discussed writing an annotation processor that would enable this, but no one has had time to tackle that yet. If you're interested, I'd love to see a contribution of this!

jkwatson avatar Mar 17 '21 16:03 jkwatson

Can you pls let me know, Is there any references to implement the this?

chandru-kumar avatar Apr 13 '21 08:04 chandru-kumar

Can you pls let me know, Is there any references to implement the this?

There are lots of examples of annotation processors out in the wild, but nothing specifically like this that I know of. Do you have any ideas of what approach you might take? I can see several options:

  1. A lombok-like approach of injecting bytecode at compile time.
  2. An autovalue-like approach of generating .java files at build time to be used directly in the code
  3. An aspectj-like approach that would do something at build time (I think?)
  4. An aspectj-like approach that would do something at runtime (Spring has something like this, I think)
  5. A runtime-dynamic-proxy creation approach (Spring might have something akin to this?)

@jsuereth do you have any thoughts on how you'd approach this? You've mentioned that you have some experience with annotation processing.

jkwatson avatar Apr 13 '21 16:04 jkwatson

I dug a bit into this and I believe that @jkwatson listed all of the possibilities. Basically what we could do:

  1. Add the code during compile time
  • cleanest approach (the otel code is embedded in the built package, period)
  • hard to get right -- standard AnnotationProcessor allows to add not to modify the code (would need to go lombok hack way) -- utilise AspectJ compile-time weaving bridge of some sort)
  • increases friction - customers need to modify their build to include a specific plugin (or AnnotationProcessor, less build-tool dependent way)
  1. Add the code during load time
  • easy to get right (bytecode manipulation - we know how to do this ;) )
  • requires agent to be added
  • could be built upon existing otel instrumentation agent code (in a much less complex way of course)
  • alternative is load-time weaving (also requires AspectJ bridge of some sort)
  1. Run the (custom, otel) code during runtime
  • as far as I can tell possible only when there is a container running the app that allows for proxying (like Spring), so is technology dependent

Looking at the alternatives in my opinion 2 - small agent implementing bytecode modifications for annotated methods looks as the best way. Introduces least friction, we have knowledge how to do it right and can reuse already implemented code. Let me know what you think.

ghost avatar Apr 23 '21 11:04 ghost

@jkwatson @kubawach @chandru-kumar what is the status of this issue? I'd like to contribute if possible.

piotr-sumo avatar May 18 '21 14:05 piotr-sumo

@jkwatson @kubawach @chandru-kumar what is the status of this issue? I'd like to contribute if possible.

Would still love someone to try something out and show it off, so if you're interested, please do so. :)

jkwatson avatar May 18 '21 14:05 jkwatson

@jkwatson @kubawach I've started work on this and I think I need a tool to find methods (and classes) annotated with WithSpan annotation. I've tried https://github.com/rmuller/infomas-asl but it does not work. https://code.google.com/archive/p/reflections/ works but it needs package name to be provided. Reflections throw class not found exception when scanning the whole project at runtime.

Do you recommend a library for scanning classes in order to find methods annotated with WithSpan?

piotr-sumo avatar May 27 '21 14:05 piotr-sumo

@jkwatson @kubawach I've started work on this and I think I need a tool to find methods (and classes) annotated with WithSpan annotation. I've tried https://github.com/rmuller/infomas-asl but it does not work. https://code.google.com/archive/p/reflections/ works but it needs package name to be provided. Reflections throw class not found exception when scanning the whole project at runtime.

Do you recommend a library for scanning classes in order to find methods annotated with WithSpan?

I don't have any recommendations here. How does spring do it, I wonder?

jkwatson avatar May 27 '21 15:05 jkwatson

Thanks for the hint with Spring - I'll check their code. Anyway, I can always use java.lang.instrument.Instrumentation#getAllLoadedClasses and search in classes returned by this method.

piotr-sumo avatar May 27 '21 15:05 piotr-sumo

@jkwatson @kubawach @piotr-sumo I have created Method level Annotations(Runtime) to create and export traces. Used AspectJ features to make it. Here is the code demo link - https://github.com/chandru-kumar/opentelemetry-annotation-example. Please have a look and share your thoughts.

Note : If anyone uses this Annotation, they need to include the aspectj maven plugin for build..

chandru-kumar avatar Jun 10 '21 10:06 chandru-kumar

Hi @jkwatson ..Did you get a chance to look into this code? https://github.com/chandru-kumar/opentelemetry-annotation-example. Can you provide some inputs if you have any??

chandru-kumar avatar Jun 17 '21 09:06 chandru-kumar

Hi @jkwatson ..Did you get a chance to look into this code? https://github.com/chandru-kumar/opentelemetry-annotation-example..Can you provide some inputs if you have any??

I took a quick peek. Curious why you used your own annotations, rather than the one in this project, but other than that it seemed like a fine approach. Is there a gradle equivalent to doing the aspectj work?

jkwatson avatar Jun 17 '21 17:06 jkwatson

Hi @jkwatson ..Did you get a chance to look into this code? https://github.com/chandru-kumar/opentelemetry-annotation-example..Can you provide some inputs if you have any??

I took a quick peek. Curious why you used your own annotations, rather than the one in this project, but other than that it seemed like a fine approach. Is there a gradle equivalent to doing the aspectj work?

I just see one Annotation WithSpan in the existing OpenTelemetry code. So created my own annotations to InjectSpanToContext create the span and adds to the existing context and makes it as current span. InjectChildSpanToContext - Adds span as child span and doesn't make it as current span. (Thinking to extend for passing Attributes, Events and Baggages, if find time)

Looking for options to create gradle equivalent example..I'll update here once i create it..

chandru-kumar avatar Jul 06 '21 11:07 chandru-kumar