Skip to content

How do I include SQL query timings in RUM? #731

Open

Description

@vinaysshenoy

I work on a very SQLite heavy application, where most of the network calls happen in the background outside the context of a user flow. One thing I'm not sure of is how I can include timings for SQL queries as part of the RUM session traces that are received. Does the SDK have support for this?

Even if the SDK has a way for me to add a generic span to the trace, that would be great as well. I just could find out a way to do this from the documentation.

Activity

  1. xgouchet commented on Oct 22, 2021

    @xgouchet
    Contributor

    Do you mean adding a span for a SQLite query performed on the device itself, or a SQL query performed in your backend? For backend tracing, you can take a look at our [APM documentation]https://docs.datadoghq.com/tracing/) page to add tracing to your stack directly. For local SQLite queries performed in your Android code, you can manually create traces by following this documentation.

  2. vinaysshenoy commented on Oct 22, 2021

    @vinaysshenoy
    Author

    This is for SQLite queries made in the mobile app. I'll take a look at the documentation, thanks for the quick reply! 🙏🏼

  3. vinaysshenoy commented on Nov 17, 2021

    @vinaysshenoy
    Author

    @xgouchet I followed the sample on that documentation, and I was able to send traces to Datadog. One thing I was not able to figure out however, is making those traces show up in the RUM sessions.

    The only thing I see in the RUM sessions is the OkHttp network calls, but the SQLite traces are only visible on the Traces dashboard. I tried looking at the code of the network interceptor, but not able to figure out exactly how these traces are added to the RUM sessions. Would you be able to give me some direction here?

  4. xgouchet commented on Nov 17, 2021

    @xgouchet
    Contributor

    Hey, on the spans that you can see in APM, can you check that you have the following attributes, and that they have "valid" values (invalid would be null, empty, or 00000000-0000-0000-0000-000000000000):
    session_id, application_id and view.id. E.g.:

    {
        "application_id": "3dd57edb-7dbf-42fe-a996-50036c2c4b3a",
        "session_id": "f7d6e3b3-ad80-4827-adca-9013814b5851",
        "view": {
            "id": "d6e9f248-87bf-4301-93f5-ecef4298fca5"
        },
       // …
    }
  5. vinaysshenoy commented on Nov 17, 2021

    @vinaysshenoy
    Author

    Yup, they are all set. I even copied the view ID from the APM trace and filtered the RUM views on that ID and was able to find the screen. The trace just doesn't show up on the RUM session 🤔 .

    Not sure if I need to change a filter or something to get it to show up.

  6. vinaysshenoy commented on Nov 17, 2021

    @vinaysshenoy
    Author

    For what it's worth, the application is open source, so there shouldn't be an issue sharing the implementation with you to see if we're missing something.

  7. xgouchet commented on Nov 18, 2021

    @xgouchet
    Contributor

    Great news, thanks a lot @vinaysshenoy

  8. vinaysshenoy commented on Nov 22, 2021

    @vinaysshenoy
    Author

    For what it's worth, the application is open source, so there shouldn't be an issue sharing the implementation with you to see if we're missing something.

    @xgouchet I'll post the relevant code snippets here, and also link the branch at the end if you would like to go through the changes we have made.

    In the main Application class, this is how we initialize the Datadog integration.

        val datadogConfig = Configuration
            .Builder(
                logsEnabled = false,
                tracesEnabled = true,
                crashReportsEnabled = true,
                rumEnabled = true
            )
            .trackBackgroundRumEvents(true)
            .trackLongTasks(5000)
            .useViewTrackingStrategy(FragmentViewTrackingStrategy(
                trackArguments = false,
                supportFragmentComponentPredicate = ResolveScreenNamesForDatadog()
            ))
            .build()
        val credentials = Credentials(
            clientToken = BuildConfig.DATADOG_CLIENT_TOKEN,
            envName = BuildConfig.DATADOG_ENVIRONMENT,
            variant = BuildConfig.FLAVOR,
            rumApplicationId = BuildConfig.DATADOG_APPLICATION_ID,
            serviceName = BuildConfig.DATADOG_SERVICE_NAME
        )
        Datadog.initialize(this, credentials, datadogConfig, TrackingConsent.GRANTED)
        GlobalRum.registerIfAbsent(RumMonitor.Builder().build())
        GlobalTracer.registerIfAbsent(AndroidTracer.Builder().build())

    We have also instrumented our Room database DAOs to provide hooks for before and after a query is executed. We use this to start and stop the spans that we are sending to Datadog.

    class DatadogSqlPerformanceReportingSink : ReportSink {
    
      /**
       * Maintains the list of running spans for every DB call.
       *
       * This can potentially lead to memory leaks if there are DB operations that start but never
       * finish. However, the chances of that seem small enough that it may not be necessary to take
       * on the maintenance effort of periodically removing long running spans.
       *
       * Something to investigate at a later date.
       **/
      private var runningSpans = emptyMap<SqlOperation, Span>()
    
      override fun begin(operation: SqlOperation) {
        Timber.tag("SqlPerformanceReporting").d("Begin operation: $operation")
        val tracer = GlobalTracer.get()
        val span = tracer
            .buildSpan("room.query")
            .withTag("dao", operation.daoName)
            .withTag("method", operation.methodName)
            .start()
    
        runningSpans = runningSpans + (operation to span)
      }
    
      override fun end(operation: SqlOperation) {
        Timber.tag("SqlPerformanceReporting").d("End operation: $operation")
        val spanForOperation = runningSpans[operation]
    
        /*
        * There is a possibility for a completed operation to not be present in the list of running
        * spans since we make copies of the map of running spans and the DB queries can run on multiple
        * threads.
        *
        * In this scenario, we'll just not report this span since the cost of thread synchronisation is
        * not worth the small chances of a span being lost.
        **/
        if (spanForOperation != null) {
          Timber.tag("SqlPerformanceReporting").d("Report operation: $operation")
          spanForOperation.finish()
          runningSpans = runningSpans - operation
        }
      }
    }

    With this setup, the room query traces show up on the Traces dashboard with all the attributes mentioned earlier set, but they don't show up on the RUM sessions themselves. Would you have any idea what we're doing wrong?

    The branch is available for review at https://github.com/simpledotorg/simple-android/commits/vs/21oct/datadog-integration.

  9. xgouchet commented on Nov 22, 2021

    @xgouchet
    Contributor

    Hi @vinaysshenoy, in theory it should work. Let me fork your project and test it on my side to see what's happening.

  10. vinaysshenoy commented on Nov 22, 2021

    @vinaysshenoy
    Author

    Hi @vinaysshenoy, in theory it should work. Let me fork your project and test it on my side to see what's happening.

    Thanks, that would be super helpful!

    A couple of notes about building and running the project:

    • The Datadog configuration parameters can be set in gradle.properties.
    • Database calls will typically not be made until you either register and/or login. Adding some test user credentials (in the order which the app will ask for them) here so you can login and trigger the database queries:
      • Country: India
      • State: Punjab
      • Phone: 1111111111
      • PIN: 1111
      • Login code: 000000
  11. xgouchet commented on Nov 22, 2021

    @xgouchet
    Contributor

    Thanks, indeed something is not working as it should. Let me collect more info, and I'll come back to you

  12. vinaysshenoy commented on Nov 22, 2021

    @vinaysshenoy
    Author

    I've run into one more issue, which might not be related. The app has multiple build flavours, each of which correspond to a different environment on Datadog. We tried building a version of the app for a SANDBOX variant, which is essentially a nightly release build which goes to internal testing.

    The RUM sessions are showing up on the dashboard, but none of the SQL query traces (which were showing up for a local development build) show up. The service itself is not visible in the list of the services for that environment, only visible for the local development environment which we had tested the Datadog integration on.

    I've tried the following:

    • Added logs to the span start and stop locations in the code. The logs show up on Logcat as expected.
    • Checked to see if it might be a problem with build minification, this does not seem to the problem as well as they don't show up even if R8 is disabled.

    Any idea what could be the problem here?

  13. vinaysshenoy commented on Nov 23, 2021

    @vinaysshenoy
    Author

    I've run into one more issue, which might not be related. The app has multiple build flavours, each of which correspond to a different environment on Datadog. We tried building a version of the app for a SANDBOX variant, which is essentially a nightly release build which goes to internal testing.

    The RUM sessions are showing up on the dashboard, but none of the SQL query traces (which were showing up for a local development build) show up. The service itself is not visible in the list of the services for that environment, only visible for the local development environment which we had tested the Datadog integration on.

    I've tried the following:

    * Added logs to the span start and stop locations in the code. The logs show up on Logcat as expected.
    
    * Checked to see if it might be a problem with build minification, this does not seem to the problem as well as they don't show up even if R8 is disabled.
    

    Any idea what could be the problem here?

    Never mind, this was my fault. I missed setting up the Android tracer.

  14. vinaysshenoy commented on Nov 25, 2021

    @vinaysshenoy
    Author

    Thanks, indeed something is not working as it should. Let me collect more info, and I'll come back to you

    @xgouchet Just checking in to see if you have any idea as to what's going on here.

  15. xgouchet commented on Nov 25, 2021

    @xgouchet
    Contributor

    Hey @vinaysshenoy, I'm still trying to synchronize with all the teams involved, to understand the exact issue here.

  16. vinaysshenoy commented on Nov 26, 2021

    @vinaysshenoy
    Author

    Thanks! Please let me know if I can provide some more information that might be helpful in any way.

  17. msasikanth commented on Jan 11, 2022

    @msasikanth

    Hey @xgouchet , checking in to see if there is any update on this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions