可觀測性

Spring for GraphQL 直接集成了 Micrometer 可觀測性支援。這為 GraphQL 請求和“非平凡的”資料獲取操作提供了指標 (metrics) 和追蹤 (traces)。由於 GraphQL 引擎執行在傳輸層之上,如果 Spring Framework 支援的話,您也應該可以 從傳輸層獲得觀測資料

僅當應用程式中配置了 ObservationRegistry 時,才會釋出觀測資料 (observations)。您可以瞭解更多關於 在 Spring Boot 中配置可觀測性基礎設施 的資訊。如果您想自定義 GraphQL 觀測資料生成的元資料,您可以 直接在 instrumentation 上配置自定義約定 (convention)。如果您的應用程式使用 Spring Boot,推薦的方式是將自定義約定貢獻為一個 bean。

伺服器請求 instrumentation

GraphQL 伺服器請求觀測資料 (observations) 的名稱為 "graphql.request",適用於傳統和響應式 (Reactive) 應用程式以及所有支援的傳輸方式。此 instrumentation 假定任何父級觀測資料 (parent observation) 必須使用眾所周知的 "micrometer.observation" 鍵在 GraphQL 上下文 (context) 中設定為當前觀測資料。對於跨網路邊界的追蹤傳播 (trace propagation),必須由傳輸層的獨立 instrumentation 負責。對於 HTTP,Spring Framework 提供了專門負責追蹤傳播的 instrumentation

應用程式需要在其應用中配置 org.springframework.graphql.observation.GraphQlObservationInstrumentation instrumentation。預設情況下,它使用 org.springframework.graphql.observation.DefaultExecutionRequestObservationConvention,由 ExecutionRequestObservationContext 提供支援。

預設情況下,會建立以下 KeyValue:

表 1. 低基數鍵

名稱

描述

graphql.operation (必需)

GraphQL 操作名稱。

graphql.outcome (必需)

GraphQL 請求的結果。

graphql.operation KeyValue 將使用所提供查詢的自定義名稱,如果未提供,則使用 操作的標準名稱 ("query""mutation""subscription")。graphql.outcome KeyValue 將是:

  • "SUCCESS" 如果已傳送有效的 GraphQL 響應且不包含任何錯誤

  • "REQUEST_ERROR" 如果請求無法解析,或者響應包含錯誤(且沒有錯誤型別為 org.springframework.graphql.execution.ErrorType.INTERNAL_ERROR

  • "INTERNAL_ERROR" 如果無法生成有效的 GraphQL 響應,或者響應包含至少一個錯誤型別為 org.springframework.graphql.execution.ErrorType.INTERNAL_ERROR 的錯誤

表 2. 高基數鍵

名稱

描述

graphql.execution.id (必需)

GraphQL 請求的 graphql.execution.ExecutionId

Spring for GraphQL 還為伺服器請求觀測資料貢獻事件 (Events)。Micrometer 觀測事件 通常在追蹤中作為 span 註解處理。此 instrumentation 將 GraphQL 響應中列出的錯誤記錄為事件。

表 3. 觀測事件

名稱

上下文名稱

GraphQL 錯誤型別,例如 InvalidSyntax

完整的 GraphQL 錯誤訊息,例如 "Invalid syntax with offending token 'invalid'…​"

DataFetcher instrumentation

GraphQL DataFetcher 觀測資料 (observations) 的名稱為 "graphql.datafetcher",僅針對被視為“非平凡的”資料獲取操作建立(獲取 Java 物件上的屬性是平凡操作)。應用程式需要在其應用中配置 org.springframework.graphql.observation.GraphQlObservationInstrumentation instrumentation。預設情況下,它使用 org.springframework.graphql.observation.DefaultDataFetcherObservationConvention,由 DataFetcherObservationContext 提供支援。

預設情況下,會建立以下 KeyValue:

表 4. 低基數鍵

名稱

描述

graphql.error.type (必需)

資料獲取錯誤的類名

graphql.field.name (必需)

正在獲取的欄位名稱。

graphql.outcome (必需)

GraphQL 資料獲取操作的結果,“SUCCESS”或“ERROR”。

表 5. 高基數鍵

名稱

描述

graphql.field.path (必需)

正在獲取欄位的路徑(例如,"/bookById")。