Skip to content

Commit b2aaa0d

Browse files
authored
optimize resource creation (#1210)
* reduce resource creation from sdk autoloader - instead of creating new resources from various factories, create them once from sdk autoloader and pass them to the factories (most of which already supported this). - check env before php.ini for config, since this is the more popular approach and saves some cycles - cache header from OtlpUtil, to save a couple of calls to Sdk detector * adding benchmark for resource creation
1 parent 89aaf71 commit b2aaa0d

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

OtlpUtil.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,16 @@ public static function method(string $signal): string
3535
*/
3636
public static function getUserAgentHeader(): array
3737
{
38-
$resource = (new Sdk())->getResource();
38+
static $header;
39+
if ($header === null) {
40+
$resource = (new Sdk())->getResource();
41+
42+
$header = ['User-Agent' => sprintf(
43+
'OTel OTLP Exporter PHP/%s',
44+
$resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_VERSION) ?: 'unknown'
45+
)];
46+
}
3947

40-
return ['User-Agent' => sprintf(
41-
'OTel OTLP Exporter PHP/%s',
42-
$resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_VERSION) ?: 'unknown'
43-
)];
48+
return $header;
4449
}
4550
}

0 commit comments

Comments
 (0)