r/androiddev • u/Commercial_Spell_488 • 2d ago
Question How to get exact timestamps for OTel
Been working to integrate OpenTelemetry Tracing into my Android app for debugging purposes, to detect where time is spent processing my HTTP requests.
The problem I have encountered is that no matter what I tried I always ran into issues with clock skew between the Android devices and my backend server. And I am not talking 2-3ms, but commonly around 1 second. This obviously kind of defeats the purpose of using Traces to figure where the time is spent and also leads to spans that seemingly break causality.
Now I've tried setting a NTP server, but unfortunately this does not really help, since the Android system time is only updated when the drift between the time retrieved from the NTP and the system time is greater than five seconds. Therefore providing a NTP server (which is rather cumbersome in itself as it is only possible through ADB on stock Android) does not help in itself.
After doing some research on this topic, it seems like I have three options:
- Integrate a custom NTP client into my app and use its time (sync at app startup and then regularly to avoid drift). This feels like an overkill solution to me and also like a waste of resources, if multiple apps on your phone would do this. Furthermore it makes my app more complex and just feels wrong from a software architectonical standpoint.
- Use currentNetworkTimeClock(). This is only supported since API Level 33+ and therefore is not an option for me. Apart from that I would still need to force my users to set a proper NTP server (via ADB?!).
- Use the TrustedTime API from the Google PlayServices. This is not option for me, because I must support Android without PlayServices and some of my devices run inside air-gapped networks where only a custom NTP server is available.
I have already found this Issue in the opentelemetry-android repo, but I can hardly believe that this is not a problem others have already had and that there is not a clean solution for this.
So did anybody already encounter a situation where they needed precise timestamps on Android devices and what have you done? I am looking for clean, easy and versatile options.
Thanks in advance for any recommendations / opinions on this topic.
