static PHP_GINIT_FUNCTION()

in prod/native/extension/code/ModuleEntry.cpp [86:124]


static PHP_GINIT_FUNCTION(elastic_otel) {
    //TODO for ZTS logger must be initialized in MINIT! (share fd between threads) - different lifecycle

    //TODO store in globals and allow watch for config change (change of level)
    auto logSinkStdErr = std::make_shared<elasticapm::php::LoggerSinkStdErr>();
    auto logSinkSysLog = std::make_shared<elasticapm::php::LoggerSinkSysLog>();
    auto logSinkFile = std::make_shared<elasticapm::php::LoggerSinkFile>();

    auto logger = std::make_shared<elasticapm::php::Logger>(std::vector<std::shared_ptr<elasticapm::php::LoggerSinkInterface>>{logSinkStdErr, logSinkSysLog, logSinkFile});

    configManager.attachLogger(logger);

    ELOGF_DEBUG(logger, MODULE, "%s: GINIT called; parent PID: %d", __FUNCTION__, static_cast<int>(elasticapm::osutils::getParentProcessId()));
    elastic_otel_globals->globals = nullptr;

    auto phpBridge = std::make_shared<elasticapm::php::PhpBridge>(logger);

    auto hooksStorage = std::make_shared<elasticapm::php::InstrumentedFunctionHooksStorage_t>();

    auto inferredSpans = std::make_shared<elasticapm::php::InferredSpans>([interruptFlag = reinterpret_cast<void *>(&EG(vm_interrupt))]() {
#if PHP_VERSION_ID >= 80200
        zend_atomic_bool_store_ex(reinterpret_cast<zend_atomic_bool *>(interruptFlag), true);
#else
        *static_cast<zend_bool *>(interruptFlag) = 1;
#endif
    }, [phpBridge](elasticapm::php::InferredSpans::time_point_t requestTime, elasticapm::php::InferredSpans::time_point_t now) {
        phpBridge->callInferredSpans(now - requestTime);
    });

    try {
        elastic_otel_globals->globals = new elasticapm::php::AgentGlobals(logger, std::move(logSinkStdErr), std::move(logSinkSysLog), std::move(logSinkFile), std::move(phpBridge), std::move(hooksStorage), std::move(inferredSpans), [](elasticapm::php::ConfigurationSnapshot &cfg) { return configManager.updateIfChanged(cfg); });
    } catch (std::exception const &e) {
        ELOGF_CRITICAL(logger, MODULE, "Unable to allocate AgentGlobals. '%s'", e.what());
    }

    // ZVAL_UNDEF(&elastic_otel_globals->lastException);
    // new (&elastic_otel_globals->lastErrorData) std::unique_ptr<elasticapm::php::PhpErrorData>;
    elastic_otel_globals->captureErrors = false;
}