-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
fix: replaced the stopwatch implementation with micrometer #37200
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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; | ||||||||||||||||||||
|
@@ -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; | ||||||||||||||||||||
|
@@ -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)) { | ||||||||||||||||||||
|
@@ -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()); | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use captured execution time instead of Replace Apply this diff: -"executionTime", sample.toString());
+"executionTime", /* captured execution time variable */);
|
||||||||||||||||||||
return analyticsService | ||||||||||||||||||||
.sendEvent( | ||||||||||||||||||||
AnalyticsEvents.UNIT_EXECUTION_TIME.getEventName(), | ||||||||||||||||||||
|
@@ -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); | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure span is ended even if an exception occurs The span should be ended in a Apply this diff: try {
// Existing code
} catch (IOException | GitAPIException e) {
log.error("Error occurred while saving files to local git repo: ", e);
- observationHelper.endSpan(span, false);
throw Exceptions.propagate(e);
+} finally {
+ observationHelper.endSpan(span, false);
}
|
||||||||||||||||||||
throw Exceptions.propagate(e); | ||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
|
@@ -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); | ||||||||||||||||||||
Comment on lines
+213
to
+215
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Activate span in the current context Use Apply this diff: Timer.Sample sample = Timer.start(meterRegistry);
Span span = observationHelper.createSpan(AnalyticsEvents.GIT_DESERIALIZE_APP_RESOURCES_FROM_FILE.getEventName());
-observationHelper.startSpan(span, true);
+try (Tracer.SpanInScope scope = tracer.withSpan(span)) {
// Existing code inside the try block
+}
|
||||||||||||||||||||
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()); | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace Use the execution time captured from the timer sample instead of calling Apply this diff: -"executionTime", sample.toString());
+"executionTime", /* captured execution time variable */);
|
||||||||||||||||||||
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()); | ||||||||||||||||||||
Comment on lines
+239
to
+242
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. End the span in a Ensure the span is ended even if an error occurs by using Apply this diff: -.doOnError(e -> {
- observationHelper.endSpan(span, false);
- log.error("Error deserializing artifact : {}", e.getMessage());
-});
+.doFinally(signalType -> {
+ boolean success = !signalType.equals(SignalType.ON_ERROR);
+ observationHelper.endSpan(span, success);
+}); 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||
}); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Properly manage span activation and deactivation
Ensure that the span is activated in the current context to correctly capture trace information. Use
try-with-resources
for automatic scope management.Apply this diff to improve span management: