Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor extension's code (v2) #26

Merged
merged 8 commits into from
Feb 7, 2020

Conversation

SergeyKleyman
Copy link
Contributor

This PR replaces PR #25.

@ezimuel
Copy link
Contributor

ezimuel commented Feb 5, 2020

@SergeyKleyman, @olksdr now I can pull from the remote repository of Sergey, thanks!

@olksdr
Copy link

olksdr commented Feb 5, 2020

@ezimuel yeah, we had to go over the settings and change few things for @SergeyKleyman . I'm glad it helped 😄

Copy link
Contributor

@ezimuel ezimuel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SergeyKleyman, I really liked the way you organized the code, awesome! There are some points that I commented and I think we should change it, but we can discuss during our 1:1 meeting. Let me know if you need more information about my review. Thanks!


#include "php_elasticapm.h"

static inline const char* getCurrentTransactionId()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As coding standard in PHP, all the function names for extension are prefixed by the name of the extension (e.g. elasticapm). Moreover, the function names use underscores between words, while class names use both the camelCase and PascalCase rules. See https://www.php.net/manual/en/userlandnaming.rules.php for more details.

In our case, we should have:

  • elasticapm_get_transcation_id();
  • elasticapm_get_trace_id();
  • elasticapm_is_enabled();

I'm proposing to omit current in the name since it's always the current transaction and trace. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good - I've changed the names to use underscores.

As we discussed, I think we should keep current part. The reason is that public API of the PHP library part, that I plan to implement next, will have functions such as beginTransaction(), beginCurrentTransaction() and getCurrentTransaction() (similar to Active Span concept in OpenTracing API for PHP) and you are correct about the fact that elasticapm extension only cares about transactions created with beginCurrentTransaction() and it doesn't need to track transactions created with beginTransaction() API but I think it will be easier to follow the code if GlobalTracer::get()->getCurrentTransaction()->getId() PHP function calls elasticapm_get_current_transaction_id() instead elasticapm_get_transaction_id(). Of course, it's just a matter of naming and we can do it the way you suggest (without the current part) but I propose to wait until we decide on initial public API of PHP library part and then we can re-evaluate this approach. WDYT?

By default, the extension is disabled. You need to enable it setting `elasticapm.enable=1`.
The required settings are: `service_name` and `host`. You need to specify a
service name and the APM server host where to send the data.
By default, the extension is enabled. You can disable it by setting `elasticapm.enabled=false`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure we want to enable it by default? This is the same behaviour of other APM agent?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we discussed, the default of elasticapm.enabled configuration is true. BTW you can find a detailed discussion about its meaning here.


static void genRandomIdBinary( Byte* buffer, UInt8 idSizeBytes )
{
FOR_EACH_INDEX( UInt8, i, idSizeBytes ) buffer[ i ] = (Byte) ( php_mt_rand_range( 0, UINT8_MAX ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you using UINT8_MAX here? I think it's enough to generate from 0 to 255 values.
Btw, if you want to generate random bytes you can also use the random_bytes() function of PHP here.
To call it in C, you can use the following function (#include "ext/standard/php_random.h") :

int php_random_bytes(void *bytes, size_t size, zend_bool should_throw);

it will generate size bytes. If should_throw == 1 it will generate an error in case the random generator is not secure.
You can read the php source code here for more information.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We discussed and decided to stay with php_mt_rand_range because random_bytes generates cryptographically secure pseudo-random bytes which is most likely is more costly from performance point of view but we don't need cryptographically secure part so we don't want to pay for it with performance impact.


static inline void getCurrentTime( TimePoint* result )
{
gettimeofday( &( result->systemClockTime ), /* timezone_info: */ NULL );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not use gettimeofday() for measuring execution time, I just discovered this. Instead, we can use clock_gettime(CLOCK_MONOTONIC, ...).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we discussed I've opened a follow up issue - #27.

src/ext/lifecycle.c Outdated Show resolved Hide resolved
config = getCurrentConfig();

// __asm__("int3");
if ( !config->enabled )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to enable/disable the elasticapm extension from a PHP script, using ini_set(), we cannot add this check here. The elasticApmModuleInit() is executed before any execution of PHP code, so if you have the elasticapm.enabled=false (in your php.ini) and you want to enable it only for some PHP script, using ini_set() you will never execute the elasticApmModuleInit() function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we discussed, according enabled specification it's should be read by agent only once at the start. I've created a follow up issue to mark it as not modifiable ini entry - #28.

@SergeyKleyman
Copy link
Contributor Author

@ezimuel I think I've finished all the changes based on the review feedback. Please take a look.

Copy link
Contributor

@ezimuel ezimuel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@SergeyKleyman SergeyKleyman merged commit 44e1caf into elastic:master Feb 7, 2020
@SergeyKleyman SergeyKleyman deleted the Refactor_ext_v2 branch February 7, 2020 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants