33import base64
44import logging
55import re
6- from enum import Enum
6+ from pathlib import Path
77
88from six import iteritems
99
10+ from .utils import ReportingPrecision , to_timestamp_in_precision
11+
1012try :
1113 from urllib2 import quote , urlopen , Request , URLError
1214except ImportError :
2729DEFAULT_INFLUX_PASSWORD = None
2830DEFAULT_INFLUX_PROTOCOL = "http"
2931
30- class ReportingPrecision (Enum ):
31- HOURS = "h"
32- MINUTES = "m"
33- SECONDS = "s"
34- MILLISECONDS = "ms"
35- MICROSECONDS = "u"
36- NANOSECONDS = "ns"
37-
38-
3932class InfluxReporter (Reporter ):
4033 """
4134 InfluxDB reporter using native http api
@@ -77,6 +70,7 @@ def __init__(
7770 self .autocreate_database = autocreate_database
7871 self ._did_create_database = False
7972 self .retention_policy = retention_policy
73+ self .reported_files = []
8074
8175 if global_tags is None :
8276 self .global_tags = {}
@@ -109,7 +103,7 @@ def report_now(self, registry=None, timestamp=None):
109103 if self .autocreate_database and not self ._did_create_database :
110104 self ._create_database ()
111105 timestamp = timestamp or self .clock .time ()
112- timestamp_in_reporting_precision = _to_timestamp_in_precision (
106+ timestamp_in_reporting_precision = to_timestamp_in_precision (
113107 timestamp = timestamp ,
114108 precision = self .reporting_precision
115109 )
@@ -145,7 +139,7 @@ def _get_influx_protocol_lines(self, metrics, timestamp):
145139 for event in metric_values .get ("events" , []):
146140 values = InfluxReporter ._stringify_values (event .values )
147141
148- event_timestamp = _to_timestamp_in_precision (
142+ event_timestamp = to_timestamp_in_precision (
149143 timestamp = event .time ,
150144 precision = self .reporting_precision
151145 )
@@ -160,6 +154,25 @@ def _get_influx_protocol_lines(self, metrics, timestamp):
160154
161155 return lines
162156
157+ def report_from_files (self , files_path : Path ) -> None :
158+ """
159+ Report to Influx from list of file in a given directory.
160+ NOTE: The files in the path must be in line protocol format.
161+
162+ :param files_path: The path where all the files stored.
163+ :return: None
164+ """
165+ if not files_path .exists ():
166+ raise FileNotFoundError
167+
168+ files = [f for f in files_path .glob ("*.txt" ) if f not in self .reported_files ]
169+
170+ for file in files :
171+ with open (file , "r" ) as metrics_file :
172+ url = self ._get_url ()
173+ if self ._try_send (url , metrics_file .read ()):
174+ self .reported_files .append (file )
175+
163176 @staticmethod
164177 def _stringify_values (metric_values ):
165178 return "," .join (
@@ -196,16 +209,16 @@ def _add_auth_data(self, request):
196209 auth = _encode_username (self .username , self .password )
197210 request .add_header ("Authorization" , "Basic %s" % auth .decode ('utf-8' ))
198211
199- def _try_send (self , url , data ):
212+ def _try_send (self , url , data ) -> bool :
200213 request = Request (url , data .encode ("utf-8" ))
201214 if self .username :
202215 self ._add_auth_data (request )
203216 try :
204217 response = urlopen (request )
205218 response .read ()
219+ return True
206220 except URLError as err :
207- response = err .read ().decode ("utf-8" )
208-
221+ response = str (err )
209222 LOG .warning (
210223 "Cannot write to %s: %s ,url: %s, data: %s, response: %s" ,
211224 self .server ,
@@ -214,28 +227,7 @@ def _try_send(self, url, data):
214227 data ,
215228 response
216229 )
217-
218- def _to_timestamp_in_precision (timestamp : float , precision : ReportingPrecision ) -> int :
219- if precision == ReportingPrecision .HOURS :
220- return int (timestamp / 60 / 60 )
221-
222- if precision == ReportingPrecision .MINUTES :
223- return int (timestamp / 60 )
224-
225- if precision == ReportingPrecision .SECONDS :
226- return int (timestamp )
227-
228- if precision == ReportingPrecision .MILLISECONDS :
229- return int (timestamp * 1e3 )
230-
231- if precision == ReportingPrecision .MICROSECONDS :
232- return int (timestamp * 1e6 )
233-
234- if precision == ReportingPrecision .NANOSECONDS :
235- return int (timestamp * 1e9 )
236-
237- raise Exception ("Unsupported ReportingPrecision" )
238-
230+ return False
239231
240232def _format_field_value (value ):
241233 if isinstance (value , MarkInt ):
0 commit comments