- 
                Notifications
    
You must be signed in to change notification settings  - Fork 354
 
[SqlClient] [EFCore] Support query parameter attributes #3015
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
Changes from all commits
b2c48e4
              e371e6d
              1e11556
              fba7b76
              26a3f01
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -50,9 +50,8 @@ internal SqlClientTraceInstrumentationOptions(IConfiguration configuration) | |
| } | ||
| 
     | 
||
| /// <summary> | ||
| /// Gets or sets a value indicating whether or not the <see | ||
| /// cref="SqlClientInstrumentation"/> should add the text of commands as | ||
| /// the <see cref="SemanticConventions.AttributeDbStatement"/> tag. | ||
| /// Gets or sets a value indicating whether or not the <see cref="SqlClientInstrumentation"/> | ||
| /// should add the text of commands as the <see cref="SemanticConventions.AttributeDbStatement"/> tag. | ||
| /// Default value: <see langword="false"/>. | ||
| /// </summary> | ||
| /// <remarks> | ||
| 
          
            
          
           | 
    @@ -134,6 +133,23 @@ internal SqlClientTraceInstrumentationOptions(IConfiguration configuration) | |
| /// </remarks> | ||
| public bool RecordException { get; set; } | ||
| 
     | 
||
| /// <summary> | ||
| /// Gets or sets a value indicating whether or not the <see cref="SqlClientInstrumentation"/> | ||
| /// should add the names and values of query parameters as the <c>db.query.parameter.{key}</c> tag. | ||
| /// Default value: <see langword="false"/>. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// <b>WARNING: SetDbQueryParameters will capture the raw | ||
| /// <c>Value</c>. Make sure your query parameters never | ||
| /// contain any sensitive data.</b> | ||
| /// </para> | ||
| /// <para> | ||
| /// <b>SetDbQueryParameters is only supported on .NET and .NET Core runtimes.</b> | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general, .NET Core is no longer supported. We are testing only with .NEt8+ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assumed this was present in the comment I copied from to cover  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It makes sense, it can be done for whole project/repo in the separate PR.  | 
||
| /// </para> | ||
| /// </remarks> | ||
| public bool SetDbQueryParameters { get; set; } | ||
| 
     | 
||
| /// <summary> | ||
| /// Gets or sets a value indicating whether the old database attributes should be emitted. | ||
| /// </summary> | ||
| 
          
            
          
           | 
    ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| 
     | 
||
| using System.Data; | ||
| using System.Diagnostics; | ||
| using System.Globalization; | ||
| 
     | 
||
| namespace OpenTelemetry.Instrumentation; | ||
| 
     | 
||
| internal static class SqlParameterProcessor | ||
| { | ||
| public static void AddQueryParameters(Activity activity, object? command) | ||
| { | ||
| if (command is not IDbCommand { Parameters.Count: > 0 } dbCommand) | ||
| { | ||
| return; | ||
| } | ||
| 
     | 
||
| int index = 0; | ||
| 
     | 
||
| foreach (var parameter in dbCommand.Parameters) | ||
| { | ||
| if (parameter is IDbDataParameter dbDataParameter) | ||
| { | ||
| // If a query parameter has no name and instead is referenced only by index, then {key} SHOULD be the 0-based index. | ||
| var key = string.IsNullOrEmpty(dbDataParameter.ParameterName) | ||
| ? index.ToString(CultureInfo.InvariantCulture) | ||
| : dbDataParameter.ParameterName; | ||
                
      
                  martincostello marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| activity.SetTag($"db.query.parameter.{key}", dbDataParameter.Value); | ||
| } | ||
| 
     | 
||
| index++; | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.