Skip to content

Commit

Permalink
Changeg flag to enable/disable async trasnport, fixed log levels
Browse files Browse the repository at this point in the history
  • Loading branch information
intuibase committed Oct 1, 2024
1 parent bd3f807 commit ae08979
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion docs/configure.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ _Currently there are no additional `OTEL_` options waiting to be contributed ups
| Option(s) | Default | Accepted values | Description |
|---|---|---|---|
|ELASTIC_OTEL_ENABLED|true|true or false|Enables the automatic bootstrapping of instrumentation code|
|ELASTIC_OTEL_DISABLE_ASYNC_TRANSPORT|false| true or false | Disables asynchronous (background) transfer of traces, metrics and logs. Brings back original OpenTelemetry SDK transfer modes|
|ELASTIC_OTEL_ASYNC_TRANSPORT|true| true or false | Use asynchronous (background) transfer of traces, metrics and logs. If false - brings back original OpenTelemetry SDK transfer modes|
|ELASTIC_OTEL_MAX_SEND_QUEUE_SIZE|2MB| integer number with optional units: B, MB or GB | Set the maximum buffer size for asynchronous (background) transfer. It is set per worker process.|
|ELASTIC_OTEL_VERIFY_SERVER_CERT|true|true or false|Enables server certificate verification for asynchronous sending|
|ELASTIC_OTEL_LOG_FILE||Filesystem path|Log file name. You can use the %p placeholder where the process ID will appear in the file name, and %t where the timestamp will appear. Please note that the PHP process must have write permissions for the specified path.|
Expand Down
2 changes: 1 addition & 1 deletion prod/native/extension/code/ModuleIniEntries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ ELASTIC_OTEL_INI_ENTRY(EL_STRINGIFY(ELASTIC_OTEL_CFG_OPT_NAME_VERIFY_SERVER_CERT

ELASTIC_OTEL_INI_ENTRY(EL_STRINGIFY(ELASTIC_OTEL_CFG_OPT_NAME_DEBUG_DIAGNOSTICS_FILE))
ELASTIC_OTEL_INI_ENTRY(EL_STRINGIFY(ELASTIC_OTEL_CFG_OPT_NAME_MAX_SEND_QUEUE_SIZE))
ELASTIC_OTEL_INI_ENTRY(EL_STRINGIFY(ELASTIC_OTEL_CFG_OPT_NAME_DISABLE_ASYNC_TRANSPORT))
ELASTIC_OTEL_INI_ENTRY(EL_STRINGIFY(ELASTIC_OTEL_CFG_OPT_NAME_ASYNC_TRANSPORT))

PHP_INI_END()

Expand Down
2 changes: 1 addition & 1 deletion prod/native/libcommon/code/ConfigurationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class ConfigurationManager {
BUILD_METADATA(ELASTIC_OTEL_CFG_OPT_NAME_DEBUG_DIAGNOSTICS_FILE, OptionMetadata::type::string, false),
BUILD_METADATA(ELASTIC_OTEL_CFG_OPT_NAME_VERIFY_SERVER_CERT, OptionMetadata::type::boolean, false),
BUILD_METADATA(ELASTIC_OTEL_CFG_OPT_NAME_MAX_SEND_QUEUE_SIZE, OptionMetadata::type::bytes, false),
BUILD_METADATA(ELASTIC_OTEL_CFG_OPT_NAME_DISABLE_ASYNC_TRANSPORT, OptionMetadata::type::boolean, false)};
BUILD_METADATA(ELASTIC_OTEL_CFG_OPT_NAME_ASYNC_TRANSPORT, OptionMetadata::type::boolean, false)};
// clang-format on
};

Expand Down
4 changes: 2 additions & 2 deletions prod/native/libcommon/code/ConfigurationSnapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#define ELASTIC_OTEL_CFG_OPT_NAME_VERIFY_SERVER_CERT verify_server_cert
#define ELASTIC_OTEL_CFG_OPT_NAME_DEBUG_DIAGNOSTICS_FILE debug_diagnostic_file
#define ELASTIC_OTEL_CFG_OPT_NAME_MAX_SEND_QUEUE_SIZE max_send_queue_size
#define ELASTIC_OTEL_CFG_OPT_NAME_DISABLE_ASYNC_TRANSPORT disable_async_transport
#define ELASTIC_OTEL_CFG_OPT_NAME_ASYNC_TRANSPORT async_transport

