Extracted from #2210 (comment), per request:
Semi-related to this, but the SqlCommand class (both the deprecated System.Data.SqlClient.SqlCommand and the now-relevant Microsoft.Data.SqlClient.SqlCommand) are a bit hard to use with OpenTelemetry in the sense that it seems virtually impossible to add a 3rd party "query name" tag to the instantiated SqlCommand. 🤔 This kind of information would be incredibly useful to be able to include in the distributed tracing data, to distinguish different queries from each other.
The reasons why I claim this is impossible:
SqlCommand is sealed, making it impossible to simply subclass it and add the relevant property
SqlCommand provides no custom Attributes dictionary or similar, where you could add user-provided data needed by your application somehow
If either one of these options were available, you could then easily use the OpenTelemetry.Instrumentation.SqlClient-provided Enrich() method, to set arbitrary tags in the tracing data being submitted. We tried setting a dummy Parameter with a user-defined name of the query, but unfortunately this can cause undesired breakage when running certain SQL queries.
Sorry for hijacking the issue quite a bit (...) but I guess what I'm stretching for is:
- Does anyone (perhaps you, @cijothomas?) have any ideas on how to workaround this short-term? I thought about a Java:ish approach of using a thread-local variable for this, but unsure if we can guarantee that the
Enrich() lambda runs on the same thread as the code creating the SqlCommand in the first place, because of the async nature of modern .NET code.
- When investigating this issue in the long run, please take this into consideration ("make it possible to provide a user-defined query name") since it'll make the tracing much more valuable and useful.
Extracted from #2210 (comment), per request:
Semi-related to this, but the
SqlCommandclass (both the deprecatedSystem.Data.SqlClient.SqlCommandand the now-relevantMicrosoft.Data.SqlClient.SqlCommand) are a bit hard to use with OpenTelemetry in the sense that it seems virtually impossible to add a 3rd party "query name" tag to the instantiatedSqlCommand. 🤔 This kind of information would be incredibly useful to be able to include in the distributed tracing data, to distinguish different queries from each other.The reasons why I claim this is impossible:
SqlCommandis sealed, making it impossible to simply subclass it and add the relevant propertySqlCommandprovides no customAttributesdictionary or similar, where you could add user-provided data needed by your application somehowIf either one of these options were available, you could then easily use the
OpenTelemetry.Instrumentation.SqlClient-providedEnrich()method, to set arbitrary tags in the tracing data being submitted. We tried setting a dummyParameterwith a user-defined name of the query, but unfortunately this can cause undesired breakage when running certain SQL queries.Sorry for hijacking the issue quite a bit (...) but I guess what I'm stretching for is:
Enrich()lambda runs on the same thread as the code creating theSqlCommandin the first place, because of theasyncnature of modern .NET code.