Skip to content

Commit 56a99ab

Browse files
committed
Track peak memory usage in root span metrics
Fixes #2832. Signed-off-by: Bob Weinand <[email protected]>
1 parent 735db41 commit 56a99ab

36 files changed

Lines changed: 173 additions & 34 deletions

appsec/tests/extension/client_init_record_span_tags.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,6 @@ Array
109109
[_dd.agent_psr] => 1
110110
[_sampling_priority_v1] => 1
111111
[php.compilation.total_time_ms] => %f
112+
[php.memory.peak_usage_bytes] => %f
113+
[php.memory.peak_real_usage_bytes] => %f
112114
)

appsec/tests/extension/rinit_record_span_tags.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,6 @@ Array
102102
[_dd.agent_psr] => 1
103103
[_sampling_priority_v1] => 1
104104
[php.compilation.total_time_ms] => %f
105+
[php.memory.peak_usage_bytes] => %f
106+
[php.memory.peak_real_usage_bytes] => %f
105107
)

appsec/tests/extension/root_span_add_tag.phpt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ array(1) {
5656
string(16) "%s"
5757
}
5858
["metrics"]=>
59-
array(4) {
59+
array(6) {
6060
[%s"]=>
6161
float(%d)
6262
["_dd.agent_psr"]=>
@@ -65,6 +65,10 @@ array(1) {
6565
float(1)
6666
["php.compilation.total_time_ms"]=>
6767
float(%s)
68+
["php.memory.peak_usage_bytes"]=>
69+
float(%f)
70+
["php.memory.peak_real_usage_bytes"]=>
71+
float(%f)
6872
}
6973
}
7074
}

appsec/tests/extension/root_span_add_tag_with_intermediate_spans.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,6 @@ Array
8484
[_dd.agent_psr] => 1
8585
[_sampling_priority_v1] => 1
8686
[php.compilation.total_time_ms] => %s
87+
[php.memory.peak_usage_bytes] => %f
88+
[php.memory.peak_real_usage_bytes] => %f
8789
)

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ services:
224224
- DD_DISABLE_ERROR_RESPONSES=true
225225
- SNAPSHOTS_DIR=/snapshots
226226
- SNAPSHOT_CI=0
227-
- SNAPSHOT_REMOVED_ATTRS=start,duration,metrics.php.compilation.total_time_ms,metrics.process_id
227+
- 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
228228
- ENABLED_CHECKS=trace_stall,trace_peer_service,trace_dd_service
229229

230230

ext/configuration.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ enum ddtrace_sampling_rules_format {
115115
CONFIG(BOOL, DD_TRACE_AUTO_FLUSH_ENABLED, "false") \
116116
CONFIG(BOOL, DD_TRACE_CLI_ENABLED, "false") \
117117
CONFIG(BOOL, DD_TRACE_MEASURE_COMPILE_TIME, "true") \
118+
CONFIG(BOOL, DD_TRACE_MEASURE_PEAK_MEMORY_USAGE, "true") \
118119
CONFIG(BOOL, DD_TRACE_DEBUG, "false", .ini_change = ddtrace_alter_dd_trace_debug) \
119120
CONFIG(BOOL, DD_TRACE_ENABLED, "true", .ini_change = ddtrace_alter_dd_trace_disabled_config, \
120121
.env_config_fallback = ddtrace_conf_otel_traces_exporter) \

ext/serializer.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,8 +1847,14 @@ void ddtrace_serialize_span_to_array(ddtrace_span_data *span, zval *array) {
18471847
}
18481848
}
18491849

