-
Notifications
You must be signed in to change notification settings - Fork 438
Introducing Performance Loggers #2706
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
Open
machavan
wants to merge
5
commits into
main
Choose a base branch
from
dev/machavan/perflogger
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,203
−827
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9e5b628
POC Perf Logger
machavan 678bed6
Added Log prefix to PerformanceLog
machavan eba9a83
Added callback handling logic and test case to validate logger behaviour
muskan124947 2a5d3d7
Addressed comments
muskan124947 33aad19
Updated token acquisition scope to onFedAuthInfo()
muskan124947 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
31 changes: 31 additions & 0 deletions
31
src/main/java/com/microsoft/sqlserver/jdbc/PerformanceActivity.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,31 @@ | ||
/* | ||
* Microsoft JDBC Driver for SQL Server Copyright(c) Microsoft Corporation All rights reserved. This program is made | ||
* available under the terms of the MIT License. See the LICENSE file in the project root for more information. | ||
*/ | ||
|
||
package com.microsoft.sqlserver.jdbc; | ||
|
||
/** | ||
* Enum representing different performance activities. | ||
*/ | ||
public enum PerformanceActivity { | ||
CONNECTION("Connection"), | ||
TOKEN_ACQUISITION("Token acquisition"), | ||
LOGIN("Login"), | ||
PRELOGIN("Prelogin"); | ||
|
||
private final String activity; | ||
|
||
PerformanceActivity(String activity) { | ||
this.activity = activity; | ||
} | ||
|
||
public String activity() { | ||
return activity; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return activity; | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
src/main/java/com/microsoft/sqlserver/jdbc/PerformanceLog.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,101 @@ | ||
/* | ||
* Microsoft JDBC Driver for SQL Server Copyright(c) Microsoft Corporation All rights reserved. This program is made | ||
* available under the terms of the MIT License. See the LICENSE file in the project root for more information. | ||
*/ | ||
|
||
package com.microsoft.sqlserver.jdbc; | ||
|
||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
class PerformanceLog { | ||
|
||
static final java.util.logging.Logger perfLoggerConnection = java.util.logging.Logger | ||
.getLogger("com.microsoft.sqlserver.jdbc.PerformanceMetrics.Connection"); | ||
|
||
private static PerformanceLogCallback callback; | ||
muskan124947 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private static boolean callbackInitialized = false; | ||
|
||
/** | ||
* Register a callback for performance log events. | ||
* | ||
* @param cb The callback to register. | ||
*/ | ||
public static synchronized void registerCallback(PerformanceLogCallback cb) { | ||
if (callbackInitialized) { | ||
throw new IllegalStateException("Callback has already been set"); | ||
} | ||
callback = cb; | ||
muskan124947 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
callbackInitialized = true; | ||
} | ||
|
||
/** | ||
* Unregister the callback for performance log events. | ||
*/ | ||
public static synchronized void unregisterCallback() { | ||
callback = null; | ||
callbackInitialized = false; | ||
} | ||
|
||
//TODO | ||
//More loggers to be added here e.g. com.microsoft.sqlserver.jdbc.PerformanceMetrics.Statement | ||
|
||
public static class Scope implements AutoCloseable { | ||
private Logger logger; | ||
private String logPrefix; | ||
private PerformanceActivity activity; | ||
private long startTime; | ||
private final boolean enabled; | ||
|
||
private Exception exception; | ||
|
||
public Scope(Logger logger, String logPrefix, PerformanceActivity activity) { | ||
|
||
// Check if logging is enabled | ||
this.enabled = logger.isLoggable(Level.INFO) || (callback != null); | ||
|
||
if (enabled) { | ||
this.logger = logger; | ||
this.logPrefix = logPrefix; | ||
this.activity = activity; | ||
this.startTime = System.currentTimeMillis(); | ||
} | ||
} | ||
|
||
public void setException(Exception e) { | ||
this.exception = e; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
|
||
if (!enabled) { | ||
return; | ||
} | ||
|
||
long endTime = System.currentTimeMillis(); | ||
muskan124947 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
long duration = endTime - startTime; | ||
|
||
if (callback != null) { | ||
try { | ||
callback.publish(duration, activity, exception); | ||
} catch (Exception e) { | ||
logger.info(String.format("Failed to publish performance log: %s", e.getMessage())); | ||
} | ||
} | ||
|
||
if (logger != null && logger.isLoggable(Level.INFO)) { | ||
if (exception != null) { | ||
logger.info(String.format("%s %s, duration: %dms, exception: %s", logPrefix, activity, duration, exception.getMessage())); | ||
} else { | ||
logger.info(String.format("%s %s, duration: %dms", logPrefix, activity, duration)); | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
public static Scope createScope(Logger logger, String logPrefix, PerformanceActivity activity) { | ||
return new Scope(logger, logPrefix, activity); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/microsoft/sqlserver/jdbc/PerformanceLogCallback.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,22 @@ | ||
/* | ||
* Microsoft JDBC Driver for SQL Server Copyright(c) Microsoft Corporation All rights reserved. This program is made | ||
* available under the terms of the MIT License. See the LICENSE file in the project root for more information. | ||
*/ | ||
|
||
package com.microsoft.sqlserver.jdbc; | ||
|
||
/** | ||
* Callback interface for publishing performance logs. | ||
*/ | ||
public interface PerformanceLogCallback { | ||
|
||
/** | ||
* Publish a performance log entry. | ||
* | ||
* @param durationMs The duration of the operation in milliseconds. | ||
* @param activity The type of activity being logged. | ||
* @param exception An exception, if an error occurred. | ||
*/ | ||
void publish(long durationMs, PerformanceActivity activity, Exception exception) throws Exception; | ||
|
||
} |
Oops, something went wrong.
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.