Skip to content

Commit d431c73

Browse files
committed
WIP
1 parent dc5f880 commit d431c73

File tree

2 files changed

+64
-28
lines changed

2 files changed

+64
-28
lines changed

instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -627,12 +627,11 @@ def __init__(self, *args, **kwargs):
627627
self.teardown_request(_teardown_request)
628628

629629

630-
def _cli_command_wrapper(wrapped, instance, args, kwargs, tracer):
631-
span_name = "cli span name"
632-
span_attributes = {}
630+
def _cli_invoke_wrapper(wrapped, instance, args, kwargs, tracer):
631+
callback = args[0] if args else kwargs.get("__callback")
633632

634-
print("AAAAAAAAAAA", args, kwargs)
635-
print("AAAAAAAAAAA", wrapped, instance)
633+
span_name = callback.__name__
634+
span_attributes = {}
636635

637636
with tracer.start_as_current_span(
638637
name=span_name,
@@ -696,16 +695,20 @@ def _instrument(self, **kwargs):
696695
tracer_provider,
697696
schema_url=_get_schema_url(sem_conv_opt_in_mode),
698697
)
698+
import click
699+
699700
wrap_function_wrapper(
700-
"flask.cli",
701-
"AppGroup.command",
702-
partial(_cli_command_wrapper, tracer=tracer),
701+
click.core.Context,
702+
"invoke",
703+
partial(_cli_invoke_wrapper, tracer=tracer),
703704
)
704705

705706
def _uninstrument(self, **kwargs):
706707
flask.Flask = self._original_flask
707708

708-
unwrap(flask.cli.AppGroup, "command")
709+
import click
710+
711+
unwrap(click.core.Context, "invoke")
709712

710713
# pylint: disable=too-many-locals
711714
@staticmethod

instrumentation/opentelemetry-instrumentation-flask/tests/test_automatic.py

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from opentelemetry import trace as trace_api
2121
from opentelemetry.instrumentation.flask import FlaskInstrumentor
2222
from opentelemetry.test.wsgitestutil import WsgiTestBase
23+
from opentelemetry.trace.status import StatusCode
2324

2425
# pylint: disable=import-error
2526
from .base_test import InstrumentationTest
@@ -96,26 +97,58 @@ def test_no_op_tracer_provider(self):
9697
span_list = self.memory_exporter.get_finished_spans()
9798
self.assertEqual(len(span_list), 0)
9899

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()
99139

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")
111142

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()
113148

149+
runner = self.app.test_cli_runner()
150+
result = runner.invoke(args=["command-raises"])
151+
(span,) = self.memory_exporter.get_finished_spans()
114152

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

Comments
 (0)