-
Notifications
You must be signed in to change notification settings - Fork 876
Add basic testing for tracing #6051
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1627ef1
Add basic testing for tracing
vonzshik c8af841
Add a few more tests
vonzshik be733c4
Also test batches
vonzshik ddb3b66
Set status on Activity
vonzshik 0ad520f
Do not set otel tags explicitly
vonzshik ceea7dd
Fix tests
vonzshik e01b396
A few more test fixes
vonzshik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace Npgsql.Tests; | ||
|
|
||
| [NonParallelizable] | ||
| public class TracingTests(MultiplexingMode multiplexingMode) : MultiplexingTestBase(multiplexingMode) | ||
| { | ||
| [Test] | ||
| public async Task Basic([Values] bool async, [Values] bool batch) | ||
| { | ||
| if (IsMultiplexing && !async) | ||
| return; | ||
|
|
||
| var activities = new List<Activity>(); | ||
|
|
||
| using var activityListener = new ActivityListener(); | ||
| activityListener.ShouldListenTo = source => source.Name == "Npgsql"; | ||
| activityListener.Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllDataAndRecorded; | ||
| activityListener.ActivityStopped = activity => activities.Add(activity); | ||
| ActivitySource.AddActivityListener(activityListener); | ||
|
|
||
| await using var dataSource = CreateDataSource(); | ||
| await using var conn = await dataSource.OpenConnectionAsync(); | ||
| await ExecuteScalar(conn, async, batch, "SELECT 42"); | ||
|
|
||
| Assert.That(activities.Count, Is.EqualTo(1)); | ||
| var activity = activities[0]; | ||
| Assert.That(activity.DisplayName, Is.EqualTo(conn.Settings.Database)); | ||
| Assert.That(activity.OperationName, Is.EqualTo(conn.Settings.Database)); | ||
| Assert.That(activity.Status, Is.EqualTo(ActivityStatusCode.Ok)); | ||
|
|
||
| Assert.That(activity.Events.Count(), Is.EqualTo(1)); | ||
| var firstResponseEvent = activity.Events.First(); | ||
| Assert.That(firstResponseEvent.Name, Is.EqualTo("received-first-response")); | ||
|
|
||
| var expectedTagCount = conn.Settings.Port == 5432 ? 9 : 10; | ||
| Assert.That(activity.TagObjects.Count(), Is.EqualTo(expectedTagCount)); | ||
|
|
||
| var queryTag = activity.TagObjects.First(x => x.Key == "db.statement"); | ||
| Assert.That(queryTag.Value, Is.EqualTo("SELECT 42")); | ||
|
|
||
| var systemTag = activity.TagObjects.First(x => x.Key == "db.system"); | ||
| Assert.That(systemTag.Value, Is.EqualTo("postgresql")); | ||
|
|
||
| var userTag = activity.TagObjects.First(x => x.Key == "db.user"); | ||
| Assert.That(userTag.Value, Is.EqualTo(conn.Settings.Username)); | ||
|
|
||
| var dbNameTag = activity.TagObjects.First(x => x.Key == "db.name"); | ||
| Assert.That(dbNameTag.Value, Is.EqualTo(conn.Settings.Database)); | ||
|
|
||
| var connStringTag = activity.TagObjects.First(x => x.Key == "db.connection_string"); | ||
| Assert.That(connStringTag.Value, Is.EqualTo(conn.ConnectionString)); | ||
|
|
||
| if (!IsMultiplexing) | ||
| { | ||
| var connIDTag = activity.TagObjects.First(x => x.Key == "db.connection_id"); | ||
| Assert.That(connIDTag.Value, Is.EqualTo(conn.ProcessID)); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Error([Values] bool async, [Values] bool batch) | ||
| { | ||
| if (IsMultiplexing && !async) | ||
| return; | ||
|
|
||
| var activities = new List<Activity>(); | ||
|
|
||
| using var activityListener = new ActivityListener(); | ||
| activityListener.ShouldListenTo = source => source.Name == "Npgsql"; | ||
| activityListener.Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllDataAndRecorded; | ||
| activityListener.ActivityStopped = activity => activities.Add(activity); | ||
| ActivitySource.AddActivityListener(activityListener); | ||
|
|
||
| await using var dataSource = CreateDataSource(); | ||
| await using var conn = await dataSource.OpenConnectionAsync(); | ||
| Assert.ThrowsAsync<PostgresException>(async () => await ExecuteScalar(conn, async, batch, "SELECT * FROM non_existing_table")); | ||
|
|
||
| Assert.That(activities.Count, Is.EqualTo(1)); | ||
| var activity = activities[0]; | ||
| Assert.That(activity.DisplayName, Is.EqualTo(conn.Settings.Database)); | ||
| Assert.That(activity.OperationName, Is.EqualTo(conn.Settings.Database)); | ||
| Assert.That(activity.Status, Is.EqualTo(ActivityStatusCode.Error)); | ||
| Assert.That(activity.StatusDescription, Is.EqualTo(PostgresErrorCodes.UndefinedTable)); | ||
|
|
||
| Assert.That(activity.Events.Count(), Is.EqualTo(1)); | ||
| var exceptionEvent = activity.Events.First(); | ||
| Assert.That(exceptionEvent.Name, Is.EqualTo("exception")); | ||
|
|
||
| Assert.That(exceptionEvent.Tags.Count(), Is.EqualTo(4)); | ||
|
|
||
| var exceptionTypeTag = exceptionEvent.Tags.First(x => x.Key == "exception.type"); | ||
| Assert.That(exceptionTypeTag.Value, Is.EqualTo("Npgsql.PostgresException")); | ||
|
|
||
| var exceptionMessageTag = exceptionEvent.Tags.First(x => x.Key == "exception.message"); | ||
| StringAssert.Contains("relation \"non_existing_table\" does not exist", (string)exceptionMessageTag.Value!); | ||
|
|
||
| var exceptionStacktraceTag = exceptionEvent.Tags.First(x => x.Key == "exception.stacktrace"); | ||
| StringAssert.Contains("relation \"non_existing_table\" does not exist", (string)exceptionStacktraceTag.Value!); | ||
|
|
||
| var exceptionEscapedTag = exceptionEvent.Tags.First(x => x.Key == "exception.escaped"); | ||
| Assert.That(exceptionEscapedTag.Value, Is.True); | ||
|
|
||
| var expectedTagCount = conn.Settings.Port == 5432 ? 9 : 10; | ||
| Assert.That(activity.TagObjects.Count(), Is.EqualTo(expectedTagCount)); | ||
|
|
||
| var queryTag = activity.TagObjects.First(x => x.Key == "db.statement"); | ||
| Assert.That(queryTag.Value, Is.EqualTo("SELECT * FROM non_existing_table")); | ||
|
|
||
| var systemTag = activity.TagObjects.First(x => x.Key == "db.system"); | ||
| Assert.That(systemTag.Value, Is.EqualTo("postgresql")); | ||
|
|
||
| var userTag = activity.TagObjects.First(x => x.Key == "db.user"); | ||
| Assert.That(userTag.Value, Is.EqualTo(conn.Settings.Username)); | ||
|
|
||
| var dbNameTag = activity.TagObjects.First(x => x.Key == "db.name"); | ||
| Assert.That(dbNameTag.Value, Is.EqualTo(conn.Settings.Database)); | ||
|
|
||
| var connStringTag = activity.TagObjects.First(x => x.Key == "db.connection_string"); | ||
| Assert.That(connStringTag.Value, Is.EqualTo(conn.ConnectionString)); | ||
|
|
||
| if (!IsMultiplexing) | ||
| { | ||
| var connIDTag = activity.TagObjects.First(x => x.Key == "db.connection_id"); | ||
| Assert.That(connIDTag.Value, Is.EqualTo(conn.ProcessID)); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Configure_tracing([Values] bool async, [Values] bool batch) | ||
| { | ||
| if (IsMultiplexing && !async) | ||
| return; | ||
|
|
||
| var activities = new List<Activity>(); | ||
|
|
||
| using var activityListener = new ActivityListener(); | ||
| activityListener.ShouldListenTo = source => source.Name == "Npgsql"; | ||
| activityListener.Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllDataAndRecorded; | ||
| activityListener.ActivityStopped = activity => activities.Add(activity); | ||
| ActivitySource.AddActivityListener(activityListener); | ||
|
|
||
| var dataSourceBuilder = CreateDataSourceBuilder(); | ||
| dataSourceBuilder.ConfigureTracing(options => | ||
| { | ||
| options | ||
| .EnableFirstResponseEvent(enable: false) | ||
| .ConfigureCommandFilter(cmd => cmd.CommandText.Contains('2')) | ||
| .ConfigureBatchFilter(batch => batch.BatchCommands[0].CommandText.Contains('2')) | ||
| .ConfigureCommandSpanNameProvider(_ => "unknown_query") | ||
| .ConfigureBatchSpanNameProvider(_ => "unknown_query") | ||
| .ConfigureCommandEnrichmentCallback((activity, _) => activity.AddTag("custom_tag", "custom_value")) | ||
| .ConfigureBatchEnrichmentCallback((activity, _) => activity.AddTag("custom_tag", "custom_value")); | ||
| }); | ||
| await using var dataSource = dataSourceBuilder.Build(); | ||
| await using var conn = await dataSource.OpenConnectionAsync(); | ||
|
|
||
| await ExecuteScalar(conn, async, batch, "SELECT 1"); | ||
|
|
||
| Assert.That(activities.Count, Is.EqualTo(0)); | ||
|
|
||
| await ExecuteScalar(conn, async, batch, "SELECT 2"); | ||
|
|
||
| Assert.That(activities.Count, Is.EqualTo(1)); | ||
| var activity = activities[0]; | ||
| Assert.That(activity.DisplayName, Is.EqualTo("unknown_query")); | ||
| Assert.That(activity.OperationName, Is.EqualTo("unknown_query")); | ||
|
|
||
| Assert.That(activity.Events.Count(), Is.EqualTo(0)); | ||
|
|
||
| var customTag = activity.TagObjects.First(x => x.Key == "custom_tag"); | ||
| Assert.That(customTag.Value, Is.EqualTo("custom_value")); | ||
| } | ||
|
|
||
| async Task<object?> ExecuteScalar(NpgsqlConnection connection, bool async, bool isBatch, string query) | ||
| { | ||
| if (!isBatch) | ||
| { | ||
| if (async) | ||
| return await connection.ExecuteScalarAsync(query); | ||
| else | ||
| return connection.ExecuteScalar(query); | ||
| } | ||
| else | ||
| { | ||
| await using var batch = connection.CreateBatch(); | ||
| var batchCommand = batch.CreateBatchCommand(); | ||
| batchCommand.CommandText = query; | ||
| batch.BatchCommands.Add(batchCommand); | ||
|
|
||
| if (async) | ||
| return await batch.ExecuteScalarAsync(); | ||
| else | ||
| return batch.ExecuteScalar(); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is this a dup of the line just above? Or are there two different things? Same below for exception.
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.
Not exactly.
Activity.Status(andActivity.Description) do not add any tags, they're essentially just fields inActivityand it's up to exporters to populate tags depending onActivity.Status. So in case there's an exporter which doesn't react onActivity.Status, then these tags will never be added if we remove them from there.I'm more-or-less OK with removing explicit tags.
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 don't know enough about this... What do ASP.NET do, for instance?
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.
From what I've been able to fine, they just call
SetStatus.https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/61164064e25a111ad141d1f0cf2c8d329e16683a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs#L263-L266
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.
Thanks for looking - I guess we can do the same then?
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.
Yeah, sure