Skip to content

Commit

Permalink
Track peak memory usage in root span metrics
Browse files Browse the repository at this point in the history
Fixes #2832.

Signed-off-by: Bob Weinand <bob.weinand@datadoghq.com>
  • Loading branch information
bwoebi committed Sep 6, 2024
1 parent 735db41 commit 56a99ab
Show file tree
Hide file tree
Showing 36 changed files with 173 additions and 34 deletions.
2 changes: 2 additions & 0 deletions appsec/tests/extension/client_init_record_span_tags.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,6 @@ Array
[_dd.agent_psr] => 1
[_sampling_priority_v1] => 1
[php.compilation.total_time_ms] => %f
[php.memory.peak_usage_bytes] => %f
[php.memory.peak_real_usage_bytes] => %f
)
2 changes: 2 additions & 0 deletions appsec/tests/extension/rinit_record_span_tags.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,6 @@ Array
[_dd.agent_psr] => 1
[_sampling_priority_v1] => 1
[php.compilation.total_time_ms] => %f
[php.memory.peak_usage_bytes] => %f
[php.memory.peak_real_usage_bytes] => %f
)
6 changes: 5 additions & 1 deletion appsec/tests/extension/root_span_add_tag.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ array(1) {
string(16) "%s"
}
["metrics"]=>
array(4) {
array(6) {
[%s"]=>
float(%d)
["_dd.agent_psr"]=>
Expand All @@ -65,6 +65,10 @@ array(1) {
float(1)
["php.compilation.total_time_ms"]=>
float(%s)
["php.memory.peak_usage_bytes"]=>
float(%f)
["php.memory.peak_real_usage_bytes"]=>
float(%f)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,6 @@ Array
[_dd.agent_psr] => 1
[_sampling_priority_v1] => 1
[php.compilation.total_time_ms] => %s
[php.memory.peak_usage_bytes] => %f
[php.memory.peak_real_usage_bytes] => %f
)
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ services:
- DD_DISABLE_ERROR_RESPONSES=true
- SNAPSHOTS_DIR=/snapshots
- SNAPSHOT_CI=0
- SNAPSHOT_REMOVED_ATTRS=start,duration,metrics.php.compilation.total_time_ms,metrics.process_id
- SNAPSHOT_REMOVED_ATTRS=start,duration,metrics.php.compilation.total_time_ms,metrics.php.memory.peak_usage_bytes,metrics.php.memory.peak_real_usage_bytes,metrics.process_id
- ENABLED_CHECKS=trace_stall,trace_peer_service,trace_dd_service


Expand Down
1 change: 1 addition & 0 deletions ext/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ enum ddtrace_sampling_rules_format {
CONFIG(BOOL, DD_TRACE_AUTO_FLUSH_ENABLED, "false") \
CONFIG(BOOL, DD_TRACE_CLI_ENABLED, "false") \
CONFIG(BOOL, DD_TRACE_MEASURE_COMPILE_TIME, "true") \
CONFIG(BOOL, DD_TRACE_MEASURE_PEAK_MEMORY_USAGE, "true") \
CONFIG(BOOL, DD_TRACE_DEBUG, "false", .ini_change = ddtrace_alter_dd_trace_debug) \
CONFIG(BOOL, DD_TRACE_ENABLED, "true", .ini_change = ddtrace_alter_dd_trace_disabled_config, \
.env_config_fallback = ddtrace_conf_otel_traces_exporter) \
Expand Down
10 changes: 8 additions & 2 deletions ext/serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1847,8 +1847,14 @@ void ddtrace_serialize_span_to_array(ddtrace_span_data *span, zval *array) {
}
}

if (ddtrace_span_is_entrypoint_root(span) && get_DD_TRACE_MEASURE_COMPILE_TIME()) {
add_assoc_double(&metrics_zv, "php.compilation.total_time_ms", ddtrace_compile_time_get() / 1000.);
if (ddtrace_span_is_entrypoint_root(span)) {
if (get_DD_TRACE_MEASURE_COMPILE_TIME()) {
add_assoc_double(&metrics_zv, "php.compilation.total_time_ms", ddtrace_compile_time_get() / 1000.);
}
if (get_DD_TRACE_MEASURE_PEAK_MEMORY_USAGE()) {
add_assoc_double(&metrics_zv, "php.memory.peak_usage_bytes", zend_memory_peak_usage(false));
add_assoc_double(&metrics_zv, "php.memory.peak_real_usage_bytes", zend_memory_peak_usage(true));
}
}

LOGEV(SPAN, {
Expand Down
2 changes: 1 addition & 1 deletion github-actions-helpers/Build.Github.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ string CleanValue(string value)
char[] charsToTrim = { ' ', ',' };
string cleaned = value.TrimStart('-', '+').Trim(charsToTrim);

string[] keysToReplace = { "start", "duration", "php.compilation.total_time_ms", "process_id" };
string[] keysToReplace = { "start", "duration", "php.compilation.total_time_ms", "metrics.php.memory.peak_usage_bytes", "metrics.php.memory.peak_real_usage_bytes", "process_id" };
foreach (var key in keysToReplace)
{
if (cleaned.Contains(key))
Expand Down
8 changes: 4 additions & 4 deletions tests/Common/SnapshotTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private function waitForTraces(string $token, int $numExpectedTraces = 0)
*/
private function stopAndCompareSnapshotSession(
string $token,
array $fieldsToIgnore = ['metrics.php.compilation.total_time_ms', 'meta.error.stack', 'meta._dd.p.tid'],
array $fieldsToIgnore = ['metrics.php.compilation.total_time_ms', 'metrics.php.memory.peak_usage_bytes', 'metrics.php.memory.peak_real_usage_bytes', 'meta.error.stack', 'meta._dd.p.tid'],
int $numExpectedTraces = 1,
bool $snapshotMetrics = false,
array $fieldsToIgnoreMetrics = ['openai.request.duration'],
Expand Down Expand Up @@ -290,7 +290,7 @@ private function filterMetrics($metrics, $fieldsToIgnore)

public function tracesFromWebRequestSnapshot(
$fn,
$fieldsToIgnore = ['metrics.php.compilation.total_time_ms', 'meta.error.stack', 'meta._dd.p.tid', 'start', 'duration'],
$fieldsToIgnore = ['metrics.php.compilation.total_time_ms', 'metrics.php.memory.peak_usage_bytes', 'metrics.php.memory.peak_real_usage_bytes', 'meta.error.stack', 'meta._dd.p.tid', 'start', 'duration'],
$numExpectedTraces = 1,
$tracer = null
) {
Expand All @@ -317,7 +317,7 @@ public function tracesFromWebRequestSnapshot(

public function isolateTracerSnapshot(
$fn,
$fieldsToIgnore = ['metrics.php.compilation.total_time_ms', 'meta.error.stack', 'meta._dd.p.tid'],
$fieldsToIgnore = ['metrics.php.compilation.total_time_ms', 'metrics.php.memory.peak_usage_bytes', 'metrics.php.memory.peak_real_usage_bytes', 'meta.error.stack', 'meta._dd.p.tid'],
$numExpectedTraces = 1,
$tracer = null,
$config = [],
Expand Down Expand Up @@ -370,7 +370,7 @@ public function isolateTracerSnapshot(

public function snapshotFromTraces(
$traces,
$fieldsToIgnore = ['metrics.php.compilation.total_time_ms', 'meta.error.stack', 'meta._dd.p.tid'],
$fieldsToIgnore = ['metrics.php.compilation.total_time_ms', 'metrics.php.memory.peak_usage_bytes', 'metrics.php.memory.peak_real_usage_bytes', 'meta.error.stack', 'meta._dd.p.tid'],
$tokenSubstitute = null,
$ignoreSampledAway = false
) {
Expand Down
8 changes: 8 additions & 0 deletions tests/Common/SpanChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public static function dumpSpansGraph(array $spansGraph, int $indent = 0)
}
if (isset($span['metrics'])) {
unset($span['metrics']['php.compilation.total_time_ms']);
unset($span['metrics']['php.memory.peak_usage_bytes']);
unset($span['metrics']['php.memory.peak_real_usage_bytes']);
unset($span['metrics']['process_id']);
foreach ($span['metrics'] as $k => $v) {
$out .= str_repeat(' ', $indent) . ' ' . $k . ' => ' . $v . "\n";
Expand Down Expand Up @@ -524,6 +526,12 @@ function ($key) use ($pattern) {
if (!isset($metrics['php.compilation.total_time_ms'])) {
unset($spanMetrics['php.compilation.total_time_ms']);
}
if (!isset($metrics['php.memory.peak_usage_bytes'])) {
unset($spanMetrics['php.memory.peak_usage_bytes']);
}
if (!isset($metrics['php.memory.peak_real_usage_bytes'])) {
unset($spanMetrics['php.memory.peak_real_usage_bytes']);
}
if (isset($metrics['process_id'])) {
unset($metrics['process_id']);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ protected static function getEnvs()
{
return array_merge(parent::getEnvs(), [
'DD_TRACE_MEASURE_COMPILE_TIME' => '0',
'DD_TRACE_MEASURE_PEAK_MEMORY_USAGE' => '0',
]);
}

Expand All @@ -26,12 +27,14 @@ protected function ddSetUp()
* For the compile-time metrics specifically, this goofs things up, so let's disable.
*/
self::putenv('DD_TRACE_MEASURE_COMPILE_TIME=0');
self::putenv('DD_TRACE_MEASURE_PEAK_MEMORY_USAGE=0');
\dd_trace_internal_fn('ddtrace_reload_config');
}

protected function ddTearDown()
{
self::putenv('DD_TRACE_MEASURE_COMPILE_TIME');
self::putenv('DD_TRACE_MEASURE_PEAK_MEMORY_USAGE');
dd_trace_internal_fn('ddtrace_reload_config');
parent::ddTearDown();
}
Expand All @@ -44,5 +47,7 @@ public function testScenario()
});

self::assertFalse(isset($traces[0][0]['metrics']['php.compilation.total_time_ms']));
self::assertFalse(isset($traces[0][0]['metrics']['php.memory.peak_usage_bytes']));
self::assertFalse(isset($traces[0][0]['metrics']['php.memory.peak_real_usage_bytes']));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ protected static function getEnvs()
{
return array_merge(parent::getEnvs(), [
'DD_TRACE_MEASURE_COMPILE_TIME' => '1',
'DD_TRACE_MEASURE_PEAK_MEMORY_USAGE' => '1',
]);
}

Expand All @@ -26,12 +27,14 @@ protected function ddSetUp()
* For the compile-time metrics specifically, this goofs things up, so let's disable.
*/
self::putenv('DD_TRACE_MEASURE_COMPILE_TIME=0');
self::putenv('DD_TRACE_MEASURE_PEAK_MEMORY_USAGE=0');
\dd_trace_internal_fn('ddtrace_reload_config');
}

protected function ddTearDown()
{
self::putenv('DD_TRACE_MEASURE_COMPILE_TIME');
self::putenv('DD_TRACE_MEASURE_PEAK_MEMORY_USAGE');
dd_trace_internal_fn('ddtrace_reload_config');
parent::ddTearDown();
}
Expand All @@ -44,5 +47,7 @@ public function testScenario()
});

self::assertTrue(isset($traces[0][0]['metrics']['php.compilation.total_time_ms']));
self::assertTrue(isset($traces[0][0]['metrics']['php.memory.peak_usage_bytes']));
self::assertTrue(isset($traces[0][0]['metrics']['php.memory.peak_real_usage_bytes']));
}
}
2 changes: 2 additions & 0 deletions tests/Integrations/Guzzle/V6/GuzzleIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,8 @@ public function testMultiExec()
}, [
'start',
'metrics.php.compilation.total_time_ms',
'metrics.php.memory.peak_usage_bytes',
'metrics.php.memory.peak_real_usage_bytes',
'meta.error.stack',
'meta._dd.p.tid',
'meta.curl.appconnect_time_us',
Expand Down
2 changes: 2 additions & 0 deletions tests/Integrations/Guzzle/V7/GuzzleIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public function testMultiExec()
}, [
'start',
'metrics.php.compilation.total_time_ms',
'metrics.php.memory.peak_usage_bytes',
'metrics.php.memory.peak_real_usage_bytes',
'meta.error.stack',
'meta._dd.p.tid',
'meta.curl.appconnect_time_us',
Expand Down
2 changes: 2 additions & 0 deletions tests/Integrations/Symfony/V4_4/MessengerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class MessengerTest extends WebFrameworkTestCase
{
const FIELDS_TO_IGNORE = [
'metrics.php.compilation.total_time_ms',
'metrics.php.memory.peak_usage_bytes',
'metrics.php.memory.peak_real_usage_bytes',
'meta.error.stack',
'meta._dd..tid',
'meta.messaging.message_id',
Expand Down
2 changes: 2 additions & 0 deletions tests/Integrations/Symfony/V5_2/MessengerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class MessengerTest extends WebFrameworkTestCase
{
const FIELDS_TO_IGNORE = [
'metrics.php.compilation.total_time_ms',
'metrics.php.memory.peak_usage_bytes',
'metrics.php.memory.peak_real_usage_bytes',
'meta.error.stack',
'meta._dd..tid',
'meta.messaging.message_id',
Expand Down
2 changes: 2 additions & 0 deletions tests/Integrations/Symfony/V6_2/MessengerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class MessengerTest extends WebFrameworkTestCase
{
const FIELDS_TO_IGNORE = [
'metrics.php.compilation.total_time_ms',
'metrics.php.memory.peak_usage_bytes',
'metrics.php.memory.peak_real_usage_bytes',
'meta.error.stack',
'meta._dd..tid',
'meta.messaging.message_id',
Expand Down
2 changes: 2 additions & 0 deletions tests/Integrations/Symfony/V7_0/MessengerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class MessengerTest extends WebFrameworkTestCase
{
const FIELDS_TO_IGNORE = [
'metrics.php.compilation.total_time_ms',
'metrics.php.memory.peak_usage_bytes',
'metrics.php.memory.peak_real_usage_bytes',
'meta.error.stack',
'meta._dd.p.tid',
'meta.messaging.message_id',
Expand Down
2 changes: 1 addition & 1 deletion tests/ext/close_spans_until.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ int(2)
[ddtrace] [span] Switching to different SpanStack: %d
int(1)
int(0)
[ddtrace] [span] Encoding span %d: trace_id=%s, name='close_spans_until.php', service='close_spans_until.php', resource: 'close_spans_until.php', type 'cli' with tags: runtime-id='%s', _dd.p.dm='-0', _dd.p.tid='%s'; and metrics: process_id='%d', _dd.agent_psr='1', _sampling_priority_v1='1', php.compilation.total_time_ms='%f'
[ddtrace] [span] Encoding span %d: trace_id=%s, name='close_spans_until.php', service='close_spans_until.php', resource: 'close_spans_until.php', type 'cli' with tags: runtime-id='%s', _dd.p.dm='-0', _dd.p.tid='%s'; and metrics: process_id='%d', _dd.agent_psr='1', _sampling_priority_v1='1', php.compilation.total_time_ms='%f', php.memory.peak_usage_bytes='%f', php.memory.peak_real_usage_bytes='%f'
[ddtrace] [span] Encoding span %d: trace_id=%s, name='traced', service='close_spans_until.php', resource: 'traced', type 'cli' with tags: -; and metrics: -
[ddtrace] [span] Encoding span %d: trace_id=%s, name='', service='close_spans_until.php', resource: '', type 'cli' with tags: -; and metrics: -
[ddtrace] [span] Encoding span %d: trace_id=%s, name='', service='close_spans_until.php', resource: '', type 'cli' with tags: -; and metrics: -
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ array(1) {
string(16) "%s"
}
["metrics"]=>
array(4) {
array(6) {
["process_id"]=>
float(%f)
["_dd.agent_psr"]=>
Expand All @@ -56,6 +56,10 @@ array(1) {
float(1)
["php.compilation.total_time_ms"]=>
float(%f)
["php.memory.peak_usage_bytes"]=>
float(%f)
["php.memory.peak_real_usage_bytes"]=>
float(%f)
}
}
}
6 changes: 5 additions & 1 deletion tests/ext/distributed_tracing/distributed_trace_inherit.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,17 @@ array(2) {
string(7) "datadog"
}
["metrics"]=>
array(3) {
array(5) {
["process_id"]=>
float(%f)
["_sampling_priority_v1"]=>
float(3)
["php.compilation.total_time_ms"]=>
float(%f)
["php.memory.peak_usage_bytes"]=>
float(%f)
["php.memory.peak_real_usage_bytes"]=>
float(%f)
}
}
[1]=>
Expand Down
6 changes: 5 additions & 1 deletion tests/ext/integrations/source_code/commit_sha_env_var.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ array(2) {
string(16) "%s"
}
["metrics"]=>
array(4) {
array(6) {
["process_id"]=>
float(%f)
["_dd.agent_psr"]=>
Expand All @@ -58,6 +58,10 @@ array(2) {
float(1)
["php.compilation.total_time_ms"]=>
float(%f)
["php.memory.peak_usage_bytes"]=>
float(%f)
["php.memory.peak_real_usage_bytes"]=>
float(%f)
}
}
[1]=>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ array(2) {
string(16) "%s"
}
["metrics"]=>
array(4) {
array(6) {
["process_id"]=>
float(%d)
["_dd.agent_psr"]=>
Expand All @@ -61,6 +61,10 @@ array(2) {
float(1)
["php.compilation.total_time_ms"]=>
float(%f)
["php.memory.peak_usage_bytes"]=>
float(%f)
["php.memory.peak_real_usage_bytes"]=>
float(%f)
}
}
[1]=>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ array(2) {
string(16) "%s"
}
["metrics"]=>
array(4) {
array(6) {
["process_id"]=>
float(%d)
["_dd.agent_psr"]=>
Expand All @@ -64,6 +64,10 @@ array(2) {
float(1)
["php.compilation.total_time_ms"]=>
float(%f)
["php.memory.peak_usage_bytes"]=>
float(%f)
["php.memory.peak_real_usage_bytes"]=>
float(%f)
}
}
[1]=>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ array(2) {
string(16) "%s"
}
["metrics"]=>
array(4) {
array(6) {
["process_id"]=>
float(%d)
["_dd.agent_psr"]=>
Expand All @@ -61,6 +61,10 @@ array(2) {
float(1)
["php.compilation.total_time_ms"]=>
float(%f)
["php.memory.peak_usage_bytes"]=>
float(%f)
["php.memory.peak_real_usage_bytes"]=>
float(%f)
}
}
[1]=>
Expand Down
Loading

0 comments on commit 56a99ab

Please sign in to comment.