@@ -27,35 +27,43 @@ protected function messageToLineProtocol(array $message)
27
27
return ;
28
28
}
29
29
30
- if (!array_key_exists ("tags " , $ message )) {
31
- $ message ["tags " ] = [];
32
- }
33
-
30
+ $ message = $ this ->prepareMessageSection ($ message );
34
31
$ message ["tags " ] = array_replace_recursive ($ this ->getOptions ()->getTags (), $ message ["tags " ]);
35
32
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
-
42
33
$ lines = [];
43
34
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 " ]);
48
37
49
38
$ tagLine = $ this ->tagsToString ($ tags );
50
39
51
40
$ 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 " ]
53
42
);
54
43
}
55
44
56
45
return implode ("\n" , $ lines );
57
46
}
58
47
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
+
59
67
protected function tagsToString (array $ tags )
60
68
{
61
69
$ tagLine = "" ;
0 commit comments