Skip to content

Commit

Permalink
feat: metric for coverage handle (#183)
Browse files Browse the repository at this point in the history
* feat: metric for coverage handle
  • Loading branch information
QizhengMo authored Mar 12, 2024
1 parent 7764477 commit 93d129d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,4 @@ public void recordInvalidIncompleteCount(InvalidIncompleteRecordRequest request)
metricListener.recordMatchingCount(METRIC_NAME, tags);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.arextest.model.mock.Mocker;
import com.arextest.model.mock.Mocker.Target;
import com.arextest.model.scenepool.Scene;
import com.arextest.storage.metric.MetricListener;
import com.arextest.storage.repository.ProviderNames;
import com.arextest.storage.repository.RepositoryProvider;
import com.arextest.storage.repository.RepositoryProviderFactory;
Expand All @@ -15,11 +16,15 @@
import com.arextest.storage.repository.scenepool.ScenePoolProviderImpl;
import com.arextest.storage.service.MockSourceEditionService;
import com.arextest.storage.trace.MDCTracer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

Expand All @@ -33,6 +38,12 @@ public class CoverageMockerHandler implements MockerSaveHandler {
private CacheProvider cacheProvider;
private ScheduledExecutorService coverageHandleDelayedPool;
private ScenePoolFactory scenePoolFactory;
public final List<MetricListener> metricListeners;
// coverage metric constants
private static final String COVERAGE_METRIC_NAME = "coverage.recording";
private static final String COVERAGE_OP_TAG_KEY = "operation";
private static final String COVERAGE_APP_TAG_KEY = "clientAppId";


@Override
public MockCategoryType getMockCategoryType() {
Expand Down Expand Up @@ -160,6 +171,9 @@ private class RecordTask implements Runnable {
private final ScenePoolProvider scenePoolProvider;
private final Mocker coverageMocker;
private static final long EXPIRATION_EXTENSION_DAYS = 14L;
private static final String NEW_SCENE_OP = "NEW_SCENE";
private static final String EXISTING_SCENE_OP = "EXISTING_SCENE";

@Override
public void run() {
try {
Expand All @@ -168,7 +182,7 @@ public void run() {
String recordId = coverageMocker.getRecordId();
String executionPath = Optional.ofNullable(coverageMocker.getTargetResponse()).map(
Target::getBody).orElse(null);

String op = NEW_SCENE_OP;
MDCTracer.addAppId(appId);
MDCTracer.addRecordId(recordId);

Expand All @@ -178,6 +192,7 @@ public void run() {
LOGGER.info("CoverageMockerHandler received existing case, recordId: {}, pathKey: {}",
coverageMocker.getRecordId(), coverageMocker.getOperationName());
} else {
op = EXISTING_SCENE_OP;
// new scene: extend mocker expiration and insert scene
Scene scene = new Scene();
scene.setSceneKey(sceneKey);
Expand All @@ -191,11 +206,25 @@ public void run() {
LOGGER.info("CoverageMockerHandler received new case, recordId: {}, pathKey: {}",
coverageMocker.getRecordId(), coverageMocker.getOperationName());
}

recordCoverageHandle(appId, op);
} catch (Exception e) {
LOGGER.error("CoverageMockerHandler handle error", e);
} finally {
MDCTracer.clear();
}
}
}

private void recordCoverageHandle(String appId, String op) {
if (CollectionUtils.isEmpty(metricListeners)) {
return;
}
Map<String, String> tags = new HashMap<>(2);
tags.put(COVERAGE_APP_TAG_KEY, appId);
tags.put(COVERAGE_OP_TAG_KEY, op);
for (MetricListener metricListener : metricListeners) {
metricListener.recordMatchingCount(COVERAGE_METRIC_NAME, tags);
}
}
}

0 comments on commit 93d129d

Please sign in to comment.