Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.

Commit f85bdd6

Browse files
Add Telemetry configuration and refactor related components
Introduced a new `Telemetry` class to handle OTEL configurations with support for endpoint, protocol, batch size, and sampling ratio. Updated `Settings` and `AccessLog` to support telemetry, enhance arrayable support, and included `TelemetryProtocolEnum`. Improved test coverage and streamlined consistency across various components.
1 parent 5acbee6 commit f85bdd6

File tree

12 files changed

+249
-31
lines changed

12 files changed

+249
-31
lines changed

composer.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@
2828
"lint": "./vendor/bin/php-cs-fixer fix --rules=@PSR12 ."
2929
},
3030
"require": {
31-
"php": "^8.3",
32-
"friendsofphp/php-cs-fixer": "^3.54",
33-
"guzzlehttp/guzzle": "^7.8.1",
34-
"nesbot/carbon": "^2.72",
31+
"php": "^8.3|^8.4",
32+
"friendsofphp/php-cs-fixer": "^3.65",
33+
"guzzlehttp/guzzle": "^7.9",
34+
"nesbot/carbon": "^3.8",
3535
"doctrine/inflector": "^2.0",
3636
"ext-curl": "*"
3737
},
3838
"require-dev": {
39-
"pestphp/pest": "^2.34",
40-
"phpstan/phpstan": "^1.10",
39+
"pestphp/pest": "^2.36",
40+
"phpstan/phpstan": "^1.12",
4141
"mockery/mockery": "^1.6",
42-
"fakerphp/faker": "^1.23"
42+
"fakerphp/faker": "^1.24"
4343
},
4444
"autoload": {
4545
"psr-4": {

src/Builders/ApplicationBuilder.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
class ApplicationBuilder
1818
{
1919
/**
20-
* @throws UnitException
21-
*
22-
* TODO: add ApplicationTypeEnum as possible type
20+
* @param $appName
21+
* @param $appData
22+
* @param string $type
23+
* @return GoExternalApplication|JavaApplication|NodeJsExternalApplication|PerlApplication|PhpApplication|PythonApplication|RubyApplication|WebAssemblyApplication|WebAssemblyComponentApplication
2324
*/
2425
public static function create($appName, $appData, string $type = '')
2526
{

src/Builders/EndpointBuilder.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ class EndpointBuilder
1616
{
1717
protected string $endpoint = '';
1818

19+
/**
20+
* @param array|string $url
21+
*/
1922
public function __construct(
2023
// TODO: change with http extension
21-
array|string $url = null
24+
array|string $url = ''
2225
) {
2326
$this->endpoint = '/' . (is_array($url) ? implode('/', $url) : $url);
2427
}

src/Config/AccessLog.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
namespace UnitPhpSdk\Config;
44

55
use UnitPhpSdk\Builders\EndpointBuilder;
6+
use UnitPhpSdk\Contracts\Arrayable;
67
use UnitPhpSdk\Contracts\Uploadable;
78
use UnitPhpSdk\Traits\CanUpload;
89

9-
class AccessLog implements Uploadable
10+
class AccessLog implements Uploadable, Arrayable
1011
{
1112
use CanUpload;
1213

@@ -22,11 +23,12 @@ class AccessLog implements Uploadable
2223
*
2324
* @var string|mixed|null
2425
*/
25-
private ?string $format;
26+
private string|array|null $format;
2627

2728
public function __construct(
2829
array $data
29-
) {
30+
)
31+
{
3032
$this->path = $data['path'] ?? null;
3133
$this->format = $data['format'] ?? null;
3234

@@ -64,4 +66,15 @@ public function setFormat(string $format): void
6466
{
6567
$this->format = $format;
6668
}
69+
70+
/**
71+
* @inheritDoc
72+
*/
73+
#[\Override] public function toArray(): array
74+
{
75+
return [
76+
'path' => $this->path,
77+
'format' => $this->format,
78+
];
79+
}
6780
}

src/Config/Application/Targets/PhpTarget.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ class PhpTarget implements Arrayable
1919
*
2020
* @var string
2121
*/
22-
private string $script = 'index.php';
22+
private string $script = '';
2323

2424
/**
2525
* Filename added to URI paths that point to directories if no script is set
2626
* The requests are served according to their URI paths; if they point to directories, index is used.
2727
*
2828
* @var string
2929
*/
30-
private string $index = 'index.php';
30+
private string $index = '';
3131

3232
public function __construct(array $data)
3333
{

src/Config/Routes/ActionType/ShareAction.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ class ShareAction implements Arrayable
1313
*
1414
* @var string
1515
*/
16-
private string $index = 'index.html';
16+
private string $index = '';
1717

1818
/**
19+
* Used if the request can’t be served by share or index.
20+
*
1921
* @var array
2022
*/
2123
private array $fallback = [];
2224

2325
/**
26+
* Used to filter the shared files.
27+
*
2428
* @var array
2529
*/
2630
private array $types = [];
@@ -41,24 +45,24 @@ class ShareAction implements Arrayable
4145
private bool $traverse_mounts = true;
4246

4347
public function __construct(
44-
private string $share,
48+
private string|array $share,
4549
$data = []
4650
) {
4751
$this->parseData($data);
4852
}
4953

5054
/**
51-
* @return string
55+
* @return string|array
5256
*/
53-
public function getShare(): string
57+
public function getShare(): string|array
5458
{
5559
return $this->share;
5660
}
5761

5862
/**
5963
* @param string $share
6064
*/
61-
public function setShare(string $share): void
65+
public function setShare(string|array $share): void
6266
{
6367
$this->share = $share;
6468
}

src/Config/Settings.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace UnitPhpSdk\Config;
44

5+
use InvalidArgumentException;
56
use UnitPhpSdk\Builders\EndpointBuilder;
67
use UnitPhpSdk\Config\Settings\Http;
8+
use UnitPhpSdk\Config\Settings\Telemetry;
79
use UnitPhpSdk\Contracts\Arrayable;
810
use UnitPhpSdk\Contracts\Jsonable;
911
use UnitPhpSdk\Contracts\Uploadable;
@@ -13,6 +15,8 @@ class Settings implements Uploadable, Arrayable, Jsonable
1315
{
1416
use CanUpload;
1517

18+
private const array REQUIRED_TELEMETRY_KEYS = ['endpoint', 'protocol'];
19+
1620
/**
1721
* Fine-tunes handling of HTTP requests from the clients
1822
*
@@ -25,6 +29,11 @@ class Settings implements Uploadable, Arrayable, Jsonable
2529
*/
2630
private string|array $js_module = [];
2731

32+
/**
33+
* @var Telemetry|mixed|null
34+
*/
35+
private ?Telemetry $telemetry = null;
36+
2837
public function __construct(array $data = [])
2938
{
3039
if (array_key_exists('http', $data)) {
@@ -35,6 +44,10 @@ public function __construct(array $data = [])
3544
$this->parseJsModule($data['js_module']);
3645
}
3746

47+
if (array_key_exists('telemetry', $data)) {
48+
$this->parseTelemetry($data['telemetry']);
49+
}
50+
3851
$this->setApiEndpoint(EndpointBuilder::create($this)->get());
3952
}
4053

@@ -56,6 +69,33 @@ private function parseJsModule(array|string $data): void
5669
$this->js_module = $data;
5770
}
5871

72+
private function parseTelemetry(array $data): void
73+
{
74+
$this->validateTelemetryData($data);
75+
76+
$this->telemetry = new Telemetry(
77+
endpoint: $data['endpoint'],
78+
protocol: $data['protocol']
79+
);
80+
81+
if (array_key_exists('sampling_ratio', $data)) {
82+
$this->telemetry->setSamplingRatio($data['sampling_ratio']);
83+
}
84+
85+
if (array_key_exists('batch_size', $data)) {
86+
$this->telemetry->setBatchSize($data['batch_size']);
87+
}
88+
}
89+
90+
private function validateTelemetryData(array $data): void
91+
{
92+
foreach (self::REQUIRED_TELEMETRY_KEYS as $key) {
93+
if (empty($data[$key])) {
94+
throw new InvalidArgumentException("Telemetry {$key} is required");
95+
}
96+
}
97+
}
98+
5999
/**
60100
* @return array|string
61101
*/
@@ -85,6 +125,6 @@ public function setJsModule(array|string $js_module): void
85125
*/
86126
#[\Override] public function toJson(int $options = 0): string
87127
{
88-
return json_encode(array_filter($this->toArray(), fn ($item) => !empty($item)), $options);
128+
return json_encode(array_filter($this->toArray(), fn($item) => !empty($item)), $options);
89129
}
90130
}

src/Config/Settings/Telemetry.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
namespace UnitPhpSdk\Config\Settings;
4+
5+
use UnitPhpSdk\Contracts\Arrayable;
6+
use UnitPhpSdk\Contracts\Jsonable;
7+
use UnitPhpSdk\Enums\TelemetryProtocolEnum;
8+
9+
class Telemetry implements Arrayable, Jsonable
10+
{
11+
/**
12+
* Number of spans to cache before triggering a transaction with the configured endpoint. This is optional.
13+
*
14+
* @var int|null $batch_size
15+
*/
16+
private ?int $batch_size = 50;
17+
18+
/**
19+
* Percentage of requests to trace. This is optional.
20+
*
21+
* @var float|null $sampling_ratio
22+
*/
23+
private ?float $sampling_ratio = 1.0;
24+
25+
public function __construct(
26+
/**
27+
* The endpoint for the OpenTelemetry (OTEL) Collector. This is required.
28+
*
29+
* @var string $endpoint
30+
*/
31+
private string $endpoint,
32+
/**
33+
* Determines the protocol used to communicate with the endpoint. This is required.
34+
*
35+
* @var TelemetryProtocolEnum $protocol
36+
*/
37+
private TelemetryProtocolEnum $protocol
38+
)
39+
{
40+
$this->setEndpoint($endpoint);
41+
$this->setProtocol($protocol);
42+
}
43+
44+
public function getEndpoint(): string
45+
{
46+
return $this->endpoint;
47+
}
48+
49+
public function setEndpoint(string $endpoint): void
50+
{
51+
$this->endpoint = $endpoint;
52+
}
53+
54+
public function getProtocol(): TelemetryProtocolEnum
55+
{
56+
return $this->protocol;
57+
}
58+
59+
public function setProtocol(TelemetryProtocolEnum $protocol): void
60+
{
61+
$this->protocol = $protocol;
62+
}
63+
64+
public function getBatchSize(): ?int
65+
{
66+
return $this->batch_size;
67+
}
68+
69+
public function setBatchSize(?int $batch_size): void
70+
{
71+
$this->batch_size = $batch_size;
72+
}
73+
74+
public function getSamplingRatio(): ?float
75+
{
76+
return $this->sampling_ratio;
77+
}
78+
79+
public function setSamplingRatio(?float $sampling_ratio): void
80+
{
81+
$this->sampling_ratio = $sampling_ratio;
82+
}
83+
84+
/**
85+
* @inheritDoc
86+
*/
87+
#[\Override] public function toArray(): array
88+
{
89+
return [
90+
'endpoint' => $this->getEndpoint(),
91+
'protocol' => $this->getProtocol(),
92+
'batch_size' => $this->getBatchSize(),
93+
'sampling_ratio' => $this->getSamplingRatio()
94+
];
95+
}
96+
97+
/**
98+
* @inheritDoc
99+
*/
100+
#[\Override] public function toJson(int $options = 0): string
101+
{
102+
return json_encode($this->toArray(), $options);
103+
}
104+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace UnitPhpSdk\Enums;
4+
5+
enum TelemetryProtocolEnum: string
6+
{
7+
case HTTP = 'http';
8+
case GRPC = 'grpc';
9+
}

src/Traits/CanUpload.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ public function upload(UnitRequest $request)
4949
{
5050
$data = $this->removeEmptyArrays($this->toArray());
5151

52-
echo '<pre>';
53-
print_r(json_encode($data));
54-
echo '</pre>';
55-
5652
try {
5753
$request->setMethod(HttpMethodsEnum::PUT->value)->send(
5854
$this->getApiEndpoint(),

0 commit comments

Comments
 (0)