99use OpenTelemetry \SDK \Resource \ResourceInfo ;
1010use OpenTelemetry \SemConv \ResourceAttributes ;
1111use function php_uname ;
12- use Throwable ;
1312
1413/**
1514 * @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.8.0/specification/resource/semantic_conventions/host.md#host
@@ -48,25 +47,37 @@ private function getMachineId(): ?string
4847 };
4948 }
5049
50+ /**
51+ * @phan-suppress PhanTypeMismatchArgumentInternal
52+ */
53+ private function readFile (string $ file ): string |false
54+ {
55+ set_error_handler (static fn () => true );
56+
57+ try {
58+ if (is_file ($ file ) && is_readable ($ file )) {
59+ $ contents = file_get_contents ($ file );
60+ if ($ contents !== false ) {
61+ return $ contents ;
62+ }
63+ }
64+ } finally {
65+ restore_error_handler ();
66+ }
67+
68+ return false ;
69+ }
70+
5171 private function getLinuxId (): ?string
5272 {
5373 $ paths = [self ::PATH_ETC_MACHINEID , self ::PATH_VAR_LIB_DBUS_MACHINEID ];
5474
5575 foreach ($ paths as $ path ) {
5676 $ file = $ this ->dir . $ path ;
5777
58- try {
59- if (is_file ($ file ) && is_readable ($ file )) {
60- $ contents = file_get_contents ($ file );
61-
62- if ($ contents === false ) {
63- continue ;
64- }
65-
66- return trim ($ contents );
67- }
68- } catch (Throwable $ t ) {
69- //do nothing
78+ $ contents = $ this ->readFile ($ file );
79+ if ($ contents !== false ) {
80+ return $ contents ;
7081 }
7182 }
7283
@@ -77,16 +88,9 @@ private function getBsdId(): ?string
7788 {
7889 $ file = $ this ->dir . self ::PATH_ETC_HOSTID ;
7990
80- try {
81- if (is_file ($ file ) && is_readable ($ file )) {
82- $ contents = file_get_contents ($ file );
83-
84- if ($ contents !== false ) {
85- return trim ($ contents );
86- }
87- }
88- } catch (Throwable $ t ) {
89- //do nothing
91+ $ contents = $ this ->readFile ($ file );
92+ if ($ contents !== false ) {
93+ return $ contents ;
9094 }
9195
9296 $ out = exec ('which kenv && kenv -q smbios.system.uuid ' );
0 commit comments