fix: add missing metrics back and enhance typing - #40935
Conversation
Pyrefly Diffbase → PR--- /tmp/pyrefly_base.txt 2026-08-20 03:27:21.002725185 +0000
+++ /tmp/pyrefly_pr.txt 2026-08-20 03:27:07.538582645 +0000
@@ -1834,8 +1834,6 @@
--> tests/unit_tests/configs/test_dify_config.py:38:32
ERROR Unpacked keyword argument `object` is not assignable to parameter `ENTERPRISE_INCLUDE_CONTENT` with type `Decimal | bool | float | int | str` in function `_IsolatedDifyConfig.__init__` [bad-argument-type]
--> tests/unit_tests/configs/test_dify_config.py:38:32
-ERROR Unpacked keyword argument `object` is not assignable to parameter `ENTERPRISE_SERVICE_NAME` with type `bytearray | bytes | str` in function `_IsolatedDifyConfig.__init__` [bad-argument-type]
- --> tests/unit_tests/configs/test_dify_config.py:38:32
ERROR Unpacked keyword argument `object` is not assignable to parameter `ENTERPRISE_OTEL_SAMPLING_RATE` with type `Decimal | bool | bytes | float | int | str` in function `_IsolatedDifyConfig.__init__` [bad-argument-type]
--> tests/unit_tests/configs/test_dify_config.py:38:32
ERROR Unpacked keyword argument `object` is not assignable to parameter `WEBAPP_PUBLIC_ACCESS_ENABLED` with type `Decimal | bool | float | int | str` in function `_IsolatedDifyConfig.__init__` [bad-argument-type]
@@ -4603,15 +4601,33 @@
ERROR `SimpleNamespace` is not assignable to attribute `_application_generate_entity` with type `AdvancedChatAppGenerateEntity` [bad-assignment]
--> tests/unit_tests/core/app/apps/advanced_chat/test_generate_task_pipeline.py:109:45
ERROR Argument `SimpleNamespace` is not assignable to parameter `application_generate_entity` with type `AdvancedChatAppGenerateEntity` in function `core.app.apps.advanced_chat.generate_task_pipeline.AdvancedChatAppGenerateTaskPipeline.__init__` [bad-argument-type]
- --> tests/unit_tests/core/app/apps/advanced_chat/test_generate_task_pipeline.py:176:37
+ --> tests/unit_tests/core/app/apps/advanced_chat/test_generate_task_pipeline.py:178:37
+ERROR Argument `SimpleNamespace` is not assignable to parameter `queue_manager` with type `AppQueueManager` in function `core.app.apps.advanced_chat.generate_task_pipeline.AdvancedChatAppGenerateTaskPipeline.__init__` [bad-argument-type]
+ --> tests/unit_tests/core/app/apps/advanced_chat/test_generate_task_pipeline.py:180:23
+ERROR Argument `SimpleNamespace` is not assignable to parameter `draft_var_saver_factory` with type `DraftVariableSaverFactory` in function `core.app.apps.advanced_chat.generate_task_pipeline.AdvancedChatAppGenerateTaskPipeline.__init__` [bad-argument-type]
+ --> tests/unit_tests/core/app/apps/advanced_chat/test_generate_task_pipeline.py:186:33
+ERROR Argument `SimpleNamespace` is not assignable to parameter `application_generate_entity` with type `AdvancedChatAppGenerateEntity` in function `core.app.apps.advanced_chat.generate_task_pipeline.AdvancedChatAppGenerateTaskPipeline.__init__` [bad-argument-type]
+ --> tests/unit_tests/core/app/apps/advanced_chat/test_generate_task_pipeline.py:250:37
+ERROR Argument `SimpleNamespace` is not assignable to parameter `queue_manager` with type `AppQueueManager` in function `core.app.apps.advanced_chat.generate_task_pipeline.AdvancedChatAppGenerateTaskPipeline.__init__` [bad-argument-type]
+ --> tests/unit_tests/core/app/apps/advanced_chat/test_generate_task_pipeline.py:252:23
+ERROR Argument `SimpleNamespace` is not assignable to parameter `draft_var_saver_factory` with type `DraftVariableSaverFactory` in function `core.app.apps.advanced_chat.generate_task_pipeline.AdvancedChatAppGenerateTaskPipeline.__init__` [bad-argument-type]
+ --> tests/unit_tests/core/app/apps/advanced_chat/test_generate_task_pipeline.py:258:33
+ERROR Argument `SimpleNamespace` is not assignable to parameter `application_generate_entity` with type `AdvancedChatAppGenerateEntity` in function `core.app.apps.advanced_chat.generate_task_pipeline.AdvancedChatAppGenerateTaskPipeline.__init__` [bad-argument-type]
+ --> tests/unit_tests/core/app/apps/advanced_chat/test_generate_task_pipeline.py:327:37
+ERROR Argument `SimpleNamespace` is not assignable to parameter `queue_manager` with type `AppQueueManager` in function `core.app.apps.advanced_chat.generate_task_pipeline.AdvancedChatAppGenerateTaskPipeline.__init__` [bad-argument-type]
+ --> tests/unit_tests/core/app/apps/advanced_chat/test_generate_task_pipeline.py:329:23
+ERROR Argument `SimpleNamespace` is not assignable to parameter `draft_var_saver_factory` with type `DraftVariableSaverFactory` in function `core.app.apps.advanced_chat.generate_task_pipeline.AdvancedChatAppGenerateTaskPipeline.__init__` [bad-argument-type]
+ --> tests/unit_tests/core/app/apps/advanced_chat/test_generate_task_pipeline.py:335:33
+ERROR Argument `SimpleNamespace` is not assignable to parameter `application_generate_entity` with type `AdvancedChatAppGenerateEntity` in function `core.app.apps.advanced_chat.generate_task_pipeline.AdvancedChatAppGenerateTaskPipeline.__init__` [bad-argument-type]
+ --> tests/unit_tests/core/app/apps/advanced_chat/test_generate_task_pipeline.py:394:37
ERROR Argument `SimpleNamespace` is not assignable to parameter `queue_manager` with type `AppQueueManager` in function `core.app.apps.advanced_chat.generate_task_pipeline.AdvancedChatAppGenerateTaskPipeline.__init__` [bad-argument-type]
- --> tests/unit_tests/core/app/apps/advanced_chat/test_generate_task_pipeline.py:178:23
+ --> tests/unit_tests/core/app/apps/advanced_chat/test_generate_task_pipeline.py:396:23
ERROR Argument `SimpleNamespace` is not assignable to parameter `draft_var_saver_factory` with type `DraftVariableSaverFactory` in function `core.app.apps.advanced_chat.generate_task_pipeline.AdvancedChatAppGenerateTaskPipeline.__init__` [bad-argument-type]
- --> tests/unit_tests/core/app/apps/advanced_chat/test_generate_task_pipeline.py:184:33
+ --> tests/unit_tests/core/app/apps/advanced_chat/test_generate_task_pipeline.py:402:33
ERROR `SimpleNamespace` is not assignable to attribute `_application_generate_entity` with type `AdvancedChatAppGenerateEntity` [bad-assignment]
- --> tests/unit_tests/core/app/apps/advanced_chat/test_generate_task_pipeline.py:218:45
+ --> tests/unit_tests/core/app/apps/advanced_chat/test_generate_task_pipeline.py:434:45
ERROR `SimpleNamespace` is not assignable to attribute `_application_generate_entity` with type `AdvancedChatAppGenerateEntity` [bad-assignment]
- --> tests/unit_tests/core/app/apps/advanced_chat/test_generate_task_pipeline.py:240:45
+ --> tests/unit_tests/core/app/apps/advanced_chat/test_generate_task_pipeline.py:456:45
ERROR Argument `SimpleNamespace` is not assignable to parameter `queue_manager` with type `AppQueueManager` in function `core.app.apps.advanced_chat.generate_task_pipeline.AdvancedChatAppGenerateTaskPipeline.__init__` [bad-argument-type]
--> tests/unit_tests/core/app/apps/advanced_chat/test_generate_task_pipeline_core.py:104:23
ERROR Argument `(**kwargs: Unknown) -> None` is not assignable to parameter `draft_var_saver_factory` with type `DraftVariableSaverFactory` in function `core.app.apps.advanced_chat.generate_task_pipeline.AdvancedChatAppGenerateTaskPipeline.__init__` [bad-argument-type]
@@ -7951,17 +7967,55 @@
ERROR Argument `TestWorkflowEntryNodeLayers.test_run_node_with_layers_reports_errors.FakeNode` is not assignable to parameter `node` with type `Node[Unknown]` in function `core.workflow.workflow_entry.WorkflowEntry._run_node_with_layers` [bad-argument-type]
--> tests/unit_tests/core/workflow/test_workflow_entry_helpers.py:818:73
ERROR Missing argument `case` in function `enterprise.telemetry.contracts.TelemetryEnvelope.__init__` [missing-argument]
- --> tests/unit_tests/enterprise/telemetry/test_contracts.py:111:30
+ --> tests/unit_tests/enterprise/telemetry/test_contracts.py:110:30
ERROR Missing argument `tenant_id` in function `enterprise.telemetry.contracts.TelemetryEnvelope.__init__` [missing-argument]
- --> tests/unit_tests/enterprise/telemetry/test_contracts.py:120:30
+ --> tests/unit_tests/enterprise/telemetry/test_contracts.py:119:30
ERROR Missing argument `event_id` in function `enterprise.telemetry.contracts.TelemetryEnvelope.__init__` [missing-argument]
- --> tests/unit_tests/enterprise/telemetry/test_contracts.py:129:30
+ --> tests/unit_tests/enterprise/telemetry/test_contracts.py:128:30
ERROR Missing argument `payload` in function `enterprise.telemetry.contracts.TelemetryEnvelope.__init__` [missing-argument]
- --> tests/unit_tests/enterprise/telemetry/test_contracts.py:138:30
+ --> tests/unit_tests/enterprise/telemetry/test_contracts.py:137:30
+ERROR Argument `SimpleNamespace` is not assignable to parameter `config` with type `DifyConfig` in function `enterprise.telemetry.exporter.EnterpriseExporter.__init__` [bad-argument-type]
+ --> tests/unit_tests/enterprise/telemetry/test_exporter.py:37:24
+ERROR Argument `SimpleNamespace` is not assignable to parameter `config` with type `DifyConfig` in function `enterprise.telemetry.exporter.EnterpriseExporter.__init__` [bad-argument-type]
+ --> tests/unit_tests/enterprise/telemetry/test_exporter.py:60:24
+ERROR Argument `SimpleNamespace` is not assignable to parameter `config` with type `DifyConfig` in function `enterprise.telemetry.exporter.EnterpriseExporter.__init__` [bad-argument-type]
+ --> tests/unit_tests/enterprise/telemetry/test_exporter.py:84:24
+ERROR Argument `SimpleNamespace` is not assignable to parameter `config` with type `DifyConfig` in function `enterprise.telemetry.exporter.EnterpriseExporter.__init__` [bad-argument-type]
+ --> tests/unit_tests/enterprise/telemetry/test_exporter.py:111:28
+ERROR Argument `SimpleNamespace` is not assignable to parameter `config` with type `DifyConfig` in function `enterprise.telemetry.exporter.EnterpriseExporter.__init__` [bad-argument-type]
+ --> tests/unit_tests/enterprise/telemetry/test_exporter.py:140:24
+ERROR Argument `SimpleNamespace` is not assignable to parameter `config` with type `DifyConfig` in function `enterprise.telemetry.exporter.EnterpriseExporter.__init__` [bad-argument-type]
+ --> tests/unit_tests/enterprise/telemetry/test_exporter.py:164:24
+ERROR Argument `SimpleNamespace` is not assignable to parameter `config` with type `DifyConfig` in function `enterprise.telemetry.exporter.EnterpriseExporter.__init__` [bad-argument-type]
+ --> tests/unit_tests/enterprise/telemetry/test_exporter.py:188:24
+ERROR Argument `SimpleNamespace` is not assignable to parameter `config` with type `DifyConfig` in function `enterprise.telemetry.exporter.EnterpriseExporter.__init__` [bad-argument-type]
+ --> tests/unit_tests/enterprise/telemetry/test_exporter.py:213:24
+ERROR Argument `SimpleNamespace` is not assignable to parameter `config` with type `DifyConfig` in function `enterprise.telemetry.exporter.EnterpriseExporter.__init__` [bad-argument-type]
+ --> tests/unit_tests/enterprise/telemetry/test_exporter.py:236:24
+ERROR Argument `SimpleNamespace` is not assignable to parameter `config` with type `DifyConfig` in function `enterprise.telemetry.exporter.EnterpriseExporter.__init__` [bad-argument-type]
+ --> tests/unit_tests/enterprise/telemetry/test_exporter.py:260:24
+ERROR Argument `SimpleNamespace` is not assignable to parameter `config` with type `DifyConfig` in function `enterprise.telemetry.exporter.EnterpriseExporter.__init__` [bad-argument-type]
+ --> tests/unit_tests/enterprise/telemetry/test_exporter.py:346:35
+ERROR Argument `SimpleNamespace` is not assignable to parameter `config` with type `DifyConfig` in function `enterprise.telemetry.exporter.EnterpriseExporter.__init__` [bad-argument-type]
+ --> tests/unit_tests/enterprise/telemetry/test_exporter.py:356:35
+ERROR Argument `SimpleNamespace` is not assignable to parameter `config` with type `DifyConfig` in function `enterprise.telemetry.exporter.EnterpriseExporter.__init__` [bad-argument-type]
+ --> tests/unit_tests/enterprise/telemetry/test_exporter.py:371:24
+ERROR Argument `SimpleNamespace` is not assignable to parameter `config` with type `DifyConfig` in function `enterprise.telemetry.exporter.EnterpriseExporter.__init__` [bad-argument-type]
+ --> tests/unit_tests/enterprise/telemetry/test_exporter.py:383:24
+ERROR Argument `SimpleNamespace` is not assignable to parameter `config` with type `DifyConfig` in function `enterprise.telemetry.exporter.EnterpriseExporter.__init__` [bad-argument-type]
+ --> tests/unit_tests/enterprise/telemetry/test_exporter.py:407:39
+ERROR Argument `SimpleNamespace` is not assignable to parameter `config` with type `DifyConfig` in function `enterprise.telemetry.exporter.EnterpriseExporter.__init__` [bad-argument-type]
+ --> tests/unit_tests/enterprise/telemetry/test_exporter.py:584:35
ERROR Argument `dict[str, str]` is not assignable to parameter `labels` with type `dict[str, AttributeValue]` in function `enterprise.telemetry.exporter.EnterpriseExporter.increment_counter` [bad-argument-type]
- --> tests/unit_tests/enterprise/telemetry/test_exporter.py:582:71
+ --> tests/unit_tests/enterprise/telemetry/test_exporter.py:590:71
+ERROR Argument `SimpleNamespace` is not assignable to parameter `config` with type `DifyConfig` in function `enterprise.telemetry.exporter.EnterpriseExporter.__init__` [bad-argument-type]
+ --> tests/unit_tests/enterprise/telemetry/test_exporter.py:599:35
+ERROR Argument `SimpleNamespace` is not assignable to parameter `config` with type `DifyConfig` in function `enterprise.telemetry.exporter.EnterpriseExporter.__init__` [bad-argument-type]
+ --> tests/unit_tests/enterprise/telemetry/test_exporter.py:617:35
ERROR Argument `dict[str, str]` is not assignable to parameter `labels` with type `dict[str, AttributeValue]` in function `enterprise.telemetry.exporter.EnterpriseExporter.record_histogram` [bad-argument-type]
- --> tests/unit_tests/enterprise/telemetry/test_exporter.py:615:85
+ --> tests/unit_tests/enterprise/telemetry/test_exporter.py:623:85
+ERROR Argument `SimpleNamespace` is not assignable to parameter `config` with type `DifyConfig` in function `enterprise.telemetry.exporter.EnterpriseExporter.__init__` [bad-argument-type]
+ --> tests/unit_tests/enterprise/telemetry/test_exporter.py:632:35
ERROR Missing argument `session` in function `events.event_handlers.update_app_dataset_join_when_app_model_config_updated.handle` [missing-argument]
--> tests/unit_tests/events/test_app_event_signals.py:229:19
ERROR Object of class `Events` has no attribute `request_start` [missing-attribute]
|
Pyrefly Type Coverage
|
e868862 to
333c861
Compare
9fd7ec2 to
7d26b30
Compare
There was a problem hiding this comment.
Pull request overview
This PR refactors the telemetry gateway to emit typed TelemetryEvent objects (instead of TelemetryCase + context/payload dicts) and restores/extends enterprise metrics for feedback, prompt generation duration, and message TTFT.
Changes:
- Introduces a
TelemetryEventprotocol plus concrete typed event classes and updatescore.telemetry.gateway.emit()to route based on event metadata. - Adds new telemetry emission points for feedback creation and prompt-generation operations (including duration/status labeling).
- Enqueues
MESSAGE_TRACEfor Advanced Chat message persistence to restore message-level trace/metrics coverage; updates/expands unit tests accordingly.
Reviewed changes
Copilot reviewed 20 out of 20 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| api/tests/unit_tests/enterprise/telemetry/test_gateway.py | Updates gateway tests to use typed event objects and validates event routing metadata. |
| api/tests/unit_tests/enterprise/telemetry/test_event_handlers.py | Updates handler tests to patch the facade core.telemetry.emit and validate event objects. |
| api/tests/unit_tests/enterprise/telemetry/test_enterprise_trace.py | Adds assertions for required labels/status on TTFT and prompt duration histograms. |
| api/tests/unit_tests/enterprise/telemetry/test_draft_trace.py | Updates draft trace tests to assert typed event fields (trace_task_name). |
| api/tests/unit_tests/enterprise/telemetry/test_contracts.py | Replaces routing-table tests with event-class metadata assertions. |
| api/tests/unit_tests/core/telemetry/test_gateway_integration.py | Adjusts integration tests for new emit(event, ...) signature and typed events. |
| api/tests/unit_tests/core/telemetry/test_facade.py | Updates facade tests to use concrete typed trace events. |
| api/tests/unit_tests/core/llm_generator/test_llm_generator.py | Updates assertions around error propagation after generator refactor/telemetry emission. |
| api/tests/unit_tests/core/app/apps/advanced_chat/test_generate_task_pipeline.py | Updates pipeline test scaffolding for newly accessed fields (extras). |
| api/services/message_service.py | Emits FeedbackCreatedEvent during feedback writes. |
| api/enterprise/telemetry/event_handlers.py | Switches enterprise signal handlers to emit typed events via core.telemetry. |
| api/enterprise/telemetry/enterprise_trace.py | Ensures prompt generation duration histogram labels include status. |
| api/enterprise/telemetry/draft_trace.py | Emits DraftNodeExecutionTraceEvent instead of generic TelemetryEvent. |
| api/enterprise/telemetry/contracts.py | Expands SignalType documentation to clarify TRACE vs METRIC_LOG semantics. |
| api/core/telemetry/gateway.py | Removes case-routing tables; routes directly using event metadata. |
| api/core/telemetry/events.py | Adds protocol + typed payloads and concrete event classes (app lifecycle, feedback, prompt generation, draft node execution). |
| api/core/telemetry/init.py | Re-exports new typed telemetry surface and makes emit the gateway entrypoint. |
| api/core/llm_generator/llm_generator.py | Adds prompt generation telemetry emission and threads app_id through generator entrypoints. |
| api/core/app/apps/advanced_chat/generate_task_pipeline.py | Enqueues MESSAGE_TRACE on message save to restore message trace metrics (TTFT, etc.). |
| api/controllers/console/app/generator.py | Passes flow_id as app_id into LLM generator methods for telemetry context. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
4962168 to
bc601e6
Compare
4661dcc to
d1cf67f
Compare
0298267 to
a00345a
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 29 out of 29 changed files in this pull request and generated no new comments.
Suppressed comments (3)
api/core/llm_generator/llm_generator.py:556
generate_rule_config()returns early on the initialInvokeError(prefix prompt generation), which skips_emit_prompt_generation(). That means prompt-generation telemetry won’t fire on this failure path, even though other paths now always emit.
except InvokeError as e:
error = str(e)
error_step = "generate prefix prompt"
rule_config["error"] = f"Failed to {error_step}. Error: {error}"
return rule_config
api/services/message_service.py:226
create_feedback()emitsFeedbackCreatedEventfor any non-nullrating, including updates to an existing feedback record. This can inflatedify_feedback_total(and the event name implies it should represent creation only). Consider emitting only when a newMessageFeedbackis created.
session.commit()
if rating:
cls._emit_feedback_telemetry(
app_model=app_model, message=message, user=user, rating=rating, content=content
)
api/enterprise/telemetry/exporter.py:116
ENTERPRISE_OTLP_PROTOCOLis now used without normalization. Previously the exporter lowercased and defaulted the value; with the current code, values likeGRPC(or an empty string) will silently fall back to the HTTP exporter path.
def __init__(self, config: DifyConfig) -> None:
endpoint: str = config.ENTERPRISE_OTLP_ENDPOINT
headers_raw: str = config.ENTERPRISE_OTLP_HEADERS
protocol: str = config.ENTERPRISE_OTLP_PROTOCOL
service_name: str = config.APPLICATION_NAME
sampling_rate: float = config.ENTERPRISE_OTEL_SAMPLING_RATE
|
|
e91f27c to
4cb4bc5
Compare
cb9b0d0 to
6509bf0
Compare
4aaf250 to
09a1426
Compare
5a0d17c to
321088b
Compare
| tenant_id, app_id, user_id = self._context_ids(workflow_info, metadata) | ||
|
|
||
| try: | ||
| with Session(db.engine) as session: |
There was a problem hiding this comment.
Does not using a repository here mean the feature won't support LogStore?
| ) | ||
| session.add_all(message_files) | ||
|
|
||
| trace_manager = self._application_generate_entity.trace_manager |
There was a problem hiding this comment.
This emits on every save, including pause and each resumed completion. This duplicates message, token, and TTFT metrics. Emit once at terminal completion or make emissions idempotent.
There was a problem hiding this comment.
moved trace emission to invoker's side.
Important
Fixes #<issue number>.Summary
resolves ESQ1-209
Screenshots
Checklist
make lint && make type-check(backend) andvp staged(frontend) to appease the lint gods