amplitude/Amplitude-Java

How to wait for .flushEvents() to finish in AWS lambda function

dario-jahnel opened this issue · 4 comments

Hi, I'm trying to use the SDK to send events on demand (not using batch)

But the function ends without executing the AmplitudeCallbacks

Here is my code:

public class AmplitudeAPI {
    private static final Logger log = LoggerFactory.getLogger(AmplitudeAPI.class);
    private Amplitude client;
    private AmplitudeApiAwsConfiguration awsConfiguration;

    public AmplitudeAPI() {
        client = Amplitude.getInstance();
        awsConfiguration = new AmplitudeApiAwsConfiguration();
        AmplitudeSecret secret = awsConfiguration.getAmplitudeSecret();
        client.init(secret.getApiKey());
    }

    public void sendEvent(String eventType, String userId, JSONObject eventProperties, JSONObject userProperties) {
        Event event = new Event(eventType, userId);
        if (eventProperties != null) {
            event.eventProperties = eventProperties;
        }
        if (userProperties != null) {
            event.userProperties = eventProperties;
        }


        AmplitudeCallbacks eventCallbacks =
                new AmplitudeCallbacks() {
                    @Override
                    public void onLogEventServerResponse(Event event, int status, String message) {
                        log.debug(String.format("parameters sent " + eventType + userId + eventProperties + userProperties)); // In the logs this line doesn't execute
                        log.debug(String.format("Before if Status: %d, Message: %s", status, message));
                        if (status == 200) {
                            log.debug(String.format("Status: %d, Message: %s", status, message));
                        } else {
                            log.error(String.format("Failed to send event to Amplitude Status: %d, Message: %s", status, message));
                        }
                    }
                };
        log.debug("About to send event");
        client.logEvent(event, eventCallbacks);
        log.debug("Flushing...");
        client.flushEvents();
        log.debug("Finished flushing"); // In the logs it reaches this line
    }

Update: It seems that after 30 minutes it fails with this message "Status: 400, Message: Request missing required field"

Edit: and also corrected userProps = eventProps

Update: The api key was incorrect, but after fixing it and trying again it didn't post an event in Amplitude or log an error after 50 minutes

I'm trying to solve it using http api but i'd like to know what happened

Hi @dario-jahnel ,

The amplitude client with default configuration usually sent out events in 10 seconds. Even with errors and retries, it won't take 30 minutes to trigger callback. Did you customize the config like client.setEventUploadThreshold() or client.setEventUploadPeriodMillis()? Can you use client.setLogMode(AmplitudeLog.LogMode.DEBUG) to collect some debug info and provide more detail? Thanks very much.