Skip to content

Commit

Permalink
resolve allegro#1738 | fix metrics for http batch client
Browse files Browse the repository at this point in the history
  • Loading branch information
SkySurferOne committed Oct 19, 2023
1 parent e71bb1e commit 8eca2b2
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ public HermesTimer http1SerialClientRequestQueueWaitingTimer() {
);
}

public HermesTimer http1BatchClientRequestQueueWaitingTimer() {
return HermesTimer.from(
meterRegistry.timer(Timers.CONSUMER_SENDER_HTTP_1_BATCH_CLIENT_REQUEST_QUEUE_WAITING_TIME),
hermesMetrics.timer(Timers.CONSUMER_SENDER_HTTP_1_BATCH_CLIENT_REQUEST_QUEUE_WAITING_TIME)
);
}

public HermesTimer http2SerialClientRequestQueueWaitingTimer() {
return HermesTimer.from(
meterRegistry.timer(Timers.CONSUMER_SENDER_HTTP_2_SERIAL_CLIENT_REQUEST_QUEUE_WAITING_TIME),
Expand All @@ -89,6 +96,13 @@ public HermesTimer http1SerialClientRequestProcessingTimer() {
);
}

public HermesTimer http1BatchClientRequestProcessingTimer() {
return HermesTimer.from(
meterRegistry.timer(Timers.CONSUMER_SENDER_HTTP_1_BATCH_CLIENT_REQUEST_PROCESSING_TIME),
hermesMetrics.timer(Timers.CONSUMER_SENDER_HTTP_1_BATCH_CLIENT_REQUEST_PROCESSING_TIME)
);
}

public HermesTimer http2SerialClientRequestProcessingTimer() {
return HermesTimer.from(
meterRegistry.timer(Timers.CONSUMER_SENDER_HTTP_2_SERIAL_CLIENT_REQUEST_PROCESSING_TIME),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ public class Timers {
"http-clients.serial.http2.request-queue-waiting-time";
public static final String CONSUMER_SENDER_HTTP_2_SERIAL_CLIENT_REQUEST_PROCESSING_TIME =
"http-clients.serial.http2.request-processing-time";
public static final String CONSUMER_SENDER_HTTP_1_BATCH_CLIENT_REQUEST_QUEUE_WAITING_TIME =
"http-clients.batch.http1.request-queue-waiting-time";
public static final String CONSUMER_SENDER_HTTP_1_BATCH_CLIENT_REQUEST_PROCESSING_TIME =
"http-clients.batch.http1.request-processing-time";
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import pl.allegro.tech.hermes.consumers.consumer.sender.http.HttpHeadersProvidersFactory;
import pl.allegro.tech.hermes.consumers.consumer.sender.http.HttpMessageBatchSenderFactory;
import pl.allegro.tech.hermes.consumers.consumer.sender.http.HttpRequestFactoryProvider;
import pl.allegro.tech.hermes.consumers.consumer.sender.http.JettyHttpClientMetrics;
import pl.allegro.tech.hermes.consumers.consumer.sender.http.JettyHttpMessageSenderProvider;
import pl.allegro.tech.hermes.consumers.consumer.sender.http.SendingResultHandlers;
import pl.allegro.tech.hermes.consumers.consumer.sender.http.SslContextFactoryProvider;
Expand All @@ -52,6 +53,7 @@
import pl.allegro.tech.hermes.consumers.consumer.sender.resolver.InterpolatingEndpointAddressResolver;
import pl.allegro.tech.hermes.consumers.consumer.sender.timeout.FutureAsyncTimeout;
import pl.allegro.tech.hermes.consumers.consumer.trace.MetadataAppender;
import pl.allegro.tech.hermes.metrics.HermesTimer;

import java.io.IOException;
import java.util.List;
Expand All @@ -77,8 +79,18 @@ public Http1ClientProperties http1SerialClientProperties() {
@Bean(name = "http1-serial-client")
public HttpClient http1SerialClient(
HttpClientsFactory httpClientsFactory,
@Named("http1-serial-client-parameters") Http1ClientParameters http1ClientParameters) {
return httpClientsFactory.createClientForHttp1("jetty-http1-serial-client", http1ClientParameters);
@Named("http1-serial-client-parameters") Http1ClientParameters http1ClientParameters,
MetricsFacade metricsFacade
) {
HttpClient client = httpClientsFactory.createClientForHttp1("jetty-http1-serial-client", http1ClientParameters);
if (http1ClientParameters.isRequestProcessingMonitoringEnabled()) {
var metrics = metricsFacade.consumerSender();
enrichWithMetrics(
client, metrics.http1SerialClientRequestQueueWaitingTimer(),
metrics.http1SerialClientRequestProcessingTimer()
);
}
return client;
}

@Bean(name = "http2-serial-client-parameters")
Expand All @@ -90,11 +102,21 @@ public Http2ClientProperties http2SerialClientProperties() {
@Bean
public Http2ClientHolder http2ClientHolder(
HttpClientsFactory httpClientsFactory,
@Named("http2-serial-client-parameters") Http2ClientProperties http2ClientProperties) {
@Named("http2-serial-client-parameters") Http2ClientProperties http2ClientProperties,
MetricsFacade metricsFacade
) {
if (!http2ClientProperties.isEnabled()) {
return new Http2ClientHolder(null);
} else {
return new Http2ClientHolder(httpClientsFactory.createClientForHttp2("jetty-http2-serial-client", http2ClientProperties));
HttpClient client = httpClientsFactory.createClientForHttp2("jetty-http2-serial-client", http2ClientProperties);
if (http2ClientProperties.isRequestProcessingMonitoringEnabled()) {
var metrics = metricsFacade.consumerSender();
enrichWithMetrics(
client, metrics.http2SerialClientRequestQueueWaitingTimer(),
metrics.http2SerialClientRequestProcessingTimer()
);
}
return new Http2ClientHolder(client);
}
}

Expand All @@ -107,8 +129,17 @@ public Http1ClientProperties http1BatchClientProperties() {
@Bean(name = "http1-batch-client")
public HttpClient http1BatchClient(
HttpClientsFactory httpClientsFactory,
@Named("http1-batch-client-parameters") Http1ClientParameters http1ClientParameters) {
return httpClientsFactory.createClientForHttp1("jetty-http1-batch-client", http1ClientParameters);
@Named("http1-batch-client-parameters") Http1ClientParameters http1ClientParameters,
MetricsFacade metricsFacade) {
HttpClient client = httpClientsFactory.createClientForHttp1("jetty-http1-batch-client", http1ClientParameters);
if (http1ClientParameters.isRequestProcessingMonitoringEnabled()) {
var metrics = metricsFacade.consumerSender();
enrichWithMetrics(
client, metrics.http1BatchClientRequestQueueWaitingTimer(),
metrics.http1BatchClientRequestProcessingTimer()
);
}
return client;
}

@Bean(name = "oauth-http-client")
Expand Down Expand Up @@ -197,9 +228,8 @@ public HttpHeadersProvidersFactory emptyHttpHeadersProvidersFactory() {

@Bean
public HttpClientsFactory httpClientsFactory(InstrumentedExecutorServiceFactory executorFactory,
SslContextFactoryProvider sslContextFactoryProvider,
MetricsFacade metricsFacade) {
return new HttpClientsFactory(executorFactory, sslContextFactoryProvider, metricsFacade.consumerSender());
SslContextFactoryProvider sslContextFactoryProvider) {
return new HttpClientsFactory(executorFactory, sslContextFactoryProvider);
}

@Bean
Expand Down Expand Up @@ -270,4 +300,12 @@ public FutureAsyncTimeout futureAsyncTimeoutFactory(InstrumentedExecutorServiceF
);
return new FutureAsyncTimeout(timeoutExecutorService);
}

private static void enrichWithMetrics(
HttpClient client, HermesTimer requestQueueWaitingTimer, HermesTimer requestProcessingTimer
) {
client.getRequestListeners().add(
new JettyHttpClientMetrics(requestQueueWaitingTimer, requestProcessingTimer)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,12 @@ public class HttpClientsFactory {
private final InstrumentedExecutorServiceFactory executorFactory;
private final SslContextFactoryProvider sslContextFactoryProvider;

private final ConsumerSenderMetrics consumerSenderMetrics;


public HttpClientsFactory(
InstrumentedExecutorServiceFactory executorFactory,
SslContextFactoryProvider sslContextFactoryProvider,
ConsumerSenderMetrics consumerSenderMetrics
SslContextFactoryProvider sslContextFactoryProvider
) {
this.executorFactory = executorFactory;
this.sslContextFactoryProvider = sslContextFactoryProvider;
this.consumerSenderMetrics = consumerSenderMetrics;
}

public HttpClient createClientForHttp1(String name, Http1ClientParameters http1ClientParameters) {
Expand All @@ -47,14 +42,6 @@ public HttpClient createClientForHttp1(String name, Http1ClientParameters http1C
client.setIdleTimeout(http1ClientParameters.getIdleTimeout().toMillis());
client.setFollowRedirects(http1ClientParameters.isFollowRedirectsEnabled());
client.setConnectTimeout(http1ClientParameters.getConnectionTimeout().toMillis());
if (http1ClientParameters.isRequestProcessingMonitoringEnabled()) {
client.getRequestListeners().add(
new JettyHttpClientMetrics(
consumerSenderMetrics.http1SerialClientRequestQueueWaitingTimer(),
consumerSenderMetrics.http1SerialClientRequestProcessingTimer()
)
);
}
return client;
}

Expand All @@ -81,14 +68,6 @@ public HttpClient createClientForHttp2(String name, Http2ClientParameters http2C
client.setIdleTimeout(http2ClientParameters.getIdleTimeout().toMillis());
client.setFollowRedirects(http2ClientParameters.isFollowRedirectsEnabled());
client.setConnectTimeout(http2ClientParameters.getConnectionTimeout().toMillis());
if (http2ClientParameters.isRequestProcessingMonitoringEnabled()) {
client.getRequestListeners().add(
new JettyHttpClientMetrics(
consumerSenderMetrics.http2SerialClientRequestQueueWaitingTimer(),
consumerSenderMetrics.http2SerialClientRequestProcessingTimer()
)
);
}
return client;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class HttpClientConnectionMonitoringTest extends Specification {
ConsumerSenderConfiguration consumerConfiguration = new ConsumerSenderConfiguration();
client = consumerConfiguration.http1SerialClient(new HttpClientsFactory(
new InstrumentedExecutorServiceFactory(threadPoolMetrics),
sslContextFactoryProvider, metrics.consumerSender()), new Http1ClientProperties()
sslContextFactoryProvider), new Http1ClientProperties(), metrics
)
batchClient = Mock(HttpClient)
client.start()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ public static void setupEnvironment() throws Exception {
new InstrumentedExecutorServiceFactory(
new ThreadPoolMetrics(TestMetricsFacadeFactory.create())
),
sslContextFactoryProvider, metricsFacade.consumerSender()),
new Http1ClientProperties()
sslContextFactoryProvider),
new Http1ClientProperties(),
metricsFacade
);
client.start();
}
Expand Down

0 comments on commit 8eca2b2

Please sign in to comment.