Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add processing latency to the client #97

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions console-framework-client-api/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2022-2024. AxonIQ B.V.
~ Copyright (c) 2022-2025. AxonIQ B.V.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
Expand All @@ -21,7 +21,7 @@
<parent>
<groupId>io.axoniq.console</groupId>
<artifactId>console-framework-client-parent</artifactId>
<version>1.8.2-SNAPSHOT</version>
<version>1.9.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023. AxonIQ B.V.
* Copyright (c) 2022-2025. AxonIQ B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -57,6 +57,7 @@ data class SegmentStatus(
val errorMessage: String?,
val ingestLatency: Double?,
val commitLatency: Double?,
val processingLatency: Double?,
val position: Long? = -1,
val resetPosition: Long? = -1,
)
Expand Down
4 changes: 2 additions & 2 deletions console-framework-client-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2022-2024. AxonIQ B.V.
~ Copyright (c) 2022-2025. AxonIQ B.V.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
Expand All @@ -21,7 +21,7 @@
<parent>
<groupId>io.axoniq.console</groupId>
<artifactId>console-framework-client-parent</artifactId>
<version>1.8.2-SNAPSHOT</version>
<version>1.9.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
4 changes: 2 additions & 2 deletions console-framework-client/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2022-2024. AxonIQ B.V.
~ Copyright (c) 2022-2025. AxonIQ B.V.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
Expand All @@ -21,7 +21,7 @@
<parent>
<groupId>io.axoniq.console</groupId>
<artifactId>console-framework-client-parent</artifactId>
<version>1.8.2-SNAPSHOT</version>
<version>1.9.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023. AxonIQ B.V.
* Copyright (c) 2022-2025. AxonIQ B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -121,6 +121,7 @@ class ProcessorReportCreator(
errorMessage = this.error?.message,
ingestLatency = metricsRegistry.ingestLatencyForProcessor(name, this.segment.segmentId).getValue(),
commitLatency = metricsRegistry.commitLatencyForProcessor(name, this.segment.segmentId).getValue(),
processingLatency = metricsRegistry.processingMessageLatencyForProcessor(name, this.segment.segmentId)?.toDouble() ?: -1.0,
position = this.currentPosition?.orElse(-1) ?: -1,
resetPosition = this.resetPosition?.orElse(-1) ?: -1,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2024. AxonIQ B.V.
* Copyright (c) 2022-2025. AxonIQ B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,8 +30,8 @@ import java.time.Instant
import java.time.temporal.ChronoUnit

class AxoniqConsoleProcessorInterceptor(
private val processorMetricsRegistry: ProcessorMetricsRegistry,
private val processorName: String,
private val processorMetricsRegistry: ProcessorMetricsRegistry,
private val processorName: String,
) : MessageHandlerInterceptor<Message<*>> {
private val logger = LoggerFactory.getLogger(this::class.java)

Expand All @@ -40,31 +40,36 @@ class AxoniqConsoleProcessorInterceptor(
if (uow == null || unitOfWork.message.payload is UnknownSerializedType) {
return interceptorChain.proceed()
}
val message = unitOfWork.message
if (message !is EventMessage) {
return interceptorChain.proceed()
}
try {
AxoniqConsoleSpanFactory.onTopLevelSpanIfActive {
it.reportProcessorName(processorName)
}
val message = unitOfWork.message
if (message is EventMessage) {
val segment = unitOfWork.resources()["Processor[$processorName]/SegmentId"] as? Int ?: -1
processorMetricsRegistry.registerIngested(
val segment = unitOfWork.resources()["Processor[$processorName]/SegmentId"] as? Int ?: -1
processorMetricsRegistry.registerIngested(
processorName,
segment,
ChronoUnit.NANOS.between(message.timestamp, Instant.now())
)
if (unitOfWork !is BatchingUnitOfWork<*> || unitOfWork.isLastMessage) {
unitOfWork.afterCommit {
processorMetricsRegistry.registerCommitted(
)
if (unitOfWork !is BatchingUnitOfWork<*> || unitOfWork.isFirstMessage) {
unitOfWork.afterCommit {
processorMetricsRegistry.registerCommitted(
processorName,
segment,
ChronoUnit.NANOS.between(message.timestamp, Instant.now())
)
}
)
}
}

return processorMetricsRegistry.doWithActiveMessageForSegment(processorName, segment, message.timestamp) {
interceptorChain.proceed()
}
} catch (e: Exception) {
logger.debug("AxonIQ Console could not register metrics for processor $processorName", e)
return interceptorChain.proceed()
}
return interceptorChain.proceed()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023. AxonIQ B.V.
* Copyright (c) 2022-2025. AxonIQ B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,14 +17,18 @@
package io.axoniq.console.framework.eventprocessor.metrics

import io.axoniq.console.framework.computeIfAbsentWithRetry
import org.axonframework.messaging.unitofwork.CurrentUnitOfWork
import java.time.Clock
import java.time.Instant
import java.time.temporal.ChronoUnit
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicLong
import java.util.concurrent.atomic.AtomicReference

class ProcessorMetricsRegistry {
private val ingestLatencyRegistry: MutableMap<String, MutableMap<Int, ExpiringLatencyValue>> = ConcurrentHashMap()
private val commitLatencyRegistry: MutableMap<String, MutableMap<Int, ExpiringLatencyValue>> = ConcurrentHashMap()
private val processingLatencyRegistry: MutableMap<String, MutableMap<Int, Instant?>> = ConcurrentHashMap()

fun registerIngested(processor: String, segment: Int, latencyInNanos: Long) {
ingestLatencyForProcessor(processor, segment).setValue(latencyInNanos.toDouble() / 1000000)
Expand All @@ -34,18 +38,44 @@ class ProcessorMetricsRegistry {
commitLatencyForProcessor(processor, segment).setValue(latencyInNanos.toDouble() / 1000000)
}

fun <T> doWithActiveMessageForSegment(processor: String, segment: Int, messageTimestamp: Instant, action: () -> T?): T? {
val processingMessageTimestampsForSegment = getProcessingLatencySegmentMap(processor)

try {
processingMessageTimestampsForSegment[segment] = messageTimestamp
return action()
} finally {
CurrentUnitOfWork.get().afterCommit {
getProcessingLatencySegmentMap(processor)
.remove(segment)
}
}
}

fun ingestLatencyForProcessor(processor: String, segment: Int): ExpiringLatencyValue {
return ingestLatencyRegistry
.computeIfAbsentWithRetry(processor) { mutableMapOf() }
.computeIfAbsentWithRetry(segment) { ExpiringLatencyValue() }
.computeIfAbsentWithRetry(processor) { ConcurrentHashMap() }
.computeIfAbsentWithRetry(segment) { ExpiringLatencyValue() }
}

fun commitLatencyForProcessor(processor: String, segment: Int): ExpiringLatencyValue {
return commitLatencyRegistry
.computeIfAbsentWithRetry(processor) { mutableMapOf() }
.computeIfAbsentWithRetry(segment) { ExpiringLatencyValue() }
.computeIfAbsentWithRetry(processor) { ConcurrentHashMap() }
.computeIfAbsentWithRetry(segment) { ExpiringLatencyValue() }
}

fun processingMessageLatencyForProcessor(processor: String, segment: Int): Long? {
val processingTimestamp = getProcessingLatencySegmentMap(processor)
.computeIfAbsentWithRetry(segment) { null }
if (processingTimestamp == null) {
return null
}
return ChronoUnit.MILLIS.between(processingTimestamp, Instant.now())
}

private fun getProcessingLatencySegmentMap(processor: String) = processingLatencyRegistry
.computeIfAbsentWithRetry(processor) { ConcurrentHashMap() }

class ExpiringLatencyValue(
private val expiryTime: Long = 30 * 60 * 1000 // Default to 1 hour
) {
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2022-2024. AxonIQ B.V.
~ Copyright (c) 2022-2025. AxonIQ B.V.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
Expand All @@ -22,7 +22,7 @@

<groupId>io.axoniq.console</groupId>
<artifactId>console-framework-client-parent</artifactId>
<version>1.8.2-SNAPSHOT</version>
<version>1.9.0-SNAPSHOT</version>

<modules>
<module>console-framework-client-api</module>
Expand Down
Loading