Skip to content

Commit

Permalink
fix: replaced the stopwatch implementation with micrometer
Browse files Browse the repository at this point in the history
  • Loading branch information
Sai6347 committed Nov 4, 2024
1 parent f2c3125 commit 7c3b9fb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ public class CommonConstants {

public static final String WIDGET_ID = "widgetId";
public static final String PARENT_ID = "parentId";

public static final String GIT_SAVE_ARTIFACT = "git.saveArtifact";
public static final String TIME_TAKEN_TO_SAVE_ARTIFACT = "Time taken to save Artifact";
public static final String GIT_DESERIALIZE_ARTIFACT = "git.deserializeArtifact";
public static final String TIME_TAKEN_TO_DESERIALIZE_ARTIFACT = "Time taken to deserialize Artifact from Git repo";
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.appsmith.external.constants.AnalyticsEvents;
import com.appsmith.external.git.FileInterface;
import com.appsmith.external.git.operations.FileOperations;
import com.appsmith.external.helpers.Stopwatch;
import com.appsmith.external.helpers.ObservationHelper;
import com.appsmith.external.models.ApplicationGitReference;
import com.appsmith.external.models.ArtifactGitReference;
import com.appsmith.external.models.BaseDomain;
Expand All @@ -25,6 +25,9 @@
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import io.micrometer.tracing.Span;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jgit.api.errors.GitAPIException;
Expand Down Expand Up @@ -66,6 +69,8 @@ public class CommonGitFileUtilsCE {
public final int INDEX_LOCK_FILE_STALE_TIME = 300;

private final JsonSchemaVersions jsonSchemaVersions;
private final MeterRegistry meterRegistry;
private final ObservationHelper observationHelper;

private ArtifactGitFileUtils<?> getArtifactBasedFileHelper(ArtifactType artifactType) {
if (ArtifactType.APPLICATION.equals(artifactType)) {
Expand Down Expand Up @@ -112,26 +117,28 @@ public Mono<Path> saveArtifactToLocalRepoWithAnalytics(
3. Save artifact to git repo
*/
// TODO: see if event needs to be generalised or kept specific
Stopwatch stopwatch = new Stopwatch(AnalyticsEvents.GIT_SERIALIZE_APP_RESOURCES_TO_LOCAL_FILE.getEventName());
Timer.Sample sample = Timer.start(meterRegistry);
Span span = observationHelper.createSpan(AnalyticsEvents.GIT_SERIALIZE_APP_RESOURCES_TO_LOCAL_FILE.getEventName());
observationHelper.startSpan(span, true);
ArtifactGitFileUtils<?> artifactGitFileUtils =
getArtifactBasedFileHelper(artifactExchangeJson.getArtifactJsonType());
String artifactConstant = artifactGitFileUtils.getConstantsMap().get(FieldName.ARTIFACT_CONTEXT);

try {
Mono<Path> repoPathMono = saveArtifactToLocalRepo(baseRepoSuffix, artifactExchangeJson, branchName);
return Mono.zip(repoPathMono, sessionUserService.getCurrentUser()).flatMap(tuple -> {
stopwatch.stopTimer();
sample.stop(Timer.builder(CommonConstants.GIT_SAVE_ARTIFACT)
.description(CommonConstants.TIME_TAKEN_TO_SAVE_ARTIFACT)
.register(meterRegistry));
observationHelper.endSpan(span, true);
Path repoPath = tuple.getT1();
// Path to repo will be : ./container-volumes/git-repo/workspaceId/defaultApplicationId/repoName/
final Map<String, Object> data = Map.of(
artifactConstant,
repoPath.getParent().getFileName().toString(),
FieldName.ORGANIZATION_ID,
repoPath.getParent().getParent().getFileName().toString(),
FieldName.FLOW_NAME,
stopwatch.getFlow(),
"executionTime",
stopwatch.getExecutionTime());
"executionTime", sample.toString());
return analyticsService
.sendEvent(
AnalyticsEvents.UNIT_EXECUTION_TIME.getEventName(),
Expand All @@ -141,6 +148,7 @@ public Mono<Path> saveArtifactToLocalRepoWithAnalytics(
});
} catch (IOException | GitAPIException e) {
log.error("Error occurred while saving files to local git repo: ", e);
observationHelper.endSpan(span, false);
throw Exceptions.propagate(e);
}
}
Expand Down Expand Up @@ -202,30 +210,36 @@ private void setDatasourcesInArtifactReference(
public Mono<ArtifactExchangeJson> reconstructArtifactExchangeJsonFromGitRepoWithAnalytics(
String workspaceId, String baseArtifactId, String repoName, String branchName, ArtifactType artifactType) {

Stopwatch stopwatch = new Stopwatch(AnalyticsEvents.GIT_DESERIALIZE_APP_RESOURCES_FROM_FILE.getEventName());
Timer.Sample sample = Timer.start(meterRegistry);
Span span = observationHelper.createSpan(AnalyticsEvents.GIT_DESERIALIZE_APP_RESOURCES_FROM_FILE.getEventName());
observationHelper.startSpan(span, true);
ArtifactGitFileUtils<?> artifactGitFileUtils = getArtifactBasedFileHelper(artifactType);
Map<String, String> constantsMap = artifactGitFileUtils.getConstantsMap();
return Mono.zip(
reconstructArtifactExchangeJsonFromGitRepo(
workspaceId, baseArtifactId, repoName, branchName, artifactType),
sessionUserService.getCurrentUser())
.flatMap(tuple -> {
stopwatch.stopTimer();
sample.stop(Timer.builder(CommonConstants.GIT_DESERIALIZE_ARTIFACT)
.description(CommonConstants.TIME_TAKEN_TO_DESERIALIZE_ARTIFACT)
.register(meterRegistry));
observationHelper.endSpan(span, true);
final Map<String, Object> data = Map.of(
constantsMap.get(FieldName.ID),
baseArtifactId,
FieldName.ORGANIZATION_ID,
workspaceId,
FieldName.FLOW_NAME,
stopwatch.getFlow(),
"executionTime",
stopwatch.getExecutionTime());
"executionTime", sample.toString());
return analyticsService
.sendEvent(
AnalyticsEvents.UNIT_EXECUTION_TIME.getEventName(),
tuple.getT2().getUsername(),
data)
.thenReturn(tuple.getT1());
})
.doOnError(e -> {
observationHelper.endSpan(span, false);
log.error("Error deserializing artifact : {}", e.getMessage());
});
}

Expand Down

0 comments on commit 7c3b9fb

Please sign in to comment.