Skip to content

Commit

Permalink
Send logdna now argument in milliseconds (#23)
Browse files Browse the repository at this point in the history
* Send logdna now argument in milliseconds

* update readme

* consistent naming

* simplify millitimestamp retrieval
  • Loading branch information
slepic authored Feb 8, 2024
1 parent f5b19ad commit f60a9ef
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions src/Monolog/Handler/LogdnaHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ class LogdnaHandler extends \Monolog\Handler\AbstractProcessingHandler
*/
private $tags = '';

/**
* @var bool
*/
private $include_request_time = true;

/**
* @var resource $curl_handle
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit f60a9ef

Please sign in to comment.