Skip to content

Commit 0496cc2

Browse files
authored
Add 'library_entrypoint.start' metric (#3009)
1 parent 55377fe commit 0496cc2

File tree

8 files changed

+66
-23
lines changed

8 files changed

+66
-23
lines changed

loader/dd_library_loader.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ static void ddloader_telemetryf(telemetry_reason reason, const char *error, cons
233233
case REASON_INCOMPATIBLE_RUNTIME:
234234
LOG(ERROR, "Aborting application instrumentation due to an incompatible runtime");
235235
break;
236+
case REASON_START:
236237
case REASON_COMPLETE:
237238
case REASON_ALREADY_LOADED:
238239
level = INFO;
@@ -268,6 +269,13 @@ static void ddloader_telemetryf(telemetry_reason reason, const char *error, cons
268269
char points_buf[256] = {0};
269270
char *points = points_buf;
270271
switch (reason) {
272+
case REASON_START:
273+
points =
274+
"\
275+
{\"name\": \"library_entrypoint.start\", \"tags\": []}\
276+
";
277+
break;
278+
271279
case REASON_ERROR:
272280
snprintf(points_buf, sizeof(points_buf), "\
273281
{\"name\": \"library_entrypoint.error\", \"tags\": [\"error_type:%s\"]}\
@@ -654,6 +662,8 @@ static int ddloader_api_no_check(int api_no) {
654662

655663
ddloader_configure();
656664

665+
TELEMETRY(REASON_START, NULL, "Starting injection");
666+
657667
switch (api_no) {
658668
case 220040412:
659669
runtime_version = "5.0";

loader/php_dd_library_loader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ void ddloader_logv(log_level level, const char *format, va_list va);
2121
void ddloader_logf(log_level level, const char *format, ...);
2222

2323
typedef enum {
24+
REASON_START,
2425
REASON_ERROR,
2526
REASON_EOL_RUNTIME,
2627
REASON_INCOMPATIBLE_RUNTIME,

loader/tests/functional/includes/assert.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
22

3-
function assertEquals($actual, $expected) {
3+
function assertEquals($actual, $expected, $msg = null) {
44
if ($actual !== $expected) {
5-
throw new \Exception(sprintf('Cannot assert "%s" equals "%s"', $actual, $expected));
5+
if (!$msg) {
6+
$msg = sprintf('Cannot assert "%s" equals "%s"', $actual, $expected);
7+
}
8+
throw new \Exception($msg);
69
}
710
}
811

@@ -68,3 +71,41 @@ function assertMatchesFormat($output, $wanted_re) {
6871
throw new \Exception("Output does not match the format\n".$output);
6972
}
7073
}
74+
75+
function assertTelemetry($telemetryLogPath, $format) {
76+
$lines = file($telemetryLogPath);
77+
assertEquals(count($lines), 2, "2 metrics were expected, but got ".count($lines));
78+
79+
$start = $lines[0];
80+
$end = $lines[1];
81+
82+
// The order is not guaranteed
83+
if (strpos($start, 'library_entrypoint.start') === false) {
84+
$start = $lines[1];
85+
$end = $lines[0];
86+
}
87+
88+
$payload = json_decode($start, true);
89+
$startFormat = <<<EOS
90+
{
91+
"metadata": {
92+
"runtime_name": "php",
93+
"runtime_version": "unknown",
94+
"language_name": "php",
95+
"language_version": "unknown",
96+
"tracer_version": "unknown",
97+
"pid": %d
98+
},
99+
"points": [
100+
{
101+
"name": "library_entrypoint.start",
102+
"tags": []
103+
}
104+
]
105+
}
106+
EOS;
107+
assertMatchesFormat(json_encode($payload, JSON_PRETTY_PRINT), $startFormat);
108+
109+
$payload = json_decode($end, true);
110+
assertMatchesFormat(json_encode($payload, JSON_PRETTY_PRINT), $format);
111+
}

loader/tests/functional/includes/autoload.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@
66
$trace = $ex->getTrace();
77
$file = $trace[0]['file'] ?: '';
88
$line = $trace[0]['line'] ?: '';
9+
$stackTrace = basename($file).':'.$line;
10+
11+
if (basename($file) === 'assert.php') {
12+
$file2 = $trace[1]['file'] ?: '';
13+
$line2 = $trace[1]['line'] ?: '';
14+
$stackTrace = basename($file2).':'.$line2.' > '.$stackTrace;
15+
}
916

1017
echo "------------------------------------\n";
1118
if ($file) {
12-
echo 'Error in test '.basename($file).':'.$line."\n";
19+
echo 'Error in test '.$stackTrace."\n";
1320
echo "------------------------------------\n";
1421
}
1522
echo $ex->getMessage()."\n";

loader/tests/functional/test_ddtrace_already_loaded.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727
// Let time to the fork to write the telemetry log
2828
usleep(5000);
2929

30-
$raw = file_get_contents($telemetryLogPath);
31-
$payload = json_decode($raw, true);
32-
3330
$format = <<<EOS
3431
{
3532
"metadata": {
@@ -50,5 +47,4 @@
5047
]
5148
}
5249
EOS;
53-
54-
assertMatchesFormat(json_encode($payload, JSON_PRETTY_PRINT), $format);
50+
assertTelemetry($telemetryLogPath, $format);

loader/tests/functional/test_telemetry_complete.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
// Let time to the fork to write the telemetry log
1414
usleep(5000);
1515

16-
$raw = file_get_contents($telemetryLogPath);
17-
$payload = json_decode($raw, true);
18-
1916
$format = <<<EOS
2017
{
2118
"metadata": {
@@ -36,5 +33,4 @@
3633
]
3734
}
3835
EOS;
39-
40-
assertMatchesFormat(json_encode($payload, JSON_PRETTY_PRINT), $format);
36+
assertTelemetry($telemetryLogPath, $format);

loader/tests/functional/test_telemetry_error.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
// Let time to the fork to write the telemetry log
1515
usleep(5000);
1616

17-
$raw = file_get_contents($telemetryLogPath);
18-
$payload = json_decode($raw, true);
19-
2017
$format = <<<EOS
2118
{
2219
"metadata": {
@@ -37,5 +34,4 @@
3734
]
3835
}
3936
EOS;
40-
41-
assertMatchesFormat(json_encode($payload, JSON_PRETTY_PRINT), $format);
37+
assertTelemetry($telemetryLogPath, $format);

loader/tests/functional/test_telemetry_php5.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
// Let time to the fork to write the telemetry log
1414
usleep(5000);
1515

16-
$raw = file_get_contents($telemetryLogPath);
17-
$payload = json_decode($raw, true);
18-
1916
$format = <<<EOS
2017
{
2118
"metadata": {
@@ -39,5 +36,4 @@
3936
]
4037
}
4138
EOS;
42-
43-
assertMatchesFormat(json_encode($payload, JSON_PRETTY_PRINT), $format);
39+
assertTelemetry($telemetryLogPath, $format);

0 commit comments

Comments
 (0)