-
Notifications
You must be signed in to change notification settings - Fork 291
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
[SqlClient] Add error.type and db.response.status_code attributes #2262
Changes from 5 commits
77c90d4
7cc7578
b6e9cff
c35133f
35f20b7
43bc9c4
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 |
---|---|---|
|
@@ -4,10 +4,11 @@ | |
#if !NETFRAMEWORK | ||
using System.Data; | ||
using System.Diagnostics; | ||
using OpenTelemetry.Trace; | ||
#if NET | ||
using System.Diagnostics.CodeAnalysis; | ||
#endif | ||
using System.Globalization; | ||
using OpenTelemetry.Trace; | ||
|
||
namespace OpenTelemetry.Instrumentation.SqlClient.Implementation; | ||
|
||
|
@@ -32,6 +33,7 @@ internal sealed class SqlClientDiagnosticListener : ListenerHandler | |
private readonly PropertyFetcher<CommandType> commandTypeFetcher = new("CommandType"); | ||
private readonly PropertyFetcher<object> commandTextFetcher = new("CommandText"); | ||
private readonly PropertyFetcher<Exception> exceptionFetcher = new("Exception"); | ||
private readonly PropertyFetcher<int> exceptionNumberFetcher = new("Number"); | ||
private readonly SqlClientTraceInstrumentationOptions options; | ||
|
||
public SqlClientDiagnosticListener(string sourceName, SqlClientTraceInstrumentationOptions? options) | ||
|
@@ -187,6 +189,13 @@ public override void OnEventWritten(string name, object? payload) | |
{ | ||
if (this.exceptionFetcher.TryFetch(payload, out Exception? exception) && exception != null) | ||
{ | ||
activity.AddTag(SemanticConventions.AttributeErrorType, exception.GetType().FullName); | ||
|
||
if (this.exceptionNumberFetcher.TryFetch(exception, out var exceptionNumber)) | ||
{ | ||
activity.AddTag(SemanticConventions.AttributeDbResponseStatusCode, exceptionNumber.ToString(CultureInfo.InvariantCulture)); | ||
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. The error has a number, severity, and state. The number is most descriptive of the problem that occurred, though I can see severity and state being useful. From the docs Severity:
State:
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. could you please check out https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/sql.md?plain=1#L102-L136 ? I believe MS SQL server doesn't support SQL STATE, but if you ever see both - state and code, you can concat them together - see the link above. If severity is interesting, we can always add it later, but it seems there is a 1:1 mapping from the error code to severity and it might be redundant. 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. The notion of state here is different from SQL STATE. From what I can tell you're correct that MSSQL does not support SQL STATE. My read is that for a given MSSQL error code, that code may be raised from different locations in the DBMS and the state specifies exactly which location the code was raised. I think I'm going to just ignore state.
I agree it's probably redundant. |
||
} | ||
|
||
activity.SetStatus(ActivityStatusCode.Error, exception.Message); | ||
|
||
if (this.options.RecordException) | ||
alanwest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not very useful because
error.type
will always equalMicrosoft.Data.SqlClient.SqlException
.Per the conventions, this is not necessarily wrong, but it's not ideal:
Doing something like the following would be more useful and would align with the response status code.
However exception message can have variable text and could cause error.type to be high cardinality. For example:
This query results in error code 207 and message:
It's tempting to do something like the following to replace anything in single-quotes with something.
But, glancing over the list of error codes, I'm not sure such a simple replacement will suffice.
Alternatively, we could maintain a dictionary of codes to messages... there are a lot.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe there is a common practice to set exception message on the span description.
It's ok if for .NET it's uncommon to subclass exceptions and that
error.type
won't be super-interesting. We still have status code.