Skip to content

Commit b1eb30b

Browse files
authored
Exposing artificial hook function if scoping is enabled (#49)
* Exposing artificial hook function if scoping is enabled * Added option to disable emulated opentelemetry extension
1 parent f823b2b commit b1eb30b

File tree

7 files changed

+30
-10
lines changed

7 files changed

+30
-10
lines changed

docs/reference/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ opentelemetry_distro.enabled=true
5757
| Option | Default | Accepted values | Description |
5858
| --- | --- | --- | --- |
5959
| `OTEL_PHP_ENABLED` | `true` | `true` or `false` | Enables automatic bootstrap |
60+
| `OTEL_PHP_OPENTELEMETRY_EXTENSION_EMULATION_ENABLED` | `true` | `true` or `false` | Enables registration of an emulated `opentelemetry` extension, allowing auto-instrumentations to work without `opentelemetry.so` |
6061
| `OTEL_PHP_NATIVE_OTLP_SERIALIZER_ENABLED` | `true` | `true` or `false` | Enables native OTLP protobuf serializer |
6162

6263
### Asynchronous data sending

prod/native/extension/code/ModuleEntry.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,14 @@ PHP_MINIT_FUNCTION(opentelemetry_distro) {
181181

182182
opentelemetry::php::moduleInit(type, module_number);
183183

184-
if (!zend_register_internal_module(&opentelemetry_distro_fake)) {
185-
ELOGF_WARNING(OTEL_G(globals)->logger_, MODULE, "Unable to create artificial OpenTelemetry extension. There might be stability issues.");
184+
if (OTEL_G(globals)->config_->get().opentelemetry_extension_emulation_enabled) {
185+
if (OTEL_G(globals)->config_->get().debug_scoper_enabled) {
186+
opentelemetry_distro_fake.functions = opentelemetry::php::module_functions::opentelemetry_distro_fake_functions;
187+
}
188+
189+
if (!zend_register_internal_module(&opentelemetry_distro_fake)) {
190+
ELOGF_WARNING(OTEL_G(globals)->logger_, MODULE, "Unable to create artificial OpenTelemetry extension - opentelemetry.so extension should be disabled. There might be stability issues.");
191+
}
186192
}
187193

188194
return SUCCESS;

prod/native/extension/code/ModuleFunctions.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,13 @@ const zend_function_entry opentelemetry_distro_functions[] = {
385385

386386
PHP_FE_END
387387
};
388+
389+
const zend_function_entry opentelemetry_distro_fake_functions[] = {
390+
ZEND_NS_FE( "OpenTelemetry\\Instrumentation", hook, hook_arginfo)
391+
392+
PHP_FE_END
393+
};
394+
388395
// clang-format on
389396

390397
} // namespace opentelemetry::php::module_functions

prod/native/extension/code/ModuleFunctions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
namespace opentelemetry::php::module_functions {
66

77
extern const zend_function_entry opentelemetry_distro_functions[];
8-
8+
extern const zend_function_entry opentelemetry_distro_fake_functions[];
99
}

prod/native/libcommon/code/ConfigurationManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ class ConfigurationManager {
109109
BUILD_OTEL_PHP_OPTION_METADATA(OTEL_PHP_INFERRED_SPANS_SAMPLING_INTERVAL, OptionMetadata::type::duration, false),
110110
BUILD_OTEL_PHP_OPTION_METADATA(OTEL_PHP_INFERRED_SPANS_MIN_DURATION, OptionMetadata::type::duration, false),
111111
BUILD_OTEL_PHP_OPTION_METADATA(OTEL_PHP_DEPENDENCY_AUTOLOADER_GUARD_ENABLED, OptionMetadata::type::boolean, false),
112+
BUILD_OTEL_PHP_OPTION_METADATA(OTEL_PHP_OPENTELEMETRY_EXTENSION_EMULATION_ENABLED, OptionMetadata::type::boolean, false),
112113
BUILD_OTEL_PHP_OPTION_METADATA(OTEL_PHP_NATIVE_OTLP_SERIALIZER_ENABLED, OptionMetadata::type::boolean, false),
113114
BUILD_OTEL_PHP_OPTION_METADATA(OTEL_PHP_OPAMP_HEADERS, OptionMetadata::type::string, false),
114115
BUILD_OTEL_PHP_OPTION_METADATA(OTEL_PHP_OPAMP_ENDPOINT, OptionMetadata::type::string, false),

prod/native/libcommon/code/ConfigurationSnapshot.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
#define OTEL_PHP_DEPENDENCY_AUTOLOADER_GUARD_ENABLED dependency_autoloader_guard_enabled
3333

34+
#define OTEL_PHP_OPENTELEMETRY_EXTENSION_EMULATION_ENABLED opentelemetry_extension_emulation_enabled
35+
3436
#define OTEL_PHP_NATIVE_OTLP_SERIALIZER_ENABLED native_otlp_serializer_enabled
3537

3638
#define OTEL_PHP_OPAMP_HEADERS opamp_headers
@@ -107,6 +109,7 @@ struct ConfigurationSnapshot {
107109
std::chrono::milliseconds OTEL_PHP_INFERRED_SPANS_MIN_DURATION = std::chrono::milliseconds(0);
108110

109111
bool OTEL_PHP_DEPENDENCY_AUTOLOADER_GUARD_ENABLED = false;
112+
bool OTEL_PHP_OPENTELEMETRY_EXTENSION_EMULATION_ENABLED = true;
110113
bool OTEL_PHP_NATIVE_OTLP_SERIALIZER_ENABLED = true;
111114

112115
std::string OTEL_PHP_OPAMP_HEADERS;

prod/php/OpenTelemetry/Instrumentation/hook.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@
3030
*
3131
* @see https://github.com/open-telemetry/opentelemetry-php-instrumentation
3232
*/
33-
function hook(
34-
?string $class,
35-
string $function,
36-
?Closure $pre = null,
37-
?Closure $post = null,
38-
): bool {
39-
return InstrumentationBridge::singletonInstance()->hook($class, $function, $pre, $post);
33+
if (!function_exists(__NAMESPACE__ . '\\hook')) {
34+
function hook(
35+
?string $class,
36+
string $function,
37+
?Closure $pre = null,
38+
?Closure $post = null,
39+
): bool {
40+
return InstrumentationBridge::singletonInstance()->hook($class, $function, $pre, $post);
41+
}
4042
}

0 commit comments

Comments
 (0)