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

Added ElasticsearchLogstashHandler #1334

Closed
wants to merge 1 commit into from
Closed

Conversation

lyrixx
Copy link
Contributor

@lyrixx lyrixx commented Jun 21, 2019

ATM, there are few options to push log to Elastic Stack in order to play with them:

  • install logstash and use a Gelf handler. It works but you have to install logstash and configure it. Not an easy task. More over, it need an extra PHP package
  • use the ES handler: It does not play well with context and extra: Kibana is not able to filter on nested object. And this handler is tightly coupled to the ElasticaFormatter formater. More over, it need an extra PHP package
  • use something to parse file logs. This is really a bad idea since it involves a parsing... More over a daemon is needed to do that (file beat / logstash / you name it)

This is why I'm introducing a new Handler.

  • There is not need to install anything (expect ES, of course)
  • It play very well with Kibana, as it uses the Logstash format
  • It requires symfony/http-client, but in a modern PHP application (SF 4.3) this dependency is already present
  • It slow down a bit the application since it trigger an HTTP request for each logs. But symfony/http-client is non-blocking. If you want to use it in production, I recommend to wrap this handler in a buffer handler or a cross-finger handle to have only one HTTP call.

Note: This handler is not aimed to be used in production


This is what you can expect out of the box
image

@Seldaek
Copy link
Owner

Seldaek commented Jun 30, 2019

Thanks @lyrixx, I am medium happy about this PR tbh, for a few reasons:

  • symfony/http-client dependency makes sense if you have symfony, but monolog is used in many contexts/frameworks that don't..
  • if at all I think this belongs more in monolog 2.x
  • and well lastly I think it probably makes more sense to publish in an extra package which can have the explicit dependency on symfony/http-client then.

parent::__construct($level, $bubble);
$this->endpoint = $endpoint;
$this->index = $index;
$this->client = $client ?: HttpClient::create();
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 remove the dependency to the component, we can remove this call and make the $client argument mandatory. That will only require the http-client-contracts, which is an abstract dependency.

Copy link
Contributor

Choose a reason for hiding this comment

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

we can also keep the argument optional, and throw an exception when the component is missing but not client has been provided, as done in https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/HttpClient/Psr18Client.php#L65

@nicolas-grekas
Copy link
Contributor

symfony/http-client dependency makes sense if you have symfony

what does this mean? it's a standalone component.

have the explicit dependency on symfony/http-client then.

if this is problematic, I'd suggest relying only on symfony/http-client-contracts, which is an abstraction

@lyrixx
Copy link
Contributor Author

lyrixx commented Jul 1, 2019

HttpClient is not an hard dependency. This is the same as for the ElasticHandler or the LogstashHandler.
I don't understand why this should be different for this handler.

@Seldaek
Copy link
Owner

Seldaek commented Jul 1, 2019

The point is those other handlers were added ages ago, and removing them here would cause breakage for not much benefit, but for new handlers with third-party dependencies IMO the right way is to publish as a third-party package, with requires on monolog and on whichever dependency is needed. It lets Composer resolve everything which makes more sense really.

@nicolas-grekas requiring the contracts package doesn't really solve the above, so IMO still best to just release as a separate package. That's why there is https://github.com/Seldaek/monolog/wiki/Third-Party-Packages - we can still add support for that third party package in the monolog-bundle for example.

@nicolas-grekas
Copy link
Contributor

nicolas-grekas commented Jul 1, 2019

This means another maintainers need to deal with these per-handle packages. It may be easier to ask ppl to send PRs here: managing a package is very different from patching a class. I'd be more confident if you (monolog maintenance team) would deal with the releasing part.
Unless the old handlers create a significant maintenance burden, please consider this aspect.

@Seldaek
Copy link
Owner

Seldaek commented Jul 2, 2019

These handlers typically don't change much, so the package management aspect is very little.. We could do a monolog organization of some sort, but I don't really want to be managing a million handler packages for stuff that I don't know and don't care about, so IMO distributing this and having everyone maintaining their own handlers they are interested in makes more sense. I can see you may not agree with this seeing how the Symfony project handles this very differently, but that's life :)

Anyway this handler in particular being dev-time only which really isn't the main use case for monolog.. If you feel it makes sense for symfony projects it could go in the monolog bridge or the monolog bundle there I guess.

@Seldaek Seldaek closed this Jul 2, 2019
@Seldaek
Copy link
Owner

Seldaek commented Jul 2, 2019

P.S; I can see how it'd be good to have a way to write to kibana directly tho, if you can make this support handleBatch and skip the http-client dependency then it might make more sense to me to have a production ready thing.

@lyrixx
Copy link
Contributor Author

lyrixx commented Jul 2, 2019

These handlers typically don't change much, so the package management aspect is very little.. We could do a monolog organization of some sort, but I don't really want to be managing a million handler packages for stuff that I don't know and don't care about, so IMO distributing this and having everyone maintaining their own handlers they are interested in makes more sense. I can see you may not agree with this seeing how the Symfony project handles this very differently, but that's life :)

