|
20 | 20 | from opentelemetry import trace as trace_api |
21 | 21 | from opentelemetry.instrumentation.flask import FlaskInstrumentor |
22 | 22 | from opentelemetry.test.wsgitestutil import WsgiTestBase |
| 23 | +from opentelemetry.trace.status import StatusCode |
23 | 24 |
|
24 | 25 | # pylint: disable=import-error |
25 | 26 | from .base_test import InstrumentationTest |
@@ -96,26 +97,58 @@ def test_no_op_tracer_provider(self): |
96 | 97 | span_list = self.memory_exporter.get_finished_spans() |
97 | 98 | self.assertEqual(len(span_list), 0) |
98 | 99 |
|
| 100 | + def test_cli_command_wrapping(self): |
| 101 | + @self.app.cli.command() |
| 102 | + def flask_command(): |
| 103 | + print("flask") |
| 104 | + pass |
| 105 | + |
| 106 | + runner = self.app.test_cli_runner() |
| 107 | + result = runner.invoke(args=["flask-command"]) |
| 108 | + (span,) = self.memory_exporter.get_finished_spans() |
| 109 | + |
| 110 | + self.assertEqual(span.status.status_code, StatusCode.UNSET) |
| 111 | + self.assertEqual(span.name, "flask_command") |
| 112 | + |
| 113 | + def test_cli_command_wrapping_with_name(self): |
| 114 | + @self.app.cli.command("mycommand") |
| 115 | + def flask_command(): |
| 116 | + print("my") |
| 117 | + pass |
| 118 | + |
| 119 | + runner = self.app.test_cli_runner() |
| 120 | + print(runner) |
| 121 | + result = runner.invoke(args=["mycommand"]) |
| 122 | + (span,) = self.memory_exporter.get_finished_spans() |
| 123 | + |
| 124 | + self.assertEqual(span.status.status_code, StatusCode.UNSET) |
| 125 | + self.assertEqual(span.name, "mycommand") |
| 126 | + |
| 127 | + def test_cli_command_wrapping_with_options(self): |
| 128 | + @self.app.cli.command() |
| 129 | + @click.option("--option", default="default") |
| 130 | + def my_command_with_opts(option): |
| 131 | + print("opts") |
| 132 | + pass |
| 133 | + |
| 134 | + runner = self.app.test_cli_runner() |
| 135 | + result = runner.invoke( |
| 136 | + args=["my-command-with-opts", "--option", "option"], |
| 137 | + ) |
| 138 | + (span,) = self.memory_exporter.get_finished_spans() |
99 | 139 |
|
100 | | -@app.cli.command("mycommand") |
101 | | -@click.option("--option", default="default") |
102 | | -def flask_command(option): |
103 | | - pass |
104 | | - |
105 | | - |
106 | | -def test_cli_command_wrapping(runner): |
107 | | - FlaskInstrumentor().instrument() |
108 | | - |
109 | | - result = runner.invoke(args=["mycommand"]) |
110 | | - (span,) = self.memory_exporter.get_finished_spans() |
| 140 | + self.assertEqual(span.status.status_code, StatusCode.UNSET) |
| 141 | + self.assertEqual(span.name, "my_command_with_opts") |
111 | 142 |
|
112 | | - FlaskInstrumentor().uninstrument() |
| 143 | + def test_cli_command_raises_error(self): |
| 144 | + @self.app.cli.command() |
| 145 | + def command_raises(): |
| 146 | + print("raises") |
| 147 | + raise ValueError() |
113 | 148 |
|
| 149 | + runner = self.app.test_cli_runner() |
| 150 | + result = runner.invoke(args=["command-raises"]) |
| 151 | + (span,) = self.memory_exporter.get_finished_spans() |
114 | 152 |
|
115 | | -def test_cli_command_wrapping_with_options(runner): |
116 | | - FlaskInstrumentor().instrument() |
117 | | - result_with_option = runner.invoke( |
118 | | - args=["mycommand", "--option", "option"] |
119 | | - ) |
120 | | - (span,) = self.memory_exporter.get_finished_spans() |
121 | | - FlaskInstrumentor().uninstrument() |
| 153 | + self.assertEqual(span.status.status_code, StatusCode.ERROR) |
| 154 | + self.assertEqual(span.name, "command_raises") |
0 commit comments