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

feat: add activity on connection #1734

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

aygalinc
Copy link
Contributor

Proposed Changes

Add activity on connection creation.
In case of creation exception register an error event.

Types of Changes

What types of changes does your code introduce to this project?
Put an x in the boxes that apply

  • Bug fix (non-breaking change which fixes issue #NNNN)
  • [X ] New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause an observable behavior change in existing systems)
  • Documentation improvements (corrections, new content, etc)
  • Cosmetic change (whitespace, formatting, etc)

Refer to #1731

@aygalinc
Copy link
Contributor Author

@michaelklishin build seems broken due to some new CVE related to system text json ?

@lukebakken lukebakken force-pushed the feat/add_activity_on_connection branch from dc89271 to 177ccbd Compare December 11, 2024 18:17
@lukebakken
Copy link
Contributor

@eerhardt - do you have time to review this PR? Thanks!

@lukebakken lukebakken added this to the 7.1.0 milestone Dec 11, 2024
@lukebakken lukebakken self-assigned this Dec 11, 2024
@lukebakken lukebakken force-pushed the feat/add_activity_on_connection branch from 6587ffe to 2a99147 Compare December 11, 2024 18:35
@aygalinc
Copy link
Contributor Author

@lukebakken when i implement the activity i create a new activity source but i m wondering if it s really usefull to have 3 ActivitySource and not use only one.

Copy link
Contributor

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

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

Is there any way to write some tests around this?

@@ -226,11 +226,10 @@ internal void TakeOver(Connection other)
internal async ValueTask<IConnection> OpenAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

Activity? connectionActivity = RabbitMQActivitySource.OpenConnection(_frameHandler);
Copy link
Contributor

Choose a reason for hiding this comment

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

How/where does this activity get completed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are totally right. Missing some using keyword.
By reading the experimental activity on dotnet 9 http client I don't know if at some point we should link producer and consumer activity to the connection activity?

Comment on lines +72 to +76
if (!s_connectionSource.HasListeners())
{
return null;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (!s_connectionSource.HasListeners())
{
return null;
}

I don't think this is necessary. ActivitySource.CreateActivity will do this check and return null.

https://github.com/dotnet/runtime/blob/1622f514684d94a521bfb41c88a27079ad943ee7/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/ActivitySource.cs#L192-L200

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I take the same logic as the rest of the implem but you are right when I read the Microsoft code this line is useless

{
ActivityTagsCollection exceptionTags = new();
exceptionTags.Add(new KeyValuePair<string, object?>(ExceptionMessageTag, exception.Message));
exceptionTags.Add(new KeyValuePair<string, object?>(ExceptionStackTraceTag, exception.ToString()));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
exceptionTags.Add(new KeyValuePair<string, object?>(ExceptionStackTraceTag, exception.ToString()));
// Note that "exception.stacktrace" is the full exception detail, not just the StackTrace property.
// See https://opentelemetry.io/docs/specs/semconv/attributes-registry/exception/
// and https://github.com/open-telemetry/opentelemetry-specification/pull/697#discussion_r453662519
exceptionTags.Add(new KeyValuePair<string, object?>(ExceptionStackTraceTag, exception.ToString()));

ActivityTagsCollection exceptionTags = new();
exceptionTags.Add(new KeyValuePair<string, object?>(ExceptionMessageTag, exception.Message));
exceptionTags.Add(new KeyValuePair<string, object?>(ExceptionStackTraceTag, exception.ToString()));
exceptionTags.Add(new KeyValuePair<string, object?>(ExceptionTypeTag, exception.GetType().ToString()));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
exceptionTags.Add(new KeyValuePair<string, object?>(ExceptionTypeTag, exception.GetType().ToString()));
exceptionTags.Add(new KeyValuePair<string, object?>(ExceptionTypeTag, exception.GetType().FullName));

@@ -142,10 +177,22 @@ public static class RabbitMQActivitySource
return activity;
}

internal static void ReportException(this Activity activity, Exception exception)
{
ActivityTagsCollection exceptionTags = new();
Copy link
Contributor

Choose a reason for hiding this comment

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

You should be able to just call .AddTag on the current Activity for each tag instead of adding a new event.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Opentelemetry spec states that exception should be recorded as event and not tag on the current activity

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

System.Diagnostics.DiagnosticSource 9.0.0 adds a AddException method to the Activity class.

You can see the implementation here.

Although it's safe to add a dependency to version 9.0.0 of this package, if you don't want to risk that, you can add it as an extension method to this project.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This recordException is basically a copy paste of the MCR method, I don't know if dependency bump is allowed

@lukebakken
Copy link
Contributor

Thank you @eerhardt !!!

@lukebakken lukebakken force-pushed the feat/add_activity_on_connection branch from 2a99147 to c9b91a2 Compare December 13, 2024 19:41
@lukebakken
Copy link
Contributor

@aygalinc just FYI, I rebased this on main to pick up my test suite fixes. Hopefully GHA will be green going forward.

@aygalinc
Copy link
Contributor Author

@eerhardt to the best of my knowledge Microsoft don't provide any testing support fort distributed tracing (contrary to metrics) . I do not find any test related to the other activity creation in rabbit. Maybe you have some resources that I miss to do this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants