Application Performance Monitoring (APM)The MongoDB driver contains an event subscriber API, which allows applications to monitor commands and internal activity pertaining to the » Server Discovery and Monitoring Specification. This tutorial will demonstrate command monitoring using the MongoDB\Driver\Monitoring\CommandSubscriber interface.
The MongoDB\Driver\Monitoring\CommandSubscriber
interface defines three methods: In this tutorial we will implement a subscriber that creates a list of all the query profiles and the average time they took. Subscriber Class ScaffoldingWe start with the framework for our subscriber:
<?php Registering the SubscriberOnce a subscriber object is instantiated, it needs to be registered with the driver's monitoring system. This is done by calling MongoDB\Driver\Monitoring\addSubscriber or MongoDB\Driver\Manager::addSubscriber to register the subscriber globally or with a specific Manager, respectively.
<?php Implementing the Logic
With the object registered, the only thing left is to implement the logic
in the subscriber class. To correlate the two events that make up a
successfully executed command (commandStarted and commandSucceeded), each
event object exposes a
To record the average time per query shape, we will start by checking for a
If we receive a corresponding commandSucceeded event with the same
If a corresponding commandFailed event is encountered, we just remove the
entry from the
<?php |