amplitude / analytics-go

Go Amplitude Analytics SDK
MIT License
12 stars 7 forks source link

fix: Override Code:0 with the StatusCode value #66

Open ekartsev opened 3 months ago

ekartsev commented 3 months ago

Summary

The Amplitude client is set up like the following:

amplitude.Client
    -> destination.amplitudePlugin
        -> storages.inMemoryEventStorage
        -> internal.AmplitudeHTTPClient
        -> internal.AmplitudeResponseProcessor

The flow works like the following:

  1. Clients call amplitude.Client.Track() to send an event.
  2. The event is added to storages.inMemoryEventStorage (by destination.amplitudePlugin)
  3. Then, destination.amplitudePlugin pulls the events from the store asynchronously and sends them to the Amplitude (server) via internal.AmplitudeHTTPClient.Send()
  4. After, destination.amplitudePlugin waits for the http response and calls internal.AmplitudeResponseProcessor.Process() to transform the response from AmplitudeResponse to AmplitudeProcessorResult
  5. And finally, AmplitudeProcessorResult is used to create types.ExecuteResult and call the client's callback

AmplitudeResponse has 2 status codes and 2 errors:

type AmplitudeResponse struct {
    Status int `json:"-"`            // An HTTP Response Code
    Err error `json:"-"`              // An HTTP Response Err

    Code int `json:"code"`        // Code from the Response Body json
    Error string `json:"error"`.  // Error from the Response Body json
    ...
}

Code field is populated from the json response body. For example:

{
  "code": 200,
  "events_ingested": 50,
  "payload_size_bytes": 50,
  "server_upload_time": 1396381378123
}

See: https://amplitude.com/docs/apis/analytics/batch-event-upload#successsummary


Q: What happens if an http request fails, there's no response body or the body is not json?

The Code field will be 0.

This leads to many issues:

One. https://github.com/amplitude/analytics-go/pull/64 - a fix, which only takes care of 413

Two. Customers get Code=0 via the callback. When this happens the customers don't know how to handle it. Does it mean we should retry or drop the message? This leads to a potential data loss.

A few examples of errors:


In this change we make internal.AmplitudeResponseProcessor to assign Code from StatusCode (effectively http response status code) if Code=0 (if request failed or json parsing failed).

This will tell the customer the real reason of a failure and the customer will be able to decide what to do with the message.

Testing

Checklist