log SlowLog if error occures (#361)

This commit is contained in:
Liangliang Gu 2021-12-06 15:38:25 +08:00 committed by GitHub
parent b61b9cd938
commit f12d19d0a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 1 deletions

View File

@ -22,5 +22,7 @@ public interface SlowLog {
SlowLogSpan start(String name);
void setError(Throwable err);
void log();
}

View File

@ -30,6 +30,9 @@ public class SlowLogEmptyImpl implements SlowLog {
return SlowLogSpanEmptyImpl.INSTANCE;
}
@Override
public void setError(Throwable err) {}
@Override
public void log() {}
}

View File

@ -35,6 +35,7 @@ public class SlowLogImpl implements SlowLog {
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("HH:mm:ss.SSS");
private final List<SlowLogSpan> slowLogSpans = new ArrayList<>();
private Throwable error = null;
private final long startMS;
private final long slowThresholdMS;
@ -63,10 +64,15 @@ public class SlowLogImpl implements SlowLog {
return slowLogSpan;
}
@Override
public void setError(Throwable err) {
this.error = err;
}
@Override
public void log() {
long currentMS = System.currentTimeMillis();
if (slowThresholdMS >= 0 && currentMS - startMS > slowThresholdMS) {
if (error != null || (slowThresholdMS >= 0 && currentMS - startMS > slowThresholdMS)) {
logger.warn("SlowLog:" + getSlowLogString(currentMS));
}
}
@ -77,6 +83,9 @@ public class SlowLogImpl implements SlowLog {
jsonObject.addProperty("start", DATE_FORMAT.format(startMS));
jsonObject.addProperty("end", DATE_FORMAT.format(currentMS));
jsonObject.addProperty("duration", (currentMS - startMS) + "ms");
if (error != null) {
jsonObject.addProperty("error", error.getMessage());
}
for (Map.Entry<String, String> entry : properties.entrySet()) {
jsonObject.addProperty(entry.getKey(), entry.getValue());

View File

@ -151,6 +151,7 @@ public class RawKVClient implements AutoCloseable {
}
} catch (Exception e) {
RAW_REQUEST_FAILURE.labels(label).inc();
slowLog.setError(e);
throw e;
} finally {
requestTimer.observeDuration();
@ -249,6 +250,7 @@ public class RawKVClient implements AutoCloseable {
}
} catch (Exception e) {
RAW_REQUEST_FAILURE.labels(label).inc();
slowLog.setError(e);
throw e;
} finally {
requestTimer.observeDuration();
@ -291,6 +293,7 @@ public class RawKVClient implements AutoCloseable {
RAW_REQUEST_SUCCESS.labels(label).inc();
} catch (Exception e) {
RAW_REQUEST_FAILURE.labels(label).inc();
slowLog.setError(e);
throw e;
} finally {
requestTimer.observeDuration();
@ -333,6 +336,7 @@ public class RawKVClient implements AutoCloseable {
}
} catch (Exception e) {
RAW_REQUEST_FAILURE.labels(label).inc();
slowLog.setError(e);
throw e;
} finally {
requestTimer.observeDuration();
@ -367,6 +371,7 @@ public class RawKVClient implements AutoCloseable {
return result;
} catch (Exception e) {
RAW_REQUEST_FAILURE.labels(label).inc();
slowLog.setError(e);
throw e;
} finally {
requestTimer.observeDuration();
@ -400,6 +405,7 @@ public class RawKVClient implements AutoCloseable {
return;
} catch (Exception e) {
RAW_REQUEST_FAILURE.labels(label).inc();
slowLog.setError(e);
throw e;
} finally {
requestTimer.observeDuration();
@ -442,6 +448,7 @@ public class RawKVClient implements AutoCloseable {
}
} catch (Exception e) {
RAW_REQUEST_FAILURE.labels(label).inc();
slowLog.setError(e);
throw e;
} finally {
requestTimer.observeDuration();
@ -589,6 +596,7 @@ public class RawKVClient implements AutoCloseable {
return result;
} catch (Exception e) {
RAW_REQUEST_FAILURE.labels(label).inc();
slowLog.setError(e);
throw e;
} finally {
requestTimer.observeDuration();
@ -677,6 +685,7 @@ public class RawKVClient implements AutoCloseable {
return result;
} catch (Exception e) {
RAW_REQUEST_FAILURE.labels(label).inc();
slowLog.setError(e);
throw e;
} finally {
requestTimer.observeDuration();
@ -745,6 +754,7 @@ public class RawKVClient implements AutoCloseable {
}
} catch (Exception e) {
RAW_REQUEST_FAILURE.labels(label).inc();
slowLog.setError(e);
throw e;
} finally {
requestTimer.observeDuration();