@@ -14,58 +14,75 @@ public class TelemetryAspect {
1414
1515 private static final Tracer tracer = GlobalOpenTelemetry .getTracer ("appsmith" );
1616 private static final long DEFAULT_THRESHOLD_NS = 1_000_000L ;
17- private static final long thresholdNanoseconds = Long .parseLong (
18- System .getenv ().getOrDefault ("TELEMETRY_THRESHOLD_NANOSECONDS" , String .valueOf (DEFAULT_THRESHOLD_NS ))
19- );
17+ private static final long thresholdNanoseconds = loadThreshold ();
2018
2119 @ Pointcut ("execution(* org.eclipse.jgit..*(..))" )
22- public void allAppsmithServerMethods () {}
23-
24- @ Pointcut ("execution(* org.eclipse.jgit.telemetry..*(..))" )
25- public void excludedSupportPackages () {}
26-
27- @ Pointcut ("allAppsmithServerMethods() && !excludedSupportPackages()" )
28- public void monitoredAppsmithMethods () {}
29-
30- @ Around ("monitoredAppsmithMethods()" )
31- public Object traceAppsmithBusinessLogic (ProceedingJoinPoint joinPoint ) throws Throwable {
32- long startTime = System .nanoTime ();
20+ public void monitoredMethods () {}
3321
22+ @ Around ("monitoredMethods()" )
23+ public Object trace (ProceedingJoinPoint joinPoint ) throws Throwable {
24+ long start = System .nanoTime ();
3425 Object result ;
35- Throwable caughtException = null ;
26+ Throwable error = null ;
27+
3628 try {
3729 result = joinPoint .proceed ();
3830 } catch (Throwable t ) {
39- caughtException = t ;
31+ error = t ;
4032 throw t ;
4133 } finally {
42- long duration = System .nanoTime () - startTime ;
34+ long duration = System .nanoTime () - start ;
4335 if (duration > thresholdNanoseconds ) {
44- String spanName = getSpanName (joinPoint );
36+ recordTrace (joinPoint , duration , error );
37+ }
38+ }
4539
46- Span span = tracer .spanBuilder (spanName ).startSpan ();
40+ return result ;
41+ }
4742
48- try (Scope scope = span .makeCurrent ()) {
49- span .setAttribute ("execution.time.ns" , duration );
50- if (caughtException != null ) {
51- span .recordException (caughtException );
52- }
53- logSpan (span , spanName , duration );
54- } finally {
55- span .end ();
56- }
43+ private void recordTrace (ProceedingJoinPoint joinPoint , long duration , Throwable error ) {
44+ String spanName = getSpanName (joinPoint );
45+ Span span = tracer .spanBuilder (spanName ).startSpan ();
46+
47+ try (Scope scope = span .makeCurrent ()) {
48+ span .setAttribute ("execution.time.ns" , duration );
49+ if (error != null ) {
50+ span .recordException (error );
5751 }
52+ log (span , spanName , duration );
53+ } finally {
54+ span .end ();
5855 }
56+ }
5957
60- return result ;
58+ private void log (Span span , String spanName , long duration ) {
59+ System .out .println ("🚀 " +
60+ span .getSpanContext ().getTraceId () + " > " +
61+ span .getSpanContext ().getSpanId () + " > " +
62+ spanName + " took: " + duration + " ns" );
6163 }
6264
6365 private String getSpanName (ProceedingJoinPoint joinPoint ) {
64- return joinPoint .getSignature ().getDeclaringTypeName () + "." + joinPoint .getSignature ().getName ();
66+ return joinPoint .getSignature ().getDeclaringTypeName () + "." +
67+ joinPoint .getSignature ().getName ();
6568 }
6669
67- private void logSpan (Span span , String spanName , long duration ) {
68- System .out .println ("🚀 " + span .getSpanContext ().getTraceId () + " > "
69- + span .getSpanContext ().getSpanId () + " > " + spanName + " took: " + duration + " ns" );
70+ private static long loadThreshold () {
71+ String raw = System .getenv ("TELEMETRY_THRESHOLD_NANOSECONDS" );
72+
73+ if (raw == null ) {
74+ return DEFAULT_THRESHOLD_NS ;
75+ }
76+
77+ raw = raw .trim ();
78+ if (raw .isEmpty ()) {
79+ return DEFAULT_THRESHOLD_NS ;
80+ }
81+
82+ try {
83+ return Long .parseLong (raw );
84+ } catch (NumberFormatException e ) {
85+ return DEFAULT_THRESHOLD_NS ;
86+ }
7087 }
7188}
0 commit comments