-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jdbc): add metrics & logs to sql query
- Loading branch information
1 parent
746bcbd
commit 8a3bae4
Showing
4 changed files
with
57 additions
and
2 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
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
50 changes: 50 additions & 0 deletions
50
jdbc/src/main/java/io/kestra/jdbc/JooqExecuteListenerProvider.java
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,50 @@ | ||
package io.kestra.jdbc; | ||
|
||
import io.kestra.core.metrics.MetricRegistry; | ||
import jakarta.inject.Inject; | ||
import jakarta.inject.Named; | ||
import jakarta.inject.Singleton; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.jooq.ExecuteContext; | ||
import org.jooq.ExecuteListener; | ||
import org.jooq.impl.DefaultExecuteListener; | ||
|
||
import java.time.Duration; | ||
import javax.validation.constraints.NotNull; | ||
|
||
@Singleton | ||
@Named("default") | ||
@Slf4j | ||
public class JooqExecuteListenerProvider implements org.jooq.ExecuteListenerProvider { | ||
@Inject | ||
MetricRegistry metricRegistry; | ||
|
||
@Override | ||
public @NotNull ExecuteListener provide() { | ||
return new DefaultExecuteListener() { | ||
Long startTime; | ||
|
||
@Override | ||
public void executeStart(ExecuteContext ctx) { | ||
super.executeStart(ctx); | ||
startTime = System.currentTimeMillis(); | ||
} | ||
|
||
@Override | ||
public void executeEnd(ExecuteContext ctx) { | ||
super.executeEnd(ctx); | ||
|
||
Duration duration = Duration.ofMillis(System.currentTimeMillis() - startTime); | ||
|
||
metricRegistry.timer(MetricRegistry.JDBC_QUERY_DURATION, "sql", ctx.sql()) | ||
.record(duration); | ||
|
||
if (log.isTraceEnabled()) { | ||
log.trace("[Duration: {}] [Rows: {}] [Query: {}]", duration, ctx.rows() , ctx.query().toString()); | ||
} else if (log.isDebugEnabled()) { | ||
log.debug("[Duration: {}] [Rows: {}] [Query: {}]", duration, ctx.rows() , ctx.sql()); | ||
} | ||
} | ||
}; | ||
} | ||
} |
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