度量和管理

本節介紹如何為 Spring Integration 捕獲指標。在最新版本中,我們更多地依賴於 Micrometer,並且計劃在未來的版本中更多地使用 Micrometer。

在高吞吐量環境中停用日誌

您可以控制主訊息流中的除錯日誌。在非常高吞吐量的應用程式中,使用某些日誌子系統呼叫 isDebugEnabled() 可能會非常昂貴。您可以停用所有此類日誌以避免這種開銷。異常日誌(除錯或其他)不受此設定影響。

以下列表顯示了控制日誌的可用選項

  • Java

  • XML

@Configuration
@EnableIntegration
@EnableIntegrationManagement(
    defaultLoggingEnabled = "true" <1>)

public static class ContextConfiguration {
...
}
<int:management default-logging-enabled="true"/> (1)
1 設定為 false 可停用主訊息流中的所有日誌,無論日誌系統類別設定如何。設定為 'true' 可啟用除錯日誌(如果也由日誌子系統啟用)。僅當您尚未在 Bean 定義中明確配置該設定時才適用。預設值為 true
defaultLoggingEnabled 僅當您尚未在 Bean 定義中明確配置相應的設定時才適用。

Micrometer 整合

概述

從版本 5.0.3 開始,應用程式上下文中存在 Micrometer MeterRegistry 會觸發對 Micrometer 指標的支援。

要使用 Micrometer,請將其中一個 MeterRegistry Bean 新增到應用程式上下文。

對於每個 MessageHandlerMessageChannel,都註冊了計時器。對於每個 MessageSource,都註冊了一個計數器。

這僅適用於擴充套件 AbstractMessageHandlerAbstractMessageChannelAbstractMessageSource 的物件(大多數框架元件都是這種情況)。

訊息通道上傳送操作的 Timer 儀表具有以下名稱或標籤

  • name: spring.integration.send

  • tag: type:channel

  • tag: name:<componentName>

  • tag: result:(success|failure)

  • tag: exception:(none|exception simple class name)

  • description: Send processing time

failure 結果和 none 異常表示通道的 send() 操作返回 false。)

可輪詢訊息通道上接收操作的 Counter 儀表具有以下名稱或標籤

  • name: spring.integration.receive

  • tag: type:channel

  • tag: name:<componentName>

  • tag: result:(success|failure)

  • tag: exception:(none|exception simple class name)

  • description: Messages received

訊息處理器上操作的 Timer 儀表具有以下名稱或標籤

  • name: spring.integration.send

  • tag: type:handler

  • tag: name:<componentName>

  • tag: result:(success|failure)

  • tag: exception:(none|exception simple class name)

  • description: Send processing time

訊息源的 Counter 儀表具有以下名稱/標籤

  • name: spring.integration.receive

  • tag: type:source

  • tag: name:<componentName>

  • tag: result:success

  • tag: exception:none

  • description: Messages received

此外,還有三個 Gauge 儀表

  • spring.integration.channels: 應用程式中 MessageChannels 的數量。

  • spring.integration.handlers: 應用程式中 MessageHandlers 的數量。

  • spring.integration.sources: 應用程式中 MessageSources 的數量。

可以透過提供 MicrometerMetricsCaptor 的子類來定製整合元件建立的 Meters 的名稱和標籤。 MicrometerCustomMetricsTests 測試用例顯示瞭如何執行此操作的簡單示例。您還可以透過過載構建器子類上的 build() 方法來進一步定製儀表。

從版本 5.1.13 開始,QueueChannel 暴露 Micrometer 指標,用於佇列大小和剩餘容量

  • name: spring.integration.channel.queue.size

  • tag: type:channel

  • tag: name:<componentName>

  • description: 佇列通道的大小

  • name: spring.integration.channel.queue.remaining.capacity

  • tag: type:channel

  • tag: name:<componentName>

  • description: 佇列通道的剩餘容量

停用儀表

預設情況下,所有儀表在首次使用時都會註冊。現在,藉助 Micrometer,您可以將 MeterFilter 新增到 MeterRegistry,以防止註冊某些或所有儀表。您可以透過提供的任何屬性(nametag 等)過濾掉(拒絕)儀表。有關更多資訊,請參閱 Micrometer 文件中的 儀表過濾器

例如,給定

@Bean
public QueueChannel noMeters() {
    return new QueueChannel(10);
}

