nipwaayoni / elastic-apm-php-agent

PHP Agent for Elastic APM
Other
28 stars 15 forks source link

Support For APM API Keys #86

Closed fmiqbal closed 1 year ago

fmiqbal commented 1 year ago

Currently as I understand from the docs and the code

https://github.com/nipwaayoni/elastic-apm-php-agent/blob/73ff8e7c10f6116199a5c160df0ec5574869c3c2/src/Middleware/Connector.php#L245-L260

there are no support for API Key authentication https://www.elastic.co/guide/en/apm/guide/current/api-key.html

The API Key use Authorization: ApiKey and it took precendence if secret token is also set on APM Server,

I am also playing around with code, and got some success, but I am not really confident with putting $apiKey in the construct, (although secretToken is also set this way)

image

dstepe commented 1 year ago

Thank you for the suggestion. I don't think this will be too difficult to add, but it will be a few days before I have time to really get into it. From what I have found, when using an API Key, the Authorization header will specify ApiKey rather than Bearer as it does when using a secret token. I think it might be easier to have the configuration determine which type to use and then pass that to the Connector.

dstepe commented 1 year ago

@fmiqbal There is an 8.2-dev branch which should provide support for API Keys. I intend to some testing before merging this, but my testing will be limited to secret token. I don't have an Elastic instance currently configured for API Keys. Can you pull that branch and test it?

fmiqbal commented 1 year ago

I've tried these cases, all seems good

This also doesn't break this package https://github.com/arkaitzgarro/elastic-apm-laravel,

only need to manipulate these 2 envs

Tested on Laravel 10.10.0

ELASTIC_APM_API_KEY="VkFyV0ZZZ0JEclk5ZGxMVUIMmVGaEZOMndUZmk3SUFTS2dqaXhzZw=="
ELASTIC_APM_SECRET_TOKEN="gKdBZN5cxH9ZQBGanhRPSHsBQw"
``` [2023-05-13 23:02:43] local.NOTICE: [Elastic APM Agent] The "appVersion" configuration option is deprecated, please use "serviceVersion" instead. [2023-05-13 23:02:43] local.DEBUG: [Elastic APM Agent] Runtime config: {"serverUrl":"https:\/\/apm.unpad.ac.id","secretToken":"g***w","apiKey":"V***=","hostname":"crimson","serviceName":"recruitment-api","serviceVersion":null,"frameworkName":"Laravel","frameworkVersion":"10.10.0","enabled":true,"timeout":10,"environment":"local","stackTraceLimit":50,"transactionSampleRate":1} [2023-05-13 23:02:43] local.DEBUG: [Elastic APM Agent] Started transaction: GET /api/v1/landing/jobs [2023-05-13 23:02:44] local.DEBUG: [Elastic APM Agent] Stopped transaction: GET /api/v1/landing/jobs [2023-05-13 23:02:44] local.DEBUG: [Elastic APM Agent] Added event: span [2023-05-13 23:02:44] local.DEBUG: [Elastic APM Agent] Added event: span [2023-05-13 23:02:44] local.DEBUG: [Elastic APM Agent] Added event: span [2023-05-13 23:02:44] local.DEBUG: [Elastic APM Agent] Added event: span [2023-05-13 23:02:44] local.DEBUG: [Elastic APM Agent] Added event: span [2023-05-13 23:02:44] local.DEBUG: [Elastic APM Agent] Added event: span [2023-05-13 23:02:44] local.DEBUG: [Elastic APM Agent] Added event: span [2023-05-13 23:02:44] local.DEBUG: [Elastic APM Agent] Added transactions to connector [2023-05-13 23:02:44] local.DEBUG: [Elastic APM Agent] Prepared request with 9 events. [2023-05-13 23:02:45] local.DEBUG: [Elastic APM Agent] Sent request, response status: 202 [2023-05-13 23:02:45] local.DEBUG: [Elastic APM Agent] Sent data to Elastic APM host [2023-05-13 23:02:45] local.DEBUG: [Elastic APM Agent] Added event: metadata ```
dstepe commented 1 year ago

Thank you for all of that testing. That's a big help. I've done some simple testing as well and am ready to merge this after updating the documentation.

dstepe commented 1 year ago

This has been completed with the release of version 8.2.0