Skip to content

Commit 63bc3d6

Browse files
committed
Fix unit tests
Change-Id: I7816571f221dbf8f20579236360ce38ef51aac1a Co-developed-by: Cursor <[email protected]>
1 parent c7f60fa commit 63bc3d6

File tree

13 files changed

+299
-300
lines changed

13 files changed

+299
-300
lines changed

instrumentation-loongsuite/loongsuite-instrumentation-dashscope/examples/basic_example.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import os
1414

15+
from dashscope import Generation, TextEmbedding
1516
from loongsuite.instrumentation.dashscope import DashScopeInstrumentor
1617

1718
from opentelemetry import trace
@@ -45,8 +46,6 @@ def main():
4546
print("Example 1: Text Generation (non-streaming)")
4647
print("=" * 60)
4748

48-
from dashscope import Generation
49-
5049
response = Generation.call(
5150
model="qwen-turbo",
5251
prompt="Hello! Please introduce yourself in one sentence.",
@@ -76,8 +75,6 @@ def main():
7675
print("Example 3: Text Embedding")
7776
print("=" * 60)
7877

79-
from dashscope import TextEmbedding
80-
8178
response = TextEmbedding.call(
8279
model="text-embedding-v1", input="Hello, world!"
8380
)

instrumentation-loongsuite/loongsuite-instrumentation-dashscope/src/loongsuite/instrumentation/dashscope/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,10 @@ def _uninstrument(self, **kwargs):
244244
**kwargs: Optional configuration parameters.
245245
"""
246246
# pylint: disable=import-outside-toplevel
247-
import dashscope.aigc.generation
248-
import dashscope.aigc.image_synthesis
249-
import dashscope.embeddings.text_embedding
250-
import dashscope.rerank.text_rerank
247+
import dashscope.aigc.generation # noqa: PLC0415
248+
import dashscope.aigc.image_synthesis # noqa: PLC0415
249+
import dashscope.embeddings.text_embedding # noqa: PLC0415
250+
import dashscope.rerank.text_rerank # noqa: PLC0415
251251

252252
unwrap(dashscope.aigc.generation.Generation, "call")
253253
unwrap(dashscope.aigc.generation.AioGeneration, "call")

instrumentation-loongsuite/loongsuite-instrumentation-dashscope/src/loongsuite/instrumentation/dashscope/patch.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@
1818
import logging
1919

2020
from opentelemetry import context
21+
from opentelemetry.util.genai._extended_semconv.gen_ai_extended_attributes import (
22+
GenAiExtendedProviderNameValues as GenAI,
23+
)
2124
from opentelemetry.util.genai.extended_types import (
2225
EmbeddingInvocation,
2326
RerankInvocation,
2427
)
25-
from opentelemetry.util.genai._extended_semconv.gen_ai_extended_attributes import (
26-
GenAiExtendedProviderNameValues as GenAI
27-
)
2828
from opentelemetry.util.genai.types import Error
2929

3030
from .utils import (
31+
_SKIP_INSTRUMENTATION_KEY,
3132
_create_accumulated_response,
3233
_create_invocation_from_generation,
3334
_create_invocation_from_image_synthesis,
3435
_extract_task_id,
3536
_get_parameter,
36-
_SKIP_INSTRUMENTATION_KEY,
3737
_update_invocation_from_image_synthesis_async_response,
3838
_update_invocation_from_image_synthesis_response,
3939
_update_invocation_from_response,
@@ -108,7 +108,9 @@ def wrap_generation_call(wrapped, instance, args, kwargs, handler=None):
108108
return wrapped(*args, **kwargs)
109109

110110

111-
async def wrap_aio_generation_call(wrapped, instance, args, kwargs, handler=None):
111+
async def wrap_aio_generation_call(
112+
wrapped, instance, args, kwargs, handler=None
113+
):
112114
"""Wrapper for AioGeneration.call (async).
113115
114116
Uses TelemetryHandler from opentelemetry-util-genai to manage span lifecycle.
@@ -123,9 +125,7 @@ async def wrap_aio_generation_call(wrapped, instance, args, kwargs, handler=None
123125
# Extract model from kwargs
124126
model = kwargs.get("model")
125127
if not model:
126-
logger.warning(
127-
"Model not found in kwargs, skipping instrumentation"
128-
)
128+
logger.warning("Model not found in kwargs, skipping instrumentation")
129129
return await wrapped(*args, **kwargs)
130130

131131
if handler is None:
@@ -270,7 +270,9 @@ def wrap_text_rerank_call(wrapped, instance, args, kwargs, handler=None):
270270

271271
try:
272272
# Create rerank invocation object
273-
invocation = RerankInvocation(provider=GenAI.DASHSCOPE.value, request_model=model)
273+
invocation = RerankInvocation(
274+
provider=GenAI.DASHSCOPE.value, request_model=model
275+
)
274276
invocation.provider = "dashscope"
275277

276278
# Start rerank invocation (creates span)
@@ -437,7 +439,9 @@ def wrap_image_synthesis_call(wrapped, instance, args, kwargs, handler=None):
437439
result = wrapped(*args, **kwargs)
438440

439441
# Update invocation with response data
440-
_update_invocation_from_image_synthesis_response(invocation, result)
442+
_update_invocation_from_image_synthesis_response(
443+
invocation, result
444+
)
441445
handler.stop_llm(invocation)
442446

443447
return result
@@ -584,7 +588,9 @@ def wrap_image_synthesis_wait(wrapped, instance, args, kwargs, handler=None):
584588
result = wrapped(*args, **kwargs)
585589

586590
# Update invocation with response data
587-
_update_invocation_from_image_synthesis_response(invocation, result)
591+
_update_invocation_from_image_synthesis_response(
592+
invocation, result
593+
)
588594
handler.stop_llm(invocation)
589595

590596
return result

instrumentation-loongsuite/loongsuite-instrumentation-dashscope/src/loongsuite/instrumentation/dashscope/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,9 @@ def _update_invocation_from_image_synthesis_response(
855855
if image_urls:
856856
# Store first image URL as attribute (or all if needed)
857857
invocation.attributes["dashscope.image.url"] = (
858-
image_urls[0] if len(image_urls) == 1 else str(image_urls)
858+
image_urls[0]
859+
if len(image_urls) == 1
860+
else str(image_urls)
859861
)
860862
except (KeyError, AttributeError):
861863
pass

instrumentation-loongsuite/loongsuite-instrumentation-dashscope/tests/cassettes/test_image_synthesis_async_call_and_wait_separate_spans.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
interactions:
2-
body: |-
2+
- request:
3+
body: |-
34
{
45
"model": "wanx-v1",
56
"parameters": {},

instrumentation-loongsuite/loongsuite-instrumentation-dashscope/tests/cassettes/test_image_synthesis_call_with_parameters.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
interactions:
2-
body: |-
2+
- request:
3+
body: |-
34
{
45
"model": "wanx-v1",
56
"parameters": {

instrumentation-loongsuite/loongsuite-instrumentation-dashscope/tests/cassettes/test_image_synthesis_wait_basic.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
interactions:
2-
body: |-
2+
- request:
3+
body: |-
34
{
45
"model": "wanx-v1",
56
"parameters": {},

instrumentation-loongsuite/loongsuite-instrumentation-dashscope/tests/test_embedding.py

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Optional
44

55
import pytest
6+
from dashscope import TextEmbedding
67

78
from opentelemetry.semconv._incubating.attributes import (
89
gen_ai_attributes as GenAIAttributes,
@@ -37,31 +38,33 @@ def _assert_embedding_span_attributes(
3738
assert span.name == f"embeddings {request_model}"
3839

3940
# Required attributes
40-
assert (
41-
GenAIAttributes.GEN_AI_OPERATION_NAME in span.attributes
42-
), f"Missing {GenAIAttributes.GEN_AI_OPERATION_NAME}"
41+
assert GenAIAttributes.GEN_AI_OPERATION_NAME in span.attributes, (
42+
f"Missing {GenAIAttributes.GEN_AI_OPERATION_NAME}"
43+
)
4344
assert (
4445
span.attributes[GenAIAttributes.GEN_AI_OPERATION_NAME] == "embeddings"
45-
), f"Expected 'embeddings', got {span.attributes.get(GenAIAttributes.GEN_AI_OPERATION_NAME)}"
46+
), (
47+
f"Expected 'embeddings', got {span.attributes.get(GenAIAttributes.GEN_AI_OPERATION_NAME)}"
48+
)
4649

47-
assert (
48-
GenAIAttributes.GEN_AI_PROVIDER_NAME in span.attributes
49-
), f"Missing {GenAIAttributes.GEN_AI_PROVIDER_NAME}"
50+
assert GenAIAttributes.GEN_AI_PROVIDER_NAME in span.attributes, (
51+
f"Missing {GenAIAttributes.GEN_AI_PROVIDER_NAME}"
52+
)
5053
assert span.attributes[GenAIAttributes.GEN_AI_PROVIDER_NAME] == "dashscope"
5154

5255
# Conditionally required attributes
53-
assert (
54-
GenAIAttributes.GEN_AI_REQUEST_MODEL in span.attributes
55-
), f"Missing {GenAIAttributes.GEN_AI_REQUEST_MODEL}"
56+
assert GenAIAttributes.GEN_AI_REQUEST_MODEL in span.attributes, (
57+
f"Missing {GenAIAttributes.GEN_AI_REQUEST_MODEL}"
58+
)
5659
assert (
5760
span.attributes[GenAIAttributes.GEN_AI_REQUEST_MODEL] == request_model
5861
)
5962

6063
# Recommended attributes - check if available
6164
if input_tokens is not None:
62-
assert (
63-
GenAIAttributes.GEN_AI_USAGE_INPUT_TOKENS in span.attributes
64-
), f"Missing {GenAIAttributes.GEN_AI_USAGE_INPUT_TOKENS}"
65+
assert GenAIAttributes.GEN_AI_USAGE_INPUT_TOKENS in span.attributes, (
66+
f"Missing {GenAIAttributes.GEN_AI_USAGE_INPUT_TOKENS}"
67+
)
6568
assert (
6669
span.attributes[GenAIAttributes.GEN_AI_USAGE_INPUT_TOKENS]
6770
== input_tokens
@@ -91,38 +94,37 @@ def _assert_embedding_span_attributes(
9194
# Dimension count should only be set if it was specified in the request
9295
if dimension_count is not None:
9396
# If dimension_count was explicitly provided in the request, it must be set in span
94-
assert (
95-
"gen_ai.embeddings.dimension.count" in span.attributes
96-
), "Missing gen_ai.embeddings.dimension.count"
97+
assert "gen_ai.embeddings.dimension.count" in span.attributes, (
98+
"Missing gen_ai.embeddings.dimension.count"
99+
)
97100
assert (
98101
span.attributes["gen_ai.embeddings.dimension.count"]
99102
== dimension_count
100103
)
101104
else:
102105
# If dimension_count was not provided in the request, it should not be set in span
103-
assert (
104-
"gen_ai.embeddings.dimension.count" not in span.attributes
105-
), "gen_ai.embeddings.dimension.count should not be set when not specified in request"
106+
assert "gen_ai.embeddings.dimension.count" not in span.attributes, (
107+
"gen_ai.embeddings.dimension.count should not be set when not specified in request"
108+
)
106109

107110
if server_address is not None:
108-
assert (
109-
ServerAttributes.SERVER_ADDRESS in span.attributes
110-
), f"Missing {ServerAttributes.SERVER_ADDRESS}"
111+
assert ServerAttributes.SERVER_ADDRESS in span.attributes, (
112+
f"Missing {ServerAttributes.SERVER_ADDRESS}"
113+
)
111114
assert (
112115
span.attributes[ServerAttributes.SERVER_ADDRESS] == server_address
113116
)
114117

115118
if server_port is not None:
116-
assert (
117-
ServerAttributes.SERVER_PORT in span.attributes
118-
), f"Missing {ServerAttributes.SERVER_PORT}"
119+
assert ServerAttributes.SERVER_PORT in span.attributes, (
120+
f"Missing {ServerAttributes.SERVER_PORT}"
121+
)
119122
assert span.attributes[ServerAttributes.SERVER_PORT] == server_port
120123

121124

122125
@pytest.mark.vcr()
123126
def test_text_embedding_basic(instrument, span_exporter):
124127
"""Test basic text embedding call."""
125-
from dashscope import TextEmbedding
126128

127129
response = TextEmbedding.call(
128130
model="text-embedding-v1", input="Hello, world!"
@@ -160,7 +162,6 @@ def test_text_embedding_basic(instrument, span_exporter):
160162
@pytest.mark.vcr()
161163
def test_text_embedding_batch(instrument, span_exporter):
162164
"""Test text embedding with batch input."""
163-
from dashscope import TextEmbedding
164165

165166
response = TextEmbedding.call(
166167
model="text-embedding-v1", input=["Hello", "World"]
@@ -198,7 +199,6 @@ def test_text_embedding_batch(instrument, span_exporter):
198199
@pytest.mark.vcr()
199200
def test_text_embedding_with_text_type(instrument, span_exporter):
200201
"""Test text embedding with text_type parameter."""
201-
from dashscope import TextEmbedding
202202

203203
response = TextEmbedding.call(
204204
model="text-embedding-v1",
@@ -238,7 +238,6 @@ def test_text_embedding_with_text_type(instrument, span_exporter):
238238
@pytest.mark.vcr()
239239
def test_text_embedding_with_dimension(instrument, span_exporter):
240240
"""Test text embedding with dimension parameter."""
241-
from dashscope import TextEmbedding
242241

243242
response = TextEmbedding.call(
244243
model="text-embedding-v1",

0 commit comments

Comments
 (0)