您可以透過以下方式抑制為此通道註冊儀表

registry.config().meterFilter(MeterFilter.deny(id ->
        "channel".equals(id.getTag("type")) &&
        "noMeters".equals(id.getTag("name"))));

Micrometer Observation

從版本 6.0 開始,Spring Integration 利用 Micrometer Observation 抽象,該抽象可以透過適當的 ObservationHandler 配置來處理指標和 跟蹤

當應用程式上下文中存在 ObservationRegistry Bean 並且配置了 @EnableIntegrationManagement 時,IntegrationManagement 元件上會啟用觀察處理。為了自定義應檢測哪些元件集,@EnableIntegrationManagement 註解上暴露了一個 observationPatterns() 屬性。有關模式匹配演算法,請參閱其 Javadoc。

預設情況下,任何 IntegrationManagement 元件都不會使用 ObservationRegistry Bean 進行檢測。可以配置為 * 以匹配所有元件。

在這種情況下,儀表不會獨立收集,而是委託給在提供的 ObservationRegistry 上配置的適當的 ObservationHandler

以下 Spring Integration 元件使用觀察邏輯進行檢測,每個元件都有一個相應的約定

  • MessageProducerSupport 作為流的入站端點,被視為 CONSUMER span 型別,並使用 IntegrationObservation.HANDLER API;

  • MessagingGatewaySupport 是入站請求-回覆端點,被視為 SERVER span 型別。它使用 IntegrationObservation.GATEWAY API;

  • AbstractMessageChannel.send() 操作是 Spring Integration 唯一產生訊息的 API。因此,它被視為 PRODUCER span 型別,並使用 IntegrationObservation.PRODUCER API。當通道是分散式實現(例如 PublishSubscribeKafkaChannelZeroMqChannel)並且必須將跟蹤資訊新增到訊息中時,這更有意義。因此,IntegrationObservation.PRODUCER 觀察基於 MessageSenderContext,其中 Spring Integration 提供 MutableMessage 以允許後續的跟蹤 Propagator 新增標頭,以便消費者可以使用它們;

  • AbstractMessageHandlerCONSUMER span 型別,並使用 IntegrationObservation.HANDLER API;

  • SourcePollingChannelAdapter(從版本 6.5 開始)作為流的入站端點,被視為 CONSUMER span 型別,並使用 IntegrationObservation.HANDLER API。

IntegrationManagement 元件上的觀察生產可以透過 ObservationConvention 配置進行自定義。例如,AbstractMessageHandler 透過其 setObservationConvention() API 期望 MessageReceiverObservationConvention

以下是 Observation API 支援的指標、跨度和約定

可觀測性 - 指標

以下是本專案宣告的所有指標列表。

閘道器

入站訊息閘道器的觀察。

指標名稱 spring.integration.gateway (由約定類 o.s.i.support.management.observation.DefaultMessageRequestReplyReceiverObservationConvention 定義)。 型別 timer

指標名稱 spring.integration.gateway.active (由約定類 o.s.i.support.management.observation.DefaultMessageRequestReplyReceiverObservationConvention 定義)。 型別 long task timer

在啟動觀測後新增的鍵值可能會從 *.active 指標中缺失。
Micrometer 內部使用 納秒 作為基本單位。但是,每個後端確定實際的基本單位。(即 Prometheus 使用秒)

封閉類 o.s.i.support.management.observation.IntegrationObservation 的完全限定名稱。

所有標籤必須以 spring.integration. 字首開頭!
表 1. 低基數鍵

名稱

描述

spring.integration.name (必需)

訊息閘道器元件的名稱。

spring.integration.outcome (必需)

請求/回覆執行的結果。

spring.integration.type (必需)

元件型別 - 'gateway'。

處理器

訊息處理器的觀察。

指標名稱 spring.integration.handler (由約定類 o.s.i.support.management.observation.DefaultMessageReceiverObservationConvention 定義)。 型別 timer

指標名稱 spring.integration.handler.active (由約定類 o.s.i.support.management.observation.DefaultMessageReceiverObservationConvention 定義)。 型別 long task timer

在啟動觀測後新增的鍵值可能會從 *.active 指標中缺失。
Micrometer 內部使用 納秒 作為基本單位。但是,每個後端確定實際的基本單位。(即 Prometheus 使用秒)

封閉類 o.s.i.support.management.observation.IntegrationObservation 的完全限定名稱。

