Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-1639

Add method call for .NET profilers to hook to get query

    XMLWordPrintableJSON

Details

    • Icon: New Feature New Feature
    • Resolution: Unresolved
    • Icon: Minor - P4 Minor - P4
    • None
    • 2.2.4
    • Diagnostics
    • None
    • Fully Compatible

    Description

      I'm trying to figure out the best way to capture the BsonDocument when queries are executed for the purposes of performance profiling. I am specifically trying to show mongodb queries in the Prefix.io profiler, which is a free tool for .NET developers.

      I played with ClusterBuilder Subscribe() event hooks for CommandStartedEvent and they work great. The problem is this would require someone to change their code every place they create a MongoClient or MongoClientSettings. That is not an idea solution as we want to be transparent and not require any changes.

      I would like to propose some global events that can be used. All I really need is the ability to set a bool on CommandEventHelper that enables the "Profiler Hook" additional methods and then some private methods would be called that don't actually do anything.

      Do you mind if I do this and submit a PR? Any other ideas on an easy to globally see the json for all commands sent to mongodb?

      Pseudo code for what I need is something like this, although I did the example in CommandStartEvent instead of CommandEventHelper for simplicity of the example:

              public static bool EnableProfilingHookMethod { get; set; } = false;
       
       
              public CommandStartedEvent(string commandName, BsonDocument command, DatabaseNamespace databaseNamespace, long? operationId, int requestId, ConnectionId connectionId)
              {
                  _commandName = Ensure.IsNotNullOrEmpty(commandName, "commandName");
                  _command = Ensure.IsNotNull(command, "command");
                  _databaseNamespace = Ensure.IsNotNull(databaseNamespace, "databaseNamespace");
                  _connectionId = Ensure.IsNotNull(connectionId, "connectionId");
                  _operationId = operationId;
                  _requestId = requestId;
       
                  if (EnableProfilingHookMethod)
                  {
                      ProfilerHookMethod(commandName, databaseNamespace.DatabaseName, _command?.ToString(), operationId, requestId);
                  }
              }
       
              [MethodImpl(MethodImplOptions.NoInlining)]
              private void ProfilerHookMethod(string commandName, string databaseName, string commandText, long? operationId, int requestId)
              {
                  //method only exists for .NET profiling inspection as BsonDocument is converted to a string to get the json via memory inspection
              }
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            spo81rty@gmail.com Matt Watson
            Votes:
            2 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: