Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotations support for non-auto instrumented code #3035

Open
chandru-kumar opened this issue Mar 17, 2021 · 13 comments
Open

Annotations support for non-auto instrumented code #3035

chandru-kumar opened this issue Mar 17, 2021 · 13 comments
Labels
Feature Request Suggest an idea for this project help wanted

Comments

@chandru-kumar
Copy link

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 chandru-kumar added the Bug Something isn't working label Mar 17, 2021
@jkwatson
Copy link
Contributor

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 jkwatson added Feature Request Suggest an idea for this project help wanted and removed Bug Something isn't working labels Mar 17, 2021
@chandru-kumar
Copy link
Author

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

@jkwatson
Copy link
Contributor

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.

@ghost
Copy link

ghost commented Apr 23, 2021

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.

@piotr-sumo
Copy link
Contributor

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

@jkwatson
Copy link
Contributor

@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. :)

@piotr-sumo
Copy link
Contributor

@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?

@jkwatson
Copy link
Contributor

@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?

@piotr-sumo
Copy link
Contributor

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.

@chandru-kumar
Copy link
Author

@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
Copy link
Author

chandru-kumar commented Jun 17, 2021

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??

@jkwatson
Copy link
Contributor

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?

@chandru-kumar
Copy link
Author

chandru-kumar commented Jul 6, 2021

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..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request Suggest an idea for this project help wanted
Projects
None yet
Development

No branches or pull requests

3 participants