-
Notifications
You must be signed in to change notification settings - Fork 118
add slow log #328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
marsishandsome
merged 5 commits into
tikv:release-3.1
from
marsishandsome:feature/add-slow-log
Nov 19, 2021
Merged
add slow log #328
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.