mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-07-29 21:23:55 +08:00
65 lines
2.4 KiB
Python
65 lines
2.4 KiB
Python
# Copyright The OpenTelemetry Authors
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
import fastapi
|
|
from starlette.testclient import TestClient
|
|
|
|
import opentelemetry.instrumentation.fastapi as otel_fastapi
|
|
from opentelemetry import trace
|
|
from opentelemetry.test.test_base import TestBase
|
|
|
|
|
|
class TestWrappedApplication(TestBase):
|
|
def setUp(self):
|
|
super().setUp()
|
|
|
|
self.app = fastapi.FastAPI()
|
|
|
|
@self.app.get("/foobar")
|
|
async def _():
|
|
return {"message": "hello world"}
|
|
|
|
otel_fastapi.FastAPIInstrumentor().instrument_app(self.app)
|
|
self.client = TestClient(self.app)
|
|
self.tracer = self.tracer_provider.get_tracer(__name__)
|
|
|
|
def tearDown(self) -> None:
|
|
super().tearDown()
|
|
with self.disable_logging():
|
|
otel_fastapi.FastAPIInstrumentor().uninstrument_app(self.app)
|
|
|
|
def test_mark_span_internal_in_presence_of_span_from_other_framework(self):
|
|
with self.tracer.start_as_current_span(
|
|
"test", kind=trace.SpanKind.SERVER
|
|
) as parent_span:
|
|
resp = self.client.get("/foobar")
|
|
self.assertEqual(200, resp.status_code)
|
|
|
|
span_list = self.memory_exporter.get_finished_spans()
|
|
for span in span_list:
|
|
print(str(span.__class__) + ": " + str(span.__dict__))
|
|
|
|
# there should be 4 spans - single SERVER "test" and three INTERNAL "FastAPI"
|
|
self.assertEqual(trace.SpanKind.INTERNAL, span_list[0].kind)
|
|
self.assertEqual(trace.SpanKind.INTERNAL, span_list[1].kind)
|
|
# main INTERNAL span - child of test
|
|
self.assertEqual(trace.SpanKind.INTERNAL, span_list[2].kind)
|
|
self.assertEqual(
|
|
parent_span.context.span_id, span_list[2].parent.span_id
|
|
)
|
|
# SERVER "test"
|
|
self.assertEqual(trace.SpanKind.SERVER, span_list[3].kind)
|
|
self.assertEqual(
|
|
parent_span.context.span_id, span_list[3].context.span_id
|
|
)
|