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)
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
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]}
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]}