Skip to content

Commit 850a5b9

Browse files
committed
feedback
Change-Id: I391b43bb0fb3b77ee7ec45910407116cf3f351e0 Co-developed-by: Cursor <[email protected]>
1 parent 9f7f7dd commit 850a5b9

File tree

7 files changed

+44
-70
lines changed

7 files changed

+44
-70
lines changed

util/opentelemetry-util-genai/README-loongsuite.rst

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,6 @@ Token 使用:
356356
- ``gen_ai.memory.input.messages``: 原始记忆内容
357357
- ``gen_ai.memory.output.messages``: 查询结果
358358

359-
操作结果:
360-
- ``gen_ai.memory.result_count``: 操作结果中的记忆记录数量
361-
362359
服务器信息:
363360
- ``server.address``: 服务器地址
364361
- ``server.port``: 服务器端口
@@ -398,7 +395,6 @@ Token 使用:
398395
{"memory_id": "mem1", "content": "用户喜欢苹果", "score": 0.95},
399396
{"memory_id": "mem2", "content": "用户喜欢橙子", "score": 0.88}
400397
]
401-
invocation.result_count = 2
402398

403399
# 更新记忆
404400
invocation = MemoryInvocation(operation="update")
@@ -600,7 +596,6 @@ Token 使用:
600596
memory_inv.input_messages = "用户偏好:喜欢轻薄型笔记本电脑"
601597
602598
# 执行添加记忆...
603-
memory_inv.result_count = 1
604599

605600
# 搜索记忆
606601
search_inv = MemoryInvocation(operation="search")
@@ -614,7 +609,6 @@ Token 使用:
614609
search_inv.output_messages = [
615610
{"memory_id": "mem1", "content": "用户偏好:喜欢轻薄型笔记本电脑", "score": 0.92}
616611
]
617-
search_inv.result_count = 1
618612

619613

620614
设计文档

util/opentelemetry-util-genai/src/opentelemetry/util/genai/_extended_memory/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,3 @@
3030
"_apply_memory_finish_attributes",
3131
"_maybe_emit_memory_event",
3232
]
33-

util/opentelemetry-util-genai/src/opentelemetry/util/genai/_extended_memory/memory_types.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from opentelemetry.util.genai.types import ContextToken, Span
2121

22+
2223
def _new_str_any_dict() -> Dict[str, Any]:
2324
"""Helper function to create a new empty dict for default factory."""
2425
return {}
@@ -53,8 +54,6 @@ class MemoryInvocation:
5354
# Memory content (optional, controlled by content capturing mode)
5455
input_messages: Any = None # Original memory content
5556
output_messages: Any = None # Query results
56-
# Memory operation result
57-
result_count: int | None = None
5857
# Server information
5958
server_address: str | None = None
6059
server_port: int | None = None

util/opentelemetry-util-genai/src/opentelemetry/util/genai/_extended_memory/memory_utils.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,17 @@
2626
from opentelemetry.semconv._incubating.attributes import (
2727
gen_ai_attributes as GenAI,
2828
)
29-
from opentelemetry.semconv.attributes import error_attributes as ErrorAttributes
30-
from opentelemetry.semconv.attributes import server_attributes as ServerAttributes
29+
from opentelemetry.semconv.attributes import (
30+
error_attributes as ErrorAttributes,
31+
)
32+
from opentelemetry.semconv.attributes import (
33+
server_attributes as ServerAttributes,
34+
)
3135
from opentelemetry.trace import Span
3236
from opentelemetry.trace.propagation import set_span_in_context
37+
from opentelemetry.util.genai._extended_memory.memory_types import (
38+
MemoryInvocation,
39+
)
3340
from opentelemetry.util.genai._extended_semconv.gen_ai_memory_attributes import (
3441
GEN_AI_MEMORY_AGENT_ID,
3542
GEN_AI_MEMORY_APP_ID,
@@ -42,7 +49,6 @@
4249
GEN_AI_MEMORY_PAGE,
4350
GEN_AI_MEMORY_PAGE_SIZE,
4451
GEN_AI_MEMORY_RERANK,
45-
GEN_AI_MEMORY_RESULT_COUNT,
4652
GEN_AI_MEMORY_RUN_ID,
4753
GEN_AI_MEMORY_THRESHOLD,
4854
GEN_AI_MEMORY_TOP_K,
@@ -56,7 +62,7 @@
5662
is_experimental_mode,
5763
should_emit_event,
5864
)
59-
from opentelemetry.util.genai._extended_memory.memory_types import MemoryInvocation
65+
6066

