Skip to content
This repository was archived by the owner on Nov 3, 2020. It is now read-only.

Commit b6bec16

Browse files
authored
Merge pull request #31 from nipwaayoni/remove-service-bindings
Replace container bindings for AgentBuilder configurations and resolv…
2 parents 257e95e + 0be18d3 commit b6bec16

File tree

4 files changed

+50
-38
lines changed

4 files changed

+50
-38
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ Refer to the [configuration docs](docs/configuration.md) for more information.
114114

115115
### HTTP Client Customization
116116

117-
It is no longer possible to provide HTTP client options through the APM PHP Agent configuration. If you need to customize the HTTP client, you must implement and configure a suitable client object and properly register it with the Laravel service container. See the "HTTP Client Configuation" section of the [configuration docs](docs/configuration.md).
117+
It is no longer possible to provide HTTP client options through the APM PHP Agent configuration. If you need to customize the HTTP client, you must implement and configure a suitable client object and provide it to the `AgentBuilder`. See the "HTTP Client Configuration" section of the [configuration docs](docs/configuration.md).
118118

119119
## Laravel Test Setup
120120

docs/breaking_changes.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Breaking Changes
22

3-
## 7.x
3+
## 7.1.0
4+
5+
1. Removed the ability to bind "ElasticApm*" objects into the container to set on the AgentBuilder. Configuring the AgentBuilder is now done by binding an implementation into the container which the ElasticApmServiceProvider will resolve and use.
6+
7+
## 7.0.0
48

59
1. The [Elastic APM PHP Agent](https://github.com/nipwaayoni/elastic-apm-php-agent) minimum version has been updated.
610
2. It is no longer recommended to call `Agent::send()` in `Exceptions\Handler::report()`. This will likely result in duplicate reporting of exceptions.

docs/configuration.md

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,67 @@ Once published, open the `config/elastic-apm.php` file and review the various se
2626

2727
## Customizing Agent Creation
2828

29+
The `Agent` object used to manage APM events is created using the provided `AgentBuilder` class. You can control many aspects of the `Agent` creation by binding your own implementation into the service container. Some helpful examples are shown below. For more details, refer to the [Elastic APM PHP Agent](https://github.com/nipwaayoni/elastic-apm-php-agent/blob/master/docs/agent.md) documentation.
30+
31+
### Binding an AgentBuilder
32+
33+
Bind your implementation as a singleton in the service container:
34+
35+
```php
36+
$this->app->bind(AgentBuilder::class, function () {
37+
$builder = new AgentBuilder();
38+
39+
// configure the builder
40+
41+
return $builder;
42+
});
43+
```
44+
45+
Note that the `ElasticApmServiceProvider` will always call the `AgentBuilder::withConfig()` and `AgentBuilder::withEnvData()` methods. You must use the provided `elastic-apm` configuration options to influence those settings.
46+
2947
### HTTP Client Configuration
3048

31-
If you need to customize the HTTP client, you must create a [PSR-18](https://www.php-fig.org/psr/psr-18/) compatible implementation and bind it in the Laravel service container. For now, we will use a GuzzleHttp adapter from the PHP-HTTP project.
49+
If you need to customize the HTTP client, you must create a [PSR-18](https://www.php-fig.org/psr/psr-18/) compatible implementation and provide it to the `AgentBuilder. For now, we will use a GuzzleHttp adapter from the PHP-HTTP project.
3250

3351
```bash
3452
composer require http-interop/http-factory-guzzle php-http/guzzle6-adapter
3553
```
3654

37-
The following example demonstrates how to create a GuzzleHttp client that will not verify server certificates. Once you create the client, bind it in the service container under the `ElasticApmHttpClient` abstract.
55+
The following example demonstrates how to create a GuzzleHttp client that will not verify server certificates.
3856

3957
```php
40-
$this->app->bind('ElasticApmHttpClient', function () {
41-
// Create the configured client
58+
$this->app->bind(AgentBuilder::class, function () {
59+
$builder = new AgentBuilder();
60+
4261
$client = new \GuzzleHttp\Client([
4362
'verify' => false,
4463
// other client options
4564
]);
4665

4766
// Wrap the client object in the adapter and return it
48-
return new \Http\Adapter\Guzzle6\Client($client);
49-
});
67+
$builder->withHttpClient(new \Http\Adapter\Guzzle6\Client($client));
5068

69+
return $builder;
70+
});
5171
```
5272

53-
If the service container has a binding for `ElasticApmHttpClient`, the concrete implementation will be retrieved and passed into the `Agent`.
73+
### APM Transaction Hooks
5474

55-
### Other Agent Services
75+
You can hook the APM HTTP request/response process to examine the data to be sent to APM and the response after sending. This may be helpful in troubleshooting issues.
5676

57-
ElasticApmEventFactory
58-
ElasticApmTransactionStore
59-
ElasticApmRequestFactory
60-
ElasticApmStreamFactory
77+
```php
78+
$this->app->bind(AgentBuilder::class, function () {
79+
$builder = new AgentBuilder();
6180

62-
### APM Transaction Hooks
81+
$builder->withPreCommitCallback(function (RequestInterface $request) {
82+
Log::info(sprintf('Pre commit url is: %s', $request->getUri()));
83+
});
6384

85+
$builder->withPostCommitCallback(function (ResponseInterface $response) {
86+
Log::info(sprintf('Post commit response status: %s', $response->getStatusCode()));
87+
Log::debug($response->getBody()->getContents());
88+
});
89+
90+
return $builder;
91+
});
92+
```

src/Providers/ElasticApmServiceProvider.php

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,9 @@ public function register()
5151
'elastic-apm'
5252
);
5353

54-
$this->app->singleton(Agent::class, function ($app) {
55-
$container = resolve(ContainerInterface::class);
56-
54+
$this->app->singleton(Agent::class, function () {
5755
$builder = resolve(AgentBuilder::class);
56+
5857
$builder->withConfig(new Config(
5958
array_merge(
6059
[
@@ -70,26 +69,6 @@ public function register()
7069

7170
$builder->withEnvData(config('elastic-apm.env'));
7271

73-
if ($container->has('ElasticApmEventFactory')) {
74-
$builder->withEventFactory($container->get('ElasticApmEventFactory'));
75-
}
76-
77-
if ($container->has('ElasticApmTransactionStore')) {
78-
$builder->withTransactionStore($container->get('ElasticApmTransactionStore'));
79-
}
80-
81-
if ($container->has('ElasticApmHttpClient')) {
82-
$builder->withHttpClient($container->get('ElasticApmHttpClient'));
83-
}
84-
85-
if ($container->has('ElasticApmRequestFactory')) {
86-
$builder->withRequestFactory($container->get('ElasticApmRequestFactory'));
87-
}
88-
89-
if ($container->has('ElasticApmStreamFactory')) {
90-
$builder->withStreamFactory($container->get('ElasticApmStreamFactory'));
91-
}
92-
9372
return $builder->build();
9473
});
9574

0 commit comments

Comments
 (0)