void elasticApmModuleInit()

in agent/native/ext/lifecycle.cpp [236:315]


void elasticApmModuleInit( int moduleType, int moduleNumber )
{
    auto const &sapi = ELASTICAPM_G(globals)->sapi_;

    ELASTIC_APM_LOG_DIRECT_DEBUG( "%s entered: moduleType: %d, moduleNumber: %d, parent PID: %d, SAPI: %s (%d) is %s", __FUNCTION__, moduleType, moduleNumber, (int)(getParentProcessId()), sapi.getName().data(), static_cast<uint8_t>(sapi.getType()), sapi.isSupported() ? "supported" : "unsupported");

    if (!sapi.isSupported()) {
        return;
    }

    registerOsSignalHandler();

    elasticapm::php::Hooking::getInstance().fetchOriginalHooks();

    ResultCode resultCode;
    Tracer* const tracer = getGlobalTracer();
    const ConfigSnapshot* config = NULL;
    CURLcode curlCode;

    ELASTIC_APM_CALL_IF_FAILED_GOTO( constructTracer( tracer ) );

    if ( ! tracer->isInited )
    {
        ELASTIC_APM_LOG_DEBUG( "Extension is not initialized" );
        ELASTIC_APM_SET_RESULT_CODE_AND_GOTO_FAILURE();
    }

    registerElasticApmIniEntries( moduleType, moduleNumber, &tracer->iniEntriesRegistrationState );

    ELASTIC_APM_CALL_IF_FAILED_GOTO( ensureLoggerInitialConfigIsLatest( tracer ) );
    ELASTIC_APM_CALL_IF_FAILED_GOTO( ensureAllComponentsHaveLatestConfig( tracer ) );

    logSupportabilityInfo( logLevel_debug );

    config = getTracerCurrentConfigSnapshot( tracer );

    if ( ! config->enabled )
    {
        resultCode = resultSuccess;
        ELASTIC_APM_LOG_DEBUG( "Extension is disabled" );
        goto finally;
    }

    registerCallbacksToLogFork();
    registerAtExitLogging();
    registerExceptionHooks(*config);

    curlCode = curl_global_init( CURL_GLOBAL_ALL );
    if ( curlCode != CURLE_OK )
    {
        resultCode = resultFailure;
        ELASTIC_APM_LOG_ERROR( "curl_global_init failed: %s (%d)", curl_easy_strerror( curlCode ), (int)curlCode );
        goto finally;
    }
    tracer->curlInited = true;

    astInstrumentationOnModuleInit( config );

    elasticapm::php::Hooking::getInstance().replaceHooks(config->captureErrors, config->profilingInferredSpansEnabled);

    if (php_check_open_basedir_ex(config->bootstrapPhpPartFile, false) != 0) {
        ELASTIC_APM_LOG_WARNING(
            "Elastic Agent bootstrap file (%s) is located outside of paths allowed by open_basedir ini setting."
            " For more details see https://www.elastic.co/guide/en/apm/agent/php/current/setup.html#limitation-open_basedir"
            , config->bootstrapPhpPartFile
        );
    }

    resultCode = resultSuccess;
    finally:

    ELASTIC_APM_LOG_DEBUG_RESULT_CODE_FUNCTION_EXIT();
    // We ignore errors because we want the monitored application to continue working
    // even if APM encountered an issue that prevent it from working
    return;

    failure:
    moveTracerToFailedState( tracer );
    goto finally;
}