-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add trace execution elapsed time logic (#497)
Signed-off-by: Paolo Di Tommaso <paolo.ditommaso@gmail.com> Co-authored-by: Munish Chouhan <munish.chouhan@seqera.io>
- Loading branch information
1 parent
702658b
commit 9e25687
Showing
8 changed files
with
132 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/groovy/io/seqera/util/trace/TraceElapsedTime.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Wave, containers provisioning service | ||
* Copyright (c) 2023-2024, Seqera Labs | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package io.seqera.util.trace | ||
|
||
import java.lang.annotation.Documented | ||
import java.lang.annotation.Retention | ||
import java.lang.annotation.Target | ||
|
||
import io.micronaut.aop.Around | ||
import static java.lang.annotation.ElementType.METHOD | ||
import static java.lang.annotation.ElementType.TYPE | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME | ||
/** | ||
* When applied to a method or a class the elapsed time to carry out the method execution | ||
* is reported in the application log file | ||
* | ||
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com> | ||
*/ | ||
@Documented | ||
@Retention(RUNTIME) | ||
@Target([TYPE, METHOD]) | ||
@Around | ||
@interface TraceElapsedTime { | ||
String thresholdMillis() default '0' | ||
} |
74 changes: 74 additions & 0 deletions
74
src/main/groovy/io/seqera/util/trace/TraceElapsedTimeInterceptor.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Wave, containers provisioning service | ||
* Copyright (c) 2023-2024, Seqera Labs | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package io.seqera.util.trace | ||
|
||
import java.time.Duration | ||
|
||
import groovy.transform.CompileStatic | ||
import groovy.util.logging.Slf4j | ||
import io.micronaut.aop.InterceptorBean | ||
import io.micronaut.aop.MethodInterceptor | ||
import io.micronaut.aop.MethodInvocationContext | ||
import jakarta.inject.Singleton | ||
/** | ||
* Implements an method interceptor that logs the elapsed time to carry out the execution | ||
* of method invocation. | ||
* | ||
* This interceptor is applied to classes or methods marked in the {@link TraceElapsedTime} annotation | ||
* | ||
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com> | ||
*/ | ||
@CompileStatic | ||
@Slf4j | ||
@Singleton | ||
@InterceptorBean(TraceElapsedTime) | ||
class TraceElapsedTimeInterceptor implements MethodInterceptor<Object,Object> { | ||
|
||
@Override | ||
Object intercept(MethodInvocationContext<Object, Object> context) { | ||
// get the threshold value from the TraceElapsedTime annotation | ||
final annot = context.getAnnotation(TraceElapsedTime) | ||
final threshold = annot.intValue('thresholdMillis').orElse(0) | ||
|
||
// apply it | ||
Object result=null | ||
final begin = System.currentTimeMillis() | ||
try { | ||
return result = context.proceed() | ||
} | ||
finally { | ||
final delta = System.currentTimeMillis() - begin | ||
if( delta>=threshold ) { | ||
log.warn(msg(delta,context,result)) | ||
} | ||
} | ||
} | ||
|
||
static private String msg(long delta,MethodInvocationContext<Object, Object> context, Object result) { | ||
final method = context.getDeclaringType().getSimpleName() + '.' + context.getMethodName() | ||
def msg = "Slow method detected - elapsed time: ${Duration.ofMillis(delta)}" | ||
msg += "\n - name : ${method}" | ||
for( Map.Entry entry : context.getParameterValueMap() ) { | ||
msg += "\n - parameter: ${entry.key}=${entry.value}" | ||
} | ||
if( !context.getReturnType().isVoid() ) | ||
msg += "\n - result : ${result}" | ||
return msg | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters