Skip to content

Commit 0fe015f

Browse files
committed
review updates
1 parent d59a67d commit 0fe015f

File tree

10 files changed

+313
-307
lines changed

10 files changed

+313
-307
lines changed

src/Instrumentation/ReactHttp/README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
This is a read-only subtree split of https://github.com/open-telemetry/opentelemetry-php-contrib.
2+
13
# OpenTelemetry ReactPHP HTTP Browser auto-instrumentation
24

35
Please read https://opentelemetry.io/docs/instrumentation/php/automatic/ for instructions on how to
@@ -18,9 +20,15 @@ The extension can be disabled via [runtime configuration](https://opentelemetry.
1820
OTEL_PHP_DISABLED_INSTRUMENTATIONS=react-http
1921
```
2022

21-
Request and/or response headers can be added as span attributes by adding to the `php.ini`:
23+
Custom HTTP methods can replace the known methods via environment variables, e.g.:
24+
25+
```shell
26+
OTEL_INSTRUMENTATION_HTTP_KNOWN_METHODS="GET,HEAD,POST,PUT,DELETE,CONNECT,OPTIONS,TRACE,PATCH,MyCustomMethod"
27+
```
28+
29+
Request and/or response headers can be added as span attributes via environment variables, e.g.:
2230

23-
```ini
24-
otel.instrumentation.http.request_headers[]="Accept"
25-
otel.instrumentation.http.response_headers[]="Content-Type"
31+
```shell
32+
OTEL_INSTRUMENTATION_HTTP_REQUEST_HEADERS=Accept
33+
OTEL_INSTRUMENTATION_HTTP_RESPONSE_HEADERS="Content-Length,Content-Type"
2634
```
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# OpenTelemetry ReactPHP HTTP Browser Examples
22

3-
These examples are all valid and working; they just show different ways of calling the ReactPHP HTTP Browser, following some of the examples in the [ReactPHP HTTP documentation](https://reactphp.org/http/).
3+
These examples show two different ways of using the [ReactPHP HTTP](https://reactphp.org/http/) Browser.
44

55
- [http_only.php](http_only.php) - a “pure” example using only the ReactPHP HTTP library.
6-
- [http_only_promises.php](http_only_promises.php) - same as above but with the [Promises](https://reactphp.org/http/#promises) handled in a separate loop.
7-
- [http+async_blocking.php](http+async_blocking.php) - uses the [ReactPHP Async library](https://reactphp.org/async/) to achieve a more [traditional blocking](https://reactphp.org/http/#blocking) request.
8-
- [http+async_nonblocking.php](http+async_nonblocking.php) - uses the [ReactPHP Async library](https://reactphp.org/async/) as intended, to make asynchronous HTTP requests while abstracting away the callables of Promises by taking advantage of PHP fibers.
6+
- [http_with_async.php](http_with_async.php) - uses the [ReactPHP Async library](https://reactphp.org/async/), to make asynchronous HTTP requests while abstracting away the callables of Promises by taking advantage of PHP fibers.

src/Instrumentation/ReactHttp/examples/http+async_blocking.php

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/Instrumentation/ReactHttp/examples/http+async_nonblocking.php

Lines changed: 0 additions & 70 deletions
This file was deleted.

src/Instrumentation/ReactHttp/examples/http_only.php

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
use OpenTelemetry\SDK\Trace\SpanProcessor\BatchSpanProcessor;
1313
use OpenTelemetry\SDK\Trace\TracerProvider;
1414
use Psr\Http\Message\ResponseInterface;
15-
use React\EventLoop\Loop;
1615
use React\Http\Browser;
1716
use React\Http\Message\Request;
17+
use React\Http\Message\ResponseException;
1818

1919
require_once dirname(__DIR__) . '/vendor/autoload.php';
2020

@@ -34,32 +34,59 @@
3434
->buildAndRegisterGlobal();
3535

3636
$root = $tracerProvider->getTracer('react-http-demo')->spanBuilder('root')->startSpan();
37+
$rootScope = $root->activate();
3738

38-
Loop::futureTick(static function () use ($root) {
39-
$rootScope = $root->activate();
39+
try {
40+
$browser = new Browser();
4041

41-
try {
42-
$browser = new Browser();
42+
$requests = [
43+
//[HTTP/1.1 200 OK] https://postman:[email protected]/get?q=query-example#fragment-example
44+
new Request('GET', 'https://postman:[email protected]/get?q=query-example#fragment-example'),
45+
//[HTTP/1.1 200 OK] https://postman-echo.com/post
46+
new Request('POST', 'https://postman-echo.com/post', ['Content-Type' => 'application/json'], '{}'),
47+
//[400: HTTP status code 400 (Bad Request)] http://postman-echo.com:443/get
48+
new Request('CUSTOM', 'http://postman-echo.com:443/get'),
49+
//[0: Invalid request URL given] unknown://postman-echo.com/get
50+
new Request('GET', 'unknown://postman-echo.com/get'),
51+
//[HTTP/1.1 200 OK] https://postman-echo.com/delay/5
52+
new Request('GET', 'https://postman-echo.com/delay/5'),
53+
];
4354

44-
$requests = [
45-
new Request('GET', 'https://postman-echo.com/get'),
46-
new Request('POST', 'https://postman-echo.com/post'),
47-
new Request('GET', 'https://httpbin.org/does-not-exist'),
48-
new Request('GET', 'https://httpbin.org/get'),
49-
new Request('PUT', 'localhost:2222/not-found'),
50-
];
51-
52-
foreach ($requests as $request) {
53-
$browser
54-
->request($request->getMethod(), $request->getUri())
55-
->then(function (ResponseInterface $response) {
56-
echo sprintf('[%d] ', $response->getStatusCode()) . json_decode($response->getBody()->getContents())->url . PHP_EOL;
57-
}, function (Throwable $t) {
58-
var_dump($t->getMessage());
59-
});
60-
}
61-
} finally {
62-
$rootScope->detach();
63-
$root->end();
55+
foreach ($requests as $request) {
56+
$browser
57+
->request($request->getMethod(), $request->getUri())
58+
->then(function (ResponseInterface $response) use ($request) {
59+
echo sprintf(
60+
'[HTTP/%s %d %s] %s%s',
61+
$response->getProtocolVersion(),
62+
$response->getStatusCode(),
63+
$response->getReasonPhrase(),
64+
$request->getUri(),
65+
PHP_EOL
66+
);
67+
}, function (Throwable $t) use ($request) {
68+
if (is_a($t, ResponseException::class)) {
69+
$response = $t->getResponse();
70+
echo sprintf(
71+
'[HTTP/%s %d %s] %s%s',
72+
$response->getProtocolVersion(),
73+
$response->getStatusCode(),
74+
$response->getReasonPhrase(),
75+
$request->getUri(),
76+
PHP_EOL
77+
);
78+
} else {
79+
echo sprintf(
80+
'[%d: %s] %s%s',
81+
$t->getCode(),
82+
$t->getMessage(),
83+
$request->getUri(),
84+
PHP_EOL
85+
);
86+
}
87+
});
6488
}
65-
});
89+
} finally {
90+
$rootScope->detach();
91+
$root->end();
92+
}

src/Instrumentation/ReactHttp/examples/http_only_promises.php

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)