1850-
if (ddtrace_span_is_entrypoint_root(span) && get_DD_TRACE_MEASURE_COMPILE_TIME()) {
1851-
add_assoc_double(&metrics_zv, "php.compilation.total_time_ms", ddtrace_compile_time_get() / 1000.);
1850+
if (ddtrace_span_is_entrypoint_root(span)) {
1851+
if (get_DD_TRACE_MEASURE_COMPILE_TIME()) {
1852+
add_assoc_double(&metrics_zv, "php.compilation.total_time_ms", ddtrace_compile_time_get() / 1000.);
1853+
}
1854+
if (get_DD_TRACE_MEASURE_PEAK_MEMORY_USAGE()) {
1855+
add_assoc_double(&metrics_zv, "php.memory.peak_usage_bytes", zend_memory_peak_usage(false));
1856+
add_assoc_double(&metrics_zv, "php.memory.peak_real_usage_bytes", zend_memory_peak_usage(true));
1857+
}
18521858
}
18531859

18541860
LOGEV(SPAN, {

github-actions-helpers/Build.Github.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ string CleanValue(string value)
164164
char[] charsToTrim = { ' ', ',' };
165165
string cleaned = value.TrimStart('-', '+').Trim(charsToTrim);
166166

167-
string[] keysToReplace = { "start", "duration", "php.compilation.total_time_ms", "process_id" };
167+
string[] keysToReplace = { "start", "duration", "php.compilation.total_time_ms", "metrics.php.memory.peak_usage_bytes", "metrics.php.memory.peak_real_usage_bytes", "process_id" };
168168
foreach (var key in keysToReplace)
169169
{
170170
if (cleaned.Contains(key))

tests/Common/SnapshotTestTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private function waitForTraces(string $token, int $numExpectedTraces = 0)
123123
*/
124124
private function stopAndCompareSnapshotSession(
125125
string $token,
126-
array $fieldsToIgnore = ['metrics.php.compilation.total_time_ms', 'meta.error.stack', 'meta._dd.p.tid'],
126+
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'],
127127
int $numExpectedTraces = 1,
128128
bool $snapshotMetrics = false,
129129
array $fieldsToIgnoreMetrics = ['openai.request.duration'],
@@ -290,7 +290,7 @@ private function filterMetrics($metrics, $fieldsToIgnore)
290290

291291
public function tracesFromWebRequestSnapshot(
292292
$fn,
293-
$fieldsToIgnore = ['metrics.php.compilation.total_time_ms', 'meta.error.stack', 'meta._dd.p.tid', 'start', 'duration'],
293+
$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'],
294294
$numExpectedTraces = 1,
295295
$tracer = null
296296
) {
@@ -317,7 +317,7 @@ public function tracesFromWebRequestSnapshot(
317317

318318
public function isolateTracerSnapshot(
319319
$fn,
320-
$fieldsToIgnore = ['metrics.php.compilation.total_time_ms', 'meta.error.stack', 'meta._dd.p.tid'],
320+
$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'],
321321
$numExpectedTraces = 1,
322322
$tracer = null,
323323
$config = [],
@@ -370,7 +370,7 @@ public function isolateTracerSnapshot(
370370

371371
public function snapshotFromTraces(
372372
$traces,
373-
$fieldsToIgnore = ['metrics.php.compilation.total_time_ms', 'meta.error.stack', 'meta._dd.p.tid'],
373+
$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'],
374374
$tokenSubstitute = null,
375375
$ignoreSampledAway = false
376376
) {

tests/Common/SpanChecker.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public static function dumpSpansGraph(array $spansGraph, int $indent = 0)
8787
}
8888
if (isset($span['metrics'])) {
8989
unset($span['metrics']['php.compilation.total_time_ms']);
90+
unset($span['metrics']['php.memory.peak_usage_bytes']);
91+
unset($span['metrics']['php.memory.peak_real_usage_bytes']);
9092
unset($span['metrics']['process_id']);
9193
foreach ($span['metrics'] as $k => $v) {
9294
$out .= str_repeat(' ', $indent) . ' ' . $k . ' => ' . $v . "\n";
@@ -524,6 +526,12 @@ function ($key) use ($pattern) {
524526
if (!isset($metrics['php.compilation.total_time_ms'])) {
525527
unset($spanMetrics['php.compilation.total_time_ms']);
526528
}
529+
if (!isset($metrics['php.memory.peak_usage_bytes'])) {
530+
unset($spanMetrics['php.memory.peak_usage_bytes']);
531+
}
532+
if (!isset($metrics['php.memory.peak_real_usage_bytes'])) {
533+
unset($spanMetrics['php.memory.peak_real_usage_bytes']);
534+
}
527535
if (isset($metrics['process_id'])) {
528536
unset($metrics['process_id']);
529537
}

0 commit comments

Comments
 (0)