Skip to content

Commit 63fc926

Browse files
smaddockNevay
andauthored
[ReactPHP] Add user-option for redacting query parameters (#379)
* add user-option for redacting query parameters * Update src/Instrumentation/ReactPHP/src/ReactPHPInstrumentation.php Co-authored-by: Tobias Bachert <git@b-privat.de> * Update src/Instrumentation/ReactPHP/src/ReactPHPInstrumentation.php Co-authored-by: Tobias Bachert <git@b-privat.de> --------- Co-authored-by: Tobias Bachert <git@b-privat.de>
1 parent de6d61b commit 63fc926

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

src/Instrumentation/ReactPHP/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,18 @@ The extension can be disabled via [runtime configuration](https://opentelemetry.
3535
OTEL_PHP_DISABLED_INSTRUMENTATIONS=reactphp
3636
```
3737

38-
Custom HTTP methods can replace the known methods via environment variables, e.g.:
38+
Custom HTTP methods can replace the known methods via an environment variable, e.g.:
3939

4040
```shell
4141
OTEL_INSTRUMENTATION_HTTP_KNOWN_METHODS="GET,HEAD,POST,PUT,DELETE,CONNECT,OPTIONS,TRACE,PATCH,MyCustomMethod"
4242
```
4343

44+
Additional HTTP query string parameters can be redacted via an environment variable, e.g.,
45+
46+
```shell
47+
OTEL_PHP_INSTRUMENTATION_URL_SANITIZE_FIELD_NAMES="password,passwd,pwd,secret"
48+
```
49+
4450
Request and/or response headers can be added as span attributes via environment variables, e.g.:
4551

4652
```shell

src/Instrumentation/ReactPHP/phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<env name="OTEL_INSTRUMENTATION_HTTP_KNOWN_METHODS" value="GET,HEAD,POST,PUT,DELETE,CONNECT,OPTIONS,TRACE,PATCH,CUSTOM" />
3333
<env name="OTEL_PHP_INSTRUMENTATION_HTTP_REQUEST_HEADERS" value="traceparent" />
3434
<env name="OTEL_PHP_INSTRUMENTATION_HTTP_RESPONSE_HEADERS" value="Content-Type" />
35+
<env name="OTEL_PHP_INSTRUMENTATION_URL_SANITIZE_FIELD_NAMES" value="password,passwd,pwd,secret" />
3536
<ini name="date.timezone" value="UTC" />
3637
<ini name="display_errors" value="On" />
3738
<ini name="display_startup_errors" value="On" />

src/Instrumentation/ReactPHP/src/ReactPHPInstrumentation.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ class ReactPHPInstrumentation
5656
* @see https://opentelemetry.io/docs/specs/semconv/http/http-spans/#http-client-span
5757
*/
5858
private const ENV_HTTP_RESPONSE_HEADERS = 'OTEL_PHP_INSTRUMENTATION_HTTP_RESPONSE_HEADERS';
59+
/**
60+
* The environment variable which adds to the URL query parameter keys to redact the values for.
61+
* This supports a comma-separated list of case-sensitive query parameter keys.
62+
*
63+
* Note that this is not currently defined in OTel SemConv, and therefore subject to change.
64+
*
65+
* @see https://github.com/open-telemetry/semantic-conventions/issues/877
66+
*/
67+
private const ENV_URL_SANITIZE_FIELD_NAMES = 'OTEL_PHP_INSTRUMENTATION_URL_SANITIZE_FIELD_NAMES';
5968
/**
6069
* The `{method}` component of the span name when the original method is not known to the instrumentation.
6170
*
@@ -313,6 +322,12 @@ private static function sanitizeUrl(UriInterface $uri): string
313322
$uri = $uri->withUserInfo(self::URL_REDACTION);
314323
}
315324

325+
$sanitizeFields = self::URL_QUERY_REDACT_KEYS;
326+
$customFields = $_ENV[self::ENV_URL_SANITIZE_FIELD_NAMES] ?? '';
327+
if ($customFields !== '') {
328+
$sanitizeFields = array_merge($sanitizeFields, explode(',', $customFields));
329+
}
330+
316331
$queryString = $uri->getQuery();
317332
// http_build_query(parse_str()) is not idempotent, so using Guzzle’s Query class for now
318333
if ($queryString !== '') {
@@ -321,7 +336,7 @@ private static function sanitizeUrl(UriInterface $uri): string
321336
$queryParameters,
322337
array_intersect_key(
323338
array_fill_keys(
324-
self::URL_QUERY_REDACT_KEYS,
339+
$sanitizeFields,
325340
self::URL_REDACTION
326341
),
327342
$queryParameters

src/Instrumentation/ReactPHP/tests/Integration/ReactPHPInstrumentationTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function test_fulfilled_promise(): void
111111
$this->assertSame(['text/plain; charset=utf-8'], $span->getAttributes()->get(sprintf('%s.%s', TraceAttributes::HTTP_RESPONSE_HEADER, 'content-type')));
112112
}
113113

114-
public function test_fulfilled_promise_with_redactions(): void
114+
public function test_fulfilled_promise_with_required_redactions(): void
115115
{
116116
$this->browser->request('GET', 'http://username@example.com/success')->then();
117117

@@ -124,6 +124,14 @@ public function test_fulfilled_promise_with_redactions(): void
124124
$this->assertSame('http://REDACTED:REDACTED@example.com/success?Signature=REDACTED', $span->getAttributes()->get(TraceAttributes::URL_FULL));
125125
}
126126

127+
public function test_fulfilled_promise_with_custom_redactions(): void
128+
{
129+
$this->browser->request('GET', 'http://example.com/success?password=private')->then();
130+
131+
$span = $this->storage->offsetGet(0);
132+
$this->assertSame('http://example.com/success?password=REDACTED', $span->getAttributes()->get(TraceAttributes::URL_FULL));
133+
}
134+
127135
public function test_fulfilled_promise_with_overridden_methods(): void
128136
{
129137
$this->browser->request('CUSTOM', 'http://example.com:8888/success')->then();

0 commit comments

Comments
 (0)