Java agent: Change span name for out of the box db span

Hi,
i'm experimenting with apm java client. I can see that APM automatically detect calls to my mssql database and shows the span inside the transactio. But if i'm trying to change the span's name using the API (code below) i'm losing most of the db metadata.
Exmple code:

connection = DsSingelton.getInstance().getBasicDs().getConnection();
ps = connection.prepareStatement("SELECT * FROM students");
Transaction parent = ElasticApm.currentTransaction();
Span span = parent.startSpan("db", "mssql", "query");
try (final Scope scope = span.activate()) {
    span.setName("getStudents");
    res = ps.executeQuery();
} catch (Exception e) {
    span.captureException(e);
    throw e;
} finally {
    span.end();
}

image from Trasaction page showing the captured span:

when clicking on the span the db metadata that usually shown for sql queries (if i'm not making changes on APM level) is missing:

Also, Span API does not have addCustomContext method like Transaction. I would like to know how to make changes on out-of-the-box spans so that they will still contain the attributes based on the type/library (http,db, etc).

Thanks!

Hi Ofir :wave:

when clicking on the span the db metadata that usually shown for sql queries (if i'm not making changes on APM level) is missing

Try creating your span with a different type (e.g. - "app"). Then it should have a child DB span with the query and other DB metadata.

Also, Span API does not have addCustomContext method like Transaction.

You are right, this is currently not supported. We have an open issue regarding that, feel free to upvote, or add your thoughts there. But I think labels may be more suitable to what you are looking for.

I would like to know how to make changes on out-of-the-box spans so that they will still contain the attributes based on the type/library (http,db, etc).

You can only update active spans, which is tricky for DB and HTTP, because your code normally doesn't run within the creation->activation->deactivaion->end cycle of such spans.

Thanks for the answers @Eyal_Koren .
I will try to create 'app' type and review the behavior.
I imagined it is going to be problematic to interfere with spans managed by the framework but hoped there is an approach I couldn't find in the docs/online.

Regarding labels - i have used them and I can put extra metadata there but is has 2 issues:

  1. the APM application on Kibana does not parse the data the same way. for example, the db.statement is shown on the span view when using the out of the box data and if the context cannot be overriden it is difficult to have the same behavior on APM UI and in general docuent parsing. view for out-of-the-box db span:
  2. the out-of-the-box span has many more useful metadata on the db transaction (like user being used, daatbase name, destintation service resource, etc) which can extracted manually but require additional development which was already done on APM side

If you keep your current manual span, only give it a different type, it should be captured and it should have a proper child DB span, with all information. Then, before you end your custom span, you can add any additional data that is not captured automatically to it through labels.
Hopefully this provides you with what you are looking for.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.