diff --git a/src/Events/Error.php b/src/Events/Error.php index 1de9208..a5e2e76 100644 --- a/src/Events/Error.php +++ b/src/Events/Error.php @@ -2,6 +2,8 @@ namespace PhilKra\Events; +use PhilKra\Helper\Encoding; + /** * * Event Bean for Error wrapping @@ -45,10 +47,10 @@ public function jsonSerialize() : array 'trace_id' => $this->getTraceId(), 'timestamp' => $this->getTimestamp(), 'context' => $this->getContext(), - 'culprit' => sprintf('%s:%d', $this->throwable->getFile(), $this->throwable->getLine()), + 'culprit' => Encoding::keywordField(sprintf('%s:%d', $this->throwable->getFile(), $this->throwable->getLine())), 'exception' => [ 'message' => $this->throwable->getMessage(), - 'type' => get_class($this->throwable), + 'type' => Encoding::keywordField(get_class($this->throwable)), 'code' => $this->throwable->getCode(), 'stacktrace' => $this->mapStacktrace(), ], diff --git a/src/Events/EventBean.php b/src/Events/EventBean.php index 560cc0b..8b42f47 100644 --- a/src/Events/EventBean.php +++ b/src/Events/EventBean.php @@ -2,6 +2,8 @@ namespace PhilKra\Events; +use PhilKra\Helper\Encoding; + /** * * EventBean for occurring events such as Exceptions or Transactions @@ -272,11 +274,11 @@ final public function generateRequest(): array 'response' => $this->contexts['response'], 'url' => [ 'protocol' => $http_or_https, - 'hostname' => $_SERVER['SERVER_NAME'] ?? '', + 'hostname' => Encoding::keywordField($_SERVER['SERVER_NAME'] ?? ''), 'port' => $_SERVER['SERVER_PORT'] ?? 0, - 'pathname' => $_SERVER['SCRIPT_NAME'] ?? '', - 'search' => '?' . (($_SERVER['QUERY_STRING'] ?? '') ?? ''), - 'full' => isset($_SERVER['HTTP_HOST']) ? $http_or_https . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] : '', + 'pathname' => Encoding::keywordField($_SERVER['SCRIPT_NAME'] ?? ''), + 'search' => Encoding::keywordField('?' . (($_SERVER['QUERY_STRING'] ?? '') ?? '')), + 'full' => Encoding::keywordField(isset($_SERVER['HTTP_HOST']) ? $http_or_https . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] : ''), ], 'headers' => [ 'user-agent' => $headers['User-Agent'] ?? '', diff --git a/src/Events/Metadata.php b/src/Events/Metadata.php index 51c163d..2e42efe 100644 --- a/src/Events/Metadata.php +++ b/src/Events/Metadata.php @@ -4,6 +4,7 @@ use PhilKra\Agent; use PhilKra\Helper\Config; +use PhilKra\Helper\Encoding; /** * @@ -40,8 +41,8 @@ final public function jsonSerialize(): array return [ 'metadata' => [ 'service' => [ - 'name' => $this->config->get('appName'), - 'version' => $this->config->get('appVersion'), + 'name' => Encoding::keywordField($this->config->get('appName')), + 'version' => Encoding::keywordField($this->config->get('appVersion')), 'framework' => [ 'name' => $this->config->get('framework') ?? '', 'version' => $this->config->get('frameworkVersion') ?? '', @@ -57,10 +58,10 @@ final public function jsonSerialize(): array 'name' => Agent::NAME, 'version' => Agent::VERSION ], - 'environment' => $this->config->get('environment') + 'environment' => Encoding::keywordField($this->config->get('environment')) ], 'system' => [ - 'hostname' => $this->config->get('hostname'), + 'hostname' => Encoding::keywordField($this->config->get('hostname')), 'architecture' => php_uname('m'), 'platform' => php_uname('s') ] diff --git a/src/Events/Span.php b/src/Events/Span.php index 10850e4..86f8fee 100644 --- a/src/Events/Span.php +++ b/src/Events/Span.php @@ -2,6 +2,7 @@ namespace PhilKra\Events; +use PhilKra\Helper\Encoding; use PhilKra\Helper\Timer; /** @@ -153,11 +154,11 @@ public function jsonSerialize() : array 'transaction_id' => $this->getParentId(), 'trace_id' => $this->getTraceId(), 'parent_id' => $this->getParentId(), - 'type' => $this->type, - 'action' => $this->action, + 'type' => Encoding::keywordField($this->type), + 'action' => Encoding::keywordField($this->action), 'context' => $this->context, 'duration' => $this->duration, - 'name' => $this->getName(), + 'name' => Encoding::keywordField($this->getName()), 'stacktrace' => $this->stacktrace, 'sync' => false, 'timestamp' => $this->getTimestamp(), diff --git a/src/Events/Transaction.php b/src/Events/Transaction.php index 63b5fbb..de92a96 100644 --- a/src/Events/Transaction.php +++ b/src/Events/Transaction.php @@ -3,6 +3,7 @@ namespace PhilKra\Events; use PhilKra\Helper\Timer; +use PhilKra\Helper\Encoding; /** * @@ -139,11 +140,11 @@ public function jsonSerialize() : array 'trace_id' => $this->getTraceId(), 'id' => $this->getId(), 'parent_id' => $this->getParentId(), - 'type' => $this->getMetaType(), + 'type' => Encoding::keywordField($this->getMetaType()), 'duration' => $this->summary['duration'], 'timestamp' => $this->getTimestamp(), 'result' => $this->getMetaResult(), - 'name' => $this->getTransactionName(), + 'name' => Encoding::keywordField($this->getTransactionName()), 'context' => $this->getContext(), 'sampled' => null, 'span_count' => [ diff --git a/src/Helper/Encoding.php b/src/Helper/Encoding.php new file mode 100644 index 0000000..92e40ef --- /dev/null +++ b/src/Helper/Encoding.php @@ -0,0 +1,31 @@ + self::KEYWORD_MAX_LENGTH && mb_strlen($value, 'UTF-8') > self::KEYWORD_MAX_LENGTH) { // strlen is faster (O(1)), so we prefer to first check using it, and then double-checking with the slower mb_strlen (O(n)) only when necessary + return mb_substr($value, 0, self::KEYWORD_MAX_LENGTH - 1, 'UTF-8') . '…'; + } + + return $value; + } +} \ No newline at end of file diff --git a/tests/Helper/EncodingTest.php b/tests/Helper/EncodingTest.php new file mode 100644 index 0000000..925a9a4 --- /dev/null +++ b/tests/Helper/EncodingTest.php @@ -0,0 +1,47 @@ +assertEquals( $input, Encoding::keywordField($input) ); + + } + + /** + * @covers \PhilKra\Helper\Encoding::keywordField + */ + public function testLongInput() { + + $input = str_repeat("abc123", 200); + $output = str_repeat("abc123", 170) . 'abc' . '…'; + + $this->assertEquals( $output, Encoding::keywordField($input) ); + + } + + /** + * @covers \PhilKra\Helper\Encoding::keywordField + */ + public function testLongMultibyteInput() { + + $input = str_repeat("中国日本韓国合衆国", 200); + $output = str_repeat("中国日本韓国合衆国", 113) . '中国日本韓国' . '…'; + + $this->assertEquals( $output, Encoding::keywordField($input) ); + + } + +}