Skip to content

Commit af87d42

Browse files
committed
add user-option for redacting query parameters
1 parent 082d535 commit af87d42

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
@@ -29,12 +29,18 @@ The extension can be disabled via [runtime configuration](https://opentelemetry.
2929
OTEL_PHP_DISABLED_INSTRUMENTATIONS=reactphp
3030
```
3131

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

3434
```shell
3535
OTEL_INSTRUMENTATION_HTTP_KNOWN_METHODS="GET,HEAD,POST,PUT,DELETE,CONNECT,OPTIONS,TRACE,PATCH,MyCustomMethod"
3636
```
3737

38+
Additional HTTP query string parameters can be redacted via an environment variable, e.g.,
39+
40+
```shell
41+
OTEL_PHP_INSTRUMENTATION_URL_SANITIZE_FIELD_NAMES="password,passwd,pwd,secret"
42+
```
43+
3844
Request and/or response headers can be added as span attributes via environment variables, e.g.:
3945

4046
```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
@@ -54,6 +54,15 @@ class ReactPHPInstrumentation
5454
* @see https://opentelemetry.io/docs/specs/semconv/http/http-spans/#http-client-span
5555
*/
5656
private const ENV_HTTP_RESPONSE_HEADERS = 'OTEL_PHP_INSTRUMENTATION_HTTP_RESPONSE_HEADERS';
57+
/**
58+
* The environment variable which adds to the URL query parameter keys to redact the values for.
59+
* This supports a comma-separated list of case-sensitive known HTTP methods.
60+
*
61+
* Note that this is not currently defined in OTel SemConv, and therefore subject to change.
62+
*
63+
* @see https://github.com/open-telemetry/semantic-conventions/issues/877
64+
*/
65+
private const ENV_URL_SANITIZE_FIELD_NAMES = 'OTEL_PHP_INSTRUMENTATION_URL_SANITIZE_FIELD_NAMES';
5766
/**
5867
* The `{method}` component of the span name when the original method is not known to the instrumentation.
5968
*
@@ -259,6 +268,12 @@ private static function sanitizeUrl(UriInterface $uri): string
259268
$uri = $uri->withUserInfo(self::URL_REDACTION);
260269
}
261270

271+
$sanitizeFields = self::URL_QUERY_REDACT_KEYS;
272+
$customFields = $_ENV[self::ENV_URL_SANITIZE_FIELD_NAMES] ?? '';
273+
if (!empty($customFields)) {
274+
$sanitizeFields = array_merge($sanitizeFields, explode(',', $customFields));
275+
}
276+
262277
$queryString = $uri->getQuery();
263278
// http_build_query(parse_str()) is not idempotent, so using Guzzle’s Query class for now
264279
if ($queryString !== '') {
@@ -267,7 +282,7 @@ private static function sanitizeUrl(UriInterface $uri): string
267282
$queryParameters,
268283
array_intersect_key(
269284
array_fill_keys(
270-
self::URL_QUERY_REDACT_KEYS,
285+
$sanitizeFields,
271286
self::URL_REDACTION
272287
),
273288
$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://[email protected]/success')->then();
117117

@@ -124,6 +124,14 @@ public function test_fulfilled_promise_with_redactions(): void
124124
$this->assertSame('http://REDACTED:[email protected]/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)