diff --git a/validator/client/src/main/java/tech/pegasys/teku/validator/client/ValidatorClientService.java b/validator/client/src/main/java/tech/pegasys/teku/validator/client/ValidatorClientService.java index 3b01c0e5f10..fb8131d3ae4 100644 --- a/validator/client/src/main/java/tech/pegasys/teku/validator/client/ValidatorClientService.java +++ b/validator/client/src/main/java/tech/pegasys/teku/validator/client/ValidatorClientService.java @@ -404,18 +404,19 @@ private void scheduleValidatorsDuties( validatorTimingChannels.add(validatorStatusProvider); final OwnedValidators validators = validatorLoader.getOwnedValidators(); final BlockContainerSigner blockContainerSigner = new MilestoneBasedBlockContainerSigner(spec); + final ValidatorDutyMetrics validatorDutyMetrics = ValidatorDutyMetrics.create(metricsSystem); final BlockDutyFactory blockDutyFactory = new BlockDutyFactory( forkProvider, validatorApiChannel, blockContainerSigner, config.getValidatorConfig().isBlindedBeaconBlocksEnabled(), - spec); + spec, + validatorDutyMetrics); final AttestationDutyFactory attestationDutyFactory = new AttestationDutyFactory(spec, forkProvider, validatorApiChannel); final BeaconCommitteeSubscriptions beaconCommitteeSubscriptions = new BeaconCommitteeSubscriptions(validatorApiChannel); - final ValidatorDutyMetrics validatorDutyMetrics = ValidatorDutyMetrics.create(metricsSystem); final DutyLoader attestationDutyLoader = new RetryingDutyLoader<>( asyncRunner, diff --git a/validator/client/src/main/java/tech/pegasys/teku/validator/client/duties/BlockDutyFactory.java b/validator/client/src/main/java/tech/pegasys/teku/validator/client/duties/BlockDutyFactory.java index 4123e9b5c9c..3ddea239e96 100644 --- a/validator/client/src/main/java/tech/pegasys/teku/validator/client/duties/BlockDutyFactory.java +++ b/validator/client/src/main/java/tech/pegasys/teku/validator/client/duties/BlockDutyFactory.java @@ -27,18 +27,21 @@ public class BlockDutyFactory implements DutyFactory private final BlockContainerSigner blockContainerSigner; private final Spec spec; private final boolean useBlindedBlock; + private final ValidatorDutyMetrics validatorDutyMetrics; public BlockDutyFactory( final ForkProvider forkProvider, final ValidatorApiChannel validatorApiChannel, final BlockContainerSigner blockContainerSigner, final boolean useBlindedBlock, - final Spec spec) { + final Spec spec, + final ValidatorDutyMetrics validatorDutyMetrics) { this.forkProvider = forkProvider; this.validatorApiChannel = validatorApiChannel; this.blockContainerSigner = blockContainerSigner; this.useBlindedBlock = useBlindedBlock; this.spec = spec; + this.validatorDutyMetrics = validatorDutyMetrics; } @Override @@ -50,7 +53,8 @@ public BlockProductionDuty createProductionDuty(final UInt64 slot, final Validat validatorApiChannel, blockContainerSigner, useBlindedBlock, - spec); + spec, + validatorDutyMetrics); } @Override diff --git a/validator/client/src/main/java/tech/pegasys/teku/validator/client/duties/BlockProductionDuty.java b/validator/client/src/main/java/tech/pegasys/teku/validator/client/duties/BlockProductionDuty.java index 26294bfce70..d0f55b184a5 100644 --- a/validator/client/src/main/java/tech/pegasys/teku/validator/client/duties/BlockProductionDuty.java +++ b/validator/client/src/main/java/tech/pegasys/teku/validator/client/duties/BlockProductionDuty.java @@ -44,6 +44,7 @@ public class BlockProductionDuty implements Duty { private final BlockContainerSigner blockContainerSigner; private final boolean useBlindedBlock; private final Spec spec; + private final ValidatorDutyMetrics validatorDutyMetrics; public BlockProductionDuty( final Validator validator, @@ -52,7 +53,8 @@ public BlockProductionDuty( final ValidatorApiChannel validatorApiChannel, final BlockContainerSigner blockContainerSigner, final boolean useBlindedBlock, - final Spec spec) { + final Spec spec, + final ValidatorDutyMetrics validatorDutyMetrics) { this.validator = validator; this.slot = slot; this.forkProvider = forkProvider; @@ -60,6 +62,7 @@ public BlockProductionDuty( this.blockContainerSigner = blockContainerSigner; this.useBlindedBlock = useBlindedBlock; this.spec = spec; + this.validatorDutyMetrics = validatorDutyMetrics; } @Override @@ -74,10 +77,18 @@ public SafeFuture performDuty() { } public SafeFuture produceBlock(final ForkInfo forkInfo) { + final String dutyType = getType().getType(); return createRandaoReveal(forkInfo) - .thenCompose(this::createUnsignedBlock) - .thenCompose(unsignedBlock -> signBlockContainer(forkInfo, unsignedBlock)) - .thenCompose(this::sendBlock) + .thenCompose( + signature -> + validatorDutyMetrics.record(() -> createUnsignedBlock(signature), dutyType, "create")) + .thenCompose( + unsignedBlock -> + validatorDutyMetrics.record( + () -> signBlockContainer(forkInfo, unsignedBlock), dutyType, "sign")) + .thenCompose( + signedBlockContainer -> + validatorDutyMetrics.record(() -> sendBlock(signedBlockContainer), dutyType, "send")) .exceptionally(error -> DutyResult.forError(validator.getPublicKey(), error)); } diff --git a/validator/client/src/main/java/tech/pegasys/teku/validator/client/duties/ValidatorDutyMetrics.java b/validator/client/src/main/java/tech/pegasys/teku/validator/client/duties/ValidatorDutyMetrics.java index 62e72c9b607..6f5f3049e17 100644 --- a/validator/client/src/main/java/tech/pegasys/teku/validator/client/duties/ValidatorDutyMetrics.java +++ b/validator/client/src/main/java/tech/pegasys/teku/validator/client/duties/ValidatorDutyMetrics.java @@ -13,6 +13,7 @@ package tech.pegasys.teku.validator.client.duties; +import java.util.function.Supplier; import org.hyperledger.besu.plugin.services.MetricsSystem; import org.hyperledger.besu.plugin.services.metrics.LabelledMetric; import org.hyperledger.besu.plugin.services.metrics.OperationTimer; @@ -38,9 +39,22 @@ public static ValidatorDutyMetrics create(final MetricsSystem metricsSystem) { } public SafeFuture performDutyWithMetrics(final Duty duty) { - final String dutyType = duty.getType().getType(); - final OperationTimer timer = dutyMetric.labels(dutyType, "total"); - final OperationTimer.TimingContext context = timer.startTimer(); + final OperationTimer.TimingContext context = startTimer(getDutyType(duty), "total"); return duty.performDuty().alwaysRun(context::stopTimer); } + + private OperationTimer.TimingContext startTimer(final String dutyType, final String step) { + final OperationTimer timer = dutyMetric.labels(dutyType, step); + return timer.startTimer(); + } + + private static String getDutyType(final Duty duty) { + return duty.getType().getType(); + } + + public SafeFuture record( + final Supplier> supplier, final String dutyType, final String step) { + final OperationTimer.TimingContext context = startTimer(dutyType, step); + return supplier.get().alwaysRun(context::stopTimer); + } } diff --git a/validator/client/src/test/java/tech/pegasys/teku/validator/client/duties/BlockProductionDutyTest.java b/validator/client/src/test/java/tech/pegasys/teku/validator/client/duties/BlockProductionDutyTest.java index bed7e18c777..4147fdd2671 100644 --- a/validator/client/src/test/java/tech/pegasys/teku/validator/client/duties/BlockProductionDutyTest.java +++ b/validator/client/src/test/java/tech/pegasys/teku/validator/client/duties/BlockProductionDutyTest.java @@ -104,7 +104,8 @@ public void setUp() { validatorApiChannel, blockContainerSigner, false, - spec); + spec, + mock(ValidatorDutyMetrics.class)); // todo fix when(forkProvider.getForkInfo(any())).thenReturn(completedFuture(fork)); } @@ -119,7 +120,8 @@ public void shouldCreateAndPublishBlock(final boolean isBlindedBlocksEnabled) { validatorApiChannel, blockContainerSigner, isBlindedBlocksEnabled, - spec); + spec, + mock(ValidatorDutyMetrics.class)); // todo fix final BLSSignature randaoReveal = dataStructureUtil.randomSignature(); final BLSSignature blockSignature = dataStructureUtil.randomSignature(); final BeaconBlock unsignedBlock; @@ -164,7 +166,8 @@ public void forDeneb_shouldCreateAndPublishBlockContents() { validatorApiChannel, blockContainerSigner, false, - spec); + spec, + mock(ValidatorDutyMetrics.class)); // todo fix final BLSSignature randaoReveal = dataStructureUtil.randomSignature(); final BLSSignature blockSignature = dataStructureUtil.randomSignature(); @@ -244,7 +247,8 @@ public void forDeneb_shouldCreateAndPublishBlindedBlockContents() { validatorApiChannel, blockContainerSigner, true, - spec); + spec, + mock(ValidatorDutyMetrics.class)); // todo fix final BLSSignature randaoReveal = dataStructureUtil.randomSignature(); final BLSSignature blockSignature = dataStructureUtil.randomSignature();