-
Notifications
You must be signed in to change notification settings - Fork 10
[Kotlin Sync] Monitoring > Cluster Monitoring #65
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
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
826abec
monitoring page
rachel-mack 00e71f2
rebase
rachel-mack ab48182
vaile
rachel-mack 99d7ec8
title change
rachel-mack d7dd4c8
NR feedback 1
rachel-mack 25df8ed
NR feedback 2
rachel-mack 790f5ca
update links
rachel-mack 19dfc01
final link updates
rachel-mack d2b111c
NR feedback
rachel-mack e473cf4
bullet formatting
rachel-mack 89120df
NH feedback
rachel-mack 7189dff
NH feedback
rachel-mack c74ddce
NH feedback
rachel-mack 7b12d20
NH feedback
rachel-mack 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import com.mongodb.kotlin.client.MongoClient | ||
import org.bson.Document | ||
import com.mongodb.MongoClientSettings | ||
import com.mongodb.ConnectionString | ||
import com.mongodb.management.JMXConnectionPoolListener | ||
|
||
fun main() { | ||
val uri = "<connection string uri>" | ||
|
||
// Instantiate your JMX listener | ||
val connectionPoolListener = JMXConnectionPoolListener() | ||
|
||
// Include the listener in your client settings | ||
val settings = MongoClientSettings.builder() | ||
.applyConnectionString(ConnectionString(uri)) | ||
.applyToConnectionPoolSettings { | ||
it.addConnectionPoolListener(connectionPoolListener) | ||
} | ||
.build() | ||
|
||
try { | ||
// Connect to your database | ||
val mongoClient = MongoClient.create(settings) | ||
val database = mongoClient.getDatabase("sample_mflix") | ||
val collection = database.getCollection<Document>("movies") | ||
collection.find().firstOrNull() | ||
collection.find().firstOrNull() | ||
println("Navigate to JConsole to see your connection pools...") | ||
|
||
// Pause execution | ||
Thread.sleep(Long.MAX_VALUE) | ||
mongoClient.close() | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
} | ||
} |
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,45 @@ | ||
import com.mongodb.kotlin.client.MongoClient | ||
import org.bson.Document | ||
import com.mongodb.event.* | ||
import com.mongodb.MongoClientSettings | ||
import com.mongodb.ConnectionString | ||
rachel-mack marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
class CommandCounter : CommandListener { | ||
private val commands = mutableMapOf<String, Int>() | ||
|
||
override fun commandSucceeded(event: CommandSucceededEvent) { | ||
nhachicha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val commandName = event.commandName | ||
val count = commands[commandName] ?: 0 | ||
commands[commandName] = count + 1 | ||
println(commands.toString()) | ||
} | ||
|
||
override fun commandFailed(event: CommandFailedEvent) { | ||
println("Failed execution of command '${event.commandName}' with id ${event.requestId}") | ||
} | ||
} | ||
|
||
|
||
fun main() { | ||
val uri = "<connection string uri>" | ||
|
||
// Instantiate your new listener | ||
val commandCounter = CommandCounter() | ||
|
||
// Include the listener in your client settings | ||
val settings = MongoClientSettings.builder() | ||
.applyConnectionString(ConnectionString(uri)) | ||
.addCommandListener(commandCounter) | ||
.build() | ||
|
||
// Connect to your database | ||
val mongoClient = MongoClient.create(settings) | ||
val database = mongoClient.getDatabase("sample_mflix") | ||
val collection = database.getCollection<Document>("movies") | ||
|
||
// Run some commands to test the counter | ||
collection.find().firstOrNull() | ||
collection.find().firstOrNull() | ||
|
||
mongoClient.close() | ||
} |
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,40 @@ | ||
import com.mongodb.kotlin.client.MongoClient | ||
import org.bson.Document | ||
import com.mongodb.event.* | ||
import com.mongodb.MongoClientSettings | ||
import com.mongodb.ConnectionString | ||
|
||
rachel-mack marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class ConnectionPoolLibrarian : ConnectionPoolListener { | ||
nhachicha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
override fun connectionCheckedOut(event: ConnectionCheckedOutEvent) { | ||
println("Let me get you the connection with id ${event.connectionId.localValue}...") | ||
} | ||
override fun connectionCheckOutFailed(event: ConnectionCheckOutFailedEvent) { | ||
println("Something went wrong! Failed to checkout connection.") | ||
} | ||
} | ||
|
||
|
||
fun main() { | ||
val uri = "<connection string uri>" | ||
|
||
// Instantiate your new listener | ||
val cpListener = ConnectionPoolLibrarian() | ||
|
||
// Include the listener in your client settings | ||
val settings = MongoClientSettings.builder() | ||
.applyConnectionString(ConnectionString(uri)) | ||
.applyToConnectionPoolSettings({ | ||
it.addConnectionPoolListener(cpListener) | ||
}) | ||
.build() | ||
|
||
// Connect to your database | ||
val mongoClient = MongoClient.create(settings) | ||
val database = mongoClient.getDatabase("sample_mflix") | ||
val collection = database.getCollection<Document>("movies") | ||
|
||
// Run some commands to test the counter | ||
collection.find().firstOrNull() | ||
|
||
mongoClient.close() | ||
} |
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,49 @@ | ||
import com.mongodb.kotlin.client.MongoClient | ||
import org.bson.Document | ||
import com.mongodb.event.* | ||
import com.mongodb.MongoClientSettings | ||
import com.mongodb.ConnectionString | ||
|
||
rachel-mack marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class IsWriteable : ClusterListener { | ||
nhachicha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private var isWritable = false | ||
|
||
override fun clusterDescriptionChanged(event: ClusterDescriptionChangedEvent) { | ||
if (!isWritable) { | ||
if (event.newDescription.hasWritableServer()) { | ||
isWritable = true | ||
println("Able to write to cluster") | ||
} | ||
} else { | ||
if (!event.newDescription.hasWritableServer()) { | ||
isWritable = false | ||
println("Unable to write to cluster") | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
fun main() { | ||
val uri = "<connection string uri>" | ||
|
||
// Instantiate your new listener | ||
val clusterListener = IsWriteable() | ||
|
||
// Include the listener in your client settings | ||
val settings = MongoClientSettings.builder() | ||
.applyConnectionString(ConnectionString(uri)) | ||
.applyToClusterSettings { builder -> | ||
builder.addClusterListener(clusterListener) | ||
} | ||
.build() | ||
|
||
// Connect to your database | ||
val mongoClient = MongoClient.create(settings) | ||
val database = mongoClient.getDatabase("sample_mflix") | ||
val collection = database.getCollection<Document>("movies") | ||
|
||
// Run a command to trigger a ClusterDescriptionChangedEvent event | ||
collection.find().firstOrNull() | ||
|
||
mongoClient.close() | ||
} |
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
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.