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

Send logdna now argument in milliseconds #23

Merged
merged 4 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 22 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,11 @@ public function setTags($tags)
$this->tags = $tags;
}

public function setIncludeRequestTime($include)
Copy link
Owner

Choose a reason for hiding this comment

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

Missing @param documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added

{
$this->include_request_time = $include;
}

/**
* @param string $ingestion_key
* @param string $hostname
Expand Down Expand Up @@ -108,6 +118,11 @@ protected function write(\Monolog\LogRecord $record): void
'ip' => $this->ip,
'tags' => $this->tags
];

if ($this->include_request_time) {
$query['now'] = $this->getCurrentTimeInMilliseconds();
Copy link
Owner

Choose a reason for hiding this comment

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

We could replace this with just (string)floor(microtime(true) * 1000).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}

$url = 'https://logs.mezmo.com/logs/ingest?' . \http_build_query(\array_filter($query));

\curl_setopt($this->curl_handle, CURLOPT_URL, $url);
Expand All @@ -128,4 +143,11 @@ protected function getDefaultFormatter(): FormatterInterface
{
return new \Zwijn\Monolog\Formatter\LogdnaFormatter();
}

private function getCurrentTimeInMilliseconds(): string
Copy link
Owner

Choose a reason for hiding this comment

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

I would just remove this function or replace it's content with

return (string)floor(microtime(true) * 1000);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

{
$time = \microtime();
$parts = \explode(' ', $time, 2);
return $parts[1] . \str_pad((string) \round(1000.0 * (float) $parts[0]), 3, '0', \STR_PAD_LEFT);
}
}