Yes, I already proposed a "Contrib" Repository.
Back in the past, we did a Contrib namespace for Sismo https://github.com/FriendsOfPHP/Sismo/tree/master/src/Sismo/Contrib
It worked quite well

P.S; I can see how it'd be good to have a way to write to kibana directly tho,

Kibana is only a "reader" on top of ES. I mean you can not write directly to kibana. You push logs to ES, and then you visualize them with Kibana

if you can make this support handleBatch and skip the http-client dependency then it might make more sense to me to have a production ready thing.

HandleBatch is really easy to do. Just 3/5 LOC to add. But I don't want to implement a new HTTP client here. Finally, http client is not added to the composer.json, it's only a soft deps, so I don't really see the issue.

But I understand having another Handler means another Handler to maintain.

@ghost ghost mentioned this pull request Jul 4, 2019
fabpot added a commit to symfony/symfony that referenced this pull request Aug 18, 2019
This PR was merged into the 4.4 branch.

Discussion
----------

[Monolog] Added ElasticsearchLogstashHandler

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

This PR was initially [submitted on Monolog](Seldaek/monolog#1334).
It has been refused , but Jordi suggested to add it to the symfony bridge. So here we go :)

---

ATM, there are few options to push log to Elastic Stack in order to play with them:

* install logstash and use a Gelf handler. It works but you have to install logstash and configure it. Not an easy task. More over, it need an extra PHP package
* use the ES handler: It does not play well with context and extra: Kibana is not able to filter on nested object. And this handler is tightly coupled to the ElasticaFormatter formater. More over, it need an extra PHP package
* use something to parse file logs. This is really a bad idea since it involves a parsing... More over a daemon is needed to do that (file beat / logstash / you name it)

This is why I'm introducing a new Handler.

* There is not need to install anything (expect ES, of course)
* It play very well with Kibana, as it uses the Logstash format
* It requires symfony/http-client, but in a modern PHP application (SF 4.3) this dependency is already present
* It slow down a bit the application since it trigger an HTTP request for each logs. But symfony/http-client is non-blocking. If you want to use it in production, I recommend to wrap this handler in a buffer handler or a cross-finger handle to have only one HTTP call.

---

Some performance consideration en a prod env with a buffer handler + this one

* with push to ES: https://blackfire.io/profiles/f94ccf35-9f9d-4df1-bfc5-7fa75a535628/graph
* with push to ES commented: https://blackfire.io/profiles/6b66bc18-6b90-4341-963f-797f7a7a689c/graph

As you can see, as requests are made synchronously, there is no penalty on `AppKernel::Handler()` 😍! But the PHP worker has more work to do, and it's busy much more time (about X2)

I explained everything in the PHP Doc Block

---

This is what you can expect **out of the box**
![image](https://user-images.githubusercontent.com/408368/59916122-9b7b7580-941e-11e9-9a22-f56bc1d1a288.png)

Commits
-------

1587e9a [Monolog] Added ElasticsearchLogstashHandler
symfony-splitter pushed a commit to symfony/monolog-bridge that referenced this pull request Aug 18, 2019
This PR was merged into the 4.4 branch.

Discussion
----------

[Monolog] Added ElasticsearchLogstashHandler

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

This PR was initially [submitted on Monolog](Seldaek/monolog#1334).
It has been refused , but Jordi suggested to add it to the symfony bridge. So here we go :)

---

ATM, there are few options to push log to Elastic Stack in order to play with them:

* install logstash and use a Gelf handler. It works but you have to install logstash and configure it. Not an easy task. More over, it need an extra PHP package
* use the ES handler: It does not play well with context and extra: Kibana is not able to filter on nested object. And this handler is tightly coupled to the ElasticaFormatter formater. More over, it need an extra PHP package
* use something to parse file logs. This is really a bad idea since it involves a parsing... More over a daemon is needed to do that (file beat / logstash / you name it)

This is why I'm introducing a new Handler.

* There is not need to install anything (expect ES, of course)
* It play very well with Kibana, as it uses the Logstash format
* It requires symfony/http-client, but in a modern PHP application (SF 4.3) this dependency is already present
* It slow down a bit the application since it trigger an HTTP request for each logs. But symfony/http-client is non-blocking. If you want to use it in production, I recommend to wrap this handler in a buffer handler or a cross-finger handle to have only one HTTP call.

---

Some performance consideration en a prod env with a buffer handler + this one

* with push to ES: https://blackfire.io/profiles/f94ccf35-9f9d-4df1-bfc5-7fa75a535628/graph
* with push to ES commented: https://blackfire.io/profiles/6b66bc18-6b90-4341-963f-797f7a7a689c/graph

As you can see, as requests are made synchronously, there is no penalty on `AppKernel::Handler()` 😍! But the PHP worker has more work to do, and it's busy much more time (about X2)

I explained everything in the PHP Doc Block

---

This is what you can expect **out of the box**
![image](https://user-images.githubusercontent.com/408368/59916122-9b7b7580-941e-11e9-9a22-f56bc1d1a288.png)

Commits
-------

1587e9a4e2 [Monolog] Added ElasticsearchLogstashHandler
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