From f60a9ef3f964a48ea083d3dbe25426bfdb04c47b Mon Sep 17 00:00:00 2001 From: slepic Date: Thu, 8 Feb 2024 22:38:48 +0100 Subject: [PATCH] Send logdna now argument in milliseconds (#23) * Send logdna now argument in milliseconds * update readme * consistent naming * simplify millitimestamp retrieval --- README.md | 9 +++++++++ src/Monolog/Handler/LogdnaHandler.php | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/README.md b/README.md index e3a948f..9c7e3aa 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,15 @@ Monolog Processors may add some extra data to the log records. This data will appear in logdna log metadata as property `monolog_extra` unless it is empty. If such a property already exists in the log record's `context`, it will be overwritten. +## Time Drift Calculation + +By default, the handler sends `now` parameter to the [Ingestion API](https://docs.mezmo.com/log-analysis-api#ingest), +which is used to calculate time drift. You can disable sending this parameter via + +``` +$logdnaHandler->setIncludeRequestTime(false); +``` + ## License This project is licensed under LGPL3.0. See `LICENSE` file for details. diff --git a/src/Monolog/Handler/LogdnaHandler.php b/src/Monolog/Handler/LogdnaHandler.php index c500a7c..c2e2a69 100644 --- a/src/Monolog/Handler/LogdnaHandler.php +++ b/src/Monolog/Handler/LogdnaHandler.php @@ -46,6 +46,11 @@ class LogdnaHandler extends \Monolog\Handler\AbstractProcessingHandler */ private $tags = ''; + /** + * @var bool + */ + private $include_request_time = true; + /** * @var resource $curl_handle */ @@ -75,6 +80,14 @@ public function setTags($tags) $this->tags = $tags; } + /** + * @param bool $include + */ + public function setIncludeRequestTime($include) + { + $this->include_request_time = $include; + } + /** * @param string $ingestion_key * @param string $hostname @@ -108,6 +121,11 @@ protected function write(\Monolog\LogRecord $record): void 'ip' => $this->ip, 'tags' => $this->tags ]; + + if ($this->include_request_time) { + $query['now'] = (string) \floor(\microtime(true) * 1000.0); + } + $url = 'https://logs.mezmo.com/logs/ingest?' . \http_build_query(\array_filter($query)); \curl_setopt($this->curl_handle, CURLOPT_URL, $url);