Skip to content

Commit

Permalink
Added the log and removed the return cURL output
Browse files Browse the repository at this point in the history
  • Loading branch information
ezimuel committed Oct 14, 2019
1 parent 5124a90 commit 76f7995
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,21 @@ extension=elasticapm.so

## Configure

You can configure the ElasticAPM agent using the following ini settings for PHP:
You can configure the Elastic APM agent using the following ini settings for PHP:

```ini
elasticapm.enable = 0
elasticapm.host=http://localhost:8200
elasticapm.service_name=
elasticapm.log=
```

By default, the extension is disabled. You need to enable it setting `elasticapm.enable=1`.
You need to set a `service_name` and the default APM server host is `localhost:8200`.
If you want to log the errors generated by the agent using the `elasticapm.log`
settings. Here you can specify the path of the file that you want to use for
logging.

You can see an example of php.ini [here](src/ext/php.ini).

If you want you can also change the ElaticAPM agent at runtime, using the
Expand All @@ -46,6 +51,7 @@ following PHP code:
ini_set('elasticapm.enable', '1');
ini_set('elasticapm.host', 'insert the APM Server host here');
ini_set('elasticapm.service_name', 'test');
ini_set('elasticapm.log', '/tmp/elasticapm.log');
```

## Configure with Elastic Cloud
Expand Down
17 changes: 13 additions & 4 deletions src/ext/elasticapm.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ PHP_RSHUTDOWN_FUNCTION(elasticapm)
struct curl_slist *chunk = NULL;
pid_t process_id;
double cpu_usage, cpu_process_usage, duration;
FILE *log_file;

if (!GA(enable)) {
return SUCCESS;
Expand Down Expand Up @@ -206,11 +207,18 @@ PHP_RSHUTDOWN_FUNCTION(elasticapm)

result = curl_easy_perform(curl);
if(result != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(result));
// Log error if elasticapm.log is set
if (strlen(INI_STR("elasticapm.log")) > 0) {
log_file = fopen(INI_STR("elasticapm.log"), "a");
if (log_file == NULL) {
zend_throw_exception(spl_ce_RuntimeException, "Cannot access the file specified in elasticapm.log", 0 TSRMLS_CC);
}
time_t t = time(NULL);
struct tm tm = *localtime(&t);
fprintf(log_file, "[%d-%d-%d %d:%d:%d] %s %s\n", tm.tm_year + 1900, tm.tm_mon + 1,tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, INI_STR("elasticapm.host"), curl_easy_strerror(result));
fclose(log_file);
}
}
long response_code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
php_printf("Response code: %ld\n", response_code);
curl_easy_cleanup(curl);

efree(url);
Expand Down Expand Up @@ -246,6 +254,7 @@ PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("elasticapm.host", "http://localhost:8200", PHP_INI_ALL, OnUpdateString, host, zend_elasticapm_globals, elasticapm_globals)
STD_PHP_INI_ENTRY("elasticapm.secret_token", "", PHP_INI_ALL, OnUpdateString, secret_token, zend_elasticapm_globals, elasticapm_globals)
STD_PHP_INI_ENTRY("elasticapm.service_name", "", PHP_INI_ALL, OnUpdateString, service_name, zend_elasticapm_globals, elasticapm_globals)
STD_PHP_INI_ENTRY("elasticapm.log", "", PHP_INI_ALL, OnUpdateString, log, zend_elasticapm_globals, elasticapm_globals)
PHP_INI_END()

PHP_MINIT_FUNCTION(elasticapm)
Expand Down
1 change: 1 addition & 0 deletions src/ext/php_elasticapm.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ ZEND_BEGIN_MODULE_GLOBALS(elasticapm)
char *host;
char *secret_token;
char *service_name;
char *log;
zend_bool enable;
ZEND_END_MODULE_GLOBALS(elasticapm)

Expand Down

0 comments on commit 76f7995

Please sign in to comment.