ResultCode syncSendEventsToApmServerWithConn()

in agent/native/ext/backend_comm.cpp [429:490]


ResultCode syncSendEventsToApmServerWithConn( const ConfigSnapshot* config, ConnectionData* connectionData, StringView serializedEvents )
{
    ResultCode resultCode;
    CURLcode curlResult;
    enum { urlBufferSize = 256 };
    char url[urlBufferSize];
    int snprintfRetVal;
    char txtOutStreamBuf[ ELASTIC_APM_TEXT_OUTPUT_STREAM_ON_STACK_BUFFER_SIZE ];
    TextOutputStream txtOutStream = ELASTIC_APM_TEXT_OUTPUT_STREAM_FROM_STATIC_BUFFER( txtOutStreamBuf );
    long responseCode = 0;
    bool isFailed = true;
    const char *serverUrlAndQuerySeparator = std::string_view(config->serverUrl).ends_with('/') ? "" : "/";

    ELASTIC_APM_ASSERT_VALID_PTR( connectionData );
    ELASTIC_APM_ASSERT( connectionData->curlHandle != NULL, "" );

    ELASTIC_APM_LOG_DEBUG_FUNCTION_ENTRY();

    ELASTIC_APM_CURL_EASY_SETOPT( connectionData->curlHandle, CURLOPT_POST, 1L );
    ELASTIC_APM_CURL_EASY_SETOPT( connectionData->curlHandle, CURLOPT_POSTFIELDS, serializedEvents.begin );
    ELASTIC_APM_CURL_EASY_SETOPT( connectionData->curlHandle, CURLOPT_POSTFIELDSIZE, serializedEvents.length );

    snprintfRetVal = snprintf( url, urlBufferSize, "%s%sintake/v2/events", config->serverUrl, serverUrlAndQuerySeparator);
    if ( snprintfRetVal < 0 || snprintfRetVal >= urlBufferSize )
    {
        ELASTIC_APM_LOG_ERROR( "Failed to build full URL to APM Server's intake API. snprintfRetVal: %d", snprintfRetVal );
        ELASTIC_APM_SET_RESULT_CODE_AND_GOTO_FAILURE();
    }
    ELASTIC_APM_CURL_EASY_SETOPT( connectionData->curlHandle, CURLOPT_URL, url );

    curlResult = curl_easy_perform( connectionData->curlHandle );
    if ( curlResult != CURLE_OK )
    {
        ELASTIC_APM_LOG_ERROR(
                "Sending events to APM Server failed"
                "; URL: `%s'"
                "; error message: `%s'"
                "; curl info: %s"
                "; current process command line: `%s'"
                , url
                , curl_easy_strerror( curlResult )
                , streamLibCurlInfo( &txtOutStream )
                , streamCurrentProcessCommandLine( &txtOutStream, /* maxLength */ 200 ) );
        ELASTIC_APM_SET_RESULT_CODE_AND_GOTO_FAILURE();
    }

    curl_easy_getinfo( connectionData->curlHandle, CURLINFO_RESPONSE_CODE, &responseCode );
    /**
     *  If the HTTP response status code isn’t 2xx or if a request is prematurely closed (either on the TCP or HTTP level) the request MUST be considered failed.
     *
     * @see https://github.com/elastic/apm/blob/d8cb5607dbfffea819ab5efc9b0743044772fb23/specs/agents/transport.md#transport-errors
     */
    isFailed = ( responseCode / 100 ) != 2;
    ELASTIC_APM_LOG_WITH_LEVEL( isFailed ? logLevel_error : logLevel_debug, "Sent events to APM Server. Response HTTP code: %ld. URL: `%s'.", responseCode, url );
    resultCode = isFailed ? resultFailure : resultSuccess;
    finally:
    ELASTIC_APM_LOG_DEBUG_RESULT_CODE_FUNCTION_EXIT();
    return resultCode;

    failure:
    goto finally;
}