所有標籤必須以 spring.integration. 字首開頭!
表 2. 低基數鍵

名稱

描述

spring.integration.name (必需)

訊息處理器元件的名稱。

spring.integration.type (必需)

元件型別 - 'handler'。

生產者

訊息生產者的觀察,例如通道。

指標名稱 spring.integration.producer (由約定類 o.s.i.support.management.observation.DefaultMessageSenderObservationConvention 定義)。 型別 timer

指標名稱 spring.integration.producer.active (由約定類 o.s.i.support.management.observation.DefaultMessageSenderObservationConvention 定義)。 型別 long task timer

在啟動觀測後新增的鍵值可能會從 *.active 指標中缺失。
Micrometer 內部使用 納秒 作為基本單位。但是,每個後端確定實際的基本單位。(即 Prometheus 使用秒)

封閉類 o.s.i.support.management.observation.IntegrationObservation 的完全限定名稱。

所有標籤必須以 spring.integration. 字首開頭!
表3. 低基數鍵

名稱

描述

spring.integration.name (必需)

訊息處理器元件的名稱。

spring.integration.type (必需)

元件型別 - 'producer'。

可觀測性 - Span

以下是本專案宣告的所有 Span 列表。

閘道器 Span

入站訊息閘道器的觀察。

Span 名稱 spring.integration.gateway (由約定類 o.s.i.support.management.observation.DefaultMessageRequestReplyReceiverObservationConvention 定義)。

封閉類 o.s.i.support.management.observation.IntegrationObservation 的完全限定名稱。

所有標籤必須以 spring.integration. 字首開頭!
表 4. 標籤鍵

名稱

描述

spring.integration.name (必需)

訊息閘道器元件的名稱。

spring.integration.outcome (必需)

請求/回覆執行的結果。

spring.integration.type (必需)

元件型別 - 'gateway'。

處理器 Span

訊息處理器的觀察。

Span 名稱 spring.integration.handler (由約定類 o.s.i.support.management.observation.DefaultMessageReceiverObservationConvention 定義)。

封閉類 o.s.i.support.management.observation.IntegrationObservation 的完全限定名稱。

所有標籤必須以 spring.integration. 字首開頭!
表 5. 標籤鍵

名稱

描述

spring.integration.name (必需)

訊息處理器元件的名稱。

spring.integration.type (必需)

元件型別 - 'handler'。

生產者 Span

訊息生產者的觀察,例如通道。

Span 名稱 spring.integration.producer (由約定類 o.s.i.support.management.observation.DefaultMessageSenderObservationConvention 定義)。

封閉類 o.s.i.support.management.observation.IntegrationObservation 的完全限定名稱。

所有標籤必須以 spring.integration. 字首開頭!
表 6. 標籤鍵

名稱

描述

spring.integration.name (必需)

訊息處理器元件的名稱。

spring.integration.type (必需)

元件型別 - 'producer'。

可觀測性 - 約定

您可以在下面找到此專案宣告的所有 GlobalObservationConventionObservationConvention 列表。

表 7. ObservationConvention 實現

ObservationConvention 類名

適用的 ObservationContext 類名

o.s.i.support.management.observation.DefaultMessageReceiverObservationConvention

MessageReceiverContext

o.s.i.support.management.observation.MessageReceiverObservationConvention

MessageReceiverContext

o.s.i.support.management.observation.DefaultMessageRequestReplyReceiverObservationConvention

MessageRequestReplyReceiverContext

o.s.i.support.management.observation.MessageRequestReplyReceiverObservationConvention

MessageRequestReplyReceiverContext

o.s.i.support.management.observation.DefaultMessageSenderObservationConvention

MessageSenderContext

o.s.i.support.management.observation.MessageSenderObservationConvention

MessageSenderContext

觀察傳播

為了在一個跟蹤中提供連線的跨度鏈,獨立於訊息流的性質,即使 MessageChannel 是持久且分散式的,也必須在此通道及其消費方(訂閱者)上啟用觀察。這樣,跟蹤資訊在傳播到消費者執行緒或持久化到資料庫之前儲存在訊息頭中。這是透過上述 MessageSenderContext 完成的。消費者(MessageHandler)端使用 MessageReceiverContext 從這些頭中恢復跟蹤資訊並開始一個新的子 Observation

Spring Integration JMX 支援

另請參閱 JMX 支援

© . This site is unofficial and not affiliated with VMware.