Skip to content
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] Include instance in db.namespace and activity name, start activity with required tags #2277

Merged
merged 5 commits into from
Oct 30, 2024

Conversation

alanwest
Copy link
Member

Fixes #2236
Towards #2224
Towards #2237

Per the MSSQL conventions:

[3]: When connected to a default instance, db.namespace SHOULD be set to the name of the database. When connected to a named instance, db.namespace SHOULD be set to the combination of instance and database name following the {instance_name}.{database_name} pattern.

In addition to including the instance name in the db.namespace attribute, this PR begins to address conforming the activity name to the conventions and starting the activity with required tags.

  1. It includes db.namespace and server.address/server.port in the activity name when appropriate.
  2. It adds the following attributes when starting the activity
    • db.system
    • db.namespace
    • server.address
    • server.port

@alanwest alanwest requested a review from a team as a code owner October 30, 2024 17:30
@github-actions github-actions bot added the comp:instrumentation.sqlclient Things related to OpenTelemetry.Instrumentation.SqlClient label Oct 30, 2024
@alanwest alanwest changed the title Start activity with required tags and name that includes the instance [SqlClient] Include instance in db.namespace and activity name, start activity with required tags Oct 30, 2024
Copy link

codecov bot commented Oct 30, 2024

Codecov Report

Attention: Patch coverage is 96.00000% with 2 lines in your changes missing coverage. Please review.

Project coverage is 90.90%. Comparing base (71655ce) to head (6a62be6).
Report is 567 commits behind head on main.

Files with missing lines Patch % Lines
...ient/Implementation/SqlClientDiagnosticListener.cs 83.33% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             main    #2277       +/-   ##
===========================================
+ Coverage   73.91%   90.90%   +16.99%     
===========================================
  Files         267        8      -259     
  Lines        9615      308     -9307     
===========================================
- Hits         7107      280     -6827     
+ Misses       2508       28     -2480     
Flag Coverage Δ
unittests-Instrumentation.SqlClient 90.90% <96.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...qlClient/Implementation/SqlActivitySourceHelper.cs 100.00% <100.00%> (ø)
...ent/Implementation/SqlEventSourceListener.netfx.cs 87.50% <100.00%> (ø)
...ient/Implementation/SqlClientDiagnosticListener.cs 83.54% <83.33%> (ø)

... and 269 files with indirect coverage changes

Comment on lines 22 to 26
private static SqlClientTestCase[] TestCases =>
[
new SqlClientTestCase
{
ConnectionString = @"Data Source=SomeServer",
Copy link
Member Author

@alanwest alanwest Oct 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will be expanding these test cases in follow up PRs, but wanted to model in this PR what I plan to do.

I find the InlineData attributes onerous when they include a lot of parameters. It's more code, but I prefer creating a class to represent complex test cases. I find it easier to read and understand the cases covered.

if (options.EmitNewAttributes && !string.IsNullOrEmpty(databaseName))
{
var dbNamespace = !string.IsNullOrEmpty(connectionDetails.InstanceName)
? $"{connectionDetails.InstanceName}.{databaseName}" // TODO: Refactor SqlConnectionDetails to include database to avoid string allocation here.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a couple TODOs here. I'm considering adding database name to the SqlConnectionDetails that gets cached. This could mean though that the cache could be a little bigger in the event that multiple databases were accessed for a given instance.


public static readonly IEnumerable<KeyValuePair<string, object?>> CreationTags = new[]
public static TagList GetTagListFromConnectionInfo(string? dataSource, string? databaseName, SqlClientTraceInstrumentationOptions options, out string activityName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TagList is a sizeable struct. It would probably be faster to return by reference a la:

-public static TagList GetTagListFromConnectionInfo(
+public static void GetTagListFromConnectionInfo(
    string? dataSource,
    string? databaseName,
    SqlClientTraceInstrumentationOptions options,
-   out string activityName)
+   out string activityName,
+   out TagList tags)

Probably the name sucks too 🤣 GetSamplerTagsFromConnectionInfo 🤷

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably the name sucks too 🤣 GetSamplerTagsFromConnectionInfo

Yea I agree. I'm gonna leave it sucky for now cause I don't know what to call it yet. This code may continue to be refactored a bit once I integrate metric instrumentation which will also need what this method is doing.

ActivityKind.Client,
default(ActivityContext),
SqlActivitySourceHelper.CreationTags);
startTags);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting that StartActivity doesn't have an overload which accepts TagList. We should ask @tarekgh for one 🤣

In the meantime, maybe don't use TagList at all. Because this will box it up.

A ThreadStatic is probably fine we did that here:

[ThreadStatic]
private static KeyValuePair<string, object?>[]? cachedTagsStorage;

Copy link
Member Author

@alanwest alanwest Oct 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I was bummed there was no StartActivity with TagList as well 😢.

A ThreadStatic is probably fine we did that here

I had a similar thought to use ThreadStatic. Ultimately, these tags will need to be shared by the metric instrumentation as well. A ThreadStatic would allow us to capture them on command start and then use them on command end when recording the metric. I'll try this in a follow up.

Copy link

@tarekgh tarekgh Oct 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting that StartActivity doesn't have an overload which accepts TagList. We should ask @tarekgh for one 🤣

You may update dotnet/runtime#103245 (comment) with whatever request. the runtime issue was talking about CreateActivity but can include StartActivity too.

Copy link
Member

@CodeBlanch CodeBlanch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some feedback but LGTM

@alanwest alanwest merged commit 72d1030 into open-telemetry:main Oct 30, 2024
56 checks passed
@alanwest alanwest deleted the alanwest/database-namespace branch October 30, 2024 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:instrumentation.sqlclient Things related to OpenTelemetry.Instrumentation.SqlClient
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[sql] Include instance name in db.namespace
3 participants