ResultCode bootstrapTracerPhpPart()

in agent/native/ext/tracer_PHP_part.cpp [97:161]


ResultCode bootstrapTracerPhpPart( const ConfigSnapshot* config, const TimePoint* requestInitStartTime )
{
    ResultCode resultCode;
    bool shouldRevertLoadingAgentPhpCode = false;
    bool bootstrapTracerPhpPartRetVal;
    zval bootstrapTracerPhpPartArgs[2];
    ZVAL_UNDEF( &bootstrapTracerPhpPartArgs[0] );
    ZVAL_UNDEF( &bootstrapTracerPhpPartArgs[1] );

    char txtOutStreamBuf[ELASTIC_APM_TEXT_OUTPUT_STREAM_ON_STACK_BUFFER_SIZE];
    TextOutputStream txtOutStream = ELASTIC_APM_TEXT_OUTPUT_STREAM_FROM_STATIC_BUFFER( txtOutStreamBuf );
    ELASTIC_APM_LOG_DEBUG_FUNCTION_ENTRY_MSG( "config->bootstrapPhpPartFile: %s, g_tracerPhpPartState: %s"
                                              , streamUserString( config->bootstrapPhpPartFile, &txtOutStream ), tracerPhpPartStateToString( g_tracerPhpPartState ) );
    textOutputStreamRewind( &txtOutStream );

    if ( g_tracerPhpPartState != tracerPhpPartState_before_bootstrap )
    {
        switchTracerPhpPartStateToFailed( /* reason */ "Unexpected current tracer PHP part state", __FUNCTION__ );
        ELASTIC_APM_SET_RESULT_CODE_AND_GOTO_FAILURE();
    }

    if ( config->bootstrapPhpPartFile == NULL )
    {
        GetConfigManagerOptionMetadataResult getMetaRes;
        getConfigManagerOptionMetadata( getGlobalTracer()->configManager, optionId_bootstrapPhpPartFile, &getMetaRes );
        switchTracerPhpPartStateToFailed( /* reason */ streamPrintf( &txtOutStream, "Configuration option `%s' is not set", getMetaRes.optName ), __FUNCTION__ );
        ELASTIC_APM_SET_RESULT_CODE_AND_GOTO_FAILURE();
    }

    elasticApmBeforeLoadingAgentPhpCode();
    shouldRevertLoadingAgentPhpCode = true;

    ELASTIC_APM_CALL_IF_FAILED_GOTO( loadPhpFile( config->bootstrapPhpPartFile ) );

    ZVAL_LONG(&bootstrapTracerPhpPartArgs[0], getGlobalTracer()->logger.maxEnabledLevel);
    ZVAL_DOUBLE(&bootstrapTracerPhpPartArgs[1], (double)timePointToEpochMicroseconds(requestInitStartTime));

    ELASTIC_APM_CALL_IF_FAILED_GOTO( callPhpFunctionRetBool(
            ELASTIC_APM_STRING_LITERAL_TO_VIEW( ELASTIC_APM_PHP_PART_BOOTSTRAP_FUNC )
            , /* argsCount */ ELASTIC_APM_STATIC_ARRAY_SIZE( bootstrapTracerPhpPartArgs )
            , /* args */ bootstrapTracerPhpPartArgs
            , &bootstrapTracerPhpPartRetVal ) );
    if ( ! bootstrapTracerPhpPartRetVal )
    {
        ELASTIC_APM_LOG_CRITICAL( "%s failed (returned false). See log for more details.", ELASTIC_APM_PHP_PART_BOOTSTRAP_FUNC );
        ELASTIC_APM_SET_RESULT_CODE_AND_GOTO_FAILURE();
    }

    g_tracerPhpPartState = tracerPhpPartState_after_bootstrap;
    resultCode = resultSuccess;

    finally:
    zval_dtor( &bootstrapTracerPhpPartArgs[0] ); // long is not refcounted - would not do anything
    zval_dtor( &bootstrapTracerPhpPartArgs[0] ); // double is not refcounted - would not do anything
    if ( shouldRevertLoadingAgentPhpCode )
    {
        elasticApmAfterLoadingAgentPhpCode();
    }
    ELASTIC_APM_LOG_DEBUG_RESULT_CODE_FUNCTION_EXIT();
    return resultCode;

    failure:
    switchTracerPhpPartStateToFailed( /* reason */ "Failed to bootstrap tracer PHP part", __FUNCTION__ );
    goto finally;
}