Skip to content

Commit 7c3b9fb

Browse files
committed
fix: replaced the stopwatch implementation with micrometer
1 parent f2c3125 commit 7c3b9fb

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

app/server/appsmith-git/src/main/java/com/appsmith/git/constants/CommonConstants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,9 @@ public class CommonConstants {
3535

3636
public static final String WIDGET_ID = "widgetId";
3737
public static final String PARENT_ID = "parentId";
38+
39+
public static final String GIT_SAVE_ARTIFACT = "git.saveArtifact";
40+
public static final String TIME_TAKEN_TO_SAVE_ARTIFACT = "Time taken to save Artifact";
41+
public static final String GIT_DESERIALIZE_ARTIFACT = "git.deserializeArtifact";
42+
public static final String TIME_TAKEN_TO_DESERIALIZE_ARTIFACT = "Time taken to deserialize Artifact from Git repo";
3843
}

app/server/appsmith-server/src/main/java/com/appsmith/server/helpers/ce/CommonGitFileUtilsCE.java

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.appsmith.external.constants.AnalyticsEvents;
44
import com.appsmith.external.git.FileInterface;
55
import com.appsmith.external.git.operations.FileOperations;
6-
import com.appsmith.external.helpers.Stopwatch;
6+
import com.appsmith.external.helpers.ObservationHelper;
77
import com.appsmith.external.models.ApplicationGitReference;
88
import com.appsmith.external.models.ArtifactGitReference;
99
import com.appsmith.external.models.BaseDomain;
@@ -25,6 +25,9 @@
2525
import com.google.gson.Gson;
2626
import com.google.gson.JsonElement;
2727
import com.google.gson.JsonObject;
28+
import io.micrometer.core.instrument.MeterRegistry;
29+
import io.micrometer.core.instrument.Timer;
30+
import io.micrometer.tracing.Span;
2831
import lombok.RequiredArgsConstructor;
2932
import lombok.extern.slf4j.Slf4j;
3033
import org.eclipse.jgit.api.errors.GitAPIException;
@@ -66,6 +69,8 @@ public class CommonGitFileUtilsCE {
6669
public final int INDEX_LOCK_FILE_STALE_TIME = 300;
6770

6871
private final JsonSchemaVersions jsonSchemaVersions;
72+
private final MeterRegistry meterRegistry;
73+
private final ObservationHelper observationHelper;
6974

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

120127
try {
121128
Mono<Path> repoPathMono = saveArtifactToLocalRepo(baseRepoSuffix, artifactExchangeJson, branchName);
122129
return Mono.zip(repoPathMono, sessionUserService.getCurrentUser()).flatMap(tuple -> {
123-
stopwatch.stopTimer();
130+
sample.stop(Timer.builder(CommonConstants.GIT_SAVE_ARTIFACT)
131+
.description(CommonConstants.TIME_TAKEN_TO_SAVE_ARTIFACT)
132+
.register(meterRegistry));
133+
observationHelper.endSpan(span, true);
124134
Path repoPath = tuple.getT1();
125135
// Path to repo will be : ./container-volumes/git-repo/workspaceId/defaultApplicationId/repoName/
126136
final Map<String, Object> data = Map.of(
127137
artifactConstant,
128138
repoPath.getParent().getFileName().toString(),
129139
FieldName.ORGANIZATION_ID,
130140
repoPath.getParent().getParent().getFileName().toString(),
131-
FieldName.FLOW_NAME,
132-
stopwatch.getFlow(),
133-
"executionTime",
134-
stopwatch.getExecutionTime());
141+
"executionTime", sample.toString());
135142
return analyticsService
136143
.sendEvent(
137144
AnalyticsEvents.UNIT_EXECUTION_TIME.getEventName(),
@@ -141,6 +148,7 @@ public Mono<Path> saveArtifactToLocalRepoWithAnalytics(
141148
});
142149
} catch (IOException | GitAPIException e) {
143150
log.error("Error occurred while saving files to local git repo: ", e);
151+
observationHelper.endSpan(span, false);
144152
throw Exceptions.propagate(e);
145153
}
146154
}
@@ -202,30 +210,36 @@ private void setDatasourcesInArtifactReference(
202210
public Mono<ArtifactExchangeJson> reconstructArtifactExchangeJsonFromGitRepoWithAnalytics(
203211
String workspaceId, String baseArtifactId, String repoName, String branchName, ArtifactType artifactType) {
204212

205-
Stopwatch stopwatch = new Stopwatch(AnalyticsEvents.GIT_DESERIALIZE_APP_RESOURCES_FROM_FILE.getEventName());
213+
Timer.Sample sample = Timer.start(meterRegistry);
214+
Span span = observationHelper.createSpan(AnalyticsEvents.GIT_DESERIALIZE_APP_RESOURCES_FROM_FILE.getEventName());
215+
observationHelper.startSpan(span, true);
206216
ArtifactGitFileUtils<?> artifactGitFileUtils = getArtifactBasedFileHelper(artifactType);
207217
Map<String, String> constantsMap = artifactGitFileUtils.getConstantsMap();
208218
return Mono.zip(
209219
reconstructArtifactExchangeJsonFromGitRepo(
210220
workspaceId, baseArtifactId, repoName, branchName, artifactType),
211221
sessionUserService.getCurrentUser())
212222
.flatMap(tuple -> {
213-
stopwatch.stopTimer();
223+
sample.stop(Timer.builder(CommonConstants.GIT_DESERIALIZE_ARTIFACT)
224+
.description(CommonConstants.TIME_TAKEN_TO_DESERIALIZE_ARTIFACT)
225+
.register(meterRegistry));
226+
observationHelper.endSpan(span, true);
214227
final Map<String, Object> data = Map.of(
215228
constantsMap.get(FieldName.ID),
216229
baseArtifactId,
217230
FieldName.ORGANIZATION_ID,
218231
workspaceId,
219-
FieldName.FLOW_NAME,
220-
stopwatch.getFlow(),
221-
"executionTime",
222-
stopwatch.getExecutionTime());
232+
"executionTime", sample.toString());
223233
return analyticsService
224234
.sendEvent(
225235
AnalyticsEvents.UNIT_EXECUTION_TIME.getEventName(),
226236
tuple.getT2().getUsername(),
227237
data)
228238
.thenReturn(tuple.getT1());
239+
})
240+
.doOnError(e -> {
241+
observationHelper.endSpan(span, false);
242+
log.error("Error deserializing artifact : {}", e.getMessage());
229243
});
230244
}
231245

0 commit comments

Comments
 (0)