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

control-service: add MeterRegistry counters for DataJobsSynchronizer #2844

Merged
merged 5 commits into from
Oct 30, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.vmware.taurus.service.model.ActualDataJobDeployment;
import com.vmware.taurus.service.model.DataJob;
import com.vmware.taurus.service.model.DesiredDataJobDeployment;
import com.vmware.taurus.service.monitoring.DataJobSynchronizerMonitor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
Expand Down Expand Up @@ -49,6 +50,8 @@ public class DataJobsSynchronizer {

private final ThreadPoolTaskExecutor dataJobsSynchronizerTaskExecutor;

private final DataJobSynchronizerMonitor dataJobSynchronizerMonitor;

@Value("${datajobs.deployment.configuration.synchronization.task.enabled:false}")
private boolean synchronizationEnabled;

Expand Down Expand Up @@ -86,6 +89,7 @@ public void synchronizeDataJobs() {
"Skipping data job deployment synchronization because deployment names cannot be loaded"
+ " from Kubernetes.",
e);
dataJobSynchronizerMonitor.countSynchronizerFailures();
return;
}

Expand Down Expand Up @@ -210,8 +214,10 @@ private void waitForSynchronizationCompletion(CountDownLatch countDownLatch) {
+ " some time...");
countDownLatch.await();
log.info("Data job deployments synchronization has successfully completed.");
dataJobSynchronizerMonitor.countSuccessfulSynchronizerInvocation();
} catch (InterruptedException e) {
log.error("An error occurred during the data job deployments' synchronization", e);
dataJobSynchronizerMonitor.countSynchronizerFailures();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2021-2023 VMware, Inc.
* SPDX-License-Identifier: Apache-2.0
*/

package com.vmware.taurus.service.monitoring;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class DataJobSynchronizerMonitor {

public static final String DATAJOBS_SUCCESSFUL_SYNCHRONIZER_INVOCATIONS_COUNTER =
"vdk.deploy.datajob.synchronizer.successful.invocations.counter";
public static final String DATAJOBS_FAILED_SYNCHRONIZER_INVOCATIONS_COUNTER =
"vdk.deploy.datajob.synchronizer.failed.invocations.counter";

private final Counter successfulInvocationsCounter;
private final Counter failedInvocationsCounter;

@Autowired(required = true)
public DataJobSynchronizerMonitor(MeterRegistry meterRegistry) {
successfulInvocationsCounter =
Counter.builder(DATAJOBS_SUCCESSFUL_SYNCHRONIZER_INVOCATIONS_COUNTER)
.description(
"Counts the number of times the synchronizeDataJobs() method is called and"
+ " completes.")
.register(meterRegistry);

failedInvocationsCounter =
Counter.builder(DATAJOBS_FAILED_SYNCHRONIZER_INVOCATIONS_COUNTER)
.description(
"Counts the number of times the synchronizeDataJobs() method failed to finish.")
.register(meterRegistry);
}

/**
* Counts the number of times the DataJobSynchronizer's synchronize method was invoked and
* completed.
*/
public void countSuccessfulSynchronizerInvocation() {
incrementCounter(successfulInvocationsCounter);
}

/**
* Counts the number of failed data job deployment synchronizations invocations by the
* DataJobsSynchronizer due to K8S issues.
*/
public void countSynchronizerFailures() {
incrementCounter(failedInvocationsCounter);
}

private void incrementCounter(Counter counter) {
try {
counter.increment();
} catch (Exception e) {
log.warn("Error while trying to increment counter.", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2021-2023 VMware, Inc.
* SPDX-License-Identifier: Apache-2.0
*/

package com.vmware.taurus.service.monitoring;

import com.vmware.taurus.ControlplaneApplication;
import io.micrometer.core.instrument.MeterRegistry;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(classes = ControlplaneApplication.class)
public class DataJobSynchronizerMonitorTest {

@Autowired DataJobSynchronizerMonitor dataJobSynchronizerMonitor;

@Autowired private MeterRegistry meterRegistry;

@Test
public void testIncrementSuccessfulInvocations() {
var counter =
meterRegistry.counter(
DataJobSynchronizerMonitor.DATAJOBS_SUCCESSFUL_SYNCHRONIZER_INVOCATIONS_COUNTER);
Assertions.assertEquals(0.0, counter.count(), 0.001);
dataJobSynchronizerMonitor.countSuccessfulSynchronizerInvocation();

Assertions.assertEquals(1, counter.count(), 0.001);
}

@Test
public void testIncrementFailedInvocations() {
var counter =
meterRegistry.counter(
DataJobSynchronizerMonitor.DATAJOBS_FAILED_SYNCHRONIZER_INVOCATIONS_COUNTER);
Assertions.assertEquals(0.0, counter.count(), 0.001);
dataJobSynchronizerMonitor.countSynchronizerFailures();

Assertions.assertEquals(1, counter.count(), 0.001);
}
}