Esempio n. 1
0
 def emit(self, record: logging.LogRecord):
     """
     Send log record to Loki.
     """
     # noinspection PyBroadException
     try:
         labels = self.build_labels(record)
         ts = rfc3339.format_microsecond(record.created)
         line = self.format(record)
         payload = {
             "streams": [{
                 "labels": labels,
                 "entries": [{
                     "ts": ts,
                     "line": line
                 }]
             }]
         }
         resp = self.session.post(self.url, json=payload)
         if resp.status_code != 204:
             raise ValueError(
                 "Unexpected Loki API response status code: %s" %
                 resp.status_code)
     except Exception:
         self.handleError(record)
Esempio n. 2
0
def test_timestamp_added_to_values(emitter_v0):
    emitter, session = emitter_v0
    emitter(create_record(), "")

    stream = get_stream(session)
    expected = rfc3339.format_microsecond(time.time())
    assert stream["entries"][0]["ts"] == expected
Esempio n. 3
0
 def build_payload(self, record: logging.LogRecord, line) -> dict:
     """Build JSON payload with a log entry."""
     labels = self.build_labels(record)
     ts = rfc3339.format_microsecond(record.created)
     stream = {
         "labels": labels,
         "entries": [{"ts": ts, "line": line}],
     }
     return {"streams": [stream]}
Esempio n. 4
0
 def build_payload(self, record: logging.LogRecord, line) -> dict:
     """Build JSON payload with a log entry."""
     labels = self.build_tags(record)
     ns = 1e9
     ts = rfc3339.format_microsecond(record.created)
     stream = {
         "stream": labels,
         "values": [[ts, line]],
     }
     return {"streams": [stream]}