示例#1
0
 def test_lifespan(self):
     self.scope["type"] = "lifespan"
     app = otel_asgi.OpenTelemetryMiddleware(simple_asgi)
     self.seed_app(app)
     self.send_default_request()
     span_list = self.memory_exporter.get_finished_spans()
     self.assertEqual(len(span_list), 0)
示例#2
0
 def test_asgi_exc_info(self):
     """Test that exception information is emitted as expected."""
     app = otel_asgi.OpenTelemetryMiddleware(error_asgi)
     self.seed_app(app)
     self.send_default_request()
     outputs = self.get_all_output()
     self.validate_outputs(outputs, error=ValueError)
示例#3
0
 def test_websocket(self):
     self.scope = {
         "type": "websocket",
         "http_version": "1.1",
         "scheme": "ws",
         "path": "/",
         "query_string": b"",
         "headers": [],
         "client": ("127.0.0.1", 32767),
         "server": ("127.0.0.1", 80),
     }
     app = otel_asgi.OpenTelemetryMiddleware(simple_asgi)
     self.seed_app(app)
     self.send_input({"type": "websocket.connect"})
     self.send_input({"type": "websocket.receive", "text": "ping"})
     self.send_input({"type": "websocket.disconnect"})
     self.get_all_output()
     span_list = self.memory_exporter.get_finished_spans()
     self.assertEqual(len(span_list), 6)
     expected = [
         "/ asgi.websocket.receive",
         "/ asgi.websocket.send",
         "/ asgi.websocket.receive",
         "/ asgi.websocket.send",
         "/ asgi.websocket.receive",
         "/ asgi",
     ]
     actual = [span.name for span in span_list]
     self.assertListEqual(actual, expected)
示例#4
0
 def test_basic_asgi_call(self):
     """Test that spans are emitted as expected."""
     app = otel_asgi.OpenTelemetryMiddleware(simple_asgi)
     self.seed_app(app)
     self.send_default_request()
     outputs = self.get_all_output()
     self.validate_outputs(outputs)
示例#5
0
    def test_user_agent(self):
        """Test that host header is converted to http.server_name."""
        user_agent = b"test-agent"

        def update_expected_user_agent(expected):
            expected[3]["attributes"].update(
                {"http.user_agent": user_agent.decode("utf8")})
            return expected

        self.scope["headers"].append([b"user-agent", user_agent])
        app = otel_asgi.OpenTelemetryMiddleware(simple_asgi)
        self.seed_app(app)
        self.send_default_request()
        outputs = self.get_all_output()
        self.validate_outputs(outputs, modifiers=[update_expected_user_agent])
示例#6
0
    def test_host_header(self):
        """Test that host header is converted to http.server_name."""
        hostname = b"server_name_1"

        def update_expected_server(expected):
            expected[3]["attributes"].update(
                {"http.server_name": hostname.decode("utf8")})
            return expected

        self.scope["headers"].append([b"host", hostname])
        app = otel_asgi.OpenTelemetryMiddleware(simple_asgi)
        self.seed_app(app)
        self.send_default_request()
        outputs = self.get_all_output()
        self.validate_outputs(outputs, modifiers=[update_expected_server])
示例#7
0
    def test_behavior_with_scope_server_as_none(self):
        """Test that middleware is ok when server is none in scope."""
        def update_expected_server(expected):
            expected[3]["attributes"].update({
                "http.host": "0.0.0.0",
                "host.port": 80,
                "http.url": "http://0.0.0.0/",
            })
            return expected

        self.scope["server"] = None
        app = otel_asgi.OpenTelemetryMiddleware(simple_asgi)
        self.seed_app(app)
        self.send_default_request()
        outputs = self.get_all_output()
        self.validate_outputs(outputs, modifiers=[update_expected_server])
示例#8
0
    def test_override_span_name(self):
        """Test that span_names can be overwritten by our callback function."""
        span_name = "Dymaxion"

        def get_predefined_span_details(_):
            return span_name, {}

        def update_expected_span_name(expected):
            for entry in expected:
                entry["name"] = " ".join([span_name] +
                                         entry["name"].split(" ")[-1:])
            return expected

        app = otel_asgi.OpenTelemetryMiddleware(
            simple_asgi, span_details_callback=get_predefined_span_details)
        self.seed_app(app)
        self.send_default_request()
        outputs = self.get_all_output()
        self.validate_outputs(outputs, modifiers=[update_expected_span_name])