-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
357 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
hermes-common/src/main/java/pl/allegro/tech/hermes/common/metric/BrokerMetrics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package pl.allegro.tech.hermes.common.metric; | ||
|
||
import io.micrometer.core.instrument.MeterRegistry; | ||
import io.micrometer.core.instrument.Timer; | ||
|
||
import java.time.Duration; | ||
|
||
public class BrokerMetrics { | ||
private final MeterRegistry meterRegistry; | ||
|
||
public BrokerMetrics(MeterRegistry meterRegistry) { | ||
this.meterRegistry = meterRegistry; | ||
} | ||
|
||
public void recordBrokerLatency(String broker, Duration duration) { | ||
Timer.builder("broker.latency") | ||
.tag("broker", broker) | ||
.publishPercentileHistogram() | ||
.maximumExpectedValue(Duration.ofSeconds(5)) | ||
.register(meterRegistry) | ||
.record(duration); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
.../main/java/pl/allegro/tech/hermes/frontend/config/BrokerLatencyReporterConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package pl.allegro.tech.hermes.frontend.config; | ||
|
||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import pl.allegro.tech.hermes.common.metric.MetricsFacade; | ||
import pl.allegro.tech.hermes.common.metric.executor.InstrumentedExecutorServiceFactory; | ||
import pl.allegro.tech.hermes.frontend.producer.BrokerLatencyReporter; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
|
||
|
||
@Configuration | ||
@EnableConfigurationProperties(BrokerLatencyReporterProperties.class) | ||
public class BrokerLatencyReporterConfiguration { | ||
|
||
@Bean | ||
BrokerLatencyReporter brokerLatencyReporter(BrokerLatencyReporterProperties properties, | ||
MetricsFacade metricsFacade, | ||
InstrumentedExecutorServiceFactory executorServiceFactory) { | ||
ExecutorService executorService = executorServiceFactory.getExecutorService( | ||
"broker-latency-reporter", | ||
8, | ||
true, | ||
1_000_000 | ||
); | ||
|
||
return new BrokerLatencyReporter( | ||
properties.isEnabled(), | ||
metricsFacade, | ||
properties.getSlowResponseLoggingThreshold(), | ||
executorService | ||
); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...src/main/java/pl/allegro/tech/hermes/frontend/config/BrokerLatencyReporterProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package pl.allegro.tech.hermes.frontend.config; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
import java.time.Duration; | ||
|
||
@ConfigurationProperties(prefix = "frontend.broker-latency-reporter") | ||
public class BrokerLatencyReporterProperties { | ||
private boolean enabled; | ||
private Duration slowResponseLoggingThreshold = Duration.ofMillis(100); | ||
|
||
|
||
public boolean isEnabled() { | ||
return enabled; | ||
} | ||
|
||
public void setEnabled(boolean enabled) { | ||
this.enabled = enabled; | ||
} | ||
|
||
public Duration getSlowResponseLoggingThreshold() { | ||
return slowResponseLoggingThreshold; | ||
} | ||
|
||
public void setSlowResponseLoggingThreshold(Duration slowResponseLoggingThreshold) { | ||
this.slowResponseLoggingThreshold = slowResponseLoggingThreshold; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...rontend/src/main/java/pl/allegro/tech/hermes/frontend/producer/BrokerLatencyReporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package pl.allegro.tech.hermes.frontend.producer; | ||
|
||
import jakarta.annotation.Nullable; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import pl.allegro.tech.hermes.api.Topic; | ||
import pl.allegro.tech.hermes.common.metric.MetricsFacade; | ||
import pl.allegro.tech.hermes.frontend.publishing.message.Message; | ||
import pl.allegro.tech.hermes.frontend.publishing.metadata.ProduceMetadata; | ||
import pl.allegro.tech.hermes.metrics.HermesTimerContext; | ||
|
||
import java.time.Duration; | ||
import java.util.Optional; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.function.Supplier; | ||
|
||
public class BrokerLatencyReporter { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(BrokerLatencyReporter.class); | ||
|
||
private final boolean perBrokerLatencyEnabled; | ||
private final MetricsFacade metricsFacade; | ||
private final Duration slowResponseThreshold; | ||
private final ExecutorService reporterExecutorService; | ||
|
||
public BrokerLatencyReporter(boolean perBrokerLatencyEnabled, | ||
MetricsFacade metricsFacade, | ||
Duration slowResponseThreshold, | ||
ExecutorService reporterExecutorService) { | ||
this.perBrokerLatencyEnabled = perBrokerLatencyEnabled; | ||
this.metricsFacade = metricsFacade; | ||
this.slowResponseThreshold = slowResponseThreshold; | ||
this.reporterExecutorService = reporterExecutorService; | ||
} | ||
|
||
public void report(HermesTimerContext timerContext, | ||
Message message, | ||
Topic.Ack ack, | ||
@Nullable Supplier<ProduceMetadata> produceMetadata) { | ||
Duration duration = timerContext.closeAndGet(); | ||
if (perBrokerLatencyEnabled) { | ||
reporterExecutorService.submit(() -> doReport(duration, message.getId(), ack, produceMetadata)); | ||
} | ||
|
||
} | ||
|
||
private void doReport(Duration duration, | ||
String messageId, | ||
Topic.Ack ack, | ||
@Nullable Supplier<ProduceMetadata> produceMetadata) { | ||
String broker = Optional.ofNullable(produceMetadata).flatMap(metadata -> metadata.get().getBroker()).orElse("unknown"); | ||
|
||
if (duration.compareTo(slowResponseThreshold) > 0) { | ||
logger.info("Slow produce request, broker response time: {} ms, ackLevel: {}, messageId: {}, broker: {}", | ||
duration.toMillis(), ack, messageId, broker); | ||
} | ||
|
||
metricsFacade.broker().recordBrokerLatency(broker, duration); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.