forked from tikv/client-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e74211
commit a881a65
Showing
17 changed files
with
571 additions
and
41 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* | ||
* Copyright 2021 PingCAP, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.tikv.common.log; | ||
|
||
public interface SlowLog { | ||
void addProperty(String key, String value); | ||
|
||
SlowLogSpan start(String name); | ||
|
||
void log(); | ||
} |
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,35 @@ | ||
/* | ||
* | ||
* Copyright 2021 PingCAP, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.tikv.common.log; | ||
|
||
public class SlowLogEmptyImpl implements SlowLog { | ||
public static final SlowLogEmptyImpl INSTANCE = new SlowLogEmptyImpl(); | ||
|
||
private SlowLogEmptyImpl() {} | ||
|
||
@Override | ||
public void addProperty(String key, String value) {} | ||
|
||
@Override | ||
public SlowLogSpan start(String name) { | ||
return SlowLogSpanEmptyImpl.INSTANCE; | ||
} | ||
|
||
@Override | ||
public void log() {} | ||
} |
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,93 @@ | ||
/* | ||
* | ||
* Copyright 2021 PingCAP, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.tikv.common.log; | ||
|
||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonObject; | ||
import java.text.SimpleDateFormat; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class SlowLogImpl implements SlowLog { | ||
private static final Logger logger = LoggerFactory.getLogger(SlowLogImpl.class); | ||
|
||
private static final int MAX_SPAN_SIZE = 1024; | ||
|
||
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("HH:mm:ss.SSS"); | ||
|
||
private final List<SlowLogSpan> slowLogSpans = new ArrayList<>(); | ||
|
||
private final long startMS; | ||
private final long slowThresholdMS; | ||
|
||
/** Key-Value pairs which will be logged, e.g. function name, key, region, etc. */ | ||
private final Map<String, String> properties; | ||
|
||
public SlowLogImpl(long slowThresholdMS, Map<String, String> properties) { | ||
this.startMS = System.currentTimeMillis(); | ||
this.slowThresholdMS = slowThresholdMS; | ||
this.properties = new HashMap<>(properties); | ||
} | ||
|
||
@Override | ||
public void addProperty(String key, String value) { | ||
this.properties.put(key, value); | ||
} | ||
|
||
@Override | ||
public synchronized SlowLogSpan start(String name) { | ||
SlowLogSpan slowLogSpan = new SlowLogSpanImpl(name); | ||
if (slowLogSpans.size() < MAX_SPAN_SIZE) { | ||
slowLogSpans.add(slowLogSpan); | ||
} | ||
slowLogSpan.start(); | ||
return slowLogSpan; | ||
} | ||
|
||
@Override | ||
public void log() { | ||
long currentMS = System.currentTimeMillis(); | ||
if (slowThresholdMS >= 0 && currentMS - startMS > slowThresholdMS) { | ||
logger.warn("SlowLog:" + getSlowLogString(currentMS)); | ||
} | ||
} | ||
|
||
private String getSlowLogString(long currentMS) { | ||
JsonObject jsonObject = new JsonObject(); | ||
|
||
jsonObject.addProperty("start", DATE_FORMAT.format(startMS)); | ||
jsonObject.addProperty("end", DATE_FORMAT.format(currentMS)); | ||
jsonObject.addProperty("duration", (currentMS - startMS) + "ms"); | ||
|
||
for (Map.Entry<String, String> entry : properties.entrySet()) { | ||
jsonObject.addProperty(entry.getKey(), entry.getValue()); | ||
} | ||
|
||
JsonArray jsonArray = new JsonArray(); | ||
for (SlowLogSpan slowLogSpan : slowLogSpans) { | ||
jsonArray.add(slowLogSpan.toJsonElement()); | ||
} | ||
jsonObject.add("spans", jsonArray); | ||
|
||
return jsonObject.toString(); | ||
} | ||
} |
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,28 @@ | ||
/* | ||
* | ||
* Copyright 2021 PingCAP, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.tikv.common.log; | ||
|
||
import com.google.gson.JsonElement; | ||
|
||
public interface SlowLogSpan { | ||
void start(); | ||
|
||
void end(); | ||
|
||
JsonElement toJsonElement(); | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/org/tikv/common/log/SlowLogSpanEmptyImpl.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,39 @@ | ||
/* | ||
* | ||
* Copyright 2021 PingCAP, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.tikv.common.log; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
|
||
public class SlowLogSpanEmptyImpl implements SlowLogSpan { | ||
|
||
public static final SlowLogSpanEmptyImpl INSTANCE = new SlowLogSpanEmptyImpl(); | ||
|
||
private SlowLogSpanEmptyImpl() {} | ||
|
||
@Override | ||
public void start() {} | ||
|
||
@Override | ||
public void end() {} | ||
|
||
@Override | ||
public JsonElement toJsonElement() { | ||
return new JsonObject(); | ||
} | ||
} |
Oops, something went wrong.