From 2e5ee2366ef81642586c5dc990223a481eb854a4 Mon Sep 17 00:00:00 2001 From: Devi A S L Date: Thu, 11 Jun 2020 16:39:11 +0530 Subject: [PATCH 01/53] Improve instrumentation - add Jaeger trace propagator - add Redis instrumentation - improve PDO and curl instrumentation - improve error instrumentation - add ability to pass root span name along with other options while creating trace - use span attribute names like db.type, db.statement etc as suggested by https://opentracing.io/specification/conventions/ --- src/Trace/Integrations/Curl.php | 36 ++++++- src/Trace/Integrations/PDO.php | 64 ++++++++++++- src/Trace/Integrations/Redis.php | 112 ++++++++++++++++++++++ src/Trace/Propagator/JaegerPropagator.php | 99 +++++++++++++++++++ src/Trace/RequestHandler.php | 62 +++++++++++- src/Trace/Tracer.php | 11 +++ 6 files changed, 373 insertions(+), 11 deletions(-) create mode 100644 src/Trace/Integrations/Redis.php create mode 100644 src/Trace/Propagator/JaegerPropagator.php diff --git a/src/Trace/Integrations/Curl.php b/src/Trace/Integrations/Curl.php index 1f0abce4199..4a0d6660b9d 100644 --- a/src/Trace/Integrations/Curl.php +++ b/src/Trace/Integrations/Curl.php @@ -55,11 +55,41 @@ public static function load() */ public static function handleCurlResource($resource) { + $info = curl_getinfo($resource); + $attrs = self::getSpanAttrsFromCurlInfo($info); + return [ - 'attributes' => [ - 'uri' => curl_getinfo($resource, CURLINFO_EFFECTIVE_URL) - ], + 'attributes' => $attrs, 'kind' => Span::KIND_CLIENT ]; } + + private static function getSpanAttrsFromCurlInfo($curlInfo) + { + $tagNameCurlInfoMap = [ + 'network.client.ip' => 'local_ip', + 'network.client.port' => 'local_port', + 'network.destination.ip' => 'primary_ip', + 'network.destination.port' => 'primary_port', + 'network.bytes_read' => 'size_download', + 'network.bytes_written' => 'size_upload', + 'time_total_in_secs' => 'total_time', + 'time_to_connect_in_secs' => 'connect_time', + 'time_to_redirect_in_secs' => 'redirect_time', + 'time_to_namelookup_in_secs' => 'namelookup_time', + 'time_to_pretransfer_in_secs' => 'pretransfer_time', + 'time_to_starttransfer_in_secs' => 'starttransfer_time', + 'primary_ip' => 'primary_ip', + 'url' => 'url' + ]; + + $attrs = []; + + foreach ($tagNameCurlInfoMap as $tagName => $curlInfoName) { + if (isset($curlInfo[$curlInfoName]) && !\trim($curlInfo[$curlInfoName]) !== '') { + $attrs[$tagName] = $curlInfo[$curlInfoName]; + } + } + return $attrs; + } } diff --git a/src/Trace/Integrations/PDO.php b/src/Trace/Integrations/PDO.php index dd846d3a624..402eb4ad419 100644 --- a/src/Trace/Integrations/PDO.php +++ b/src/Trace/Integrations/PDO.php @@ -71,7 +71,7 @@ public static function load() public static function handleQuery($pdo, $query) { return [ - 'attributes' => ['query' => $query], + 'attributes' => ['db.statement' => $query], 'kind' => Span::KIND_CLIENT ]; } @@ -86,9 +86,10 @@ public static function handleQuery($pdo, $query) */ public static function handleConnect($pdo, $dsn) { - return [ - 'attributes' => ['dsn' => $dsn], - 'kind' => Span::KIND_CLIENT + $attributes = ['dsn' => $dsn, 'db.type' => 'sql']; + + return [ 'attributes' => $attributes, + 'kind' => Span::KIND_CLIENT, ]; } @@ -101,8 +102,61 @@ public static function handleConnect($pdo, $dsn) */ public static function handleStatementExecute($statement) { + /* + refer following for SQL return codes + https://docstore.mik.ua/orelly/java-ent/jenut/ch08_06.htm + */ + + $rowCount = $statement->rowCount(); + $errorCode = $statement->errorCode(); + $error = substr($errorCode, 0, 2); + $errorTags = []; + + switch ($error) { + case (string) '01': + $errorTags = ['warning' => 'true', 'warning.code' => $errorCode]; + break; + }; + + $errorCodeMsgArray = [ + "02" => "No Data", + "07" => "Dynamic SQL error", + "08" => "Connection Exception", + "0A" => "Feature not supported", + "21" => "Cardinality violation", + "22" => "Data exception", + "23" => "Integrity constraint violation", + "24" => "Invalid Cursor State", + "25" => "Invalid Transaction state", + "26" => "Invalid SQL Statement Name", + "27" => "Triggered Data Change Violation", + "28" => "Invalid Authorization Specification", + "2A" => "Syntax Error or Access Rule Violation in Direct SQL Statement", + "2B" => "Dependent Privilege Descriptors Still Exist", + "2C" => "Invalid Character Set Name", + "2D" => "Invalid Transaction Termination", + "2E" => "Invalid Connection Name", + "33" => "Invalid SQL Descriptor Name", + "34" => "Invalid Cursor Name", + "35" => "Invalid Condition Number", + "37" => "Syntax Error or Access Rule Violation in Dynamic SQL Statement", + "3C" => "Ambigous Cursor Name", + "3F" => "No Data", + "40" => "Transition Rollback", + "42" => "Syntax Error or Access Rule Violation", + "44" => "With Check Option Violation" + ]; + + if (array_key_exists($error, $errorCodeMsgArray)){ + $errorTags['error'] = 'true'; + $errorTags['error.code'] = $errorCode; + $errorTags['error.message'] = $errorCodeMsgArray[$error] ?? ''; + } + + $tags = ['db.statement' => $statement->queryString, 'db.row_count' => $rowCount]; + return [ - 'attributes' => ['query' => $statement->queryString], + 'attributes' => $tags + $errorTags, 'kind' => Span::KIND_CLIENT ]; } diff --git a/src/Trace/Integrations/Redis.php b/src/Trace/Integrations/Redis.php new file mode 100644 index 00000000000..7ff304c4595 --- /dev/null +++ b/src/Trace/Integrations/Redis.php @@ -0,0 +1,112 @@ + [ + 'peer.hostname' => $params['host'], + 'peer.port' => $params['port'], + 'db.type' => 'redis' + ], + 'kind' => Span::KIND_CLIENT + ]; + }); + + // covers all basic commands + opencensus_trace_method('Predis\Client', 'executeCommand', function ($predis, $command) { + $arguments = $command->getArguments(); + array_unshift($arguments, $command->getId()); + $query = Redis::formatArguments($arguments); + return ['attributes' => [ + 'command' => $command->getId(), + 'service.name' => 'redis', + 'redis.raw_command' => $query, + 'redis.args_length' => count($arguments) + ], + 'kind' => Span::KIND_CLIENT + ]; + }); + + } + + + public static function formatArguments($arguments) + { + $len = 0; + $out = []; + + foreach ($arguments as $argument) { + // crude test to skip binary + if (strpos($argument, "\0") !== false) { + continue; + } + + $cmd = (string)$argument; + + if (strlen($cmd) > VALUE_MAX_LEN) { + $cmd = substr($cmd, 0, VALUE_MAX_LEN) . VALUE_TOO_LONG_MARK; + } + + if (($len + strlen($cmd)) > CMD_MAX_LEN) { + $prefix = substr($cmd, 0, CMD_MAX_LEN - $len); + $out[] = $prefix . VALUE_TOO_LONG_MARK; + break; + } + + $out[] = $cmd; + $len += strlen($cmd); + } + + return implode(' ', $out); + } +} diff --git a/src/Trace/Propagator/JaegerPropagator.php b/src/Trace/Propagator/JaegerPropagator.php new file mode 100644 index 00000000000..731bf7ca342 --- /dev/null +++ b/src/Trace/Propagator/JaegerPropagator.php @@ -0,0 +1,99 @@ +header = $header; + } + + public function extract(HeaderGetter $headers): SpanContext + { + // normalize header name that comes in, like php does it + $extract_header = 'HTTP_' . strtoupper(str_replace('-', '_', $this->header)); + + $data = $headers->get($extract_header); + + if (!$data) { + return new SpanContext(); + } + + $n = sscanf($data, self::CONTEXT_HEADER_FORMAT, $traceId, $spanId, $parentSpanId, $flags); + + if ($n == 0){ + return new SpanContext(); + } + + $enabled = $flags & 0x01; + + $fromHeader = true; + + return new SpanContext($traceId, $spanId, $enabled, $fromHeader); + } + + public function inject(SpanContext $context, HeaderSetter $setter) + { + $traceId = $context->traceId(); + $spanId = $context->spanId(); + $parentID = ''; // this is deprecated anyway + $enabled = $context->enabled(); + + $value = sprintf(self::CONTEXT_HEADER_FORMAT, $traceId, $spanId, $parentID, $enabled); + + if (!headers_sent()) { + header("$this->header: $value"); + } + $setter->set($this->header, $value); + } +} diff --git a/src/Trace/RequestHandler.php b/src/Trace/RequestHandler.php index 2c83426e981..0d752fc74c3 100644 --- a/src/Trace/RequestHandler.php +++ b/src/Trace/RequestHandler.php @@ -90,6 +90,7 @@ public function __construct( array $options = [] ) { $this->exporter = $exporter; + $this->propagator = $propagator; $this->headers = new ArrayHeaders($options['headers'] ?? $_SERVER); $spanContext = $propagator->extract($this->headers); @@ -113,10 +114,14 @@ public function __construct( $this->tracer = new NullTracer(); } + $rootSpanName = $this->nameFromOptions($options) ?? $this->nameFromHeaders($this->headers->toArray()); + $rootSpanAttrs = $this->spanAttrsFromOptions($options); + unset($options['root_span_options']); + $spanOptions = $options + [ 'startTime' => $this->startTimeFromHeaders($this->headers->toArray()), - 'name' => $this->nameFromHeaders($this->headers->toArray()), - 'attributes' => [], + 'name' => $rootSpanName, + 'attributes' => $rootSpanAttrs, 'kind' => Span::KIND_SERVER, 'sameProcessAsParentSpan' => false ]; @@ -268,7 +273,13 @@ private function addCommonRequestAttributes(array $headers) $this->tracer->addAttribute(Span::ATTRIBUTE_STATUS_CODE, $responseCode, [ 'spanId' => $this->rootSpan->spanId() ]); + if ($responseCode >= 400) { + $this->tracer->addAttribute('error', 'true', [ + 'spanId' => $this->rootSpan->spanId() + ]); + } } + foreach (self::ATTRIBUTE_MAP as $attributeKey => $headerKeys) { if ($val = $this->detectKey($headerKeys, $headers)) { $this->tracer->addAttribute($attributeKey, $val, [ @@ -276,6 +287,23 @@ private function addCommonRequestAttributes(array $headers) ]); } } + + if (array_key_exists('QUERY_STRING', $headers)){ + // add all query parameters as tags + parse_str($headers['QUERY_STRING'], $queryParams); + + foreach ($queryParams as $key => $value) { + if(is_array($value)){ + $value = implode(', ', $value); + } + + $key = 'http.query.params.' . $key; + + $this->tracer->addAttribute($key, $value, [ + 'spanId' => $this->rootSpan->spanId() + ]); + } + } } private function startTimeFromHeaders(array $headers) @@ -289,9 +317,32 @@ private function startTimeFromHeaders(array $headers) return null; } + private function nameFromOptions(array $options) + { + $rootSpanOptions = array_key_exists('root_span_options', $options) + ? $options['root_span_options'] + : array(); + + return array_key_exists('name', $rootSpanOptions) ? $rootSpanOptions['name'] : null; + } + + private function spanAttrsFromOptions(array $options): array + { + $rootSpanOptions = array_key_exists('root_span_options', $options) + ? $options['root_span_options'] + : array(); + return array_key_exists('attributes', $rootSpanOptions) ? $rootSpanOptions['attributes'] : array(); + } + private function nameFromHeaders(array $headers): string { - return $headers['REQUEST_URI'] ?? self::DEFAULT_ROOT_SPAN_NAME; + // omit query parameters in the span name + if (array_key_exists('REQUEST_URI', $headers) and ($headers['REQUEST_URI'])) { + return strtok($headers['REQUEST_URI'], '?'); + } + else { + return self::DEFAULT_ROOT_SPAN_NAME; + } } private function detectKey(array $keys, array $array) @@ -303,4 +354,9 @@ private function detectKey(array $keys, array $array) } return null; } + + public function inject(SpanContext $context, ArrayHeaders $headers) + { + $this->propagator->inject($context, $headers); + } } diff --git a/src/Trace/Tracer.php b/src/Trace/Tracer.php index 3c05ddb24d8..b9084167e43 100644 --- a/src/Trace/Tracer.php +++ b/src/Trace/Tracer.php @@ -23,6 +23,7 @@ use OpenCensus\Trace\Exporter\ExporterInterface; use OpenCensus\Trace\Propagator\PropagatorInterface; use OpenCensus\Trace\Propagator\HttpHeaderPropagator; +use OpenCensus\Trace\Propagator\ArrayHeaders; /** * This class provides static functions to give you access to the current @@ -301,4 +302,14 @@ public static function spanContext(): SpanContext { return isset(self::$instance) ? self::$instance->tracer()->spanContext() : new SpanContext(null, null, false); } + + public static function injectContext(ArrayHeaders $headers) + { + $context = self::spanContext(); + + if (isset(self::$instance)) + { + self::$instance->inject($context, $headers); + } + } } From 56dbb029b69903db42e62ac6fe5759fe2b3bb458 Mon Sep 17 00:00:00 2001 From: Devi A S L Date: Fri, 31 Jul 2020 18:06:41 +0530 Subject: [PATCH 02/53] dummy commit to run CI tests on nenad/opencensus https://github.com/nenad/opencensus-php/pull/41#issuecomment-667035888 From bd622c31471db7461961d02746ed5ebaaf0cf861 Mon Sep 17 00:00:00 2001 From: Devi A S L Date: Fri, 31 Jul 2020 18:55:53 +0530 Subject: [PATCH 03/53] fix php code style --- src/Trace/Integrations/PDO.php | 2 +- src/Trace/Integrations/Redis.php | 3 +-- src/Trace/Propagator/JaegerPropagator.php | 2 +- src/Trace/RequestHandler.php | 7 +++---- src/Trace/Tracer.php | 3 +-- 5 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/Trace/Integrations/PDO.php b/src/Trace/Integrations/PDO.php index 402eb4ad419..248ba254e37 100644 --- a/src/Trace/Integrations/PDO.php +++ b/src/Trace/Integrations/PDO.php @@ -147,7 +147,7 @@ public static function handleStatementExecute($statement) "44" => "With Check Option Violation" ]; - if (array_key_exists($error, $errorCodeMsgArray)){ + if (array_key_exists($error, $errorCodeMsgArray)) { $errorTags['error'] = 'true'; $errorTags['error.code'] = $errorCode; $errorTags['error.message'] = $errorCodeMsgArray[$error] ?? ''; diff --git a/src/Trace/Integrations/Redis.php b/src/Trace/Integrations/Redis.php index 7ff304c4595..57bfb133ffa 100644 --- a/src/Trace/Integrations/Redis.php +++ b/src/Trace/Integrations/Redis.php @@ -51,7 +51,7 @@ public static function load() trigger_error('opencensus extension required to load Redis integrations.', E_USER_WARNING); } - opencensus_trace_method('Predis\Client', '__construct', function ($predis, $params){ + opencensus_trace_method('Predis\Client', '__construct', function ($predis, $params) { return [ 'attributes' => [ 'peer.hostname' => $params['host'], @@ -76,7 +76,6 @@ public static function load() 'kind' => Span::KIND_CLIENT ]; }); - } diff --git a/src/Trace/Propagator/JaegerPropagator.php b/src/Trace/Propagator/JaegerPropagator.php index 731bf7ca342..5d6d77a9f1d 100644 --- a/src/Trace/Propagator/JaegerPropagator.php +++ b/src/Trace/Propagator/JaegerPropagator.php @@ -71,7 +71,7 @@ public function extract(HeaderGetter $headers): SpanContext $n = sscanf($data, self::CONTEXT_HEADER_FORMAT, $traceId, $spanId, $parentSpanId, $flags); - if ($n == 0){ + if ($n == 0) { return new SpanContext(); } diff --git a/src/Trace/RequestHandler.php b/src/Trace/RequestHandler.php index 0d752fc74c3..df83a917456 100644 --- a/src/Trace/RequestHandler.php +++ b/src/Trace/RequestHandler.php @@ -288,12 +288,12 @@ private function addCommonRequestAttributes(array $headers) } } - if (array_key_exists('QUERY_STRING', $headers)){ + if (array_key_exists('QUERY_STRING', $headers)) { // add all query parameters as tags parse_str($headers['QUERY_STRING'], $queryParams); foreach ($queryParams as $key => $value) { - if(is_array($value)){ + if (is_array($value)) { $value = implode(', ', $value); } @@ -339,8 +339,7 @@ private function nameFromHeaders(array $headers): string // omit query parameters in the span name if (array_key_exists('REQUEST_URI', $headers) and ($headers['REQUEST_URI'])) { return strtok($headers['REQUEST_URI'], '?'); - } - else { + } else { return self::DEFAULT_ROOT_SPAN_NAME; } } diff --git a/src/Trace/Tracer.php b/src/Trace/Tracer.php index b9084167e43..72e2011da19 100644 --- a/src/Trace/Tracer.php +++ b/src/Trace/Tracer.php @@ -307,8 +307,7 @@ public static function injectContext(ArrayHeaders $headers) { $context = self::spanContext(); - if (isset(self::$instance)) - { + if (isset(self::$instance)) { self::$instance->inject($context, $headers); } } From 6b911e12d77a96f7b9cd5d08c9c6fba09baf1ee5 Mon Sep 17 00:00:00 2001 From: Devi A S L Date: Wed, 5 Aug 2020 10:42:07 +0530 Subject: [PATCH 04/53] fix unit tests --- src/Trace/Integrations/Curl.php | 2 +- tests/unit/Trace/Integrations/CurlTest.php | 11 ++++------- tests/unit/Trace/Integrations/PDOTest.php | 5 +++-- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/Trace/Integrations/Curl.php b/src/Trace/Integrations/Curl.php index 4a0d6660b9d..d434ce272fe 100644 --- a/src/Trace/Integrations/Curl.php +++ b/src/Trace/Integrations/Curl.php @@ -80,7 +80,7 @@ private static function getSpanAttrsFromCurlInfo($curlInfo) 'time_to_pretransfer_in_secs' => 'pretransfer_time', 'time_to_starttransfer_in_secs' => 'starttransfer_time', 'primary_ip' => 'primary_ip', - 'url' => 'url' + 'uri' => 'url' ]; $attrs = []; diff --git a/tests/unit/Trace/Integrations/CurlTest.php b/tests/unit/Trace/Integrations/CurlTest.php index ec189522d1f..51a2d5e72e0 100644 --- a/tests/unit/Trace/Integrations/CurlTest.php +++ b/tests/unit/Trace/Integrations/CurlTest.php @@ -31,13 +31,10 @@ public function testLoadUrlFromResource() $resource = curl_init('https://www.google.com'); $spanOptions = Curl::handleCurlResource($resource); - $expected = [ - 'attributes' => [ - 'uri' => 'https://www.google.com' - ], - 'kind' => Span::KIND_CLIENT - ]; + $expected_uri = 'https://www.google.com'; + $expected_kind = Span::KIND_CLIENT; - $this->assertEquals($expected, $spanOptions); + $this->assertEquals($expected_uri, $spanOptions['attributes']['uri']); + $this->assertEquals($expected_kind, $spanOptions['kind']); } } diff --git a/tests/unit/Trace/Integrations/PDOTest.php b/tests/unit/Trace/Integrations/PDOTest.php index d79625e8f87..0ac032c8a90 100644 --- a/tests/unit/Trace/Integrations/PDOTest.php +++ b/tests/unit/Trace/Integrations/PDOTest.php @@ -34,7 +34,7 @@ public function testHandleQuery() $spanOptions = PDO::handleQuery($scope, $query); $expected = [ 'attributes' => [ - 'query' => 'select * from users' + 'db.statement' => 'select * from users' ], 'kind' => Span::KIND_CLIENT ]; @@ -48,7 +48,8 @@ public function testHandleConnect() $spanOptions = PDO::handleConnect(null, $dsn); $expected = [ 'attributes' => [ - 'dsn' => 'mysql:host=localhost;dbname=testdb' + 'dsn' => 'mysql:host=localhost;dbname=testdb', + 'db.type' => 'sql' ], 'kind' => Span::KIND_CLIENT ]; From 5bfaf75e1e709ece0dee8b43f2340344b27fd4b3 Mon Sep 17 00:00:00 2001 From: Devi Date: Thu, 22 Oct 2020 12:16:10 +0530 Subject: [PATCH 05/53] limit spans per trace (#2) * limit spans in a trace * add docker file --- ext/Dockerfile.rzp | 4 ++++ ext/README.md | 4 ++++ ext/opencensus_trace.c | 38 +++++++++++++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 ext/Dockerfile.rzp diff --git a/ext/Dockerfile.rzp b/ext/Dockerfile.rzp new file mode 100644 index 00000000000..d24316b90a9 --- /dev/null +++ b/ext/Dockerfile.rzp @@ -0,0 +1,4 @@ +FROM razorpay/onggi:php-7.2-apache-api +COPY . /app + +RUN cd /app && phpize && ./configure --enable-opencensus && make && make test && make install diff --git a/ext/README.md b/ext/README.md index c14f0c55925..411907b29e0 100644 --- a/ext/README.md +++ b/ext/README.md @@ -342,3 +342,7 @@ See [CONTRIBUTING](../CONTRIBUTING.md) for more information on how to get starte ## License Apache 2.0 - See [LICENSE](LICENSE) for more information. + +## Limiting spans in traces +SPAN_LIMIT variable in `opencensus_trace.c` defines max number of spans in any trace. This is to +contain memory bloat that's caused by holiding the spans till the request-response cycle is finished. diff --git a/ext/opencensus_trace.c b/ext/opencensus_trace.c index 9072e182284..3e8d8a21eef 100644 --- a/ext/opencensus_trace.c +++ b/ext/opencensus_trace.c @@ -21,12 +21,15 @@ #include "standard/php_math.h" #include "standard/php_rand.h" + /** * True globals for storing the original zend_execute_ex and * zend_execute_internal function pointers */ static void (*opencensus_original_zend_execute_ex) (zend_execute_data *execute_data); static void (*opencensus_original_zend_execute_internal) (zend_execute_data *execute_data, zval *return_value); +// global value for max number of spans in any trace +static int SPAN_LIMIT = 100; void opencensus_trace_ginit() { @@ -127,6 +130,25 @@ static opencensus_trace_span_t *span_from_options(zval *options) return span; } +/** + * Find number of spans in current trace + * + * Adapted from implementation of `opencensus_trace_list` function, + * which returns all the spans in current trace +*/ + +int num_spans_in_trace(){ + opencensus_trace_span_t *trace_span; + + int num_spans = 0; + + ZEND_HASH_FOREACH_PTR(OPENCENSUS_G(spans), trace_span) { + num_spans++; + } ZEND_HASH_FOREACH_END(); + + return num_spans; +} + /** * Add a attribute to the current trace span * @@ -553,6 +575,7 @@ PHP_FUNCTION(opencensus_trace_context) * opencensus_original_zend_execute_ex */ void opencensus_trace_execute_ex (zend_execute_data *execute_data TSRMLS_DC) { + zend_string *function_name = opencensus_trace_add_scope_name( EG(current_execute_data)->func->common.function_name, EG(current_execute_data)->func->common.scope @@ -567,6 +590,20 @@ void opencensus_trace_execute_ex (zend_execute_data *execute_data TSRMLS_DC) { return; } + /* + * Add span limit + * + * if the number of spans have reached the limit, + * execute the original function and return, without calling the + * trace handler. + */ + int num_spans = num_spans_in_trace(); + + if (num_spans >= SPAN_LIMIT){ + opencensus_original_zend_execute_ex(execute_data TSRMLS_CC); + return; + } + trace_handler = zend_hash_find(OPENCENSUS_G(user_traced_functions), function_name); /* Function is not registered for execution - continue normal execution */ @@ -746,4 +783,3 @@ PHP_FUNCTION(opencensus_trace_list) add_next_index_zval(return_value, &span TSRMLS_CC); } ZEND_HASH_FOREACH_END(); } - From e3be8d73a6570266acb5f921978929d1986e77bc Mon Sep 17 00:00:00 2001 From: Devi Date: Thu, 26 Nov 2020 12:28:21 +0530 Subject: [PATCH 06/53] add span.kind as a tag in PDO, redis extensions (#3) [opentracing spec](https://github.com/opentracing/specification/blob/master/semantic_conventions.md#span-tags-table) suggests to add it as a tag --- src/Trace/Integrations/PDO.php | 10 +++++++--- src/Trace/Integrations/Redis.php | 6 ++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/Trace/Integrations/PDO.php b/src/Trace/Integrations/PDO.php index 248ba254e37..4e6a55db7b0 100644 --- a/src/Trace/Integrations/PDO.php +++ b/src/Trace/Integrations/PDO.php @@ -71,7 +71,7 @@ public static function load() public static function handleQuery($pdo, $query) { return [ - 'attributes' => ['db.statement' => $query], + 'attributes' => ['db.statement' => $query, 'span.kind' => Span::KIND_CLIENT], 'kind' => Span::KIND_CLIENT ]; } @@ -86,7 +86,7 @@ public static function handleQuery($pdo, $query) */ public static function handleConnect($pdo, $dsn) { - $attributes = ['dsn' => $dsn, 'db.type' => 'sql']; + $attributes = ['dsn' => $dsn, 'db.type' => 'sql', 'span.kind' => Span::KIND_CLIENT]; return [ 'attributes' => $attributes, 'kind' => Span::KIND_CLIENT, @@ -153,7 +153,11 @@ public static function handleStatementExecute($statement) $errorTags['error.message'] = $errorCodeMsgArray[$error] ?? ''; } - $tags = ['db.statement' => $statement->queryString, 'db.row_count' => $rowCount]; + $tags = [ + 'db.statement' => $statement->queryString, + 'db.row_count' => $rowCount, + 'span.kind' => Span::KIND_CLIENT + ]; return [ 'attributes' => $tags + $errorTags, diff --git a/src/Trace/Integrations/Redis.php b/src/Trace/Integrations/Redis.php index 57bfb133ffa..660ca806735 100644 --- a/src/Trace/Integrations/Redis.php +++ b/src/Trace/Integrations/Redis.php @@ -56,7 +56,8 @@ public static function load() 'attributes' => [ 'peer.hostname' => $params['host'], 'peer.port' => $params['port'], - 'db.type' => 'redis' + 'db.type' => 'redis', + 'span.kind' => Span::KIND_CLIENT ], 'kind' => Span::KIND_CLIENT ]; @@ -71,7 +72,8 @@ public static function load() 'command' => $command->getId(), 'service.name' => 'redis', 'redis.raw_command' => $query, - 'redis.args_length' => count($arguments) + 'redis.args_length' => count($arguments), + 'span.kind' => Span::KIND_CLIENT ], 'kind' => Span::KIND_CLIENT ]; From f393a0f0f7613c25e56230d1f16d41ab96e3476b Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Mon, 25 Jan 2021 20:04:30 +0530 Subject: [PATCH 07/53] checkpoint-1 --- ext/Dockerfile.rzp | 4 +- ext/opencensus.c | 9 + ext/opencensus_trace.c | 353 ++++++++++++++++++++++++++--- ext/opencensus_trace.h | 4 + ext/php-helper/DaemonFunctions.php | 4 + 5 files changed, 343 insertions(+), 31 deletions(-) diff --git a/ext/Dockerfile.rzp b/ext/Dockerfile.rzp index d24316b90a9..a549a8a14b6 100644 --- a/ext/Dockerfile.rzp +++ b/ext/Dockerfile.rzp @@ -1,4 +1,4 @@ -FROM razorpay/onggi:php-7.2-apache-api +FROM c.rzp.io/razorpay/onggi:php-7.2-apache-api COPY . /app -RUN cd /app && phpize && ./configure --enable-opencensus && make && make test && make install +RUN cd /app && phpize && ./configure --enable-opencensus && make && make install diff --git a/ext/opencensus.c b/ext/opencensus.c index fa524bfed6a..41720445da9 100644 --- a/ext/opencensus.c +++ b/ext/opencensus.c @@ -54,6 +54,12 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_opencensus_trace_add_attribute, 0, 0, 2) ZEND_ARG_ARRAY_INFO(0, options, 0) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO_EX(arginfo_opencensus_trace_remove_span, 0, 0, 2) + ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0) + ZEND_ARG_TYPE_INFO(0, value, IS_STRING, 0) + ZEND_ARG_ARRAY_INFO(0, options, 0) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(arginfo_opencensus_trace_add_annotation, 0, 0, 1) ZEND_ARG_TYPE_INFO(0, description, IS_STRING, 0) ZEND_ARG_ARRAY_INFO(0, options, 0) @@ -90,12 +96,15 @@ static zend_function_entry opencensus_functions[] = { PHP_FE(opencensus_trace_function, arginfo_opencensus_trace_function) PHP_FE(opencensus_trace_method, arginfo_opencensus_trace_method) PHP_FE(opencensus_trace_list, NULL) +// PHP_FE(opencensus_trace_count, 0) PHP_FE(opencensus_trace_begin, arginfo_opencensus_trace_begin) PHP_FE(opencensus_trace_finish, NULL) PHP_FE(opencensus_trace_clear, NULL) +// PHP_FE(opencensus_trace_clr, NULL) PHP_FE(opencensus_trace_set_context, arginfo_opencensus_trace_set_context) PHP_FE(opencensus_trace_context, NULL) PHP_FE(opencensus_trace_add_attribute, arginfo_opencensus_trace_add_attribute) + PHP_FE(opencensus_trace_remove_span, arginfo_opencensus_trace_remove_span) PHP_FE(opencensus_trace_add_annotation, arginfo_opencensus_trace_add_annotation) PHP_FE(opencensus_trace_add_link, arginfo_opencensus_trace_add_link) PHP_FE(opencensus_trace_add_message_event, arginfo_opencensus_trace_add_message_event) diff --git a/ext/opencensus_trace.c b/ext/opencensus_trace.c index 3e8d8a21eef..27092ce9788 100644 --- a/ext/opencensus_trace.c +++ b/ext/opencensus_trace.c @@ -20,6 +20,8 @@ #include "Zend/zend_exceptions.h" #include "standard/php_math.h" #include "standard/php_rand.h" +#include +#include /** @@ -28,7 +30,6 @@ */ static void (*opencensus_original_zend_execute_ex) (zend_execute_data *execute_data); static void (*opencensus_original_zend_execute_internal) (zend_execute_data *execute_data, zval *return_value); -// global value for max number of spans in any trace static int SPAN_LIMIT = 100; void opencensus_trace_ginit() @@ -54,7 +55,7 @@ void opencensus_trace_gshutdown() void opencensus_trace_rinit() { /* initialize storage for user traced functions - per request basis */ - ALLOC_HASHTABLE(OPENCENSUS_G(user_traced_functions)); + ALLOC_HASHTABLE(OPENCENSUS_G(user_traced_functions)); zend_hash_init(OPENCENSUS_G(user_traced_functions), 16, NULL, ZVAL_PTR_DTOR, 0); /* initialize storage for recorded spans - per request basis */ @@ -83,6 +84,15 @@ void opencensus_trace_rshutdown() */ static zend_string *span_id_from_options(HashTable *options) { + FILE *fptr5 = fopen("sampletestt.txt", "a"); + + if (fptr5 != NULL) + { + fprintf(fptr5, "span_from_options\n"); + + fclose(fptr5); + } + zval *val; zend_string *str = NULL; if (options == NULL) { @@ -137,17 +147,17 @@ static opencensus_trace_span_t *span_from_options(zval *options) * which returns all the spans in current trace */ -int num_spans_in_trace(){ - opencensus_trace_span_t *trace_span; - - int num_spans = 0; - - ZEND_HASH_FOREACH_PTR(OPENCENSUS_G(spans), trace_span) { - num_spans++; - } ZEND_HASH_FOREACH_END(); - - return num_spans; -} +//int num_spans_in_trace(){ +// opencensus_trace_span_t *trace_span; +// +// int num_spans = 0; +// +// ZEND_HASH_FOREACH_PTR(OPENCENSUS_G(spans), trace_span) { +// num_spans++; +// } ZEND_HASH_FOREACH_END(); +// +// return num_spans; +//} /** * Add a attribute to the current trace span @@ -158,6 +168,14 @@ int num_spans_in_trace(){ */ PHP_FUNCTION(opencensus_trace_add_attribute) { + FILE *fptr5 = fopen("sampletestt.txt", "a"); + + if (fptr5 != NULL) + { + fprintf(fptr5, "php_function_opencensus_trace_add_attribute\n"); + + fclose(fptr5); + } zend_string *k, *v; opencensus_trace_span_t *span; zval *options = NULL; @@ -182,6 +200,81 @@ PHP_FUNCTION(opencensus_trace_add_attribute) RETURN_FALSE; } +PHP_FUNCTION(opencensus_trace_remove_span) +{ + zend_string *k, *v; + opencensus_trace_span_t *span; + zval *options = NULL; + + FILE *fptr = fopen("sampletestt.txt", "a"); + + if (fptr != NULL) + { + + fprintf(fptr, "\nopencensus_trace_remove_span 1"); + + fclose(fptr); + } + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SS|a", &k, &v, &options) == FAILURE) { + RETURN_FALSE; + } + + FILE *fptr2 = fopen("sampletestt.txt", "a"); + + if (fptr2 != NULL) + { + fprintf(fptr2, "opencensus_trace_remove_span 2"); + + fclose(fptr2); + } + + span = span_from_options(options); + + if (span == NULL) { + RETURN_FALSE; + } + + FILE *fptr3 = fopen("sampletestt.txt", "a"); + + if (fptr3 != NULL) + { + fprintf(fptr3, "opencensus_trace_remove_span 3"); + + fclose(fptr3); + } + +// opencensus_trace_span_free(span); +// +// FILE *fptr4 = fopen("sampletestt.txt", "a"); +// +// if (fptr4 != NULL) +// { +// fprintf(fptr4, "opencensus_trace_remove_span 4"); +// +// fclose(fptr4); +// } + + + if (zend_hash_del(OPENCENSUS_G(spans), k) == SUCCESS) { +// php_printf("Removed value at key"); + FILE *fptr5 = fopen("sampletestt.txt", "a"); + + if (fptr5 != NULL) + { + fprintf(fptr5, "%s", ZSTR_VAL(k)); + fprintf(fptr5, "opencensus_trace_remove_span 5"); + + fclose(fptr5); + } + + } + + + + RETURN_TRUE; +} + /** * Add an annotation to the current trace span * @@ -191,6 +284,14 @@ PHP_FUNCTION(opencensus_trace_add_attribute) */ PHP_FUNCTION(opencensus_trace_add_annotation) { + FILE *fptr5 = fopen("sampletestt.txt", "a"); + + if (fptr5 != NULL) + { + fprintf(fptr5, "php_function_opencensus_trace_add_annotation\n"); + + fclose(fptr5); + } zend_string *description; opencensus_trace_span_t *span; zval *options = NULL; @@ -225,6 +326,14 @@ PHP_FUNCTION(opencensus_trace_add_annotation) */ PHP_FUNCTION(opencensus_trace_add_link) { + FILE *fptr5 = fopen("sampletestt.txt", "a"); + + if (fptr5 != NULL) + { + fprintf(fptr5, "php_function_opencensus_trace_add_link\n"); + + fclose(fptr5); + } zend_string *trace_id, *span_id; opencensus_trace_span_t *span; zval *options = NULL; @@ -315,6 +424,14 @@ static void opencensus_free_args(zval *args, int num_args) */ static int opencensus_trace_call_user_function_callback(zval *args, int num_args, zend_execute_data *execute_data, opencensus_trace_span_t *span, zval *callback, zval *callback_result TSRMLS_DC) { + FILE *fptr5 = fopen("sampletestt.txt", "a"); + + if (fptr5 != NULL) + { + fprintf(fptr5, "opencensus_trace_call_user_function_callback\n"); + + fclose(fptr5); + } if (call_user_function_ex(EG(function_table), NULL, callback, callback_result, num_args, args, 0, NULL) != SUCCESS) { return FAILURE; } @@ -343,6 +460,8 @@ static int opencensus_trace_call_user_function_callback(zval *args, int num_args static zend_string *generate_span_id() { zval zv; + + #if PHP_VERSION_ID < 70100 if (!BG(mt_rand_is_seeded)) { php_mt_srand(GENERATE_SEED()); @@ -359,7 +478,17 @@ static zend_string *generate_span_id() */ static opencensus_trace_span_t *opencensus_trace_begin(zend_string *name, zend_execute_data *execute_data, zend_string *span_id TSRMLS_DC) { + opencensus_trace_span_t *span = opencensus_trace_span_alloc(); +// opencensus_trace_span_t parentSpan; + + FILE *fptr = fopen("sampletestt.txt", "a"); + if (fptr != NULL) + { + fprintf(fptr, "in trace begin\n"); + fclose(fptr); + } + zend_fetch_debug_backtrace(&span->stackTrace, 1, DEBUG_BACKTRACE_IGNORE_ARGS, 0); @@ -371,11 +500,35 @@ static opencensus_trace_span_t *opencensus_trace_begin(zend_string *name, zend_e span->span_id = generate_span_id(); } + FILE *fptr1 = fopen("sampletestt.txt", "a"); + if (fptr1 != NULL) + { + fprintf(fptr1, "in trace begin %s\n", ZSTR_VAL(span->span_id)); + fclose(fptr1); + } + +// if (!OPENCENSUS_G(current_span)) { +// +// if (OPENCENSUS_G(current_span_value).parent) { +// OPENCENSUS_G(current_span) = &OPENCENSUS_G(current_span_value); +// } +// } + if (OPENCENSUS_G(current_span)) { span->parent = OPENCENSUS_G(current_span); - } + } +// parentSpan = *OPENCENSUS_G(current_span); +// } else if (parentSpan.parent){ +// span->parent = &parentSpan; +// } + +// else if (OPENCENSUS_G(current_span_value).parent){ +// span->parent = &OPENCENSUS_G(current_span_value); +// } OPENCENSUS_G(current_span) = span; +// OPENCENSUS_G(current_span_value) = *span; + /* add the span to the list of spans */ zend_hash_add_ptr(OPENCENSUS_G(spans), span->span_id, span); @@ -391,15 +544,41 @@ static int opencensus_trace_finish() { opencensus_trace_span_t *span = OPENCENSUS_G(current_span); + FILE *fptr4 = fopen("sampletestt.txt", "a"); + + if (fptr4 != NULL) + { + fprintf(fptr4, "\nopencensus_trace_finish"); + + fclose(fptr4); + } + if (!span) { return FAILURE; } + FILE *fptr1 = fopen("sampletestt.txt", "a"); + + if (fptr1 != NULL) + { + fprintf(fptr1, "opencensus_trace_finish1"); + + fclose(fptr1); + } + /* set current time for now */ span->stop = opencensus_now(); OPENCENSUS_G(current_span) = span->parent; + FILE *fptr5 = fopen("sampletestt.txt", "a"); + + if (fptr5 != NULL) + { + fprintf(fptr5, "opencensus_trace_finish2\n"); + + fclose(fptr5); + } return SUCCESS; } @@ -444,6 +623,14 @@ static zend_string *opencensus_trace_add_scope_name(zend_string *function_name, */ PHP_FUNCTION(opencensus_trace_begin) { + FILE *fptr5 = fopen("sampletestt.txt", "a"); + + if (fptr5 != NULL) + { + fprintf(fptr5, "php_function_opencensus_trace_begin\n"); + + fclose(fptr5); + } zend_string *function_name, *span_id; zval *span_options = NULL, default_span_options; opencensus_trace_span_t *span; @@ -476,6 +663,15 @@ PHP_FUNCTION(opencensus_trace_begin) */ PHP_FUNCTION(opencensus_trace_finish) { + FILE *fptr5 = fopen("sampletestt.txt", "a"); + + if (fptr5 != NULL) + { + fprintf(fptr5, "php_function_opencensus_trace_finish\n"); + + fclose(fptr5); + } + if (opencensus_trace_finish() == SUCCESS) { RETURN_TRUE; } @@ -484,6 +680,15 @@ PHP_FUNCTION(opencensus_trace_finish) void span_dtor(zval *zv) { + + FILE *fptr5 = fopen("sampletestt.txt", "a"); + + if (fptr5 != NULL) + { + fprintf(fptr5, "span_dtor\n"); + + fclose(fptr5); + } opencensus_trace_span_t *span = Z_PTR_P(zv); opencensus_trace_span_free(span); ZVAL_PTR_DTOR(zv); @@ -495,6 +700,14 @@ void span_dtor(zval *zv) */ void opencensus_trace_clear(int reset TSRMLS_DC) { + FILE *fptr5 = fopen("sampletestt.txt", "a"); + + if (fptr5 != NULL) + { + fprintf(fptr5, "opencensus_trace_clear\n"); + + fclose(fptr5); + } /* free the hashtable */ zend_hash_destroy(OPENCENSUS_G(spans)); FREE_HASHTABLE(OPENCENSUS_G(spans)); @@ -517,6 +730,41 @@ void opencensus_trace_clear(int reset TSRMLS_DC) } } +/** + * Reset the list of spans and free any allocated memory used. + * If reset is set, reallocate request globals so we can start capturing spans. + */ +//void opencensus_trace_span_clr() +//{ +// zval *span_ids = NULL; + +// if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SS|a", &span_id) == FAILURE) { +// RETURN_FALSE; +// } + +// FILE *fptr = fopen("sampletestt.txt", "a"); +// if (fptr != NULL) +// { +// fprintf(fptr, "test shivam"); +// fclose(fptr); +// } + +// opencensus_trace_span_free(span); + +// opencensus_trace_finish(); + +// zend_hash_destroy(OPENCENSUS_G(spans)); +// FREE_HASHTABLE(OPENCENSUS_G(spans)); + + /* reallocate and setup the hashtable for captured spans */ + +// ALLOC_HASHTABLE(OPENCENSUS_G(spans)); +// zend_hash_init(OPENCENSUS_G(spans), 16, NULL, span_dtor, 0); + + +// OPENCENSUS_G(current_span) = NULL; +//} + /** * Reset the list of spans * @@ -528,6 +776,13 @@ PHP_FUNCTION(opencensus_trace_clear) RETURN_TRUE; } +//PHP_FUNCTION(opencensus_trace_clr) +//{ +// opencensus_trace_clr(1 TSRMLS_CC); +// RETURN_TRUE +//} + + /** * Set the initial trace context * @@ -556,6 +811,14 @@ PHP_FUNCTION(opencensus_trace_set_context) */ PHP_FUNCTION(opencensus_trace_context) { + FILE *fptr5 = fopen("sampletestt.txt", "a"); + + if (fptr5 != NULL) + { + fprintf(fptr5, "opencensus_trace_context\n"); + + fclose(fptr5); + } opencensus_trace_span_t *span = OPENCENSUS_G(current_span); object_init_ex(return_value, opencensus_trace_context_ce); @@ -576,6 +839,14 @@ PHP_FUNCTION(opencensus_trace_context) */ void opencensus_trace_execute_ex (zend_execute_data *execute_data TSRMLS_DC) { +// FILE *fptr5 = fopen("sampletestt.txt", "a"); +// +// if (fptr5 != NULL) +// { +// fprintf(fptr5, "opencensus_trace_execute_ex\n"); +// +// fclose(fptr5); +// } zend_string *function_name = opencensus_trace_add_scope_name( EG(current_execute_data)->func->common.function_name, EG(current_execute_data)->func->common.scope @@ -590,20 +861,6 @@ void opencensus_trace_execute_ex (zend_execute_data *execute_data TSRMLS_DC) { return; } - /* - * Add span limit - * - * if the number of spans have reached the limit, - * execute the original function and return, without calling the - * trace handler. - */ - int num_spans = num_spans_in_trace(); - - if (num_spans >= SPAN_LIMIT){ - opencensus_original_zend_execute_ex(execute_data TSRMLS_CC); - return; - } - trace_handler = zend_hash_find(OPENCENSUS_G(user_traced_functions), function_name); /* Function is not registered for execution - continue normal execution */ @@ -634,6 +891,14 @@ void opencensus_trace_execute_ex (zend_execute_data *execute_data TSRMLS_DC) { opencensus_trace_span_apply_span_options(span, trace_handler); } } +// FILE *fptr4 = fopen("sampletestt.txt", "a"); +// +// if (fptr4 != NULL) +// { +// fprintf(fptr4, "opencensus_trace_execute_ex_end\n"); +// +// fclose(fptr4); +// } zend_string_release(callback_name); opencensus_trace_finish(); } @@ -671,8 +936,23 @@ void opencensus_trace_execute_internal(INTERNAL_FUNCTION_PARAMETERS) return; } +// FILE *fptr9 = fopen("sampletestt.txt", "a"); +// +// if (fptr9 != NULL) +// { +// fprintf(fptr9, "\nopencensus_trace_execute_internal 9"); +// +// fclose(fptr9); +// } trace_handler = zend_hash_find(OPENCENSUS_G(user_traced_functions), function_name); - +// FILE *fptr = fopen("sampletestt.txt", "a"); +// +// if (fptr != NULL) +// { +// fprintf(fptr, "\nopencensus_trace_execute_internal 99"); +// +// fclose(fptr); +// } /* Function is not registered for execution - continue normal execution */ if (trace_handler == NULL) { resume_execute_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU); @@ -683,6 +963,7 @@ void opencensus_trace_execute_internal(INTERNAL_FUNCTION_PARAMETERS) span = opencensus_trace_begin(function_name, execute_data, NULL TSRMLS_CC); zend_string_release(function_name); + if (zend_is_callable(trace_handler, 0, &callback_name)) { /* Registered handler is callable - execute the callback */ zval callback_result, *args; @@ -702,6 +983,15 @@ void opencensus_trace_execute_internal(INTERNAL_FUNCTION_PARAMETERS) } } zend_string_release(callback_name); + + FILE *fptr4 = fopen("sampletestt.txt", "a"); + + if (fptr4 != NULL) + { + fprintf(fptr4, "\nopencensus_trace_relese_callback"); + + fclose(fptr4); + } opencensus_trace_finish(); } @@ -783,3 +1073,8 @@ PHP_FUNCTION(opencensus_trace_list) add_next_index_zval(return_value, &span TSRMLS_CC); } ZEND_HASH_FOREACH_END(); } + +//PHP_FUNCTION(opencensus_trace_count) +//{ +// RETURN_LONG(SPAN_COUNT) +//} diff --git a/ext/opencensus_trace.h b/ext/opencensus_trace.h index 730c0cc2e09..a089108487a 100644 --- a/ext/opencensus_trace.h +++ b/ext/opencensus_trace.h @@ -28,12 +28,15 @@ PHP_FUNCTION(opencensus_trace_function); PHP_FUNCTION(opencensus_trace_method); PHP_FUNCTION(opencensus_trace_list); +//PHP_FUNCTION(opencensus_trace_count); PHP_FUNCTION(opencensus_trace_begin); PHP_FUNCTION(opencensus_trace_finish); PHP_FUNCTION(opencensus_trace_clear); +//PHP_FUNCTION(opencensus_trace_clr); PHP_FUNCTION(opencensus_trace_set_context); PHP_FUNCTION(opencensus_trace_context); PHP_FUNCTION(opencensus_trace_add_attribute); +PHP_FUNCTION(opencensus_trace_remove_span); PHP_FUNCTION(opencensus_trace_add_annotation); PHP_FUNCTION(opencensus_trace_add_link); PHP_FUNCTION(opencensus_trace_add_message_event); @@ -43,6 +46,7 @@ void opencensus_trace_gshutdown(); void opencensus_trace_rinit(); void opencensus_trace_rshutdown(); void opencensus_trace_clear(int reset TSRMLS_DC); +//void opencensus_trace_clr(int reset TSRMLS_DC); void opencensus_trace_execute_ex (zend_execute_data *execute_data TSRMLS_DC); void opencensus_trace_execute_internal(INTERNAL_FUNCTION_PARAMETERS); void span_dtor(zval *zv); diff --git a/ext/php-helper/DaemonFunctions.php b/ext/php-helper/DaemonFunctions.php index d4583f45b5d..1dd031ae8e6 100644 --- a/ext/php-helper/DaemonFunctions.php +++ b/ext/php-helper/DaemonFunctions.php @@ -101,6 +101,10 @@ function opencensus_trace_set_context($traceId, $parentSpanId = null): void { */ function opencensus_trace_add_attribute($key, $value, $options = []): void { +} + +function opencensus_trace_remove_span($key, $value, $options = []): void { + } /** * Add an annotation to a span From 2d81ac2a618fb4ca4c9a8c3496b62b400dae331a Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Tue, 26 Jan 2021 18:10:40 +0530 Subject: [PATCH 08/53] modified removed span method --- ext/opencensus.c | 6 +- ext/opencensus_trace.c | 327 ++------------------------- ext/opencensus_trace.h | 3 - ext/php-helper/DaemonFunctions.php | 2 +- src/Trace/Integrations/Curl.php | 13 ++ src/Trace/Integrations/PDO.php | 21 ++ src/Trace/Integrations/Redis.php | 17 ++ src/Trace/RequestHandler.php | 2 +- src/Trace/Tracer/ExtensionTracer.php | 46 +++- 9 files changed, 113 insertions(+), 324 deletions(-) diff --git a/ext/opencensus.c b/ext/opencensus.c index 41720445da9..9966606d397 100644 --- a/ext/opencensus.c +++ b/ext/opencensus.c @@ -54,10 +54,8 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_opencensus_trace_add_attribute, 0, 0, 2) ZEND_ARG_ARRAY_INFO(0, options, 0) ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_INFO_EX(arginfo_opencensus_trace_remove_span, 0, 0, 2) +ZEND_BEGIN_ARG_INFO_EX(arginfo_opencensus_trace_remove_span, 0, 0, 1) ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0) - ZEND_ARG_TYPE_INFO(0, value, IS_STRING, 0) - ZEND_ARG_ARRAY_INFO(0, options, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_opencensus_trace_add_annotation, 0, 0, 1) @@ -96,11 +94,9 @@ static zend_function_entry opencensus_functions[] = { PHP_FE(opencensus_trace_function, arginfo_opencensus_trace_function) PHP_FE(opencensus_trace_method, arginfo_opencensus_trace_method) PHP_FE(opencensus_trace_list, NULL) -// PHP_FE(opencensus_trace_count, 0) PHP_FE(opencensus_trace_begin, arginfo_opencensus_trace_begin) PHP_FE(opencensus_trace_finish, NULL) PHP_FE(opencensus_trace_clear, NULL) -// PHP_FE(opencensus_trace_clr, NULL) PHP_FE(opencensus_trace_set_context, arginfo_opencensus_trace_set_context) PHP_FE(opencensus_trace_context, NULL) PHP_FE(opencensus_trace_add_attribute, arginfo_opencensus_trace_add_attribute) diff --git a/ext/opencensus_trace.c b/ext/opencensus_trace.c index 27092ce9788..daa638e5bc4 100644 --- a/ext/opencensus_trace.c +++ b/ext/opencensus_trace.c @@ -84,15 +84,6 @@ void opencensus_trace_rshutdown() */ static zend_string *span_id_from_options(HashTable *options) { - FILE *fptr5 = fopen("sampletestt.txt", "a"); - - if (fptr5 != NULL) - { - fprintf(fptr5, "span_from_options\n"); - - fclose(fptr5); - } - zval *val; zend_string *str = NULL; if (options == NULL) { @@ -140,25 +131,6 @@ static opencensus_trace_span_t *span_from_options(zval *options) return span; } -/** - * Find number of spans in current trace - * - * Adapted from implementation of `opencensus_trace_list` function, - * which returns all the spans in current trace -*/ - -//int num_spans_in_trace(){ -// opencensus_trace_span_t *trace_span; -// -// int num_spans = 0; -// -// ZEND_HASH_FOREACH_PTR(OPENCENSUS_G(spans), trace_span) { -// num_spans++; -// } ZEND_HASH_FOREACH_END(); -// -// return num_spans; -//} - /** * Add a attribute to the current trace span * @@ -168,14 +140,6 @@ static opencensus_trace_span_t *span_from_options(zval *options) */ PHP_FUNCTION(opencensus_trace_add_attribute) { - FILE *fptr5 = fopen("sampletestt.txt", "a"); - - if (fptr5 != NULL) - { - fprintf(fptr5, "php_function_opencensus_trace_add_attribute\n"); - - fclose(fptr5); - } zend_string *k, *v; opencensus_trace_span_t *span; zval *options = NULL; @@ -200,77 +164,25 @@ PHP_FUNCTION(opencensus_trace_add_attribute) RETURN_FALSE; } +/** + * Removes a span corresponding to key/span_id from hashtable + * @param string $key + * @return bool + */ PHP_FUNCTION(opencensus_trace_remove_span) { - zend_string *k, *v; - opencensus_trace_span_t *span; - zval *options = NULL; - - FILE *fptr = fopen("sampletestt.txt", "a"); - - if (fptr != NULL) - { - - fprintf(fptr, "\nopencensus_trace_remove_span 1"); - - fclose(fptr); - } - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SS|a", &k, &v, &options) == FAILURE) { - RETURN_FALSE; - } - - FILE *fptr2 = fopen("sampletestt.txt", "a"); - - if (fptr2 != NULL) - { - fprintf(fptr2, "opencensus_trace_remove_span 2"); - - fclose(fptr2); - } - - span = span_from_options(options); + zend_string *k; - if (span == NULL) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S", &k) == FAILURE) { RETURN_FALSE; } - FILE *fptr3 = fopen("sampletestt.txt", "a"); - - if (fptr3 != NULL) - { - fprintf(fptr3, "opencensus_trace_remove_span 3"); - - fclose(fptr3); + if (zend_hash_del(OPENCENSUS_G(spans), k) != SUCCESS) { + RETURN_FALSE } -// opencensus_trace_span_free(span); -// -// FILE *fptr4 = fopen("sampletestt.txt", "a"); -// -// if (fptr4 != NULL) -// { -// fprintf(fptr4, "opencensus_trace_remove_span 4"); -// -// fclose(fptr4); -// } - - - if (zend_hash_del(OPENCENSUS_G(spans), k) == SUCCESS) { -// php_printf("Removed value at key"); - FILE *fptr5 = fopen("sampletestt.txt", "a"); - - if (fptr5 != NULL) - { - fprintf(fptr5, "%s", ZSTR_VAL(k)); - fprintf(fptr5, "opencensus_trace_remove_span 5"); - - fclose(fptr5); - } - - } - - + // release zend string + zend_string_release(k); RETURN_TRUE; } @@ -284,14 +196,6 @@ PHP_FUNCTION(opencensus_trace_remove_span) */ PHP_FUNCTION(opencensus_trace_add_annotation) { - FILE *fptr5 = fopen("sampletestt.txt", "a"); - - if (fptr5 != NULL) - { - fprintf(fptr5, "php_function_opencensus_trace_add_annotation\n"); - - fclose(fptr5); - } zend_string *description; opencensus_trace_span_t *span; zval *options = NULL; @@ -326,14 +230,6 @@ PHP_FUNCTION(opencensus_trace_add_annotation) */ PHP_FUNCTION(opencensus_trace_add_link) { - FILE *fptr5 = fopen("sampletestt.txt", "a"); - - if (fptr5 != NULL) - { - fprintf(fptr5, "php_function_opencensus_trace_add_link\n"); - - fclose(fptr5); - } zend_string *trace_id, *span_id; opencensus_trace_span_t *span; zval *options = NULL; @@ -424,14 +320,6 @@ static void opencensus_free_args(zval *args, int num_args) */ static int opencensus_trace_call_user_function_callback(zval *args, int num_args, zend_execute_data *execute_data, opencensus_trace_span_t *span, zval *callback, zval *callback_result TSRMLS_DC) { - FILE *fptr5 = fopen("sampletestt.txt", "a"); - - if (fptr5 != NULL) - { - fprintf(fptr5, "opencensus_trace_call_user_function_callback\n"); - - fclose(fptr5); - } if (call_user_function_ex(EG(function_table), NULL, callback, callback_result, num_args, args, 0, NULL) != SUCCESS) { return FAILURE; } @@ -480,16 +368,6 @@ static opencensus_trace_span_t *opencensus_trace_begin(zend_string *name, zend_e { opencensus_trace_span_t *span = opencensus_trace_span_alloc(); -// opencensus_trace_span_t parentSpan; - - FILE *fptr = fopen("sampletestt.txt", "a"); - if (fptr != NULL) - { - fprintf(fptr, "in trace begin\n"); - fclose(fptr); - } - - zend_fetch_debug_backtrace(&span->stackTrace, 1, DEBUG_BACKTRACE_IGNORE_ARGS, 0); span->start = opencensus_now(); @@ -500,35 +378,11 @@ static opencensus_trace_span_t *opencensus_trace_begin(zend_string *name, zend_e span->span_id = generate_span_id(); } - FILE *fptr1 = fopen("sampletestt.txt", "a"); - if (fptr1 != NULL) - { - fprintf(fptr1, "in trace begin %s\n", ZSTR_VAL(span->span_id)); - fclose(fptr1); - } - -// if (!OPENCENSUS_G(current_span)) { -// -// if (OPENCENSUS_G(current_span_value).parent) { -// OPENCENSUS_G(current_span) = &OPENCENSUS_G(current_span_value); -// } -// } - if (OPENCENSUS_G(current_span)) { span->parent = OPENCENSUS_G(current_span); - } -// parentSpan = *OPENCENSUS_G(current_span); -// } else if (parentSpan.parent){ -// span->parent = &parentSpan; -// } - -// else if (OPENCENSUS_G(current_span_value).parent){ -// span->parent = &OPENCENSUS_G(current_span_value); -// } + } OPENCENSUS_G(current_span) = span; -// OPENCENSUS_G(current_span_value) = *span; - /* add the span to the list of spans */ zend_hash_add_ptr(OPENCENSUS_G(spans), span->span_id, span); @@ -544,41 +398,15 @@ static int opencensus_trace_finish() { opencensus_trace_span_t *span = OPENCENSUS_G(current_span); - FILE *fptr4 = fopen("sampletestt.txt", "a"); - - if (fptr4 != NULL) - { - fprintf(fptr4, "\nopencensus_trace_finish"); - - fclose(fptr4); - } - if (!span) { return FAILURE; } - FILE *fptr1 = fopen("sampletestt.txt", "a"); - - if (fptr1 != NULL) - { - fprintf(fptr1, "opencensus_trace_finish1"); - - fclose(fptr1); - } - /* set current time for now */ span->stop = opencensus_now(); OPENCENSUS_G(current_span) = span->parent; - FILE *fptr5 = fopen("sampletestt.txt", "a"); - - if (fptr5 != NULL) - { - fprintf(fptr5, "opencensus_trace_finish2\n"); - - fclose(fptr5); - } return SUCCESS; } @@ -623,14 +451,6 @@ static zend_string *opencensus_trace_add_scope_name(zend_string *function_name, */ PHP_FUNCTION(opencensus_trace_begin) { - FILE *fptr5 = fopen("sampletestt.txt", "a"); - - if (fptr5 != NULL) - { - fprintf(fptr5, "php_function_opencensus_trace_begin\n"); - - fclose(fptr5); - } zend_string *function_name, *span_id; zval *span_options = NULL, default_span_options; opencensus_trace_span_t *span; @@ -663,15 +483,6 @@ PHP_FUNCTION(opencensus_trace_begin) */ PHP_FUNCTION(opencensus_trace_finish) { - FILE *fptr5 = fopen("sampletestt.txt", "a"); - - if (fptr5 != NULL) - { - fprintf(fptr5, "php_function_opencensus_trace_finish\n"); - - fclose(fptr5); - } - if (opencensus_trace_finish() == SUCCESS) { RETURN_TRUE; } @@ -680,15 +491,6 @@ PHP_FUNCTION(opencensus_trace_finish) void span_dtor(zval *zv) { - - FILE *fptr5 = fopen("sampletestt.txt", "a"); - - if (fptr5 != NULL) - { - fprintf(fptr5, "span_dtor\n"); - - fclose(fptr5); - } opencensus_trace_span_t *span = Z_PTR_P(zv); opencensus_trace_span_free(span); ZVAL_PTR_DTOR(zv); @@ -700,14 +502,6 @@ void span_dtor(zval *zv) */ void opencensus_trace_clear(int reset TSRMLS_DC) { - FILE *fptr5 = fopen("sampletestt.txt", "a"); - - if (fptr5 != NULL) - { - fprintf(fptr5, "opencensus_trace_clear\n"); - - fclose(fptr5); - } /* free the hashtable */ zend_hash_destroy(OPENCENSUS_G(spans)); FREE_HASHTABLE(OPENCENSUS_G(spans)); @@ -730,41 +524,6 @@ void opencensus_trace_clear(int reset TSRMLS_DC) } } -/** - * Reset the list of spans and free any allocated memory used. - * If reset is set, reallocate request globals so we can start capturing spans. - */ -//void opencensus_trace_span_clr() -//{ -// zval *span_ids = NULL; - -// if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SS|a", &span_id) == FAILURE) { -// RETURN_FALSE; -// } - -// FILE *fptr = fopen("sampletestt.txt", "a"); -// if (fptr != NULL) -// { -// fprintf(fptr, "test shivam"); -// fclose(fptr); -// } - -// opencensus_trace_span_free(span); - -// opencensus_trace_finish(); - -// zend_hash_destroy(OPENCENSUS_G(spans)); -// FREE_HASHTABLE(OPENCENSUS_G(spans)); - - /* reallocate and setup the hashtable for captured spans */ - -// ALLOC_HASHTABLE(OPENCENSUS_G(spans)); -// zend_hash_init(OPENCENSUS_G(spans), 16, NULL, span_dtor, 0); - - -// OPENCENSUS_G(current_span) = NULL; -//} - /** * Reset the list of spans * @@ -776,13 +535,6 @@ PHP_FUNCTION(opencensus_trace_clear) RETURN_TRUE; } -//PHP_FUNCTION(opencensus_trace_clr) -//{ -// opencensus_trace_clr(1 TSRMLS_CC); -// RETURN_TRUE -//} - - /** * Set the initial trace context * @@ -811,14 +563,6 @@ PHP_FUNCTION(opencensus_trace_set_context) */ PHP_FUNCTION(opencensus_trace_context) { - FILE *fptr5 = fopen("sampletestt.txt", "a"); - - if (fptr5 != NULL) - { - fprintf(fptr5, "opencensus_trace_context\n"); - - fclose(fptr5); - } opencensus_trace_span_t *span = OPENCENSUS_G(current_span); object_init_ex(return_value, opencensus_trace_context_ce); @@ -839,14 +583,6 @@ PHP_FUNCTION(opencensus_trace_context) */ void opencensus_trace_execute_ex (zend_execute_data *execute_data TSRMLS_DC) { -// FILE *fptr5 = fopen("sampletestt.txt", "a"); -// -// if (fptr5 != NULL) -// { -// fprintf(fptr5, "opencensus_trace_execute_ex\n"); -// -// fclose(fptr5); -// } zend_string *function_name = opencensus_trace_add_scope_name( EG(current_execute_data)->func->common.function_name, EG(current_execute_data)->func->common.scope @@ -891,14 +627,7 @@ void opencensus_trace_execute_ex (zend_execute_data *execute_data TSRMLS_DC) { opencensus_trace_span_apply_span_options(span, trace_handler); } } -// FILE *fptr4 = fopen("sampletestt.txt", "a"); -// -// if (fptr4 != NULL) -// { -// fprintf(fptr4, "opencensus_trace_execute_ex_end\n"); -// -// fclose(fptr4); -// } + zend_string_release(callback_name); opencensus_trace_finish(); } @@ -936,23 +665,8 @@ void opencensus_trace_execute_internal(INTERNAL_FUNCTION_PARAMETERS) return; } -// FILE *fptr9 = fopen("sampletestt.txt", "a"); -// -// if (fptr9 != NULL) -// { -// fprintf(fptr9, "\nopencensus_trace_execute_internal 9"); -// -// fclose(fptr9); -// } trace_handler = zend_hash_find(OPENCENSUS_G(user_traced_functions), function_name); -// FILE *fptr = fopen("sampletestt.txt", "a"); -// -// if (fptr != NULL) -// { -// fprintf(fptr, "\nopencensus_trace_execute_internal 99"); -// -// fclose(fptr); -// } + /* Function is not registered for execution - continue normal execution */ if (trace_handler == NULL) { resume_execute_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU); @@ -984,14 +698,6 @@ void opencensus_trace_execute_internal(INTERNAL_FUNCTION_PARAMETERS) } zend_string_release(callback_name); - FILE *fptr4 = fopen("sampletestt.txt", "a"); - - if (fptr4 != NULL) - { - fprintf(fptr4, "\nopencensus_trace_relese_callback"); - - fclose(fptr4); - } opencensus_trace_finish(); } @@ -1073,8 +779,3 @@ PHP_FUNCTION(opencensus_trace_list) add_next_index_zval(return_value, &span TSRMLS_CC); } ZEND_HASH_FOREACH_END(); } - -//PHP_FUNCTION(opencensus_trace_count) -//{ -// RETURN_LONG(SPAN_COUNT) -//} diff --git a/ext/opencensus_trace.h b/ext/opencensus_trace.h index a089108487a..58dd6346ffd 100644 --- a/ext/opencensus_trace.h +++ b/ext/opencensus_trace.h @@ -28,11 +28,9 @@ PHP_FUNCTION(opencensus_trace_function); PHP_FUNCTION(opencensus_trace_method); PHP_FUNCTION(opencensus_trace_list); -//PHP_FUNCTION(opencensus_trace_count); PHP_FUNCTION(opencensus_trace_begin); PHP_FUNCTION(opencensus_trace_finish); PHP_FUNCTION(opencensus_trace_clear); -//PHP_FUNCTION(opencensus_trace_clr); PHP_FUNCTION(opencensus_trace_set_context); PHP_FUNCTION(opencensus_trace_context); PHP_FUNCTION(opencensus_trace_add_attribute); @@ -46,7 +44,6 @@ void opencensus_trace_gshutdown(); void opencensus_trace_rinit(); void opencensus_trace_rshutdown(); void opencensus_trace_clear(int reset TSRMLS_DC); -//void opencensus_trace_clr(int reset TSRMLS_DC); void opencensus_trace_execute_ex (zend_execute_data *execute_data TSRMLS_DC); void opencensus_trace_execute_internal(INTERNAL_FUNCTION_PARAMETERS); void span_dtor(zval *zv); diff --git a/ext/php-helper/DaemonFunctions.php b/ext/php-helper/DaemonFunctions.php index 1dd031ae8e6..3d7e7449829 100644 --- a/ext/php-helper/DaemonFunctions.php +++ b/ext/php-helper/DaemonFunctions.php @@ -103,7 +103,7 @@ function opencensus_trace_add_attribute($key, $value, $options = []): void { } -function opencensus_trace_remove_span($key, $value, $options = []): void { +function opencensus_trace_remove_span($key): void { } /** diff --git a/src/Trace/Integrations/Curl.php b/src/Trace/Integrations/Curl.php index d434ce272fe..efc5210ac80 100644 --- a/src/Trace/Integrations/Curl.php +++ b/src/Trace/Integrations/Curl.php @@ -31,6 +31,8 @@ */ class Curl implements IntegrationInterface { + static $tracer; + /** * Static method to add instrumentation to curl requests */ @@ -46,6 +48,13 @@ public static function load() opencensus_trace_function('curl_multi_remove_handle'); } + /** + * Static method to add tracer + */ + public static function setTracer($tracer){ + PDO::$tracer = $tracer; + } + /** * Handle extracting the uri from a given curl resource handler * @@ -58,6 +67,10 @@ public static function handleCurlResource($resource) $info = curl_getinfo($resource); $attrs = self::getSpanAttrsFromCurlInfo($info); + if (Curl::$tracer != null) { + Curl::$tracer->checkSpanLimit(); + } + return [ 'attributes' => $attrs, 'kind' => Span::KIND_CLIENT diff --git a/src/Trace/Integrations/PDO.php b/src/Trace/Integrations/PDO.php index 4e6a55db7b0..49b829a12ab 100644 --- a/src/Trace/Integrations/PDO.php +++ b/src/Trace/Integrations/PDO.php @@ -31,6 +31,8 @@ */ class PDO implements IntegrationInterface { + static $tracer; + /** * Static method to add instrumentation to the PDO requests */ @@ -60,6 +62,13 @@ public static function load() opencensus_trace_method('PDOStatement', 'execute', [static::class, 'handleStatementExecute']); } + /** + * Static method to add tracer + */ + public static function setTracer($tracer){ + PDO::$tracer = $tracer; + } + /** * Handle extracting the SQL query from the first argument * @@ -70,6 +79,10 @@ public static function load() */ public static function handleQuery($pdo, $query) { + if (PDO::$tracer != null) { + PDO::$tracer->checkSpanLimit(); + } + return [ 'attributes' => ['db.statement' => $query, 'span.kind' => Span::KIND_CLIENT], 'kind' => Span::KIND_CLIENT @@ -86,6 +99,10 @@ public static function handleQuery($pdo, $query) */ public static function handleConnect($pdo, $dsn) { + if (PDO::$tracer != null) { + PDO::$tracer->checkSpanLimit(); + } + $attributes = ['dsn' => $dsn, 'db.type' => 'sql', 'span.kind' => Span::KIND_CLIENT]; return [ 'attributes' => $attributes, @@ -107,6 +124,10 @@ public static function handleStatementExecute($statement) https://docstore.mik.ua/orelly/java-ent/jenut/ch08_06.htm */ + if (PDO::$tracer != null) { + PDO::$tracer->checkSpanLimit(); + } + $rowCount = $statement->rowCount(); $errorCode = $statement->errorCode(); $error = substr($errorCode, 0, 2); diff --git a/src/Trace/Integrations/Redis.php b/src/Trace/Integrations/Redis.php index 660ca806735..e68fa824897 100644 --- a/src/Trace/Integrations/Redis.php +++ b/src/Trace/Integrations/Redis.php @@ -42,6 +42,8 @@ class Redis implements IntegrationInterface { + static $tracer; + /** * Static method to add instrumentation to redis requests */ @@ -52,6 +54,10 @@ public static function load() } opencensus_trace_method('Predis\Client', '__construct', function ($predis, $params) { + if (Redis::$tracer != null) { + Redis::$tracer->checkSpanLimit(); + } + return [ 'attributes' => [ 'peer.hostname' => $params['host'], @@ -68,6 +74,11 @@ public static function load() $arguments = $command->getArguments(); array_unshift($arguments, $command->getId()); $query = Redis::formatArguments($arguments); + + if (Redis::$tracer != null) { + Redis::$tracer->checkSpanLimit(); + } + return ['attributes' => [ 'command' => $command->getId(), 'service.name' => 'redis', @@ -80,6 +91,12 @@ public static function load() }); } + /** + * Static method to add tracer + */ + public static function setTracer($tracer){ + PDO::$tracer = $tracer; + } public static function formatArguments($arguments) { diff --git a/src/Trace/RequestHandler.php b/src/Trace/RequestHandler.php index df83a917456..826e48387be 100644 --- a/src/Trace/RequestHandler.php +++ b/src/Trace/RequestHandler.php @@ -108,7 +108,7 @@ public function __construct( if ($spanContext->enabled()) { $this->tracer = extension_loaded('opencensus') ? - new ExtensionTracer($spanContext) : + new ExtensionTracer($spanContext, $exporter) : new ContextTracer($spanContext); } else { $this->tracer = new NullTracer(); diff --git a/src/Trace/Tracer/ExtensionTracer.php b/src/Trace/Tracer/ExtensionTracer.php index 42c45fddc6e..ddda3118c5a 100644 --- a/src/Trace/Tracer/ExtensionTracer.php +++ b/src/Trace/Tracer/ExtensionTracer.php @@ -42,16 +42,23 @@ class ExtensionTracer implements TracerInterface, SpanEventHandlerInterface */ private $hasSpans = false; + private $exporter; + + const DEFAULT_SPAN_LIMIT = 100; + /** * Create a new ExtensionTracer * * @param SpanContext|null $initialContext The starting span context. + * @param null $exporter */ - public function __construct(SpanContext $initialContext = null) + public function __construct(SpanContext $initialContext = null, $exporter = null) { if ($initialContext) { opencensus_trace_set_context($initialContext->traceId(), $initialContext->spanId()); } + + $this->exporter = $exporter; } public function inSpan(array $spanOptions, callable $callable, array $arguments = []) @@ -114,6 +121,43 @@ public function spans(): array }, opencensus_trace_list()); } + public function checkSpanLimit() + { + $count = count($this->spans()); + + // TODO:: to read from config + $limit = self::DEFAULT_SPAN_LIMIT; + + if ($count > $limit) { + + $closedSpans = []; + $ids = []; + $spns = $this->spans(); + + foreach ($spns as $k) { + $endTime = $k->endTime(); + + if ($endTime->getTimestamp() != 0) + { + $closedSpans[] = $k; + $ids[] = $k->spanId(); + } + } + + $this->export($closedSpans, $ids); + } + } + + public function export($closedSpans, $ids) + { + if ($this->exporter != null) { + $this->exporter->export($closedSpans); + foreach ($ids as $id) { + opencensus_trace_remove_span($id); + } + } + } + public function addAttribute($attribute, $value, $options = []) { if (array_key_exists('span', $options)) { From 1cc8918b606da71fd6b596115e24f420a3fd2374 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Tue, 26 Jan 2021 18:25:33 +0530 Subject: [PATCH 09/53] added comments --- ext/Dockerfile.rzp | 2 +- ext/opencensus_trace.c | 8 -------- ext/php-helper/DaemonFunctions.php | 2 +- src/Trace/Integrations/Curl.php | 1 + src/Trace/Integrations/PDO.php | 3 +++ src/Trace/Integrations/Redis.php | 2 ++ src/Trace/Tracer/ExtensionTracer.php | 23 +++++++++++++---------- 7 files changed, 21 insertions(+), 20 deletions(-) diff --git a/ext/Dockerfile.rzp b/ext/Dockerfile.rzp index a549a8a14b6..f0c5e22441c 100644 --- a/ext/Dockerfile.rzp +++ b/ext/Dockerfile.rzp @@ -1,4 +1,4 @@ FROM c.rzp.io/razorpay/onggi:php-7.2-apache-api COPY . /app -RUN cd /app && phpize && ./configure --enable-opencensus && make && make install +RUN cd /app && phpize && ./configure --enable-opencensus && make && make test && make install diff --git a/ext/opencensus_trace.c b/ext/opencensus_trace.c index daa638e5bc4..3f5eda361b3 100644 --- a/ext/opencensus_trace.c +++ b/ext/opencensus_trace.c @@ -20,8 +20,6 @@ #include "Zend/zend_exceptions.h" #include "standard/php_math.h" #include "standard/php_rand.h" -#include -#include /** @@ -348,8 +346,6 @@ static int opencensus_trace_call_user_function_callback(zval *args, int num_args static zend_string *generate_span_id() { zval zv; - - #if PHP_VERSION_ID < 70100 if (!BG(mt_rand_is_seeded)) { php_mt_srand(GENERATE_SEED()); @@ -366,7 +362,6 @@ static zend_string *generate_span_id() */ static opencensus_trace_span_t *opencensus_trace_begin(zend_string *name, zend_execute_data *execute_data, zend_string *span_id TSRMLS_DC) { - opencensus_trace_span_t *span = opencensus_trace_span_alloc(); zend_fetch_debug_backtrace(&span->stackTrace, 1, DEBUG_BACKTRACE_IGNORE_ARGS, 0); @@ -627,7 +622,6 @@ void opencensus_trace_execute_ex (zend_execute_data *execute_data TSRMLS_DC) { opencensus_trace_span_apply_span_options(span, trace_handler); } } - zend_string_release(callback_name); opencensus_trace_finish(); } @@ -677,7 +671,6 @@ void opencensus_trace_execute_internal(INTERNAL_FUNCTION_PARAMETERS) span = opencensus_trace_begin(function_name, execute_data, NULL TSRMLS_CC); zend_string_release(function_name); - if (zend_is_callable(trace_handler, 0, &callback_name)) { /* Registered handler is callable - execute the callback */ zval callback_result, *args; @@ -697,7 +690,6 @@ void opencensus_trace_execute_internal(INTERNAL_FUNCTION_PARAMETERS) } } zend_string_release(callback_name); - opencensus_trace_finish(); } diff --git a/ext/php-helper/DaemonFunctions.php b/ext/php-helper/DaemonFunctions.php index 3d7e7449829..bd784031942 100644 --- a/ext/php-helper/DaemonFunctions.php +++ b/ext/php-helper/DaemonFunctions.php @@ -103,7 +103,7 @@ function opencensus_trace_add_attribute($key, $value, $options = []): void { } -function opencensus_trace_remove_span($key): void { +function opencensus_trace_remove_span($key): bool { } /** diff --git a/src/Trace/Integrations/Curl.php b/src/Trace/Integrations/Curl.php index efc5210ac80..4012236b82e 100644 --- a/src/Trace/Integrations/Curl.php +++ b/src/Trace/Integrations/Curl.php @@ -67,6 +67,7 @@ public static function handleCurlResource($resource) $info = curl_getinfo($resource); $attrs = self::getSpanAttrsFromCurlInfo($info); + // checks if spanlimit has reached and if yes flushes the closed spans if (Curl::$tracer != null) { Curl::$tracer->checkSpanLimit(); } diff --git a/src/Trace/Integrations/PDO.php b/src/Trace/Integrations/PDO.php index 49b829a12ab..687fb725d58 100644 --- a/src/Trace/Integrations/PDO.php +++ b/src/Trace/Integrations/PDO.php @@ -79,6 +79,7 @@ public static function setTracer($tracer){ */ public static function handleQuery($pdo, $query) { + // checks if spanlimit has reached and if yes flushes the closed spans if (PDO::$tracer != null) { PDO::$tracer->checkSpanLimit(); } @@ -99,6 +100,7 @@ public static function handleQuery($pdo, $query) */ public static function handleConnect($pdo, $dsn) { + // checks if spanlimit has reached and if yes flushes the closed spans if (PDO::$tracer != null) { PDO::$tracer->checkSpanLimit(); } @@ -124,6 +126,7 @@ public static function handleStatementExecute($statement) https://docstore.mik.ua/orelly/java-ent/jenut/ch08_06.htm */ + // checks if spanlimit has reached and if yes flushes the closed spans if (PDO::$tracer != null) { PDO::$tracer->checkSpanLimit(); } diff --git a/src/Trace/Integrations/Redis.php b/src/Trace/Integrations/Redis.php index e68fa824897..419bed2e348 100644 --- a/src/Trace/Integrations/Redis.php +++ b/src/Trace/Integrations/Redis.php @@ -54,6 +54,7 @@ public static function load() } opencensus_trace_method('Predis\Client', '__construct', function ($predis, $params) { + // checks if spanlimit has reached and if yes flushes the closed spans if (Redis::$tracer != null) { Redis::$tracer->checkSpanLimit(); } @@ -75,6 +76,7 @@ public static function load() array_unshift($arguments, $command->getId()); $query = Redis::formatArguments($arguments); + // checks if spanlimit has reached and if yes flushes the closed spans if (Redis::$tracer != null) { Redis::$tracer->checkSpanLimit(); } diff --git a/src/Trace/Tracer/ExtensionTracer.php b/src/Trace/Tracer/ExtensionTracer.php index ddda3118c5a..b6b16918470 100644 --- a/src/Trace/Tracer/ExtensionTracer.php +++ b/src/Trace/Tracer/ExtensionTracer.php @@ -76,6 +76,9 @@ public function inSpan(array $spanOptions, callable $callable, array $arguments public function startSpan(array $spanOptions): Span { + // checks if spanlimit has reached and if yes flushes the closed spans + $this->checkSpanLimit(); + if (!array_key_exists('name', $spanOptions)) { $spanOptions['name'] = $this->generateSpanName(); } @@ -121,6 +124,8 @@ public function spans(): array }, opencensus_trace_list()); } + /* This checks the numbet of spans in memory and if the count is more than the set limit, it exports all + the closed span present in memory, to free up the memory */ public function checkSpanLimit() { $count = count($this->spans()); @@ -131,29 +136,27 @@ public function checkSpanLimit() if ($count > $limit) { $closedSpans = []; - $ids = []; - $spns = $this->spans(); + $spans = $this->spans(); - foreach ($spns as $k) { + foreach ($spans as $k) { $endTime = $k->endTime(); - if ($endTime->getTimestamp() != 0) - { + if ($endTime->getTimestamp() != 0) { $closedSpans[] = $k; - $ids[] = $k->spanId(); } } - $this->export($closedSpans, $ids); + $this->export($closedSpans); } } - public function export($closedSpans, $ids) + // Exports all the span provided as argument and also remove from memory + public function export($closedSpans) { if ($this->exporter != null) { $this->exporter->export($closedSpans); - foreach ($ids as $id) { - opencensus_trace_remove_span($id); + foreach ($closedSpans as $span) { + opencensus_trace_remove_span($span->spanId()); } } } From bdfcbbf5c6a807fe3b87776ef6533dcb732bdb17 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Sat, 30 Jan 2021 13:10:44 +0530 Subject: [PATCH 10/53] added get span count function --- ext/opencensus.c | 1 + ext/opencensus_trace.c | 20 +++++++++++++++++--- ext/opencensus_trace.h | 1 + ext/php-helper/DaemonFunctions.php | 17 +++++++++++++++++ src/Trace/Integrations/Curl.php | 2 +- src/Trace/Tracer/ExtensionTracer.php | 4 ++-- 6 files changed, 39 insertions(+), 6 deletions(-) diff --git a/ext/opencensus.c b/ext/opencensus.c index 9966606d397..f811801fb0a 100644 --- a/ext/opencensus.c +++ b/ext/opencensus.c @@ -94,6 +94,7 @@ static zend_function_entry opencensus_functions[] = { PHP_FE(opencensus_trace_function, arginfo_opencensus_trace_function) PHP_FE(opencensus_trace_method, arginfo_opencensus_trace_method) PHP_FE(opencensus_trace_list, NULL) + PHP_FE(opencensus_trace_count, NULL) PHP_FE(opencensus_trace_begin, arginfo_opencensus_trace_begin) PHP_FE(opencensus_trace_finish, NULL) PHP_FE(opencensus_trace_clear, NULL) diff --git a/ext/opencensus_trace.c b/ext/opencensus_trace.c index 3f5eda361b3..97695cfaf1c 100644 --- a/ext/opencensus_trace.c +++ b/ext/opencensus_trace.c @@ -28,7 +28,8 @@ */ static void (*opencensus_original_zend_execute_ex) (zend_execute_data *execute_data); static void (*opencensus_original_zend_execute_internal) (zend_execute_data *execute_data, zval *return_value); -static int SPAN_LIMIT = 100; + +static int SPAN_COUNT = 0; void opencensus_trace_ginit() { @@ -53,7 +54,7 @@ void opencensus_trace_gshutdown() void opencensus_trace_rinit() { /* initialize storage for user traced functions - per request basis */ - ALLOC_HASHTABLE(OPENCENSUS_G(user_traced_functions)); + ALLOC_HASHTABLE(OPENCENSUS_G(user_traced_functions)); zend_hash_init(OPENCENSUS_G(user_traced_functions), 16, NULL, ZVAL_PTR_DTOR, 0); /* initialize storage for recorded spans - per request basis */ @@ -175,6 +176,7 @@ PHP_FUNCTION(opencensus_trace_remove_span) RETURN_FALSE; } + // deleting th span assosciated with the given span_id if (zend_hash_del(OPENCENSUS_G(spans), k) != SUCCESS) { RETURN_FALSE } @@ -381,7 +383,7 @@ static opencensus_trace_span_t *opencensus_trace_begin(zend_string *name, zend_e /* add the span to the list of spans */ zend_hash_add_ptr(OPENCENSUS_G(spans), span->span_id, span); - + SPAN_COUNT++; return span; } @@ -489,6 +491,7 @@ void span_dtor(zval *zv) opencensus_trace_span_t *span = Z_PTR_P(zv); opencensus_trace_span_free(span); ZVAL_PTR_DTOR(zv); + SPAN_COUNT--; } /** @@ -771,3 +774,14 @@ PHP_FUNCTION(opencensus_trace_list) add_next_index_zval(return_value, &span TSRMLS_CC); } ZEND_HASH_FOREACH_END(); } + +/** + * Return the count of trace spans that have been collected for this + * request + * + * @return long + */ +PHP_FUNCTION(opencensus_trace_count) +{ + RETURN_LONG(SPAN_COUNT) +} diff --git a/ext/opencensus_trace.h b/ext/opencensus_trace.h index 58dd6346ffd..d0ea0e6ad34 100644 --- a/ext/opencensus_trace.h +++ b/ext/opencensus_trace.h @@ -28,6 +28,7 @@ PHP_FUNCTION(opencensus_trace_function); PHP_FUNCTION(opencensus_trace_method); PHP_FUNCTION(opencensus_trace_list); +PHP_FUNCTION(opencensus_trace_count); PHP_FUNCTION(opencensus_trace_begin); PHP_FUNCTION(opencensus_trace_finish); PHP_FUNCTION(opencensus_trace_clear); diff --git a/ext/php-helper/DaemonFunctions.php b/ext/php-helper/DaemonFunctions.php index bd784031942..11ddbbbcf49 100644 --- a/ext/php-helper/DaemonFunctions.php +++ b/ext/php-helper/DaemonFunctions.php @@ -61,6 +61,16 @@ function opencensus_trace_list(): array { return []; } +/** + * Retrieve the count of collected trace spans + * + * @return int + */ +function opencensus_trace_count(): int { + return 0; +} + + /** * Clear the list of collected trace spans * @@ -103,9 +113,16 @@ function opencensus_trace_add_attribute($key, $value, $options = []): void { } +/** + * Removes a span from the list. + * + * @param string $key + * + */ function opencensus_trace_remove_span($key): bool { } + /** * Add an annotation to a span * @param string $description diff --git a/src/Trace/Integrations/Curl.php b/src/Trace/Integrations/Curl.php index 4012236b82e..057c89d495b 100644 --- a/src/Trace/Integrations/Curl.php +++ b/src/Trace/Integrations/Curl.php @@ -52,7 +52,7 @@ public static function load() * Static method to add tracer */ public static function setTracer($tracer){ - PDO::$tracer = $tracer; + Curl::$tracer = $tracer; } /** diff --git a/src/Trace/Tracer/ExtensionTracer.php b/src/Trace/Tracer/ExtensionTracer.php index b6b16918470..f86be01387d 100644 --- a/src/Trace/Tracer/ExtensionTracer.php +++ b/src/Trace/Tracer/ExtensionTracer.php @@ -146,12 +146,12 @@ public function checkSpanLimit() } } - $this->export($closedSpans); + $this->exportAndDeleteSpans($closedSpans); } } // Exports all the span provided as argument and also remove from memory - public function export($closedSpans) + public function exportAndDeleteSpans($closedSpans) { if ($this->exporter != null) { $this->exporter->export($closedSpans); From 9e008992d68b5f1d30999a0df9d09af951e81280 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Sat, 30 Jan 2021 18:35:23 +0530 Subject: [PATCH 11/53] request time bug fix --- ext/opencensus_trace.c | 4 ---- ext/php-helper/DaemonFunctions.php | 2 +- src/Trace/Integrations/Curl.php | 16 ++++------------ src/Trace/Integrations/PDO.php | 28 ++++++++++------------------ src/Trace/Integrations/Redis.php | 7 ++++--- src/Trace/RequestHandler.php | 2 +- src/Trace/Tracer.php | 12 +++++++++++- src/Trace/Tracer/ExtensionTracer.php | 20 ++++++++++++-------- 8 files changed, 43 insertions(+), 48 deletions(-) diff --git a/ext/opencensus_trace.c b/ext/opencensus_trace.c index 97695cfaf1c..f67d9375577 100644 --- a/ext/opencensus_trace.c +++ b/ext/opencensus_trace.c @@ -171,7 +171,6 @@ PHP_FUNCTION(opencensus_trace_add_attribute) PHP_FUNCTION(opencensus_trace_remove_span) { zend_string *k; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S", &k) == FAILURE) { RETURN_FALSE; } @@ -181,9 +180,6 @@ PHP_FUNCTION(opencensus_trace_remove_span) RETURN_FALSE } - // release zend string - zend_string_release(k); - RETURN_TRUE; } diff --git a/ext/php-helper/DaemonFunctions.php b/ext/php-helper/DaemonFunctions.php index 11ddbbbcf49..af5ba804720 100644 --- a/ext/php-helper/DaemonFunctions.php +++ b/ext/php-helper/DaemonFunctions.php @@ -62,7 +62,7 @@ function opencensus_trace_list(): array { } /** - * Retrieve the count of collected trace spans + * Retrieve the count of collected trace spans, currently in memory * * @return int */ diff --git a/src/Trace/Integrations/Curl.php b/src/Trace/Integrations/Curl.php index 057c89d495b..b38bbaa8277 100644 --- a/src/Trace/Integrations/Curl.php +++ b/src/Trace/Integrations/Curl.php @@ -18,6 +18,7 @@ namespace OpenCensus\Trace\Integrations; use OpenCensus\Trace\Span; +use OpenCensus\Trace\Tracer; /** * This class handles instrumenting curl requests using the opencensus extension. @@ -31,8 +32,6 @@ */ class Curl implements IntegrationInterface { - static $tracer; - /** * Static method to add instrumentation to curl requests */ @@ -48,13 +47,6 @@ public static function load() opencensus_trace_function('curl_multi_remove_handle'); } - /** - * Static method to add tracer - */ - public static function setTracer($tracer){ - Curl::$tracer = $tracer; - } - /** * Handle extracting the uri from a given curl resource handler * @@ -67,9 +59,9 @@ public static function handleCurlResource($resource) $info = curl_getinfo($resource); $attrs = self::getSpanAttrsFromCurlInfo($info); - // checks if spanlimit has reached and if yes flushes the closed spans - if (Curl::$tracer != null) { - Curl::$tracer->checkSpanLimit(); + // checks if span limit has reached and if yes exports the closed spans + if (Tracer::$tracer != null) { + Tracer::$tracer->checkSpanLimit(); } return [ diff --git a/src/Trace/Integrations/PDO.php b/src/Trace/Integrations/PDO.php index 687fb725d58..684b012a584 100644 --- a/src/Trace/Integrations/PDO.php +++ b/src/Trace/Integrations/PDO.php @@ -18,6 +18,7 @@ namespace OpenCensus\Trace\Integrations; use OpenCensus\Trace\Span; +use OpenCensus\Trace\Tracer; /** * This class handles instrumenting PDO requests using the opencensus extension. @@ -31,8 +32,6 @@ */ class PDO implements IntegrationInterface { - static $tracer; - /** * Static method to add instrumentation to the PDO requests */ @@ -62,13 +61,6 @@ public static function load() opencensus_trace_method('PDOStatement', 'execute', [static::class, 'handleStatementExecute']); } - /** - * Static method to add tracer - */ - public static function setTracer($tracer){ - PDO::$tracer = $tracer; - } - /** * Handle extracting the SQL query from the first argument * @@ -79,9 +71,9 @@ public static function setTracer($tracer){ */ public static function handleQuery($pdo, $query) { - // checks if spanlimit has reached and if yes flushes the closed spans - if (PDO::$tracer != null) { - PDO::$tracer->checkSpanLimit(); + // checks if span limit has reached and if yes exports the closed spans + if (Tracer::$tracer != null) { + Tracer::$tracer->checkSpanLimit(); } return [ @@ -100,9 +92,9 @@ public static function handleQuery($pdo, $query) */ public static function handleConnect($pdo, $dsn) { - // checks if spanlimit has reached and if yes flushes the closed spans - if (PDO::$tracer != null) { - PDO::$tracer->checkSpanLimit(); + // checks if span limit has reached and if yes exports the closed spans + if (Tracer::$tracer != null) { + Tracer::$tracer->checkSpanLimit(); } $attributes = ['dsn' => $dsn, 'db.type' => 'sql', 'span.kind' => Span::KIND_CLIENT]; @@ -126,9 +118,9 @@ public static function handleStatementExecute($statement) https://docstore.mik.ua/orelly/java-ent/jenut/ch08_06.htm */ - // checks if spanlimit has reached and if yes flushes the closed spans - if (PDO::$tracer != null) { - PDO::$tracer->checkSpanLimit(); + // checks if span limit has reached and if yes flushes the closed spans + if (Tracer::$tracer != null) { + Tracer::$tracer->checkSpanLimit(); } $rowCount = $statement->rowCount(); diff --git a/src/Trace/Integrations/Redis.php b/src/Trace/Integrations/Redis.php index 419bed2e348..f7609e5bb5c 100644 --- a/src/Trace/Integrations/Redis.php +++ b/src/Trace/Integrations/Redis.php @@ -18,6 +18,7 @@ namespace OpenCensus\Trace\Integrations; use OpenCensus\Trace\Span; +use OpenCensus\Trace\Tracer; /** * This class handles instrumenting Redis requests using the opencensus extension. @@ -54,9 +55,9 @@ public static function load() } opencensus_trace_method('Predis\Client', '__construct', function ($predis, $params) { - // checks if spanlimit has reached and if yes flushes the closed spans - if (Redis::$tracer != null) { - Redis::$tracer->checkSpanLimit(); + // checks if span limit has reached and if yes flushes the closed spans + if (Tracer::$tracer != null) { + Tracer::$tracer->checkSpanLimit(); } return [ diff --git a/src/Trace/RequestHandler.php b/src/Trace/RequestHandler.php index 826e48387be..42225d18522 100644 --- a/src/Trace/RequestHandler.php +++ b/src/Trace/RequestHandler.php @@ -108,7 +108,7 @@ public function __construct( if ($spanContext->enabled()) { $this->tracer = extension_loaded('opencensus') ? - new ExtensionTracer($spanContext, $exporter) : + new ExtensionTracer($spanContext, $exporter, $options) : new ContextTracer($spanContext); } else { $this->tracer = new NullTracer(); diff --git a/src/Trace/Tracer.php b/src/Trace/Tracer.php index 72e2011da19..06ac236235a 100644 --- a/src/Trace/Tracer.php +++ b/src/Trace/Tracer.php @@ -101,6 +101,12 @@ class Tracer */ private static $instance; + /** + * @var RequestHandler Singleton instance + */ + public static $tracer; + + /** * Start a new trace session for this request. You should call this as early as * possible for the most accurate results. @@ -129,7 +135,11 @@ public static function start(ExporterInterface $reporter, array $options = []): : new HttpHeaderPropagator(); unset($options['propagator']); - return self::$instance = new RequestHandler($reporter, $sampler, $propagator, $options); + self::$instance = new RequestHandler($reporter, $sampler, $propagator, $options); + + self::$tracer = self::$instance->tracer(); + + return self::$instance; } /** diff --git a/src/Trace/Tracer/ExtensionTracer.php b/src/Trace/Tracer/ExtensionTracer.php index f86be01387d..d1d2a8ddf31 100644 --- a/src/Trace/Tracer/ExtensionTracer.php +++ b/src/Trace/Tracer/ExtensionTracer.php @@ -44,21 +44,28 @@ class ExtensionTracer implements TracerInterface, SpanEventHandlerInterface private $exporter; - const DEFAULT_SPAN_LIMIT = 100; + private $spanLimit = 100; /** * Create a new ExtensionTracer * * @param SpanContext|null $initialContext The starting span context. - * @param null $exporter + * @param null $exporter. + * @param array $options */ - public function __construct(SpanContext $initialContext = null, $exporter = null) + public function __construct(SpanContext $initialContext = null, $exporter = null, $options = []) { if ($initialContext) { opencensus_trace_set_context($initialContext->traceId(), $initialContext->spanId()); } $this->exporter = $exporter; + + // set span limit from options if present + if (isset($options['span_limit'])){ + $this->spanLimit = $options['span_limit']; + } + } public function inSpan(array $spanOptions, callable $callable, array $arguments = []) @@ -128,12 +135,9 @@ public function spans(): array the closed span present in memory, to free up the memory */ public function checkSpanLimit() { - $count = count($this->spans()); - - // TODO:: to read from config - $limit = self::DEFAULT_SPAN_LIMIT; + $count = opencensus_trace_count(); - if ($count > $limit) { + if ($count > $this->spanLimit) { $closedSpans = []; $spans = $this->spans(); From a5ed06f46c9feb9f40e915c9b2175739f9f47136 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Sat, 30 Jan 2021 18:43:06 +0530 Subject: [PATCH 12/53] comments --- ext/opencensus_trace.c | 1 + src/Trace/Integrations/Redis.php | 9 --------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/ext/opencensus_trace.c b/ext/opencensus_trace.c index f67d9375577..6987edfef25 100644 --- a/ext/opencensus_trace.c +++ b/ext/opencensus_trace.c @@ -29,6 +29,7 @@ static void (*opencensus_original_zend_execute_ex) (zend_execute_data *execute_data); static void (*opencensus_original_zend_execute_internal) (zend_execute_data *execute_data, zval *return_value); +// Global value to keep the current number of spans in memory static int SPAN_COUNT = 0; void opencensus_trace_ginit() diff --git a/src/Trace/Integrations/Redis.php b/src/Trace/Integrations/Redis.php index f7609e5bb5c..ca12bd4eac5 100644 --- a/src/Trace/Integrations/Redis.php +++ b/src/Trace/Integrations/Redis.php @@ -43,8 +43,6 @@ class Redis implements IntegrationInterface { - static $tracer; - /** * Static method to add instrumentation to redis requests */ @@ -94,13 +92,6 @@ public static function load() }); } - /** - * Static method to add tracer - */ - public static function setTracer($tracer){ - PDO::$tracer = $tracer; - } - public static function formatArguments($arguments) { $len = 0; From 4197723f2070c6a890360b404286392b5780a4fa Mon Sep 17 00:00:00 2001 From: Prashant Saraswat Date: Tue, 2 Feb 2021 20:34:37 +0530 Subject: [PATCH 13/53] Fix extraction from headers --- src/Trace/Propagator/JaegerPropagator.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Trace/Propagator/JaegerPropagator.php b/src/Trace/Propagator/JaegerPropagator.php index 5d6d77a9f1d..b096bbe5bab 100644 --- a/src/Trace/Propagator/JaegerPropagator.php +++ b/src/Trace/Propagator/JaegerPropagator.php @@ -68,12 +68,16 @@ public function extract(HeaderGetter $headers): SpanContext if (!$data) { return new SpanContext(); } - - $n = sscanf($data, self::CONTEXT_HEADER_FORMAT, $traceId, $spanId, $parentSpanId, $flags); - - if ($n == 0) { + + $data = explode($data, ':'); + if (count($data) < 4) { return new SpanContext(); } + + $traceId = $data[0]; + $spanId = $data[1]; + $parentSpanId = $data[2]; + $flags = $data[3]; $enabled = $flags & 0x01; From c409365f03dc6c648843c67304980f3063ee8d11 Mon Sep 17 00:00:00 2001 From: Prashant Saraswat Date: Tue, 2 Feb 2021 21:11:13 +0530 Subject: [PATCH 14/53] Update JaegerPropagator.php --- src/Trace/Propagator/JaegerPropagator.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Trace/Propagator/JaegerPropagator.php b/src/Trace/Propagator/JaegerPropagator.php index b096bbe5bab..dd09fe0ed7a 100644 --- a/src/Trace/Propagator/JaegerPropagator.php +++ b/src/Trace/Propagator/JaegerPropagator.php @@ -69,6 +69,8 @@ public function extract(HeaderGetter $headers): SpanContext return new SpanContext(); } + // Jaeger trace id can be of length either 16 or 32. (https://www.jaegertracing.io/docs/1.21/client-libraries/#value) + // We have decided to continue with trace id of length 32 for injection. While extraction can accept both length 16 and 32. $data = explode($data, ':'); if (count($data) < 4) { return new SpanContext(); From 7d3d509e1f92550092ea1ff1250ba6beaa94e2ec Mon Sep 17 00:00:00 2001 From: Prashant Saraswat Date: Thu, 4 Feb 2021 11:52:37 +0530 Subject: [PATCH 15/53] Update JaegerPropagator.php --- src/Trace/Propagator/JaegerPropagator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Trace/Propagator/JaegerPropagator.php b/src/Trace/Propagator/JaegerPropagator.php index dd09fe0ed7a..a5b1af0f5e4 100644 --- a/src/Trace/Propagator/JaegerPropagator.php +++ b/src/Trace/Propagator/JaegerPropagator.php @@ -71,7 +71,7 @@ public function extract(HeaderGetter $headers): SpanContext // Jaeger trace id can be of length either 16 or 32. (https://www.jaegertracing.io/docs/1.21/client-libraries/#value) // We have decided to continue with trace id of length 32 for injection. While extraction can accept both length 16 and 32. - $data = explode($data, ':'); + $data = explode(":", $data); if (count($data) < 4) { return new SpanContext(); } From f1b39d585c623df8ef1083f73c38592e5bc540e0 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Sun, 7 Feb 2021 21:39:41 +0530 Subject: [PATCH 16/53] made changes in readme --- ext/README.md | 30 +++++++++++++++++++++++++--- ext/opencensus_trace.c | 1 + src/Trace/Tracer/ExtensionTracer.php | 17 +++++++++++----- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/ext/README.md b/ext/README.md index 411907b29e0..bc5b1ba8ca2 100644 --- a/ext/README.md +++ b/ext/README.md @@ -317,6 +317,26 @@ You can retrieve the version of this extension at runtime. function opencensus_trace_version(); ``` +```php +/** + * Retrieve the count of collected trace spans, currently in memory + * + * @return int + */ +function opencensus_trace_count(); +} +``` + +```php +/** + * Removes a span from the list. + * + * @param string $key + * + */ +function opencensus_trace_remove_span($key); +``` + This library follows [Semantic Versioning](http://semver.org/). Please note it is currently under active development. Any release versioned @@ -343,6 +363,10 @@ See [CONTRIBUTING](../CONTRIBUTING.md) for more information on how to get starte Apache 2.0 - See [LICENSE](LICENSE) for more information. -## Limiting spans in traces -SPAN_LIMIT variable in `opencensus_trace.c` defines max number of spans in any trace. This is to -contain memory bloat that's caused by holiding the spans till the request-response cycle is finished. +## Exporting span once limit is reached to stop memory from bloating + +Have added $spanBufferLimit variable in ExtensionTracer.php to control the maximun number +of spans that can be hold in memory at any time, when the limit is reached the tracer itself +export all the closed spans. + + diff --git a/ext/opencensus_trace.c b/ext/opencensus_trace.c index 6987edfef25..c13985204a0 100644 --- a/ext/opencensus_trace.c +++ b/ext/opencensus_trace.c @@ -177,6 +177,7 @@ PHP_FUNCTION(opencensus_trace_remove_span) } // deleting th span assosciated with the given span_id + // When inserting the sapn, we also pass the destructor function for span span_dtor, which gets called on zend_hash_del if (zend_hash_del(OPENCENSUS_G(spans), k) != SUCCESS) { RETURN_FALSE } diff --git a/src/Trace/Tracer/ExtensionTracer.php b/src/Trace/Tracer/ExtensionTracer.php index d1d2a8ddf31..ed86a546168 100644 --- a/src/Trace/Tracer/ExtensionTracer.php +++ b/src/Trace/Tracer/ExtensionTracer.php @@ -44,7 +44,12 @@ class ExtensionTracer implements TracerInterface, SpanEventHandlerInterface private $exporter; - private $spanLimit = 100; + /** + * @var int + * Number of max spans that can be hold in a memory, if number goes beyond this value, + * tracer will export the closed spans till then. + */ + private $spanBufferLimit = 100; /** * Create a new ExtensionTracer @@ -52,6 +57,7 @@ class ExtensionTracer implements TracerInterface, SpanEventHandlerInterface * @param SpanContext|null $initialContext The starting span context. * @param null $exporter. * @param array $options + * @type int span_buffer_limit, overrides the default span buffer limit */ public function __construct(SpanContext $initialContext = null, $exporter = null, $options = []) { @@ -62,8 +68,8 @@ public function __construct(SpanContext $initialContext = null, $exporter = null $this->exporter = $exporter; // set span limit from options if present - if (isset($options['span_limit'])){ - $this->spanLimit = $options['span_limit']; + if (isset($options['span_buffer_limit'])){ + $this->spanBufferLimit = $options['span_buffer_limit']; } } @@ -132,12 +138,13 @@ public function spans(): array } /* This checks the numbet of spans in memory and if the count is more than the set limit, it exports all - the closed span present in memory, to free up the memory */ + the closed span present in memory, to free up the memory. We are only exporting closed spans as only those spans use is over, + the open ones stop time along with other attributes might not have been set yet.*/ public function checkSpanLimit() { $count = opencensus_trace_count(); - if ($count > $this->spanLimit) { + if ($count >= $this->spanBufferLimit) { $closedSpans = []; $spans = $this->spans(); From 65557ca630b926618ddd7906bb1de4562b3d5f04 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Mon, 8 Feb 2021 20:44:17 +0530 Subject: [PATCH 17/53] added tests --- ext/tests/span_count.phpt | 14 ++++++++++++++ ext/tests/span_remove.phpt | 28 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 ext/tests/span_count.phpt create mode 100644 ext/tests/span_remove.phpt diff --git a/ext/tests/span_count.phpt b/ext/tests/span_count.phpt new file mode 100644 index 00000000000..5c59d7676a9 --- /dev/null +++ b/ext/tests/span_count.phpt @@ -0,0 +1,14 @@ +--TEST-- +OpenCensus Trace: Test span count method +--FILE-- + null]); +opencensus_trace_finish(); + +$count = opencensus_trace_count(); +echo "Number of traces: " . $count . "\n"; +?> +--EXPECTF-- +Warning: opencensus_trace_begin(): Provided kind should be a string in %s on line %d +Number of traces: 1 diff --git a/ext/tests/span_remove.phpt b/ext/tests/span_remove.phpt new file mode 100644 index 00000000000..8aa1c5dfadf --- /dev/null +++ b/ext/tests/span_remove.phpt @@ -0,0 +1,28 @@ +--TEST-- +OpenCensus Trace: Test removing span by id +--FILE-- + null]); +opencensus_trace_finish(); + +$traces = opencensus_trace_list(); +echo "Number of traces: " . count($traces) . "\n"; +$span = $traces[0]; +$id = $span->spanId(); + +$isRemoved = opencensus_trace_remove_span($id); +echo $isRemoved; + +$count = opencensus_trace_count(); +echo "Number of traces: " . $count . "\n"; + +$traces = opencensus_trace_list(); +echo "Number of traces: " . count($traces) . "\n"; +?> +--EXPECTF-- +Warning: opencensus_trace_begin(): Provided kind should be a string in %s on line %d +Number of traces: 1 +true +Number of traces: 0 +Number of traces: 0 From 731dc8f18f764496f87a70d36699c7abac66d6a6 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Mon, 8 Feb 2021 21:00:28 +0530 Subject: [PATCH 18/53] test fix --- ext/tests/span_remove.phpt | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/ext/tests/span_remove.phpt b/ext/tests/span_remove.phpt index 8aa1c5dfadf..892c5c4e48b 100644 --- a/ext/tests/span_remove.phpt +++ b/ext/tests/span_remove.phpt @@ -10,19 +10,11 @@ $traces = opencensus_trace_list(); echo "Number of traces: " . count($traces) . "\n"; $span = $traces[0]; $id = $span->spanId(); - -$isRemoved = opencensus_trace_remove_span($id); -echo $isRemoved; - +opencensus_trace_remove_span($id); $count = opencensus_trace_count(); echo "Number of traces: " . $count . "\n"; - -$traces = opencensus_trace_list(); -echo "Number of traces: " . count($traces) . "\n"; ?> --EXPECTF-- Warning: opencensus_trace_begin(): Provided kind should be a string in %s on line %d Number of traces: 1 -true -Number of traces: 0 Number of traces: 0 From 028ee0efc21daf2c891a53d88e8250983e720a29 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Wed, 10 Feb 2021 15:45:16 +0530 Subject: [PATCH 19/53] test fix --- ext/tests/span_count.phpt | 3 +-- ext/tests/span_remove.phpt | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/ext/tests/span_count.phpt b/ext/tests/span_count.phpt index 5c59d7676a9..55a0b1cbfb1 100644 --- a/ext/tests/span_count.phpt +++ b/ext/tests/span_count.phpt @@ -3,12 +3,11 @@ OpenCensus Trace: Test span count method --FILE-- null]); +opencensus_trace_begin('root', ['spanId' => 123]); opencensus_trace_finish(); $count = opencensus_trace_count(); echo "Number of traces: " . $count . "\n"; ?> --EXPECTF-- -Warning: opencensus_trace_begin(): Provided kind should be a string in %s on line %d Number of traces: 1 diff --git a/ext/tests/span_remove.phpt b/ext/tests/span_remove.phpt index 892c5c4e48b..8fa9624ab6f 100644 --- a/ext/tests/span_remove.phpt +++ b/ext/tests/span_remove.phpt @@ -3,7 +3,7 @@ OpenCensus Trace: Test removing span by id --FILE-- null]); +opencensus_trace_begin('root', ['spanId' => 123]); opencensus_trace_finish(); $traces = opencensus_trace_list(); @@ -15,6 +15,5 @@ $count = opencensus_trace_count(); echo "Number of traces: " . $count . "\n"; ?> --EXPECTF-- -Warning: opencensus_trace_begin(): Provided kind should be a string in %s on line %d Number of traces: 1 Number of traces: 0 From c544db775c7f63aafcdeed196b7c985638332730 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Wed, 10 Feb 2021 18:03:07 +0530 Subject: [PATCH 20/53] typo fix --- src/Trace/Integrations/Redis.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Trace/Integrations/Redis.php b/src/Trace/Integrations/Redis.php index ca12bd4eac5..73af443c020 100644 --- a/src/Trace/Integrations/Redis.php +++ b/src/Trace/Integrations/Redis.php @@ -76,8 +76,8 @@ public static function load() $query = Redis::formatArguments($arguments); // checks if spanlimit has reached and if yes flushes the closed spans - if (Redis::$tracer != null) { - Redis::$tracer->checkSpanLimit(); + if (Tracer::$tracer != null) { + Tracer::$tracer->checkSpanLimit(); } return ['attributes' => [ From 014163bd00f1e5dc6854a664946ef4e44f424432 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Thu, 11 Feb 2021 21:24:29 +0530 Subject: [PATCH 21/53] github action integration --- .github/workflows/build_images.yml | 46 ++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 .github/workflows/build_images.yml diff --git a/.github/workflows/build_images.yml b/.github/workflows/build_images.yml new file mode 100644 index 00000000000..570dc3c9192 --- /dev/null +++ b/.github/workflows/build_images.yml @@ -0,0 +1,46 @@ +on: [push] +jobs: + cancel: + runs-on: [self-hosted] + name: Cancel Previous Runs + if: always() + steps: + - uses: styfle/cancel-workflow-action@d57d93c + if: github.ref != 'refs/heads/master' + name: cancel old workflows + id: cancel + with: + workflow_id: "build_images.yml" + access_token: ${{ github.token }} + - if: github.ref == 'refs/heads/master' + name: Don't cancel old workflows + id: dont_cancel + run: | + echo "Don't cancel old workflow" + + build-web: + runs-on: [self-hosted] + needs: [cancel] + continue-on-error: false + name: Build Web Harbor + steps: + - name: checkout + id: checkout + uses: razorpay/checkout-action@v2 + - name: Login to Harbor + uses: docker/login-action@v1 + with: + registry: c.rzp.io + username: ${{ secrets.HARBOR_DOCKER_USERNAME }} + password: ${{ secrets.HARBOR_DOCKER_PASSWORD }} + - name: build + uses: docker/build-push-action@v1 + with: + registry: c.rzp.io + username: ${{ secrets.HARBOR_DOCKER_USERNAME }} + password: ${{ secrets.HARBOR_DOCKER_PASSWORD }} + repository: razorpay/opencensus + tags: ${{ github.sha }} + dockerfile: ext/Dockerfile.rzp + build_args: GIT_COMMIT_HASH=${{ github.sha }},GIT_TOKEN=${{ secrets.GIT_ACTION_TOKEN }},GIT_USERNAME=rzp,ONGGI_IMAGE=c.rzp.io/razorpay/onggi:php-7.2-api-web,ARMORY_IMAGE=c.rzp.io/razorpay/armory:wkhtmltopdf-v182,OPENCENSUS_IMAGE=razorpay/opencensus-php:v2020.10.28 + From c86071f1d2f63caaf6634d0d95caf9c194a78a7f Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Thu, 11 Feb 2021 21:33:05 +0530 Subject: [PATCH 22/53] fixed repo name --- .github/workflows/build_images.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_images.yml b/.github/workflows/build_images.yml index 570dc3c9192..4591fc6b2ac 100644 --- a/.github/workflows/build_images.yml +++ b/.github/workflows/build_images.yml @@ -39,8 +39,8 @@ jobs: registry: c.rzp.io username: ${{ secrets.HARBOR_DOCKER_USERNAME }} password: ${{ secrets.HARBOR_DOCKER_PASSWORD }} - repository: razorpay/opencensus + repository: razorpay/opencensus-php tags: ${{ github.sha }} dockerfile: ext/Dockerfile.rzp - build_args: GIT_COMMIT_HASH=${{ github.sha }},GIT_TOKEN=${{ secrets.GIT_ACTION_TOKEN }},GIT_USERNAME=rzp,ONGGI_IMAGE=c.rzp.io/razorpay/onggi:php-7.2-api-web,ARMORY_IMAGE=c.rzp.io/razorpay/armory:wkhtmltopdf-v182,OPENCENSUS_IMAGE=razorpay/opencensus-php:v2020.10.28 + build_args: GIT_COMMIT_HASH=${{ github.sha }},GIT_TOKEN=${{ secrets.GIT_ACTION_TOKEN }},GIT_USERNAME=rzp From 0133970eb46938b72be1865f740a53c18be81de9 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Fri, 12 Feb 2021 10:45:57 +0530 Subject: [PATCH 23/53] changed run-on in github action --- .github/workflows/build_images.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_images.yml b/.github/workflows/build_images.yml index 4591fc6b2ac..43e98340372 100644 --- a/.github/workflows/build_images.yml +++ b/.github/workflows/build_images.yml @@ -1,7 +1,7 @@ on: [push] jobs: cancel: - runs-on: [self-hosted] + runs-on: [ubuntu-latest] name: Cancel Previous Runs if: always() steps: @@ -19,7 +19,7 @@ jobs: echo "Don't cancel old workflow" build-web: - runs-on: [self-hosted] + runs-on: [ubuntu-latest] needs: [cancel] continue-on-error: false name: Build Web Harbor From 60943507276adcd6396722ba72a450b633bd9f00 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Fri, 12 Feb 2021 10:57:08 +0530 Subject: [PATCH 24/53] updated build image file --- .github/workflows/build_images.yml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/.github/workflows/build_images.yml b/.github/workflows/build_images.yml index 43e98340372..168aa3e103a 100644 --- a/.github/workflows/build_images.yml +++ b/.github/workflows/build_images.yml @@ -27,12 +27,6 @@ jobs: - name: checkout id: checkout uses: razorpay/checkout-action@v2 - - name: Login to Harbor - uses: docker/login-action@v1 - with: - registry: c.rzp.io - username: ${{ secrets.HARBOR_DOCKER_USERNAME }} - password: ${{ secrets.HARBOR_DOCKER_PASSWORD }} - name: build uses: docker/build-push-action@v1 with: From 2828d6c2cce695e35b175f95639c9dc289bb9fda Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Fri, 12 Feb 2021 11:50:00 +0530 Subject: [PATCH 25/53] changed docker file --- ext/Dockerfile.rzp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/Dockerfile.rzp b/ext/Dockerfile.rzp index f0c5e22441c..e9bcdd70b40 100644 --- a/ext/Dockerfile.rzp +++ b/ext/Dockerfile.rzp @@ -1,4 +1,4 @@ -FROM c.rzp.io/razorpay/onggi:php-7.2-apache-api +FROM c.rzp.io/razorpay/onggi:php-7.2-api-web COPY . /app RUN cd /app && phpize && ./configure --enable-opencensus && make && make test && make install From 93525225e901f00b324e42faf08e202771ae0604 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Fri, 12 Feb 2021 11:59:05 +0530 Subject: [PATCH 26/53] changed dokcerfile --- .github/workflows/build_images.yml | 7 ++----- ext/Dockerfile.rzp | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build_images.yml b/.github/workflows/build_images.yml index 168aa3e103a..b4344e820c2 100644 --- a/.github/workflows/build_images.yml +++ b/.github/workflows/build_images.yml @@ -24,10 +24,7 @@ jobs: continue-on-error: false name: Build Web Harbor steps: - - name: checkout - id: checkout - uses: razorpay/checkout-action@v2 - - name: build + - name: Build and push uses: docker/build-push-action@v1 with: registry: c.rzp.io @@ -35,6 +32,6 @@ jobs: password: ${{ secrets.HARBOR_DOCKER_PASSWORD }} repository: razorpay/opencensus-php tags: ${{ github.sha }} - dockerfile: ext/Dockerfile.rzp + dockerfile: ./ext/Dockerfile.rzp build_args: GIT_COMMIT_HASH=${{ github.sha }},GIT_TOKEN=${{ secrets.GIT_ACTION_TOKEN }},GIT_USERNAME=rzp diff --git a/ext/Dockerfile.rzp b/ext/Dockerfile.rzp index e9bcdd70b40..f0c5e22441c 100644 --- a/ext/Dockerfile.rzp +++ b/ext/Dockerfile.rzp @@ -1,4 +1,4 @@ -FROM c.rzp.io/razorpay/onggi:php-7.2-api-web +FROM c.rzp.io/razorpay/onggi:php-7.2-apache-api COPY . /app RUN cd /app && phpize && ./configure --enable-opencensus && make && make test && make install From fbfe9475d9e65eaaff3af5c967840909b204be81 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Fri, 12 Feb 2021 12:01:40 +0530 Subject: [PATCH 27/53] changed dokcerfile --- .github/workflows/build_images.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_images.yml b/.github/workflows/build_images.yml index b4344e820c2..877afda0278 100644 --- a/.github/workflows/build_images.yml +++ b/.github/workflows/build_images.yml @@ -32,6 +32,6 @@ jobs: password: ${{ secrets.HARBOR_DOCKER_PASSWORD }} repository: razorpay/opencensus-php tags: ${{ github.sha }} - dockerfile: ./ext/Dockerfile.rzp + dockerfile: ext/Dockerfile.rzp build_args: GIT_COMMIT_HASH=${{ github.sha }},GIT_TOKEN=${{ secrets.GIT_ACTION_TOKEN }},GIT_USERNAME=rzp From 9a976d09f8eef41c25289f2a534bc754617f8d6c Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Fri, 12 Feb 2021 12:07:01 +0530 Subject: [PATCH 28/53] changed dokcerfile --- .github/workflows/build_images.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build_images.yml b/.github/workflows/build_images.yml index 877afda0278..dae80851f5d 100644 --- a/.github/workflows/build_images.yml +++ b/.github/workflows/build_images.yml @@ -24,6 +24,8 @@ jobs: continue-on-error: false name: Build Web Harbor steps: + - uses: actions/checkout@v2 + - - name: Build and push uses: docker/build-push-action@v1 with: @@ -32,6 +34,6 @@ jobs: password: ${{ secrets.HARBOR_DOCKER_PASSWORD }} repository: razorpay/opencensus-php tags: ${{ github.sha }} - dockerfile: ext/Dockerfile.rzp + dockerfile: ./ext/Dockerfile.rzp build_args: GIT_COMMIT_HASH=${{ github.sha }},GIT_TOKEN=${{ secrets.GIT_ACTION_TOKEN }},GIT_USERNAME=rzp From f3cb43ef05786f1726e06f722af02358d7ab3b43 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Fri, 12 Feb 2021 12:07:53 +0530 Subject: [PATCH 29/53] changed dokcerfile --- .github/workflows/build_images.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_images.yml b/.github/workflows/build_images.yml index dae80851f5d..d76c3287856 100644 --- a/.github/workflows/build_images.yml +++ b/.github/workflows/build_images.yml @@ -25,7 +25,7 @@ jobs: name: Build Web Harbor steps: - uses: actions/checkout@v2 - - + - name: Build and push uses: docker/build-push-action@v1 with: From 19c034e90a864c3abc15321bd603d269b4331347 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Fri, 12 Feb 2021 12:19:52 +0530 Subject: [PATCH 30/53] minor change --- .github/workflows/build_images.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build_images.yml b/.github/workflows/build_images.yml index d76c3287856..9f6e380ddef 100644 --- a/.github/workflows/build_images.yml +++ b/.github/workflows/build_images.yml @@ -19,13 +19,14 @@ jobs: echo "Don't cancel old workflow" build-web: + name: Extension Build runs-on: [ubuntu-latest] needs: [cancel] continue-on-error: false - name: Build Web Harbor steps: - - uses: actions/checkout@v2 - + - name: Checkout code + uses: actions/checkout@v2 + - name: Build and push uses: docker/build-push-action@v1 with: From 875b70812f664647f04c715232b9067a68a321cd Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Fri, 12 Feb 2021 12:26:53 +0530 Subject: [PATCH 31/53] minor change --- .github/workflows/build_images.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build_images.yml b/.github/workflows/build_images.yml index 9f6e380ddef..16d3cb234d0 100644 --- a/.github/workflows/build_images.yml +++ b/.github/workflows/build_images.yml @@ -24,10 +24,9 @@ jobs: needs: [cancel] continue-on-error: false steps: - - name: Checkout code + - name: Checkout repo uses: actions/checkout@v2 - - - name: Build and push + - name: Push To Harbor uses: docker/build-push-action@v1 with: registry: c.rzp.io From 77abdb085b9f79108e0f7ae45353f87928cd1363 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Fri, 12 Feb 2021 12:44:39 +0530 Subject: [PATCH 32/53] minor change --- .github/workflows/build_images.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/build_images.yml b/.github/workflows/build_images.yml index 16d3cb234d0..973bf6b8794 100644 --- a/.github/workflows/build_images.yml +++ b/.github/workflows/build_images.yml @@ -26,6 +26,12 @@ jobs: steps: - name: Checkout repo uses: actions/checkout@v2 + - name: Login to Harbor + uses: docker/login-action@v1 + with: + registry: c.rzp.io + username: ${{ secrets.HARBOR_DOCKER_USERNAME }} + password: ${{ secrets.HARBOR_DOCKER_PASSWORD }} - name: Push To Harbor uses: docker/build-push-action@v1 with: From f4eb3f79a58609a1969468da43cf52ac762d8080 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Fri, 12 Feb 2021 12:56:43 +0530 Subject: [PATCH 33/53] minor change --- .github/workflows/build_images.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build_images.yml b/.github/workflows/build_images.yml index 973bf6b8794..901bac10c0f 100644 --- a/.github/workflows/build_images.yml +++ b/.github/workflows/build_images.yml @@ -32,13 +32,14 @@ jobs: registry: c.rzp.io username: ${{ secrets.HARBOR_DOCKER_USERNAME }} password: ${{ secrets.HARBOR_DOCKER_PASSWORD }} + repository: ${{ github.repository }} - name: Push To Harbor uses: docker/build-push-action@v1 with: registry: c.rzp.io username: ${{ secrets.HARBOR_DOCKER_USERNAME }} password: ${{ secrets.HARBOR_DOCKER_PASSWORD }} - repository: razorpay/opencensus-php + repository: ${{ github.repository }} tags: ${{ github.sha }} dockerfile: ./ext/Dockerfile.rzp build_args: GIT_COMMIT_HASH=${{ github.sha }},GIT_TOKEN=${{ secrets.GIT_ACTION_TOKEN }},GIT_USERNAME=rzp From d9eff17930254c0a7349dd5c8bcf2bed02dd5b69 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Fri, 12 Feb 2021 12:59:27 +0530 Subject: [PATCH 34/53] minor change --- .github/workflows/build_images.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/build_images.yml b/.github/workflows/build_images.yml index 901bac10c0f..ab2bc775cba 100644 --- a/.github/workflows/build_images.yml +++ b/.github/workflows/build_images.yml @@ -32,7 +32,6 @@ jobs: registry: c.rzp.io username: ${{ secrets.HARBOR_DOCKER_USERNAME }} password: ${{ secrets.HARBOR_DOCKER_PASSWORD }} - repository: ${{ github.repository }} - name: Push To Harbor uses: docker/build-push-action@v1 with: From c5ba5c556e7fec471d330363acb1fa3d19d283ed Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Fri, 12 Feb 2021 17:25:07 +0530 Subject: [PATCH 35/53] chenged username key --- .github/workflows/build_images.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build_images.yml b/.github/workflows/build_images.yml index ab2bc775cba..c7c8900d248 100644 --- a/.github/workflows/build_images.yml +++ b/.github/workflows/build_images.yml @@ -30,14 +30,14 @@ jobs: uses: docker/login-action@v1 with: registry: c.rzp.io - username: ${{ secrets.HARBOR_DOCKER_USERNAME }} - password: ${{ secrets.HARBOR_DOCKER_PASSWORD }} + username: ${{ secrets.PUBLIC_DOCKER_USERNAME }} + password: ${{ secrets.PUBLIC_DOCKER_PASSWORD }} - name: Push To Harbor uses: docker/build-push-action@v1 with: registry: c.rzp.io - username: ${{ secrets.HARBOR_DOCKER_USERNAME }} - password: ${{ secrets.HARBOR_DOCKER_PASSWORD }} + username: ${{ secrets.PUBLIC_DOCKER_PASSWORD }} + password: ${{ secrets.PUBLIC_DOCKER_PASSWORD }} repository: ${{ github.repository }} tags: ${{ github.sha }} dockerfile: ./ext/Dockerfile.rzp From 277cce7192c2971af18c2499d5e6a8f1239b9e3c Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Fri, 12 Feb 2021 17:30:53 +0530 Subject: [PATCH 36/53] chenged username key --- .github/workflows/build_images.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_images.yml b/.github/workflows/build_images.yml index c7c8900d248..918a9407382 100644 --- a/.github/workflows/build_images.yml +++ b/.github/workflows/build_images.yml @@ -36,7 +36,7 @@ jobs: uses: docker/build-push-action@v1 with: registry: c.rzp.io - username: ${{ secrets.PUBLIC_DOCKER_PASSWORD }} + username: ${{ secrets.PUBLIC_DOCKER_USERNAME }} password: ${{ secrets.PUBLIC_DOCKER_PASSWORD }} repository: ${{ github.repository }} tags: ${{ github.sha }} From aeedadcac553ce2b36da3884805289bb3580e432 Mon Sep 17 00:00:00 2001 From: Giridharan Manivannan Date: Fri, 12 Feb 2021 17:47:16 +0530 Subject: [PATCH 37/53] [ci_integration] Fixes registry. --- .github/workflows/build_images.yml | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/.github/workflows/build_images.yml b/.github/workflows/build_images.yml index 918a9407382..e7b40c7e5d6 100644 --- a/.github/workflows/build_images.yml +++ b/.github/workflows/build_images.yml @@ -1,7 +1,7 @@ on: [push] jobs: cancel: - runs-on: [ubuntu-latest] + runs-on: ubuntu-latest name: Cancel Previous Runs if: always() steps: @@ -17,25 +17,22 @@ jobs: id: dont_cancel run: | echo "Don't cancel old workflow" - build-web: name: Extension Build - runs-on: [ubuntu-latest] + runs-on: ubuntu-latest needs: [cancel] continue-on-error: false steps: - name: Checkout repo uses: actions/checkout@v2 - - name: Login to Harbor + - name: Login to Dockerhub uses: docker/login-action@v1 with: - registry: c.rzp.io username: ${{ secrets.PUBLIC_DOCKER_USERNAME }} password: ${{ secrets.PUBLIC_DOCKER_PASSWORD }} - - name: Push To Harbor + - name: Push To Dockerhub uses: docker/build-push-action@v1 with: - registry: c.rzp.io username: ${{ secrets.PUBLIC_DOCKER_USERNAME }} password: ${{ secrets.PUBLIC_DOCKER_PASSWORD }} repository: ${{ github.repository }} From db7fdafd0312a82b92f0724c68ba8c21260a3e74 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Fri, 12 Feb 2021 17:52:09 +0530 Subject: [PATCH 38/53] chenged username key --- ext/Dockerfile.rzp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/Dockerfile.rzp b/ext/Dockerfile.rzp index f0c5e22441c..d24316b90a9 100644 --- a/ext/Dockerfile.rzp +++ b/ext/Dockerfile.rzp @@ -1,4 +1,4 @@ -FROM c.rzp.io/razorpay/onggi:php-7.2-apache-api +FROM razorpay/onggi:php-7.2-apache-api COPY . /app RUN cd /app && phpize && ./configure --enable-opencensus && make && make test && make install From ea73f1692a1a5585a463954784e05bdc58e0787c Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Fri, 12 Feb 2021 18:01:56 +0530 Subject: [PATCH 39/53] docker changes --- ext/Dockerfile.rzp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/ext/Dockerfile.rzp b/ext/Dockerfile.rzp index d24316b90a9..1c62dd77340 100644 --- a/ext/Dockerfile.rzp +++ b/ext/Dockerfile.rzp @@ -1,4 +1,27 @@ -FROM razorpay/onggi:php-7.2-apache-api +FROM php:7.2 + +RUN apt update +RUN apt install -y \ + autoconf \ + build-essential \ + g++ \ + gcc \ + git \ + jq \ + libc-dev \ + libmemcached-dev \ + libmemcached11 \ + libpq-dev \ + libpqxx-dev \ + make \ + unzip \ + zip \ + zlib1g \ + zlib1g-dev && \ + pecl install memcached && \ + docker-php-ext-enable memcached && \ + docker-php-ext-install pcntl pdo_mysql pdo_pgsql + COPY . /app RUN cd /app && phpize && ./configure --enable-opencensus && make && make test && make install From 73e71cb4646ec1807999d7ee4573d30db3df139b Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Fri, 12 Feb 2021 18:12:55 +0530 Subject: [PATCH 40/53] dockerfile new --- .github/workflows/build_images.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_images.yml b/.github/workflows/build_images.yml index e7b40c7e5d6..dff132fab85 100644 --- a/.github/workflows/build_images.yml +++ b/.github/workflows/build_images.yml @@ -37,6 +37,6 @@ jobs: password: ${{ secrets.PUBLIC_DOCKER_PASSWORD }} repository: ${{ github.repository }} tags: ${{ github.sha }} - dockerfile: ./ext/Dockerfile.rzp + dockerfile: Dockerfile build_args: GIT_COMMIT_HASH=${{ github.sha }},GIT_TOKEN=${{ secrets.GIT_ACTION_TOKEN }},GIT_USERNAME=rzp From 67a123c2762af2db4d30dfe124ffb3d22e936478 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Mon, 15 Feb 2021 21:28:46 +0530 Subject: [PATCH 41/53] docker file changes --- Dockerfile | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 740782513e2..5428fcac6ad 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,10 +25,5 @@ RUN apt install -y \ COPY ext /ext RUN cd /ext && phpize && ./configure --enable-opencensus && make install && docker-php-ext-enable opencensus -RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && \ -php -r "if (hash_file('sha384', 'composer-setup.php') === 'a5c698ffe4b8e849a443b120cd5ba38043260d5c4023dbf93e1558871f1f07f58274fc6f4c93bcfd858c6bd0775cd8d1') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" && \ -php composer-setup.php && \ -php -r "unlink('composer-setup.php');" && \ -mv composer.phar /usr/bin/composer && chmod +x /usr/bin/composer ENTRYPOINT ["bash"] From e6d7e446bfb5a477a341d7f3fd45f2fd663ab8fb Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Tue, 16 Feb 2021 15:33:04 +0530 Subject: [PATCH 42/53] php7.2 --- Dockerfile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5428fcac6ad..f29a8251447 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM php:7.3 +FROM php:7.2 RUN apt update RUN apt install -y \ @@ -25,5 +25,4 @@ RUN apt install -y \ COPY ext /ext RUN cd /ext && phpize && ./configure --enable-opencensus && make install && docker-php-ext-enable opencensus - ENTRYPOINT ["bash"] From 1b9822aeaa5eff1fe102f7440e278f71b8b050a8 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Tue, 16 Feb 2021 15:57:50 +0530 Subject: [PATCH 43/53] docker changes --- .github/workflows/build_images.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_images.yml b/.github/workflows/build_images.yml index dff132fab85..e7b40c7e5d6 100644 --- a/.github/workflows/build_images.yml +++ b/.github/workflows/build_images.yml @@ -37,6 +37,6 @@ jobs: password: ${{ secrets.PUBLIC_DOCKER_PASSWORD }} repository: ${{ github.repository }} tags: ${{ github.sha }} - dockerfile: Dockerfile + dockerfile: ./ext/Dockerfile.rzp build_args: GIT_COMMIT_HASH=${{ github.sha }},GIT_TOKEN=${{ secrets.GIT_ACTION_TOKEN }},GIT_USERNAME=rzp From de847ac412a4a16d423966e2a195b521a0cfc0fb Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Tue, 16 Feb 2021 16:24:50 +0530 Subject: [PATCH 44/53] minor changes --- .github/workflows/build_images.yml | 2 +- Dockerfile | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build_images.yml b/.github/workflows/build_images.yml index e7b40c7e5d6..dff132fab85 100644 --- a/.github/workflows/build_images.yml +++ b/.github/workflows/build_images.yml @@ -37,6 +37,6 @@ jobs: password: ${{ secrets.PUBLIC_DOCKER_PASSWORD }} repository: ${{ github.repository }} tags: ${{ github.sha }} - dockerfile: ./ext/Dockerfile.rzp + dockerfile: Dockerfile build_args: GIT_COMMIT_HASH=${{ github.sha }},GIT_TOKEN=${{ secrets.GIT_ACTION_TOKEN }},GIT_USERNAME=rzp diff --git a/Dockerfile b/Dockerfile index f29a8251447..e387f17471a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,6 +23,4 @@ RUN apt install -y \ docker-php-ext-install pcntl pdo_mysql pdo_pgsql COPY ext /ext -RUN cd /ext && phpize && ./configure --enable-opencensus && make install && docker-php-ext-enable opencensus - -ENTRYPOINT ["bash"] +RUN cd /ext && phpize && ./configure --enable-opencensus && make install From 0c9ae55b4887853084766011bacd27b6e2736d99 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Wed, 17 Feb 2021 14:25:43 +0530 Subject: [PATCH 45/53] minor change --- Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index e387f17471a..f3c4d189455 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,4 +23,5 @@ RUN apt install -y \ docker-php-ext-install pcntl pdo_mysql pdo_pgsql COPY ext /ext -RUN cd /ext && phpize && ./configure --enable-opencensus && make install + +RUN cd /ext && phpize && ./configure --enable-opencensus && make install && docker-php-ext-enable opencensus From a996c5119d81ba79741301cc35bbedda7d641482 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Wed, 17 Feb 2021 14:36:36 +0530 Subject: [PATCH 46/53] docker changes --- Dockerfile | 24 +----------------------- 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/Dockerfile b/Dockerfile index f3c4d189455..9e227f83a3a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,26 +1,4 @@ -FROM php:7.2 - -RUN apt update -RUN apt install -y \ - autoconf \ - build-essential \ - g++ \ - gcc \ - git \ - jq \ - libc-dev \ - libmemcached-dev \ - libmemcached11 \ - libpq-dev \ - libpqxx-dev \ - make \ - unzip \ - zip \ - zlib1g \ - zlib1g-dev && \ - pecl install memcached && \ - docker-php-ext-enable memcached && \ - docker-php-ext-install pcntl pdo_mysql pdo_pgsql +FROM razorpay/onggi:php-7.2-apache-api COPY ext /ext From 6bea44b734560cdc2f78b595f4a3437ec1874c86 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Wed, 17 Feb 2021 14:38:58 +0530 Subject: [PATCH 47/53] docker changes --- .github/workflows/build_images.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_images.yml b/.github/workflows/build_images.yml index dff132fab85..1093c34415e 100644 --- a/.github/workflows/build_images.yml +++ b/.github/workflows/build_images.yml @@ -5,7 +5,7 @@ jobs: name: Cancel Previous Runs if: always() steps: - - uses: styfle/cancel-workflow-action@d57d93c + - uses: styfle/cancel-workflow-action@d57d93c3a8110b00c3a2c0b64b8516013c9fd4c9 if: github.ref != 'refs/heads/master' name: cancel old workflows id: cancel From 6f1b922699077ef25cce36a7b1badb660a738bc4 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Wed, 17 Feb 2021 14:41:44 +0530 Subject: [PATCH 48/53] docker changes --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 9e227f83a3a..aec0576d5aa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,4 +2,4 @@ FROM razorpay/onggi:php-7.2-apache-api COPY ext /ext -RUN cd /ext && phpize && ./configure --enable-opencensus && make install && docker-php-ext-enable opencensus +RUN cd /ext && phpize && ./configure --enable-opencensus && make install From 80d6d9ac24f0ad8c9c9a147941e0c56ba40463c2 Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Wed, 17 Feb 2021 14:58:15 +0530 Subject: [PATCH 49/53] docker changes --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index aec0576d5aa..92563202276 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM razorpay/onggi:php-7.2-apache-api +FROM razorpay/onggi:php-base COPY ext /ext From 9994a459ce8f86a2eedda60e6609f5dc55a1d85e Mon Sep 17 00:00:00 2001 From: Devi A S L Date: Thu, 11 Feb 2021 15:07:32 +0530 Subject: [PATCH 50/53] add standard tags for db and redis suggested by [otel](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/database.md#mysql) --- src/Trace/Integrations/PDO.php | 100 +++++++++++++++++++++++++++++-- src/Trace/Integrations/Redis.php | 34 +++++++---- 2 files changed, 118 insertions(+), 16 deletions(-) diff --git a/src/Trace/Integrations/PDO.php b/src/Trace/Integrations/PDO.php index 4e6a55db7b0..1a29c913d25 100644 --- a/src/Trace/Integrations/PDO.php +++ b/src/Trace/Integrations/PDO.php @@ -71,8 +71,12 @@ public static function load() public static function handleQuery($pdo, $query) { return [ - 'attributes' => ['db.statement' => $query, 'span.kind' => Span::KIND_CLIENT], - 'kind' => Span::KIND_CLIENT + 'attributes' => [ + 'db.statement' => $query, + 'span.kind' => Span::KIND_CLIENT + ], + 'kind' => Span::KIND_CLIENT, + 'sameProcessAsParentSpan' => false ]; } @@ -86,10 +90,48 @@ public static function handleQuery($pdo, $query) */ public static function handleConnect($pdo, $dsn) { - $attributes = ['dsn' => $dsn, 'db.type' => 'sql', 'span.kind' => Span::KIND_CLIENT]; + // https://www.php.net/manual/en/ref.pdo-mysql.connection.php + // example $dsn: mysql:host=localhost;dbname=testdb + + $db_system = ''; + $connection_params = []; + $attributes = []; + + $dbtype_connection = explode(":", $dsn); + if (count($dbtype_connection) >= 2){ + $db_system = $dbtype_connection[0]; + $connection = $dbtype_connection[1]; + foreach (explode(";", $connection) as $kv){ + $params = explode("=", $kv); + $connection_params[$params[0]] = $params[1]; + } + } + + if ($db_system){ + $attributes['db.system'] = $db_system; + } + if (array_key_exists('dbname', $connection_params)){ + $attributes['db.name'] = $connection_params['dbname']; + } + if (array_key_exists('port', $connection_params)){ + $attributes['net.peer.port'] = $connection_params['port']; + } + if (array_key_exists('host', $connection_params)){ + $attributes['net.peer.name'] = $connection_params['host']; + } + + $attributes += [ + 'dsn' => $dsn, + 'db.type' => 'sql', + 'db.connection_string' => $dsn, + 'span.kind' => Span::KIND_CLIENT + ]; - return [ 'attributes' => $attributes, + return [ + 'attributes' => $attributes, 'kind' => Span::KIND_CLIENT, + 'sameProcessAsParentSpan' => false, + 'name' => 'PDO connect' ]; } @@ -153,15 +195,61 @@ public static function handleStatementExecute($statement) $errorTags['error.message'] = $errorCodeMsgArray[$error] ?? ''; } + $query = $statement->queryString; + $operation = PDO::getOperationName($query); + $tableName = PDO::getTableName($query, $operation); + $tags = [ - 'db.statement' => $statement->queryString, + 'db.statement' => $query, 'db.row_count' => $rowCount, + 'db.operation' => $operation, + 'db.table' => $tableName, + 'db.sql.table' => $tableName, 'span.kind' => Span::KIND_CLIENT ]; return [ 'attributes' => $tags + $errorTags, - 'kind' => Span::KIND_CLIENT + 'kind' => Span::KIND_CLIENT, + 'sameProcessAsParentSpan' => false, + 'name' => sprintf("PDO %s %s", $operation, $tableName) ]; } + + public static function getOperationName($query){ + // select/insert/update/delete + + // some queries are enclosed in (). trim them before figuring out operation. + $operation = explode(" ", trim($query, "( "))[0]; + return $operation; + } + + public static function getTableName($query, $operation){ + $tableName = ""; + $operation = strtolower($operation); + $query = strtolower(trim($query)); + $query_parts = explode(" ", $query); + + if (($operation === 'select') or ($operation === 'delete')){ + // select <...> from where ... + // delete from where ... + $from_index = array_search('from', $query_parts); + if (($from_index) and ($from_index+1 < count($query_parts))){ + $tableName = $query_parts[$from_index+1]; + } + } + else if (strtolower($operation) === 'update'){ + // update set ... where ... + $tableName = $query_parts[1]; + } + else if (strtolower($operation) === 'insert'){ + // insert into ... + $into_index = array_search('into', $query_parts); + if (($into_index) and ($into_index+1 < count($query_parts))){ + $tableName = $query_parts[$into_index+1]; + } + } + + return trim($tableName, " \n\r\t\v\0`"); + } } diff --git a/src/Trace/Integrations/Redis.php b/src/Trace/Integrations/Redis.php index 660ca806735..e62698370f7 100644 --- a/src/Trace/Integrations/Redis.php +++ b/src/Trace/Integrations/Redis.php @@ -52,15 +52,22 @@ public static function load() } opencensus_trace_method('Predis\Client', '__construct', function ($predis, $params) { - return [ - 'attributes' => [ - 'peer.hostname' => $params['host'], - 'peer.port' => $params['port'], - 'db.type' => 'redis', - 'span.kind' => Span::KIND_CLIENT - ], - 'kind' => Span::KIND_CLIENT - ]; + $connection_str = sprintf("%s:%s", $params[0]['host'], $params[0]['port']); + return [ + 'attributes' => [ + 'peer.hostname' => $params[0]['host'], + 'peer.port' => $params[0]['port'], + 'net.peer.name' => $params[0]['host'], + 'db.type' => 'redis', + 'db.system' => 'redis', + 'db.connection_string' => $connection_str, + 'redis.connection' => $connection_str, + 'span.kind' => Span::KIND_CLIENT + ], + 'kind' => Span::KIND_CLIENT, + 'name' => 'Predis connect', + 'sameProcessAsParentSpan' => false + ]; }); // covers all basic commands @@ -68,14 +75,21 @@ public static function load() $arguments = $command->getArguments(); array_unshift($arguments, $command->getId()); $query = Redis::formatArguments($arguments); + return ['attributes' => [ + 'db.type' => 'redis', + 'db.system' => 'redis', + 'db.statement' => $query, + 'db.operation' => $command->getId(), 'command' => $command->getId(), 'service.name' => 'redis', 'redis.raw_command' => $query, 'redis.args_length' => count($arguments), 'span.kind' => Span::KIND_CLIENT ], - 'kind' => Span::KIND_CLIENT + 'kind' => Span::KIND_CLIENT, + 'name' => 'Predis ' . $command->getId(), + 'sameProcessAsParentSpan' => false ]; }); } From f9daba413397b7357e3096fe94abcc8caab99cc4 Mon Sep 17 00:00:00 2001 From: Devi Date: Tue, 2 Mar 2021 13:36:15 +0530 Subject: [PATCH 51/53] fix redis and database tags (#10) 1. remove non standard redis.connection tag, blocking redis to be recognized as backend 2. for mysql dsn without host, take host as an optional param to PDO load --- src/Trace/Integrations/PDO.php | 9 ++++++++- src/Trace/Integrations/Redis.php | 1 - 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Trace/Integrations/PDO.php b/src/Trace/Integrations/PDO.php index 1a29c913d25..978377a854e 100644 --- a/src/Trace/Integrations/PDO.php +++ b/src/Trace/Integrations/PDO.php @@ -31,16 +31,18 @@ */ class PDO implements IntegrationInterface { + static $db_host = ""; /** * Static method to add instrumentation to the PDO requests */ - public static function load() + public static function load($db_host="") { if (!extension_loaded('opencensus')) { trigger_error('opencensus extension required to load PDO integrations.', E_USER_WARNING); return; } + PDO::$db_host = $db_host; // public int PDO::exec(string $query) opencensus_trace_method('PDO', 'exec', [static::class, 'handleQuery']); @@ -92,6 +94,7 @@ public static function handleConnect($pdo, $dsn) { // https://www.php.net/manual/en/ref.pdo-mysql.connection.php // example $dsn: mysql:host=localhost;dbname=testdb + // example $dsn: mysql:unix_socket=/tmp/mysql.sock;dbname=testdb $db_system = ''; $connection_params = []; @@ -116,9 +119,13 @@ public static function handleConnect($pdo, $dsn) if (array_key_exists('port', $connection_params)){ $attributes['net.peer.port'] = $connection_params['port']; } + if (array_key_exists('host', $connection_params)){ $attributes['net.peer.name'] = $connection_params['host']; } + else if (array_key_exists('unix_socket', $connection_params)){ + $attributes['net.peer.name'] = PDO::$db_host; + } $attributes += [ 'dsn' => $dsn, diff --git a/src/Trace/Integrations/Redis.php b/src/Trace/Integrations/Redis.php index e62698370f7..1ebae4a43fa 100644 --- a/src/Trace/Integrations/Redis.php +++ b/src/Trace/Integrations/Redis.php @@ -61,7 +61,6 @@ public static function load() 'db.type' => 'redis', 'db.system' => 'redis', 'db.connection_string' => $connection_str, - 'redis.connection' => $connection_str, 'span.kind' => Span::KIND_CLIENT ], 'kind' => Span::KIND_CLIENT, From 7f0317f65da513a88bda8697ff4d6f46f42dbbff Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Wed, 3 Mar 2021 15:04:18 +0530 Subject: [PATCH 52/53] updated docker file --- .github/workflows/build_images.yml | 2 +- Dockerfile | 37 +++++++++++++++++++++++++++--- Dockerfile.rzp | 5 ++++ ext/Dockerfile.rzp | 27 ---------------------- 4 files changed, 40 insertions(+), 31 deletions(-) create mode 100644 Dockerfile.rzp delete mode 100644 ext/Dockerfile.rzp diff --git a/.github/workflows/build_images.yml b/.github/workflows/build_images.yml index 1093c34415e..2d71f372f7c 100644 --- a/.github/workflows/build_images.yml +++ b/.github/workflows/build_images.yml @@ -37,6 +37,6 @@ jobs: password: ${{ secrets.PUBLIC_DOCKER_PASSWORD }} repository: ${{ github.repository }} tags: ${{ github.sha }} - dockerfile: Dockerfile + dockerfile: Dockerfile.rzp build_args: GIT_COMMIT_HASH=${{ github.sha }},GIT_TOKEN=${{ secrets.GIT_ACTION_TOKEN }},GIT_USERNAME=rzp diff --git a/Dockerfile b/Dockerfile index 92563202276..2ef15f1f8f2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,36 @@ -FROM razorpay/onggi:php-base +FROM php:7.3 FROM razorpay/onggi:php-base -COPY ext /ext +RUN apt update +RUN apt install -y \ + autoconf \ + build-essential \ + g++ \ + gcc \ + git \ + jq \ + libc-dev \ + libmemcached-dev \ + libmemcached11 \ + libpq-dev \ + libpqxx-dev \ + make \ + unzip \ + zip \ + zlib1g \ + zlib1g-dev && \ + pecl install memcached && \ + docker-php-ext-enable memcached && \ + docker-php-ext-install pcntl pdo_mysql pdo_pgsql -RUN cd /ext && phpize && ./configure --enable-opencensus && make install + +COPY ext /ext COPY ext /ext +RUN cd /ext && phpize && ./configure --enable-opencensus && make install && docker-php-ext-enable opencensus + +RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && \ +php -r "if (hash_file('sha384', 'composer-setup.php') === 'a5c698ffe4b8e849a443b120cd5ba38043260d5c4023dbf93e1558871f1f07f58274fc6f4c93bcfd858c6bd0775cd8d1') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" && \ +php composer-setup.php && \ +php -r "unlink('composer-setup.php');" && \ +mv composer.phar /usr/bin/composer && chmod +x /usr/bin/composer + + +ENTRYPOINT ["bash"] diff --git a/Dockerfile.rzp b/Dockerfile.rzp new file mode 100644 index 00000000000..92563202276 --- /dev/null +++ b/Dockerfile.rzp @@ -0,0 +1,5 @@ +FROM razorpay/onggi:php-base + +COPY ext /ext + +RUN cd /ext && phpize && ./configure --enable-opencensus && make install diff --git a/ext/Dockerfile.rzp b/ext/Dockerfile.rzp deleted file mode 100644 index 1c62dd77340..00000000000 --- a/ext/Dockerfile.rzp +++ /dev/null @@ -1,27 +0,0 @@ -FROM php:7.2 - -RUN apt update -RUN apt install -y \ - autoconf \ - build-essential \ - g++ \ - gcc \ - git \ - jq \ - libc-dev \ - libmemcached-dev \ - libmemcached11 \ - libpq-dev \ - libpqxx-dev \ - make \ - unzip \ - zip \ - zlib1g \ - zlib1g-dev && \ - pecl install memcached && \ - docker-php-ext-enable memcached && \ - docker-php-ext-install pcntl pdo_mysql pdo_pgsql - -COPY . /app - -RUN cd /app && phpize && ./configure --enable-opencensus && make && make test && make install From dae70b4f5b331bd1ff72d262e334aa68e8b138dd Mon Sep 17 00:00:00 2001 From: Shivam Yuvraj Date: Wed, 3 Mar 2021 15:05:42 +0530 Subject: [PATCH 53/53] updated docker file --- Dockerfile | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 2ef15f1f8f2..740782513e2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM php:7.3 FROM razorpay/onggi:php-base +FROM php:7.3 RUN apt update RUN apt install -y \ @@ -22,8 +22,7 @@ RUN apt install -y \ docker-php-ext-enable memcached && \ docker-php-ext-install pcntl pdo_mysql pdo_pgsql - -COPY ext /ext COPY ext /ext +COPY ext /ext RUN cd /ext && phpize && ./configure --enable-opencensus && make install && docker-php-ext-enable opencensus RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && \ @@ -32,5 +31,4 @@ php composer-setup.php && \ php -r "unlink('composer-setup.php');" && \ mv composer.phar /usr/bin/composer && chmod +x /usr/bin/composer - ENTRYPOINT ["bash"]