Last active
October 21, 2021 08:27
-
-
Save billpratt/0ccd6a507bf69c2050b0d1718832b30b to your computer and use it in GitHub Desktop.
Examples of how to track .NET Mongodb queries in Application Insights
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
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var telemetryClient = new TelemetryClient(new TelemetryConfiguration("APP_INSIGHTS_KEY")); | |
var mongoConnectionString = "mongodb://localhost:27017/test"; | |
TrackMongoByEvents(mongoConnectionString, telemetryClient); | |
TrackMongoManually(mongoConnectionString, telemetryClient); | |
} | |
private static void TrackMongoManually(string mongoConnectionString, TelemetryClient telemetryClient) | |
{ | |
var mongoClient = new MongoClient(mongoConnectionString); | |
var database = mongoClient.GetDatabase("foo"); | |
var collection = database.GetCollection<BsonDocument>("bar"); | |
var document = new BsonDocument | |
{ | |
{ "name", "MongoDB" }, | |
{ "type", "Database" }, | |
{ "count", 1 }, | |
{ "info", new BsonDocument | |
{ | |
{ "x", 203 }, | |
{ "y", 102 } | |
}} | |
}; | |
var timestamp = DateTimeOffset.Now; | |
var stopwatch = Stopwatch.StartNew(); | |
// Insert one document | |
collection.InsertOne(document); | |
stopwatch.Stop(); | |
var dependency = new DependencyTelemetry | |
{ | |
Type = "Mongo", | |
Name = "InsertOne", | |
Target = collection.ToString(), | |
Timestamp = timestamp, | |
Duration = stopwatch.Elapsed, | |
Success = true, | |
}; | |
telemetryClient.TrackDependency(dependency); | |
} | |
private static void TrackMongoByEvents(string mongoConnectionString, TelemetryClient telemetryClient) | |
{ | |
var mongoUrl = new MongoUrl(mongoConnectionString); | |
var mongoClientSettings = MongoClientSettings.FromUrl(mongoUrl); | |
// Send mongo events to Application Insights | |
mongoClientSettings.ClusterConfigurator = builder => | |
{ | |
builder.Subscribe<CommandSucceededEvent>(e => | |
{ | |
if (e.OperationId == null) | |
return; | |
telemetryClient.TrackDependency("mongodb", e.CommandName, DateTime.Now.Subtract(e.Duration), e.Duration, true); | |
}); | |
builder.Subscribe<CommandFailedEvent>(e => | |
{ | |
telemetryClient.TrackDependency("mongodb", $"{e.CommandName} - {e.ToString()}", DateTime.Now.Subtract(e.Duration), e.Duration, false); | |
}); | |
}; | |
var mongoClient = new MongoClient(mongoClientSettings); | |
var database = mongoClient.GetDatabase("foo"); | |
var collection = database.GetCollection<BsonDocument>("bar"); | |
var document = new BsonDocument | |
{ | |
{ "name", "MongoDB" }, | |
{ "type", "Database" }, | |
{ "count", 1 }, | |
{ "info", new BsonDocument | |
{ | |
{ "x", 203 }, | |
{ "y", 102 } | |
}} | |
}; | |
collection.InsertOne(document); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment