cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
anton_konikov
Dynatrace Guide
Dynatrace Guide

Abstract

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.

Resolution

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:

anton_konikov_0-1732803914582.png
Screenshot 2024-11-28 at 15.27.31.png

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: 

anton_konikov_0-1732804590942.png

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.

Screenshot 2024-12-06 at 10.58.50.png

Navigating to a trace view, you can see details about an outgoing HTTP request and a request to DB:

Screenshot 2024-12-06 at 11.24.29.png

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.

Version history
Last update:
‎20 Dec 2024 10:25 AM
Updated by:
Comments
AntonPineiro
DynaMight Guru
DynaMight Guru

Hi,

Nice information!! :take_my_money:

Do you need to restart process after custom service is created? Or is it catch it automatically?

Best regards

anton_konikov
Dynatrace Guide
Dynatrace Guide

Hi @AntonPineiro ,

With the enabled real-time updates feature, restarting the JVM is not required.

 

Best regards,

Anton