Use Java annotations to define custom services

While many frameworks use interfaces in their designs, an increasingly high number of frameworks now use Java annotations to designate components. To accommodate this, Dynatrace now enables you to define custom services via Java annotations.

When defining a custom service based on Java annotations, you have the option of defining custom services for various scenarios (for example, Spring controllers).

Define a custom Java service

Rather than focus on annotations at this point, let’s first consider an example custom service that is based on a particular incarnation of a component. Think of this as a template.

This example is based on the EasyTravel sample service called Booking. First, we select the methods we want to monitor (these are the methods that will exist across all components). If possible you can use the base class or an interface for such an exercise. As long as the same method signature exists across all classes, this works just fine.

We’ve added a new section to the entry point details available on the Define custom Java service settings page (see image below).

  • Monitor all subclasses of the specified class shows the class that defines the class search scope.
  • Annotation filter shows which annotations are used to match the specific implementations of class that you are interested in.

custom Java service based on annotations

Specify where to look for annotations

Click the Edit scope button (see image above) to view the processes that are available in the process group. Select the process that contains the entry point you want and then click Continue to view all the annotations that exist on that class.

custom Java service based on annotations - selecting a process

Select one or more of the annotations provided by your custom service classes.

To widen the class match, choose an interface or superclass as the basis for the match. Click the Load inheritance button to load the inheritance of superclasses and expand the list of annotations.

custom Java service based on annotations - selecting annotations

Based on the above, the resulting entry point will appear as follows. The custom service will monitor the getId method in all implementations of com.dynatrace.easytravel.jpa.Base that have the javax.persistence.Entity annotation.

custom Java service based on annotations - reviewing the configuration

The resulting request list for the new custom service looks like this:

custom Java service based on annotations - the resulting request list

This means that instead of just looking for one specific implementation (the Booking class) or all implementations of the base class you can now tell Dynatrace too monitor all implementations of an interface or base class that feature the defined annotations. This makes it possible to use Spring Controllers and other concepts to define custom services.

Indirect matching in all super classes

Importantly, this feature looks for the specified annotations in all superclasses and interfaces. This makes indirect matching possible, where the annotation is on a base class not directly related to the interface match.

For example, let’s look at the following class hierarchy:

public interface Component
   boolean executeService();
public abstract class BaseServiceA
public class MyServiceA extends BaseServiceA implements Component

While you can easily define a custom service based on the Component interface method executeService, you may want only those implementations that have the MyComponent annotation. To do this, begin by defining the custom service for MyServiceA (this will act as a template) and then edit the scope to use the Component interface and the MyComponent annotation.

custom Java service based on annotations - indirect matching

Annotation matches by package

This feature is available starting with Dynatrace cluster version 144.

A number of frameworks now rely solely on annotations. When this is the case, you may not always be able to define an interface or base class. To handle such scenarios, we’re adding the ability to monitor all classes that match the annotation filter in a certain package.

Starting with Dynatrace cluster version 144, the Edit scope page offers an additional option, precisely for this purpose: Use all classes and interfaces with selected annotations and names starting with…  This option is grayed out until you select an annotation.

custom Java service based on annotations - by package

Once you select an annotation, you can select the Use all classes and interfaces… option and specify a package match instead of an interface. Dynatrace will then look for any class that starts with the given string which has the selected annotations within itself or any of its superclasses and interfaces.

custom Java service based on annotations - by package - selecting annotations

The result would look like this:

custom Java service based on annotations - by package - reviewing the results

This option opens up some interesting possibilities:

  • You can instrument your own Enterprise Java Beans (EJBs) using EJB annotation and your package.
  • You can instrument your own Spring controllers using Spring controller annotation and your package.

Remember, this feature requires Dynatrace SaaS or Dynatrace Managed cluster version 144 and Dynatrace OneAgent v1.133 or higher.

Those who know me know that I'm passionate about 3 things: rock climbing, physics, and performance. I've worked in performance monitoring and optimizations in enterprise environments for the better part of the last 10 years. Now as a Product Manager I am doing my best to build those experiences into Dynatrace.

Looking for answers?

Start a new discussion or ask for help in our Q&A forum.

Go to forum