pl.allegro.tech.hermes.common.schema.ReadMetricsTrackingRawSchemaClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hermes-common Show documentation
Show all versions of hermes-common Show documentation
Fast and reliable message broker built on top of Kafka.
package pl.allegro.tech.hermes.common.schema;
import pl.allegro.tech.hermes.api.RawSchema;
import pl.allegro.tech.hermes.api.RawSchemaWithMetadata;
import pl.allegro.tech.hermes.api.TopicName;
import pl.allegro.tech.hermes.common.metric.MetricsFacade;
import pl.allegro.tech.hermes.metrics.HermesTimer;
import pl.allegro.tech.hermes.metrics.HermesTimerContext;
import pl.allegro.tech.hermes.schema.RawSchemaClient;
import pl.allegro.tech.hermes.schema.SchemaId;
import pl.allegro.tech.hermes.schema.SchemaVersion;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
public class ReadMetricsTrackingRawSchemaClient implements RawSchemaClient {
private final RawSchemaClient rawSchemaClient;
private final MetricsFacade metricsFacade;
public ReadMetricsTrackingRawSchemaClient(
RawSchemaClient rawSchemaClient,
MetricsFacade metricsFacade) {
this.rawSchemaClient = rawSchemaClient;
this.metricsFacade = metricsFacade;
}
@Override
public Optional getRawSchemaWithMetadata(TopicName topic, SchemaVersion version) {
return timedSchema(() -> rawSchemaClient.getRawSchemaWithMetadata(topic, version));
}
@Override
public Optional getRawSchemaWithMetadata(TopicName topic, SchemaId schemaId) {
return timedSchema(() -> rawSchemaClient.getRawSchemaWithMetadata(topic, schemaId));
}
@Override
public Optional getLatestRawSchemaWithMetadata(TopicName topic) {
return timedSchema(() -> rawSchemaClient.getLatestRawSchemaWithMetadata(topic));
}
@Override
public List getVersions(TopicName topic) {
return timedVersions(() -> rawSchemaClient.getVersions(topic));
}
@Override
public void registerSchema(TopicName topic, RawSchema rawSchema) {
rawSchemaClient.registerSchema(topic, rawSchema);
}
@Override
public void deleteAllSchemaVersions(TopicName topic) {
rawSchemaClient.deleteAllSchemaVersions(topic);
}
@Override
public void validateSchema(TopicName topic, RawSchema rawSchema) {
rawSchemaClient.validateSchema(topic, rawSchema);
}
private T timedSchema(Supplier extends T> callable) {
return timed(callable, metricsFacade.schemaClient().schemaTimer());
}
private T timedVersions(Supplier extends T> callable) {
return timed(callable, metricsFacade.schemaClient().versionsTimer());
}
private T timed(Supplier extends T> callable, HermesTimer timer) {
try (HermesTimerContext time = timer.time()) {
return callable.get();
}
}
}