Skip to content

Commit

Permalink
Avoid reporting filesytem integration on telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
estringana committed Nov 26, 2024
1 parent 2e94092 commit f8a0af2
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 4 deletions.
9 changes: 7 additions & 2 deletions ext/integrations/integrations.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include <hook/hook.h>
#include <sandbox/sandbox.h>
#undef INTEGRATION
#undef INTEGRATION_CUSTOM_ENABLED

static bool is_filesystem_enabled() { return get_DD_TRACE_FILESYSTEM_ENABLED() && get_DD_APPSEC_RASP_ENABLED(); }

#define DDTRACE_DEFERRED_INTEGRATION_LOADER(class, fname, integration_name) \
dd_hook_method_and_unhook_on_first_call((zai_str)ZAI_STRL(class), (zai_str)ZAI_STRL(fname), \
Expand All @@ -24,13 +27,15 @@
dd_set_up_deferred_loading_by_method(name, (zai_str)ZAI_STR_EMPTY, (zai_str)ZAI_STRL(fname), \
(zai_str)ZAI_STRL(integration), false)

#define INTEGRATION(id, lcname, ...) \
#define INTEGRATION(id, lcname, ...) INTEGRATION_AUX(id, lcname, get_DD_TRACE_##id##_ENABLED)
#define INTEGRATION_CUSTOM_ENABLED(id, lcname, is_enabled_func, ...) INTEGRATION_AUX(id, lcname, is_enabled_func)
#define INTEGRATION_AUX(id, lcname, is_enabled_func) \
{ \
.name = DDTRACE_INTEGRATION_##id, \
.name_ucase = #id, \
.name_lcase = (lcname), \
.name_len = sizeof(lcname) - 1, \
.is_enabled = get_DD_TRACE_##id##_ENABLED, \
.is_enabled = is_enabled_func, \
.is_analytics_enabled = get_DD_TRACE_##id##_ANALYTICS_ENABLED, \
.get_sample_rate = get_DD_TRACE_##id##_ANALYTICS_SAMPLE_RATE, \
.aux = {0}, \
Expand Down
3 changes: 2 additions & 1 deletion ext/integrations/integrations.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
INTEGRATION(DRUPAL, "drupal") \
INTEGRATION(ELASTICSEARCH, "elasticsearch") \
INTEGRATION(ELOQUENT, "eloquent") \
INTEGRATION(FILESYSTEM, "filesystem") \
INTEGRATION_CUSTOM_ENABLED(FILESYSTEM, "filesystem", is_filesystem_enabled) \
INTEGRATION(FRANKENPHP, "frankenphp") \
INTEGRATION(GOOGLESPANNER, "googlespanner") \
INTEGRATION(GUZZLE, "guzzle") \
Expand Down Expand Up @@ -54,6 +54,7 @@
INTEGRATION(ZENDFRAMEWORK, "zendframework")

#define INTEGRATION(id, ...) DDTRACE_INTEGRATION_##id,
#define INTEGRATION_CUSTOM_ENABLED(id, ...) INTEGRATION(id)
typedef enum { DD_INTEGRATIONS } ddtrace_integration_name;
#undef INTEGRATION

Expand Down
15 changes: 14 additions & 1 deletion tests/ext/telemetry/integration.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ PUBLIC STATIC METHOD
test_access hook
array(1) {
["integrations"]=>
array(2) {
array(3) {
[0]=>
array(5) {
["name"]=>
Expand All @@ -84,6 +84,19 @@ array(1) {
NULL
}
[1]=>
array(5) {
["name"]=>
string(10) "filesystem"
["enabled"]=>
bool(false)
["version"]=>
string(0) ""
["compatible"]=>
NULL
["auto_enabled"]=>
NULL
}
[2]=>
array(5) {
["name"]=>
string(4) "logs"
Expand Down
105 changes: 105 additions & 0 deletions tests/ext/telemetry/integration_filesystem.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
--TEST--
Filesystem integration depends on RASP. If RASP enabled, integration is enabled
--SKIPIF--
<?php
if (getenv('PHP_PEAR_RUNTESTS') === '1') die("skip: pecl run-tests does not support {PWD}");
if (PHP_OS === "WINNT" && PHP_VERSION_ID < 70400) die("skip: Windows on PHP 7.2 and 7.3 have permission issues with synchronous access to telemetry");
if (getenv('USE_ZEND_ALLOC') === '0' && !getenv("SKIP_ASAN")) die('skip timing sensitive test - valgrind is too slow');
require __DIR__ . '/../includes/clear_skipif_telemetry.inc'
?>
--ENV--
DD_TRACE_GENERATE_ROOT_SPAN=0
_DD_LOAD_TEST_INTEGRATIONS=1
DD_INSTRUMENTATION_TELEMETRY_ENABLED=1
DD_APPSEC_RASP_ENABLED=1
--INI--
datadog.trace.agent_url="file://{PWD}/integration-telemetry.out"
--FILE--
<?php

namespace DDTrace\Test
{
class TestSandboxedIntegration implements \DDTrace\Integration
{
function init(): int
{
dd_trace_method("Test", "public_static_method", function() {
echo "test_access hook" . PHP_EOL;
});
return self::LOADED;
}
}
}

namespace
{
class Test
{
public static function public_static_method()
{
echo "PUBLIC STATIC METHOD\n";
}
}

Test::public_static_method();

dd_trace_internal_fn("finalize_telemetry");

for ($i = 0; $i < 100; ++$i) {
usleep(100000);
if (file_exists(__DIR__ . '/integration-telemetry.out')) {
foreach (file(__DIR__ . '/integration-telemetry.out') as $l) {
if ($l) {
$json = json_decode($l, true);
$batch = $json["request_type"] == "message-batch" ? $json["payload"] : [$json];
foreach ($batch as $json) {
if ($json["request_type"] == "app-integrations-change") {
var_dump($json["payload"]);
break 3;
}
}
}
}
}
}
}

?>
--EXPECT--
PUBLIC STATIC METHOD
test_access hook
array(1) {
["integrations"]=>
array(2) {
[0]=>
array(5) {
["name"]=>
string(37) "ddtrace\test\testsandboxedintegration"
["enabled"]=>
bool(true)
["version"]=>
NULL
["compatible"]=>
NULL
["auto_enabled"]=>
NULL
}
[1]=>
array(5) {
["name"]=>
string(4) "logs"
["enabled"]=>
bool(false)
["version"]=>
string(0) ""
["compatible"]=>
NULL
["auto_enabled"]=>
NULL
}
}
}
--CLEAN--
<?php

@unlink(__DIR__ . '/integration-telemetry.out');

0 comments on commit f8a0af2

Please sign in to comment.