6167
def _get_memory_common_attributes(
6268
invocation: MemoryInvocation,
@@ -156,15 +162,19 @@ def _get_memory_content_attributes(
156162

157163
if invocation.input_messages is not None:
158164
if isinstance(invocation.input_messages, str):
159-
attributes[GEN_AI_MEMORY_INPUT_MESSAGES] = invocation.input_messages
165+
attributes[GEN_AI_MEMORY_INPUT_MESSAGES] = (
166+
invocation.input_messages
167+
)
160168
else:
161169
attributes[GEN_AI_MEMORY_INPUT_MESSAGES] = gen_ai_json_dumps(
162170
invocation.input_messages
163171
)
164172

165173
if invocation.output_messages is not None:
166174
if isinstance(invocation.output_messages, str):
167-
attributes[GEN_AI_MEMORY_OUTPUT_MESSAGES] = invocation.output_messages
175+
attributes[GEN_AI_MEMORY_OUTPUT_MESSAGES] = (
176+
invocation.output_messages
177+
)
168178
else:
169179
attributes[GEN_AI_MEMORY_OUTPUT_MESSAGES] = gen_ai_json_dumps(
170180
invocation.output_messages
@@ -189,16 +199,16 @@ def _apply_memory_finish_attributes(
189199
attributes.update(_get_memory_parameter_attributes(invocation))
190200

191201
# Recommended attributes
192-
if invocation.result_count is not None:
193-
attributes[GEN_AI_MEMORY_RESULT_COUNT] = invocation.result_count
194202
if invocation.server_address is not None:
195203
attributes[ServerAttributes.SERVER_ADDRESS] = invocation.server_address
196204
if invocation.server_port is not None:
197205
attributes[ServerAttributes.SERVER_PORT] = invocation.server_port
198206

199207
# Content attributes (controlled by content capturing mode)
200208
# For spans, only capture if SPAN_ONLY or SPAN_AND_EVENT
201-
attributes.update(_get_memory_content_attributes(invocation, for_span=True))
209+
attributes.update(
210+
_get_memory_content_attributes(invocation, for_span=True)
211+
)
202212

203213
# Custom attributes
204214
attributes.update(invocation.attributes)
@@ -228,7 +238,9 @@ def _maybe_emit_memory_event(
228238
attributes.update(_get_memory_parameter_attributes(invocation))
229239

230240
# Content attributes for events (controlled by content capturing mode)
231-
attributes.update(_get_memory_content_attributes(invocation, for_span=False))
241+
attributes.update(
242+
_get_memory_content_attributes(invocation, for_span=False)
243+
)
232244

233245
# Add error.type if operation ended in error
234246
if error is not None:

util/opentelemetry-util-genai/src/opentelemetry/util/genai/_extended_semconv/gen_ai_memory_attributes.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,6 @@
100100
The query results returned from the memory operation.
101101
"""
102102

103-
# Memory operation result
104-
GEN_AI_MEMORY_RESULT_COUNT: Final = "gen_ai.memory.result_count"
105-
"""
106-
The number of memory records in the operation result.
107-
"""
108-
109103

110104
class GenAiMemoryOperationValues(Enum):
111105
"""Memory operation type values."""
@@ -139,4 +133,3 @@ class GenAiMemoryOperationValues(Enum):
139133

140134
DELETE_ALL = "delete_all"
141135
"""Delete all memory records."""
142-

util/opentelemetry-util-genai/tests/test_extended_handler.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,10 @@
2222
OTEL_SEMCONV_STABILITY_OPT_IN,
2323
_OpenTelemetrySemanticConventionStability,
2424
)
25-
26-
# Backward compatibility for InMemoryLogExporter -> InMemoryLogRecordExporter rename
27-
# Changed in [email protected]
28-
try:
29-
from opentelemetry.sdk._logs.export import ( # pylint: disable=no-name-in-module
30-
InMemoryLogRecordExporter,
31-
SimpleLogRecordProcessor,
32-
)
33-
except ImportError:
34-
# Fallback to old name for compatibility with older SDK versions
35-
from opentelemetry.sdk._logs.export import (
36-
InMemoryLogExporter as InMemoryLogRecordExporter,
37-
)
38-
from opentelemetry.sdk._logs.export import (
39-
SimpleLogRecordProcessor,
40-
)
41-
25+
from opentelemetry.sdk._logs.export import ( # pylint: disable=no-name-in-module
26+
InMemoryLogRecordExporter,
27+
SimpleLogRecordProcessor,
28+
)
4229
from opentelemetry.sdk._logs import LoggerProvider
4330
from opentelemetry.sdk.trace import ReadableSpan, TracerProvider
4431
from opentelemetry.sdk.trace.export import SimpleSpanProcessor

util/opentelemetry-util-genai/tests/test_extended_memory.py

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
GEN_AI_MEMORY_PAGE,
6969
GEN_AI_MEMORY_PAGE_SIZE,
7070
GEN_AI_MEMORY_RERANK,
71-
GEN_AI_MEMORY_RESULT_COUNT,
7271
GEN_AI_MEMORY_RUN_ID,
7372
GEN_AI_MEMORY_THRESHOLD,
7473
GEN_AI_MEMORY_TOP_K,
@@ -83,9 +82,7 @@
8382
)
8483

8584

86-
def patch_env_vars(
87-
stability_mode, content_capturing=None, emit_event=None
88-
):
85+
def patch_env_vars(stability_mode, content_capturing=None, emit_event=None):
8986
def decorator(test_case):
9087
env_vars = {
9188
OTEL_SEMCONV_STABILITY_OPT_IN: stability_mode,
@@ -202,7 +199,6 @@ def test_memory_search_with_parameters(self):
202199
invocation.threshold = 0.7
203200
invocation.rerank = True
204201
invocation.top_k = 5
205-
invocation.result_count = 3
206202

207203
span = _get_single_span(self.span_exporter)
208204
self.assertEqual(span.name, "memory_operation search")
@@ -217,7 +213,6 @@ def test_memory_search_with_parameters(self):
217213
GEN_AI_MEMORY_THRESHOLD: 0.7,
218214
GEN_AI_MEMORY_RERANK: True,
219215
GEN_AI_MEMORY_TOP_K: 5,
220-
GEN_AI_MEMORY_RESULT_COUNT: 3,
221216
},
222217
)
223218

@@ -338,7 +333,6 @@ def test_memory_manual_start_and_stop(self):
338333

339334
self.telemetry_handler.start_memory(invocation)
340335
assert invocation.span is not None
341-
invocation.result_count = 5
342336
self.telemetry_handler.stop_memory(invocation)
343337

344338
span = _get_single_span(self.span_exporter)
@@ -350,27 +344,26 @@ def test_memory_manual_start_and_stop(self):
350344
GEN_AI_MEMORY_OPERATION: "search",
351345
GEN_AI_MEMORY_USER_ID: "user_123",
352346
GEN_AI_MEMORY_LIMIT: 20,
353-
GEN_AI_MEMORY_RESULT_COUNT: 5,
354347
},
355348
)
356349

357350
def test_memory_error_handling(self):
358-
class MemoryError(RuntimeError):
351+
class MemoryOperationError(RuntimeError):
359352
pass
360353

361-
with self.assertRaises(MemoryError):
354+
with self.assertRaises(MemoryOperationError):
362355
invocation = MemoryInvocation(operation="add")
363356
with self.telemetry_handler.memory(invocation) as invocation:
364357
invocation.user_id = "user_123"
365-
raise MemoryError("Memory operation failed")
358+
raise MemoryOperationError("Memory operation failed")
366359

367360
span = _get_single_span(self.span_exporter)
368361
self.assertEqual(span.status.status_code, StatusCode.ERROR)
369362
span_attrs = _get_span_attributes(span)
370363
_assert_span_attributes(
371364
span_attrs,
372365
{
373-
ErrorAttributes.ERROR_TYPE: MemoryError.__qualname__,
366+
ErrorAttributes.ERROR_TYPE: MemoryOperationError.__qualname__,
374367
},
375368
)
376369

@@ -385,7 +378,6 @@ def test_memory_with_content_capturing(self):
385378
invocation.user_id = "user_123"
386379
invocation.input_messages = "What does the user like?"
387380
invocation.output_messages = "The user likes apples"
388-
invocation.result_count = 1
389381

390382
span = _get_single_span(self.span_exporter)
391383
span_attrs = _get_span_attributes(span)
@@ -394,7 +386,8 @@ def test_memory_with_content_capturing(self):
394386
self.assertIn(GEN_AI_MEMORY_INPUT_MESSAGES, span_attrs)
395387
self.assertIn(GEN_AI_MEMORY_OUTPUT_MESSAGES, span_attrs)
396388
self.assertEqual(
397-
span_attrs[GEN_AI_MEMORY_INPUT_MESSAGES], "What does the user like?"
389+
span_attrs[GEN_AI_MEMORY_INPUT_MESSAGES],
390+
"What does the user like?",
398391
)
399392
self.assertEqual(
400393
span_attrs[GEN_AI_MEMORY_OUTPUT_MESSAGES], "The user likes apples"
@@ -428,7 +421,6 @@ def test_memory_emits_event(self):
428421
invocation.agent_id = "agent_456"
429422
invocation.input_messages = "What does the user like?"
430423
invocation.output_messages = "The user likes apples"
431-
invocation.result_count = 1
432424

433425
# Check that event was emitted
434426
logs = self.log_exporter.get_finished_logs()
@@ -444,7 +436,9 @@ def test_memory_emits_event(self):
444436
# Verify event attributes
445437
attrs = log_record.attributes
446438
self.assertIsNotNone(attrs)
447-
self.assertEqual(attrs[GenAI.GEN_AI_OPERATION_NAME], "memory_operation")
439+
self.assertEqual(
440+
attrs[GenAI.GEN_AI_OPERATION_NAME], "memory_operation"
441+
)
448442
self.assertEqual(attrs[GEN_AI_MEMORY_OPERATION], "search")
449443
self.assertEqual(attrs[GEN_AI_MEMORY_USER_ID], "user_123")
450444
self.assertEqual(attrs[GEN_AI_MEMORY_AGENT_ID], "agent_456")
@@ -462,7 +456,6 @@ def test_memory_emits_event_and_span(self):
462456
with self.telemetry_handler.memory(invocation) as invocation:
463457
invocation.user_id = "user_123"
464458
invocation.input_messages = "User likes apples"
465-
invocation.result_count = 1
466459

467460
# Check span was created
468461
span = _get_single_span(self.span_exporter)
@@ -487,15 +480,15 @@ def test_memory_emits_event_and_span(self):
487480
def test_memory_emits_event_with_error(self):
488481
"""Test that memory operation emits event with error when operation fails."""
489482

490-
class MemoryError(RuntimeError):
483+
class MemoryOperationError(RuntimeError):
491484
pass
492485

493-
with self.assertRaises(MemoryError):
486+
with self.assertRaises(MemoryOperationError):
494487
invocation = MemoryInvocation(operation="add")
495488
with self.telemetry_handler.memory(invocation) as invocation:
496489
invocation.user_id = "user_123"
497490
invocation.input_messages = "Test memory"
498-
raise MemoryError("Memory operation failed")
491+
raise MemoryOperationError("Memory operation failed")
499492

500493
# Check event was emitted
501494
logs = self.log_exporter.get_finished_logs()
@@ -506,9 +499,11 @@ class MemoryError(RuntimeError):
506499
# Verify error attribute is present
507500
self.assertEqual(
508501
attrs[ErrorAttributes.ERROR_TYPE],
509-
MemoryError.__qualname__,
502+
MemoryOperationError.__qualname__,
503+
)
504+
self.assertEqual(
505+
attrs[GenAI.GEN_AI_OPERATION_NAME], "memory_operation"
510506
)
511-
self.assertEqual(attrs[GenAI.GEN_AI_OPERATION_NAME], "memory_operation")
512507

513508
def test_memory_does_not_emit_event_when_disabled(self):
514509
"""Test that memory operation does not emit event when emit_event is disabled."""
@@ -526,7 +521,6 @@ def test_memory_batch_update_operation(self):
526521
with self.telemetry_handler.memory(invocation) as invocation:
527522
invocation.user_id = "user_123"
528523
invocation.agent_id = "agent_456"
529-
invocation.result_count = 5
530524

531525
span = _get_single_span(self.span_exporter)
532526
span_attrs = _get_span_attributes(span)
@@ -536,7 +530,6 @@ def test_memory_batch_update_operation(self):
536530
GEN_AI_MEMORY_OPERATION: "batch_update",
537531
GEN_AI_MEMORY_USER_ID: "user_123",
538532
GEN_AI_MEMORY_AGENT_ID: "agent_456",
539-
GEN_AI_MEMORY_RESULT_COUNT: 5,
540533
},
541534
)
542535

@@ -561,7 +554,6 @@ def test_memory_batch_delete_operation(self):
561554
invocation = MemoryInvocation(operation="batch_delete")
562555
with self.telemetry_handler.memory(invocation) as invocation:
563556
invocation.user_id = "user_123"
564-
invocation.result_count = 3
565557

566558
span = _get_single_span(self.span_exporter)
567559
span_attrs = _get_span_attributes(span)
@@ -570,7 +562,6 @@ def test_memory_batch_delete_operation(self):
570562
{
571563
GEN_AI_MEMORY_OPERATION: "batch_delete",
572564
GEN_AI_MEMORY_USER_ID: "user_123",
573-
GEN_AI_MEMORY_RESULT_COUNT: 3,
574565
},
575566
)
576567

@@ -618,4 +609,3 @@ def test_memory_with_json_input_output(self):
618609
# Should be JSON strings
619610
self.assertIsInstance(span_attrs[GEN_AI_MEMORY_INPUT_MESSAGES], str)
620611
self.assertIsInstance(span_attrs[GEN_AI_MEMORY_OUTPUT_MESSAGES], str)
621-

0 commit comments

Comments
 (0)