Skip to content

Commit bdc5787

Browse files
committed
Refactor method message to line protocol
This solution should improves the actual message to line protocol conversion strategy and it includes a new feature in order to set point-to-point capture timing ```php $client->mark([ "time" => "2009-01-01T10:28:00Z", //All point use this "points" => [ ... ] ... ]); ``` In this PR you can specify time for single measured point ```php $client->mark([ "points" => [ [ "measurement" => "cpu", "time" => "2009-11-10T23:00:00Z", "fields" => [ // ... ] ] ]); ``` It is also possibile to mix previous solution in order to apply a base timing to existing points without a time capture
1 parent a122e8d commit bdc5787

File tree

2 files changed

+210
-141
lines changed

2 files changed

+210
-141
lines changed

src/Adapter/AdapterAbstract.php

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,43 @@ protected function messageToLineProtocol(array $message)
2727
return;
2828
}
2929

30-
if (!array_key_exists("tags", $message)) {
31-
$message["tags"] = [];
32-
}
33-
30+
$message = $this->prepareMessageSection($message);
3431
$message["tags"] = array_replace_recursive($this->getOptions()->getTags(), $message["tags"]);
3532

36-
$unixepoch = (int)(microtime(true) * 1e9);
37-
if (array_key_exists("time", $message)) {
38-
$dt = new DateTime($message["time"]);
39-
$unixepoch = (int)($dt->format("U") * 1e9);
40-
}
41-
4233
$lines = [];
4334
foreach ($message["points"] as $point) {
44-
$tags = $message["tags"];
45-
if (array_key_exists("tags", $point)) {
46-
$tags = array_replace_recursive($tags, $point["tags"]);
47-
}
35+
$point = $this->prepareMessageSection($point, $message["time"]);
36+
$tags = array_replace_recursive($message["tags"], $point["tags"]);
4837

4938
$tagLine = $this->tagsToString($tags);
5039

5140
$lines[] = sprintf(
52-
"%s%s %s %d", $point["measurement"], $tagLine, $this->pointsToString($point["fields"]), $unixepoch
41+
"%s%s %s %d", $point["measurement"], $tagLine, $this->pointsToString($point["fields"]), $point["time"]
5342
);
5443
}
5544

5645
return implode("\n", $lines);
5746
}
5847

48+
private function prepareMessageSection(array $message, $unixepoch = false)
49+
{
50+
if (!array_key_exists("tags", $message)) {
51+
$message["tags"] = [];
52+
}
53+
54+
if (!$unixepoch) {
55+
$unixepoch = (int)(microtime(true) * 1e9);
56+
}
57+
58+
if (array_key_exists("time", $message)) {
59+
$dt = new DateTime($message["time"]);
60+
$unixepoch = (int)($dt->format("U") * 1e9);
61+
}
62+
$message["time"] = $unixepoch;
63+
64+
return $message;
65+
}
66+
5967
protected function tagsToString(array $tags)
6068
{
6169
$tagLine = "";

0 commit comments

Comments
 (0)