Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

Commit

Permalink
Merge pull request #103 from FreezeWarp/master
Browse files Browse the repository at this point in the history
Add handling for keyword fields with a maximum length of 1024 characters
  • Loading branch information
philkra authored Nov 7, 2019
2 parents 6664743 + f40dcbc commit 076d9c4
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 15 deletions.
6 changes: 4 additions & 2 deletions src/Events/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PhilKra\Events;

use PhilKra\Helper\Encoding;

/**
*
* Event Bean for Error wrapping
Expand Down Expand Up @@ -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(),
],
Expand Down
10 changes: 6 additions & 4 deletions src/Events/EventBean.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PhilKra\Events;

use PhilKra\Helper\Encoding;

/**
*
* EventBean for occurring events such as Exceptions or Transactions
Expand Down Expand Up @@ -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'] ?? '',
Expand Down
9 changes: 5 additions & 4 deletions src/Events/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhilKra\Agent;
use PhilKra\Helper\Config;
use PhilKra\Helper\Encoding;

/**
*
Expand Down Expand Up @@ -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') ?? '',
Expand All @@ -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')
]
Expand Down
7 changes: 4 additions & 3 deletions src/Events/Span.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhilKra\Events;

use PhilKra\Helper\Encoding;
use PhilKra\Helper\Timer;

/**
Expand Down Expand Up @@ -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(),
Expand Down
5 changes: 3 additions & 2 deletions src/Events/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PhilKra\Events;

use PhilKra\Helper\Timer;
use PhilKra\Helper\Encoding;

/**
*
Expand Down Expand Up @@ -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' => [
Expand Down
31 changes: 31 additions & 0 deletions src/Helper/Encoding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace PhilKra\Helper;

/*
* Functions to convert values for transmission to ElasticSearch.
*/
class Encoding
{

/**
* The maximum number of characters that are accepted in a keyword field.
*/
const KEYWORD_MAX_LENGTH = 1024;


/**
* Limit the size of keyword fields. This is the same approach used by the Python APM client.
*
* @param string $value
* @return string
*/
public static function keywordField($value)
{
if (strlen($value) > 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;
}
}
47 changes: 47 additions & 0 deletions tests/Helper/EncodingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
namespace PhilKra\Tests\Helper;

use \PhilKra\Helper\Encoding;
use PhilKra\Tests\TestCase;

/**
* Test Case for @see \PhilKra\Helper\Encoding
*/
final class EncodingTest extends TestCase {

/**
* @covers \PhilKra\Helper\Encoding::keywordField
*/
public function testShortInput() {

$input = "abcdefghijklmnopqrstuvwxyz1234567890";

$this->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) );

}

}

0 comments on commit 076d9c4

Please sign in to comment.