Exemplo n.º 1
0
    def makePickle(self, record):
        """
        Prepare all info and use influx.protocol.encode_line to create
        the message.
        """
        ts = int(record.created * NANOSECONDS_PER_SECOND)
        msg = record.getMessage()
        if self.localname:
            host = self.localname
        else:
            host = socket.getfqdn() if self.fqdn else socket.gethostname()

        tags = {
            "host": host,
            "level": SYSLOG_LEVELS.get(record.levelno, record.levelno),
            "level_name": logging.getLevelName(record.levelno),
            "logger": record.name,
        }
        tags.update(self.global_tags)

        fields = {
            "message": msg,
        }
        # Add debugging fields if enabled
        if self.debugging_fields:
            fields.update({
                "file": record.pathname,
                "line": record.lineno,
                "function": record.funcName,
                "pid": record.process,
                "thread_name": record.threadName,
            })
            pn = getattr(record, "processName", None)
            if pn is not None:
                fields["process_name"] = pn

        # Use pre-formatted exception information in cases where the primary
        # exception information was removed, eg. for LogRecord serialization
        if record.exc_info and not record.exc_text:
            # format exception information if present
            formatter = logging._defaultFormatter
            record.exc_text = formatter.formatException(record.exc_info)
        if record.exc_text:
            fields["full_message"] = "\n".join([msg, record.exc_text])

        if self.extra_fields:
            fields = add_extra_fields(fields, record)

        return encode_line(self.measurement, tags, fields, ts=ts)
Exemplo n.º 2
0
 def test__empty_measurement__fail(self):
     with self.assertRaisesRegex(ValueError, "none or empty measurement"):
         encode_line("", self.xy_tag, self.xy_field)
Exemplo n.º 3
0
 def test__ts_not_int__fail(self):
     with self.assertRaisesRegex(ValueError, "ts not an integer"):
         encode_line("m", self.xy_tag, self.xy_field, ts=5.0)
Exemplo n.º 4
0
 def test__empty_fields__fail(self):
     with self.assertRaisesRegex(ValueError,
                                 "need at least one field value"):
         encode_line("weather", {}, {})
Exemplo n.º 5
0
 def test__escape_field_keys_awkward(self):
     result = encode_line("m", {}, {"t\\,t": "val"}).decode("utf-8")
     self.assertEqual('m t\\\\\\,t="val"', result)
Exemplo n.º 6
0
 def test__escape_field_keys(self):
     result = encode_line("m", {}, {
         "t t": "v,v",
         "u=u": "v,v"
     }).decode("utf-8")
     self.assertEqual('m t\\ t="v,v",u\\=u="v,v"', result)
Exemplo n.º 7
0
 def test__escape_tags_key_and_val(self):
     result = encode_line("m", {
         "t t": "v,v",
         "u=u": "v,v"
     }, self.xy_field).decode("utf-8")
     self.assertEqual('m,t\\ t=v\\,v,u\\=u=v\\,v x="y"', result)
Exemplo n.º 8
0
 def test__field_bool_value_false(self):
     result = encode_line("m", {}, {"b": False}).decode("utf-8")
     self.assertEqual("m b=false", result)
Exemplo n.º 9
0
 def test__field_bool_value_true(self):
     result = encode_line("m", {}, {"b": True}).decode("utf-8")
     self.assertEqual("m b=true", result)
Exemplo n.º 10
0
 def test__field_int_value(self):
     result = encode_line("m", {}, {"i": 3}).decode("utf-8")
     self.assertEqual("m i=3i", result)
Exemplo n.º 11
0
 def test__tag_int_value(self):
     result = encode_line("m", {"t": 1}, self.xy_field).decode("utf-8")
     self.assertEqual('m,t=1 x="y"', result)
Exemplo n.º 12
0
 def test__one_field_with_timestamp(self):
     result = encode_line("weather", {}, self.weather_fields,
                          ts=self.ts).decode("utf-8")
     self.assertEqual("weather temperature=8.0 1533390240607501568", result)
Exemplo n.º 13
0
 def test__one_tag_one_field(self):
     result = encode_line("weather", self.station_tags,
                          self.weather_fields).decode("utf-8")
     self.assertEqual("weather,station=A1 temperature=8.0", result)
Exemplo n.º 14
0
 def test__one_field(self):
     result = encode_line("weather", {},
                          self.weather_fields).decode("utf-8")
     self.assertEqual("weather temperature=8.0", result)