-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: save client timestamp in database if present #31
Conversation
Pull Request Test Coverage Report for Build #9
💛 - Coveralls |
👀 |
@darahayes would it not be better to be more opinionated and validate the timestamp should be a number, and always present (we may be using it in future)? @maleck13 Thoughts on this? |
We could go deeper and check if timestamp can be used to construct valid date. |
@david-martin can you point me at the code that you are referring to? |
@david-martin I strongly considered this however there is one benefit to the approach I've implemented here. You will notice that I previously defined the ClientTimestamp field in the Metric struct as an integer. With this approach, when the client sends an invalid value for timestamp, e.g. a string of gibberish, or even a valid integer but in a string format, It seems that using the We get this:
instead of this:
Now we could choose to send back whatever error message comes from @wtrocki we check that the timestamp is a valid integer guarantees that a valid date can be constructed. |
@darahayes if sending the metrics many times, new rows are added, right? If so, it works using the app 👍 |
@david-martin @maleck13 Please see my updated comment above ^^ @maleck13 the code in question is the definition of ClientTimestamp as a This is the definition: https://github.com/aerogear/aerogear-app-metrics/pull/31/files#diff-2833ae78c6b668c514241e15283138a4L14 This is the validation logic: https://github.com/aerogear/aerogear-app-metrics/pull/31/files#diff-2833ae78c6b668c514241e15283138a4R49 This is where we eventually convert the string value to an integer (in the business logic): https://github.com/aerogear/aerogear-app-metrics/pull/31/files#diff-f0640233955767d355b6f2bdb5ee3b10R23 Note that the validation logic is called first in the handler, which means we never reach the business logic with an invalid timestamp. Only a good one or an empty one. |
@david-martin I'm just realising now you're also asking should we just make the timestamp always required. I'm totally happy to do this if you would like! |
Verified locally. Results were: String:
Integer:
Not present:
Invalid (response in curl):
|
The timestamp field is left empty when no timestamp is submitted. Maybe the server should just generate and insert one? |
This approach looks reasonable to me, we do validate that it is a number. The only concern I have is that we don't know that it is a valid timestamp (how important is that I am not sure) which @david-martin is perhaps what you are asking about |
In real case scenario that will be developer/integration error. Best to log that out as warning or something like this.
Getting your point. There are many different format of timestamps. Unix and Java timestamps are different (1000 times smaller number) etc. |
@maleck13 @david-martin We use the integer value (which is validated) to create a time object with |
+1 for timestamps being optional and generated on server time if not supplied. Since metrics (either custom or our own defined ones) are for general telemetry most of them will not be very time-sensitive. The SDKs will always include the field anyway, so this should only cover users that for some reason decide to hit the http API directly. It could allow for exotic clients like small iot devices that don't even keep track of time themselves. |
@paolobueno I think there's value in having the client timestamp & server timestamp. Server timestamp is good to have so we know when a metric entry was inserted in the db.
If this becomes a requirement, lets address it then. IOT devices may not be online all the time either, so how to tell when a metric was actually generated? Probably best not to think about these scenarios for IOT |
@maleck13 If we validate it as a valid timestamp (can be parsed to a Date object) that would be sufficient IMO. |
I think that would be best. It would avoid a situation where we use the client timestamp on the server in future, and there are old clients out there that don't send it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Finally managed to get that running on my slow network. Verified with Android SDK having hardcoded config to localhost. Leaving approval to golang gurus.
@darahayes I'm gonna approve for now. I've sent a mail to the group to help tease out some decision around timestamps rather than holding things up here |
Verification Steps
Because this PR introduces changes to the database you must delete the old db container cached by docker-compose. Do the following:
docker rm
the ids of any containers returned back by the previous commanddocker-compose up --force-recreate --build db
For each case we are going to make a
curl
request and then check the results in the database.Logging into the database and checking the contents of the
mobileappmetrics
table can be done as follows:For each section below, perform the curl request and then check the database for the expected result. We are going to test the following cases:
Client Timestamp Present (as a string)
Expected Result
200 OK
Client Timestamp Preset as an Integer
Expected Result
200 OK
Client Timestamp is not present
Expected Result
200 OK
Client Timestamp is Invalid
Expected Result
400 Bad Request
Nothing should be saved in database