amplitude / Amplitude-Kotlin

Amplitude Kotlin SDK
MIT License
27 stars 10 forks source link

User IDs less than 5 characters cause analytics to fail #105

Closed jp2014 closed 1 year ago

jp2014 commented 1 year ago

Expected Behavior

User IDs with less than 5 characters can be used. Analytics events work as normal.

Current Behavior

User Ids with less than 5 characters cause this response from https://api2.amplitude.com/2/httpapi

{
  "code": 400,
  "events_with_invalid_id_lengths": {
    "user_id": [
      1
    ]
  },
  "error": "Invalid id length for user_id or device_id"
}

example captured request:

curl 'https://api2.amplitude.com/2/httpapi' \
-X POST \
-H 'Accept: application/json' \
-H 'User-Agent: Dalvik/2.1.0 (Linux; U; Android 13; Pixel 5 Build/TQ1A.221205.011)' \
-H 'Host: api2.amplitude.com' \
-H 'Connection: close' \
-H 'Content-Length: 955' \
-H 'Content-Type: application/json; charset=utf-8' \
-d '{
  "api_key": "hidden",
  "events": [
    {
      "event_type": "$identify",
      "user_id": "2",
      "device_id": "hidden",
      "time": 1675959091145,
      "event_properties": {},
      "user_properties": {
        "$set": {
          "Test prop": "test",      
        }
      },
      "groups": {},
      "group_properties": {},
      "platform": "Android",
      "os_name": "android",
      "os_version": "13",
      "device_brand": "google",
      "device_manufacturer": "Google",
      "device_model": "Pixel 5",
      "carrier": "",
      "language": "en",
      "ip": "$remote",
      "adid": "00000000-0000-0000-0000-000000000000",
      "event_id": 884,
      "session_id": hidden,
      "insert_id": "hidden",
      "library": "amplitude-analytics-android/1.6.1",
      "android_app_set_id": "hidden"
    },
  ]
}' \
--proxy http://localhost:9090

After a while, the SDK gets in a loop and starts sending 10 failing requests per second

image

Editing and resending a request with an ID longer than 5 characters results in a successful 200 response.

Possible Solution

  1. Remove 5 character limit.
  2. Enforce the 5 character limit in the kotlin android analytics sdk

Steps to Reproduce

  1. In an Android application, bring in the analytics-android:1.6.1 dependency
  2. Using the amplitude sdk, set the user id to any id that is under 5 characters
  3. In logcat, you will see this logged when sending events: "Handle response, status: BAD_REQUEST, error: Invalid id length for user_id or device_id"
  4. Using a proxy you will see the response mentioned in current behavior above

Environment

qingzhuozhen commented 1 year ago

Hi @jp2014, thanks for reporting this! Regarding the 5 characters limit, that's a default limit in our HTTP v2 API. There is a configuration minIdLength that could be used and adjust the value for it.

Thanks for the suggestions on SDK behavior. Sending retries in this scenario could be avoided, and the character limit warning could be enforced earlier. We would make the improvements on our end.

cc @justin-fiedler @yuhao900914 @liuyang1520

jp2014 commented 1 year ago

Thanks @qingzhuozhen! Good to know. I've updated to use the minIdLength configuration option and it works as expected.