Ejemplo n.º 1
0
 def test_print_writer_stdout(self):
     f = io.StringIO()
     with redirect_stdout(f):
         with closing(
                 SidecarWriter.create("stdout")) as w:  # type: PrintWriter
             w.write_line("foo")
             self.assertEqual("foo\n", f.getvalue())
Ejemplo n.º 2
0
 def __init__(
     self,
     meter_id: MeterId,
     writer: SidecarWriter = SidecarWriter.create("none")
 ) -> None:
     super().__init__(meter_id, "m")
     self._writer = writer
Ejemplo n.º 3
0
 def test_noop_writer(self):
     noop = SidecarWriter.create("none")
     try:
         noop.write_impl("foo")
         noop.close()
     except NotImplementedError:
         self.fail("NoopWriter raised NotImplementedError unexpectedly!")
Ejemplo n.º 4
0
 def test_udp(self) -> None:
     with closing(UdpServer()) as server:  # type: UdpServer
         with closing(SidecarWriter.create(
                 server.address())) as w:  # type: SidecarWriter
             w.write_line("foo")
             self.assertEqual("foo", server.read())
             w.write_line("bar")
             self.assertEqual("bar", server.read())
Ejemplo n.º 5
0
    def test_print_writer_custom(self):
        with TemporaryDirectory() as tmpdir:
            tmpfile = os.path.join(tmpdir, "print-writer.txt")
            writer = SidecarWriter.create("file://{}".format(tmpfile))

            with closing(writer) as w:  # type: PrintWriter
                w.write_line("foo")

            with open(tmpfile, encoding="utf-8") as f:
                line = f.read()
                self.assertEqual("foo\n", line)
Ejemplo n.º 6
0
 def __init__(
     self,
     meter_id: MeterId,
     meter_type: str = "d",
     writer: SidecarWriter = SidecarWriter.create("none")
 ) -> None:
     if meter_type not in ["d", "D"]:
         raise ValueError(
             "Distribution Summaries must have a meter type of 'd' or 'D'.")
     super().__init__(meter_id, meter_type)
     self._writer = writer
Ejemplo n.º 7
0
 def __init__(
     self,
     meter_id: MeterId,
     ttl_seconds: Optional[int] = None,
     writer: SidecarWriter = SidecarWriter.create("none")
 ) -> None:
     if ttl_seconds is None:
         meter_type = "g"
     else:
         meter_type = "g,{}".format(ttl_seconds)
     super().__init__(meter_id, meter_type)
     self._writer = writer
Ejemplo n.º 8
0
    def __init__(
        self,
        meter_id: MeterId,
        clock: Clock = SystemClock(),
        meter_type: str = "t",
        writer: SidecarWriter = SidecarWriter.create("none")
    ) -> None:
        if meter_type not in ["t", "T"]:
            raise ValueError("Timers must have a meter type of 't' or 'T'.")

        super().__init__(meter_id, meter_type)
        self._clock = clock
        self._writer = writer
Ejemplo n.º 9
0
    def test_concurrent_writes(self) -> None:
        """When running with four writer threads and one reader thread, there appears to
        be an upper limit of 18,000 +/- 500 messages. This can be observed by increasing
        the messages_per_thread to 10000. We may need to investigate performance here. The
        current value for messages_per_thread is the largest value that allows the test to
        complete successfully in most cases. Even adding a debug log line to the UdpWriter
        class can impact the number of messages received."""

        messages_per_thread = 2500
        writer_thread_count = 4
        lines = []

        with closing(UdpServer()) as server:  # type: UdpServer
            with closing(SidecarWriter.create(
                    server.address())) as w:  # type: SidecarWriter

                def reader_target() -> None:
                    while True:
                        line = server.read()
                        if line != "done":
                            lines.append(line)
                        else:
                            break

                reader = threading.Thread(target=reader_target)
                reader.start()

                def writer_target(n: int) -> None:
                    base = n * messages_per_thread
                    for i in range(0, messages_per_thread):
                        w.write_line("{}".format(base + i))

                threads = []
                for j in range(0, writer_thread_count):
                    thread = threading.Thread(target=writer_target, args=[j])
                    threads.append(thread)
                    thread.start()

                for t in threads:
                    t.join()

                w.write_line("done")
                reader.join()

        m = writer_thread_count * messages_per_thread
        self.assertEqual(m, len(lines))
        self.assertEqual(m * (m - 1) / 2, sum(map(int, lines)))
Ejemplo n.º 10
0
 def test_invalid_location(self):
     with self.assertRaises(ValueError):
         SidecarWriter.create("foo")
Ejemplo n.º 11
0
 def test_close_requires_concrete_class(self):
     with self.assertRaises(NotImplementedError):
         SidecarWriter("foo").close()
Ejemplo n.º 12
0
 def test_write_impl_requires_concrete_class(self):
     with self.assertRaises(NotImplementedError):
         SidecarWriter("foo").write_impl("bar")
Ejemplo n.º 13
0
 def __init__(self, clock: Clock = SystemClock(),
              config: SidecarConfig = SidecarConfig()) -> None:
     self._clock = clock
     self._common_tags = config.common_tags()
     self._writer = SidecarWriter.create(config.output_location())