namespace elasticapm::php {

Expand All @@ -51,7 +51,7 @@ struct ConfigurationSnapshot {
std::string ELASTIC_OTEL_CFG_OPT_NAME_DEBUG_DIAGNOSTICS_FILE;
bool ELASTIC_OTEL_CFG_OPT_NAME_VERIFY_SERVER_CERT = true;
std::size_t ELASTIC_OTEL_CFG_OPT_NAME_MAX_SEND_QUEUE_SIZE = 2 * 1024 * 1204;
bool ELASTIC_OTEL_CFG_OPT_NAME_DISABLE_ASYNC_TRANSPORT = false;
bool ELASTIC_OTEL_CFG_OPT_NAME_ASYNC_TRANSPORT = true;

uint64_t revision = 0;
};
Expand Down
11 changes: 6 additions & 5 deletions prod/native/libcommon/code/transport/CurlSender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace elasticapm::php::transport {

static int CurlDebugFunc(CURL *handle, curl_infotype type, char *data, size_t size, void *logger) {
auto &log = *static_cast<std::shared_ptr<LoggerInterface> *>(logger);
if (log->doesMeetsLevelCondition(LogLevel::logLevel_info) && type < 3) {
if (logger && log->doesMeetsLevelCondition(LogLevel::logLevel_trace) && type < 3) {
char prefix = type == CURLINFO_TEXT ? '*' : (type == CURLINFO_HEADER_IN ? '<' : '>');
log->printf(LogLevel::logLevel_trace, "CurlSender %c %.*s", prefix, size - 1, data);
}
Expand All @@ -50,10 +50,11 @@ CurlSender::CurlSender(std::shared_ptr<LoggerInterface> logger, std::chrono::mil
curl_easy_setopt(handle_, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(handle_, CURLOPT_FORBID_REUSE, 0L);

curl_easy_setopt(handle_, CURLOPT_DEBUGFUNCTION, CurlDebugFunc);
curl_easy_setopt(handle_, CURLOPT_DEBUGDATA, &log_);

curl_easy_setopt(handle_, CURLOPT_VERBOSE, 1L);
if (log_ && log_->doesMeetsLevelCondition(LogLevel::logLevel_trace)) {
curl_easy_setopt(handle_, CURLOPT_DEBUGFUNCTION, CurlDebugFunc);
curl_easy_setopt(handle_, CURLOPT_DEBUGDATA, &log_);
curl_easy_setopt(handle_, CURLOPT_VERBOSE, 1L);
}
}

int16_t CurlSender::sendPayload(std::string const &endpointUrl, struct curl_slist *headers, std::vector<std::byte> const &payload) const {
Expand Down
20 changes: 10 additions & 10 deletions prod/native/libcommon/code/transport/HttpTransportAsync.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ class HttpTransportAsync : public ForkableInterface, public boost::noncopyable {
}

void initializeConnection(std::string endpointUrl, size_t endpointHash, std::string contentType, HttpEndpoint::enpointHeaders_t const &endpointHeaders, std::chrono::milliseconds timeout, std::size_t maxRetries, std::chrono::milliseconds retryDelay) {
ELOG_TRACE(log_, "HttpTransportAsync::initializeConnection endpointUrl '%s' enpointHash: %X timeout: %zums retries: %zu retry delay: %zums", endpointUrl.c_str(), endpointHash, timeout.count(), maxRetries, retryDelay.count());
ELOG_DEBUG(log_, "HttpTransportAsync::initializeConnection endpointUrl '%s' enpointHash: %X timeout: %zums retries: %zu retry delay: %zums", endpointUrl.c_str(), endpointHash, timeout.count(), maxRetries, retryDelay.count());

try {
std::lock_guard<std::mutex> lock(mutex_);

HttpEndpoint endpoint(std::move(endpointUrl), std::move(contentType), endpointHeaders, maxRetries, retryDelay);

if (connections_.try_emplace(endpoint.getConnectionId(), log_, timeout, config_->get().verify_server_cert).second) { // CurlSender
ELOG_TRACE(log_, "HttpTransportAsync::initializeConnection endpointUrl '%s' enpointHash: %X initialize new connectionId: %X", endpoint.getEndpoint().c_str(), endpointHash, endpoint.getConnectionId());
ELOG_DEBUG(log_, "HttpTransportAsync::initializeConnection endpointUrl '%s' enpointHash: %X initialize new connectionId: %X", endpoint.getEndpoint().c_str(), endpointHash, endpoint.getConnectionId());
}
endpoints_.emplace(std::make_pair(endpointHash, std::move(endpoint)));

Expand All @@ -160,7 +160,7 @@ class HttpTransportAsync : public ForkableInterface, public boost::noncopyable {
ELOG_TRACE(log_, "HttpTransportAsync::enqueue enpointHash: %X payload size: %zu, current queue size %zu usage %zu bytes", endpointHash, payload.size(), payloadsToSend_.size(), payloadsByteUsage_);

if (payloadsByteUsage_ + payload.size() > config_->get().max_send_queue_size) {
ELOG_TRACE(log_, "HttpTransportAsync::enqueue payloadsByteUsageLimit %zu reached. Payload will be dropped. enpointHash: %X payload size: %zu, current queue size %zu usage %zu bytes", config_->get().max_send_queue_size, endpointHash, payload.size(), payloadsToSend_.size(), payloadsByteUsage_);
ELOG_DEBUG(log_, "HttpTransportAsync::enqueue payloadsByteUsageLimit %zu reached. Payload will be dropped. enpointHash: %X payload size: %zu, current queue size %zu usage %zu bytes", config_->get().max_send_queue_size, endpointHash, payload.size(), payloadsToSend_.size(), payloadsByteUsage_);
return;
}

Expand All @@ -173,15 +173,15 @@ class HttpTransportAsync : public ForkableInterface, public boost::noncopyable {
void prefork() final {
shutdownThread();

ELOG_TRACE(log_, "HttpTransportAsync::prefork payloads queue size %zu", payloadsToSend_.size());
ELOG_DEBUG(log_, "HttpTransportAsync::prefork payloads queue size %zu", payloadsToSend_.size());
CurlCleanup();
}

void postfork([[maybe_unused]] bool child) final {
CurlInit();

if (child && !payloadsToSend_.empty()) {
ELOG_TRACE(log_, "HttpTransportAsync::postfork child emptying payloads queue. %zu will be sent from parent", payloadsToSend_.size());
ELOG_DEBUG(log_, "HttpTransportAsync::postfork child emptying payloads queue. %zu will be sent from parent", payloadsToSend_.size());
decltype(payloadsToSend_) q;
payloadsToSend_.swap(q);
}
Expand All @@ -193,14 +193,14 @@ class HttpTransportAsync : public ForkableInterface, public boost::noncopyable {
protected:
void startThread() {
if (!thread_) {
ELOG_TRACE(log_, "HttpTransportAsync startThread");
ELOG_DEBUG(log_, "HttpTransportAsync startThread");
thread_ = std::make_unique<std::thread>([this]() { asyncSender(); });
}
}

void shutdownThread() {
if (thread_) {
ELOG_TRACE(log_, "HttpTransportAsync shutdownThread");
ELOG_DEBUG(log_, "HttpTransportAsync shutdownThread");
}

{
Expand All @@ -216,7 +216,7 @@ class HttpTransportAsync : public ForkableInterface, public boost::noncopyable {
}

void asyncSender() {
ELOG_TRACE(log_, "HttpTransportAsync::asyncSender blocking signals and starting work");
ELOG_DEBUG(log_, "HttpTransportAsync::asyncSender blocking signals and starting work");

elasticapm::utils::blockApacheAndPHPSignals();

Expand Down Expand Up @@ -252,7 +252,7 @@ class HttpTransportAsync : public ForkableInterface, public boost::noncopyable {
continue;
}

ELOG_TRACE(log_, "HttpTransportAsync::send enpointHash: %X connectionId: %X payload size: %zu", endpointHash, endpoint->second.getConnectionId(), payload.size());
ELOG_DEBUG(log_, "HttpTransportAsync::send enpointHash: %X connectionId: %X payload size: %zu", endpointHash, endpoint->second.getConnectionId(), payload.size());

auto maxRetries = std::max(static_cast<std::size_t>(1), static_cast<std::size_t>(endpoint->second.getMaxRetries()));
auto retryDelay = endpoint->second.getRetryDelay();
Expand All @@ -265,7 +265,7 @@ class HttpTransportAsync : public ForkableInterface, public boost::noncopyable {
while (retry < maxRetries) {
auto responseCode = connection->second.sendPayload(endpoint->second.getEndpoint(), endpoint->second.getHeaders(), payload);

ELOG_DEBUG(log_, "HttpTransportAsync::send enpointHash: %X connectionId: %X payload size: %zu responseCode %d", endpointHash, endpoint->second.getConnectionId(), payload.size(), static_cast<int>(responseCode));
ELOG_TRACE(log_, "HttpTransportAsync::send enpointHash: %X connectionId: %X payload size: %zu responseCode %d", endpointHash, endpoint->second.getConnectionId(), payload.size(), static_cast<int>(responseCode));

if (responseCode >= 200 && responseCode < 300) {
break;
Expand Down
5 changes: 3 additions & 2 deletions prod/php/ElasticOTel/PhpPartFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,12 @@ private static function registerAutoloader(): void

private static function registerAsyncTransportFactory(): void
{
if (elastic_otel_get_config_option_by_name('disable_async_transport') === true) {
BootstrapStageLogger::logDebug('TRANSPORT_DISABLED', __FILE__, __LINE__, __CLASS__, __FUNCTION__);
if (elastic_otel_get_config_option_by_name('async_transport') === false) {
BootstrapStageLogger::logDebug('ELASTIC_OTEL_ASYNC_TRANSPORT set to false', __FILE__, __LINE__, __CLASS__, __FUNCTION__);
return;
}

// TODO remove after autoloader implementation
require __DIR__ . DIRECTORY_SEPARATOR . 'HttpTransport' . DIRECTORY_SEPARATOR . 'ElasticHttpTransport.php';
require __DIR__ . DIRECTORY_SEPARATOR . 'HttpTransport' . DIRECTORY_SEPARATOR . 'ElasticHttpTransportFactory.php';
\OpenTelemetry\SDK\Registry::registerTransportFactory('http', \Elastic\Otel\HttpTransport\ElasticHttpTransportFactory::class, true);
Expand Down

0 comments on commit ae08979

Please sign in to comment.