Skip to content

Commit e4b7335

Browse files
committed
Refactored code to use switch statement
1 parent f1833ea commit e4b7335

File tree

2 files changed

+36
-42
lines changed

2 files changed

+36
-42
lines changed

src/Instrumentation/Session/src/PhpSessionInstrumentation.php

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -79,52 +79,41 @@ private static function end(?Throwable $exception, string $function): void
7979
}
8080
$scope->detach();
8181
$span = Span::fromContext($scope->context());
82-
83-
// Add session-specific attributes in the post hook
84-
if ($function === 'session_start') {
85-
$isSessionActive = !empty(session_id());
86-
$span->setAttribute('php.session.id', session_id());
87-
$span->setAttribute('php.session.name', session_name());
88-
$span->setAttribute('php.session.status', $isSessionActive ? 'active' : 'inactive');
8982

90-
// Add session cookie parameters
91-
$cookieParams = session_get_cookie_params();
92-
foreach ($cookieParams as $key => $value) {
93-
if (is_scalar($value)) {
94-
$span->setAttribute("php.session.cookie.$key", '<redacted>');
83+
switch ($function) {
84+
case 'session_start':
85+
$isSessionActive = !empty(session_id());
86+
$span->setAttribute('php.session.status', $isSessionActive ? 'active' : 'inactive');
87+
// Add session cookie parameters
88+
$cookieParams = session_get_cookie_params();
89+
$index = 0;
90+
ksort($cookieParams);
91+
foreach ($cookieParams as $key => $value) {
92+
if (is_scalar($value)) {
93+
$span->setAttribute("php.session.cookie.key[$index]", $key);
94+
}
95+
$index++;
96+
9597
}
96-
}
97-
} elseif ($function === 'session_write_close') {
98-
$sessionId = session_id();
99-
$sessionName = session_name();
100-
101-
// Add session information
102-
if (!empty($sessionId)) {
103-
$span->setAttribute('php.session.id', $sessionId);
104-
$span->setAttribute('php.session.name', $sessionName);
105-
}
106-
107-
} elseif ($function === 'session_unset') {
108-
$sessionId = session_id();
109-
$sessionName = session_name();
110-
111-
// Add session information
112-
if (!empty($sessionId)) {
113-
$span->setAttribute('php.session.id', $sessionId);
114-
$span->setAttribute('php.session.name', $sessionName);
115-
}
116-
117-
} elseif ($function === 'session_abort') {
118-
$sessionId = session_id();
119-
$sessionName = session_name();
98+
// no break
99+
case 'session_write_close':
100+
case 'session_unset':
101+
case 'session_abort':
102+
$sessionId = session_id();
103+
$sessionName = session_name();
120104

121-
// Add session information
122-
if (!empty($sessionId)) {
123-
$span->setAttribute('php.session.id', $sessionId);
124-
$span->setAttribute('php.session.name', $sessionName);
125-
}
105+
// Add session information
106+
if (!empty($sessionId)) {
107+
$span->setAttribute('php.session.id', $sessionId);
108+
$span->setAttribute('php.session.name', $sessionName);
109+
}
110+
111+
break;
112+
case 'session_destroy':
113+
break;
126114

127115
}
116+
128117
if ($exception) {
129118
$span->recordException($exception);
130119
$span->setStatus(StatusCode::STATUS_ERROR, $exception->getMessage());

src/Instrumentation/Session/tests/Integration/PhpSessionInstrumentationTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,14 @@ public function test_session_start(): void
6969

7070
// Check cookie parameters
7171
$cookieParams = session_get_cookie_params();
72+
$index = 0;
73+
ksort($cookieParams);
7274
foreach ($cookieParams as $key => $value) {
7375
if (is_scalar($value)) {
74-
$this->assertEquals('<redacted>', $attributes->get("php.session.cookie.$key"));
76+
$expected = $key;
77+
$actual = $attributes->get("php.session.cookie.key[$index]");
78+
$this->assertEquals($expected, $actual);
79+
$index++;
7580
}
7681
}
7782

0 commit comments

Comments
 (0)