1. Go to this page and download the library: Download pluggit/monitoring library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
pluggit / monitoring example snippets
public function addDefaultTags(array $tags);
public function send(AbstractMetric $metric);
public function send(Event $event);
public function pushMetricSender(MetricSender $sender);
public function pushEventSender(EventSender $sender);
public function getMetricFactory();
public function getEventFactory();
public function counter($metric, $count = 1, array $tags = [], $sampleRate = AbstractMetric::DEFAULT_SAMPLE_RATE)
$monitor->counter('wh.queries_executed', 38);
public function increment($metric, array $tags = [], $sampleRate = AbstractMetric::DEFAULT_SAMPLE_RATE)
$monitor->increment('wh.page_views', ['page' => 'site/home']);
public function decrement($metric, array $tags = [], $sampleRate = AbstractMetric::DEFAULT_SAMPLE_RATE)
$monitor->decrement('wh.remaining_slots');
public function gauge($metric, $level, array $tags = [], $sampleRate = AbstractMetric::DEFAULT_SAMPLE_RATE)
$monitor->gauge('wh.redis.memory_used', 736827);
public function set($metric, $uniqueValue, array $tags = [], $sampleRate = AbstractMetric::DEFAULT_SAMPLE_RATE)
$monitor->set('wh.user', 314646);
public function histogram($metric, $duration, array $tags = [], $sampleRate = AbstractMetric::DEFAULT_SAMPLE_RATE)
$monitor->histogram('wh.user_creation', 325, ['controller' => 'api.user']);
public function start($metric, array $tags = [], $sampleRate = AbstractMetric::DEFAULT_SAMPLE_RATE)
$monitor->start('wh.user_creation', ['controller' => 'site.registration']);
public function end($metric, array $tags = [])
$monitor->end('wh.user_creation', ['result' => 'ok']);
public function sendMetric(AbstractMetric $metric);
public function sendEvent(Event $event);
// Build the metric factory with your choosen default tags
$metricFactory = new MetricFactory(['environment' => 'dev', 'mode' => 'production'], 'myapp-prefix.');
// Build the event factory with the host name and your choosen default tags
$eventFactory = new EventFactory('my_docker_hostname', ['environment' => 'dev', 'mode' => 'production', 'domain' => 'my_domain']);
// Build a Monolog Logger and push handlers
$logger = new Monolog\Logger('monitoring');
$logger->pushHandler(/* build handler for logging messages */);
// Build the monitor
$monitor = new Monitor($metricFactory, $eventFactory, $logger);
// Populate the monitor with the desired metric and event senders
$monitor
->pushMetricSender(new MonologMetricSender(/* object dependencies */))
->pushMetricSender(new DataDogMetricSender(/* object dependencies */))
->pushEventSender(new MonologMetricSender(/* object dependencies */))
->pushEventSender(new MonologMetricSender(/* object dependencies */));
// Use the monitor to send metrics and events
$monitor->increment('wh.page_views');
$monitor = MonitorFactory::create([
'hostname' => 'fooserver', # Hostname of the server
'default_tags' => ['foo' => 'bar'], # A key-value array with default tags for metrics and events
'prefix' => 'my-app.', # A prefix for the metrics
'logger' => [
'instance' => $logger, # A Psr\LoggerInterface instance
'debug' => true, # If true, it will log debug messages from the monitor
'level' => LogLevel::DEBUG, # The level for debug message
'metrics' => true, # If true, metrics will be sent trough the provided logger instance
'events' => true, # If true, events will be sent trough the provided logger instance
],
'datadog' => [
'metrics' => true, # If true, metrics will be sent trough the datadog agent
'events' => true, # If true, events will be sent trough the datadog agent
'host' => '10.0.0.1', # The datadog agent host, if null the default will be used
'port' => 8822, # The datadog agent port, if null the default will be used
],
]);
// Build the logger
$logger = new \Monolog\Logger('monitoring');
$logger->pushHandler(new \Monolog\Handler\StreamHandler('/tmp/monitoring.log'));
// Push as many monolog handlers as you want
// Build the serializer
$serializer = \JMS\Serializer\SerializerBuilder::create()->build();
// Create the metric and/or event sender
$eventSender = new \Cmp\Infrastructure\Application\Monitoring\Monolog\Event\Sender($logger, $serializer);
$metricSender = new \Cmp\Infrastructure\Application\Monitoring\Monolog\Metric\Sender($logger, $serializer);
// Push them to the monitor
$monitor->pushMetricSender($metricSender);
$monitor->pushEventSender($eventSender);
$logger = new \Monolog\Logger('monitoring');
$logger->pushHandler(new \Monolog\AbstractHandler\StreamHandler('/tmp/monitoring.log'));
$logger->pushHandler(new \Monolog\AbstractHandler\StreamHandler('/tmp/monitoring_errors.log'), Logger::ERROR);
// This event sender will use error as level for generating the logger message, so only the second monolog handler will treat the events
$eventSender = new \Cmp\Infrastructure\Application\Monitoring\Monolog\Event\Sender($logger, serializer, Logger::ERROR);
// This event sender will log all events because both monolog handlers accept debug level and above
$eventSender = new \Cmp\Infrastructure\Application\Monitoring\Monolog\Event\Sender($logger, serializer, Logger::DEBUG);
// Datadog configuration
$datadogAgent = ['ip' => '127.0.0.1', 'port' => 8125];
// Build the sender
$socket = \Cmp\Infrastructure\Application\Monitoring\DataDog\Metric\Socket();
$metricSender = new \Cmp\Infrastructure\Application\Monitoring\DataDog\Metric\Sender($socket, datadogAgent['ip'], datadogAgent['port']);
// Push to the monitor
$monitor->pushMetricSender($metricSender);
// Build the API
$apiKey = 'YOUR API KEY';
$appKey = 'YOUR APPLICATION KEY';
$apiTransport = \Cmp\Infrastructure\Application\Monitoring\DataDog\Api\Transport\Curl();
$api = \Cmp\Infrastructure\Application\Monitoring\DataDog\Api\Api($apiTransport, $apiKey, $appKey);
// Create the events api SDK
$eventsApi = new \Cmp\Infrastructure\Application\Monitoring\DataDog\Api\Method\Events($api);
// Build the sender
$eventSender = new \Cmp\Infrastructure\Application\Monitoring\DataDog\Event\Sender($eventsApi);
// Push it to the monitor
$monitor->pushEventSender($eventSender);
//Setup the Rabbit producer
$rabbitProducer = \Cmp\Infrastructure\Application\Notification\RabbitMQ\Message\Producer($rabbitConfig);
// Build the sender with the producer and providing a destination exchange
$messageEventSender = new \Cmp\Infrastructure\Application\Monitoring\Message\Event\Sender($rabbitProducer, 'monitoring.events');
// Push it to the monitor
$monitor->pushEventSender($eventSender);
// Build the rabbit consumer
$rabbitConsumer = new Cmp\Infrastructure\Application\Notification\RabbitMQ\Message\Consumer($rabbitConfig);
// The consumer nsumer with the correct origin to read the messages
$messageEventConsumer = new \Cmp\Infrastructure\Application\Monitoring\Message\Event\Consumer($rabbitConsumer, $logger, 'monitoring.events';
// Push as many event senders as you want
$messageEventConsumer->pushSender($datadogEventSender);
$messageEventConsumer->pushSender($monologEventSender);
// Start listening to messages in an infinite loop
$messageEventConsumer->start();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.