on 13 Dec 2024 03:47 PM - edited on 20 Dec 2024 10:25 AM by Michal_Gebacki
Distributed traces generally begin at entry points where user interactions or external requests enter your application. These entry points are often incoming HTTP requests or messages, as they signify user actions or external service calls that initiate a transaction. Database calls don't initiate traces because they're part of the broader business logic. In the trace view in UI, these calls are displayed within the context of the entire transaction flow, enabling you to identify which part of the application executed the call. Additionally, the execution of business logic does not start a trace unless a custom service is specified for a method call within the class or an entry point is defined using OneAgent SDK or OpenTelemetry.
Spring Scheduled Tasks allow you to automate the execution of tasks at specific intervals or times using the Spring Framework. This is achieved primarily through the @Scheduled
annotation, which can be applied to methods that you want to run periodically. Let's look at the following example, which executes the mentioned actions:
@Component
public class FetchUsersTask {
private final GetUserOverHttpService getUserOverHttpService;
private final UserService userService;
private int counter = 0;
public FetchUsersTask(GetUserOverHttpService getUserOverHttpService, UserService userService) {
this.getUserOverHttpService = getUserOverHttpService;
this.userService = userService;
}
@Scheduled(fixedDelay = 5, initialDelay = 5, timeUnit = TimeUnit.SECONDS)
public void getUserAndSave() {
User user = getUserOverHttpService.getUserById(++counter);
userService.storeUser(user);
}
}
As the application doesn't receive any incoming HTTP requests, only the Background activity service is detected, which shows outgoing calls to a backend:
To represent the full logic would be required to create a custom service that will start a trace from the desired method as an entry point. In this particular case, it would be necessary to use the fully qualified class name com.dynatrace.scheduling.tasks.FetchUsersTask
and the annotated method getUserAndSave
and using the embedded wizard, create a custom service for Java:
After enabling the custom service, it will soon be visible in the process group instance view. The database service is displayed only because the example uses an embedded H2 database.
Navigating to a trace view, you can see details about an outgoing HTTP request and a request to DB:
If you don't have access to the source code and are looking for a way to find and then instrument a proper method, a method hotspot view should be used to analyze stack traces.
Hi,
Nice information!!
Do you need to restart process after custom service is created? Or is it catch it automatically?
Best regards
Hi @AntonPineiro ,
With the enabled real-time updates feature, restarting the JVM is not required.
Best regards,
Anton