-
Notifications
You must be signed in to change notification settings - Fork 780
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
[hosting] Register OpenTelemetry at the beginning of IServiceCollection #4883
Conversation
@@ -47,8 +46,10 @@ public static OpenTelemetryBuilder AddOpenTelemetry(this IServiceCollection serv | |||
{ | |||
Guard.ThrowIfNull(services); | |||
|
|||
services.TryAddEnumerable( | |||
ServiceDescriptor.Singleton<IHostedService, TelemetryHostedService>()); | |||
if (!services.Any((ServiceDescriptor d) => d.ServiceType == typeof(IHostedService) && d.ImplementationType == typeof(TelemetryHostedService))) |
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 able to comment on the line but should the summary/remarks this method calls this out?
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.
Good idea to update the remarks! I would make it "Notes:" and then use a bulleted list. There should be other examples around doing that. Probably also a good idea to update the CHANGELOG. Something like * Changed the behavior of the OpenTelemetryBuilder.AddOpenTelemetry extension to INSERT OpenTelemetry services at index 0 instead of ADDING at the end to improve the experience of users capturing telemetry in hosted services registered before the OpenTelemetry registration.
Kind of a mouthful feel free to improve it 😄
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.
the entire TelemetryHostedService
is an internal implementation detail, so probably okay to not mention about it in the summary.
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.
changelog suggestions : lets clarify that this still does not guarantee as the TelemetryHostedService may not have done initializing while other hosted services start, so possible to still miss telemetry until its fully initialized.
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.
Let me know if you think the wording needs any additional clarity.
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.
lets clarify that this still does not guarantee as the TelemetryHostedService may not have done initializing while other hosted services start, so possible to still miss telemetry until its fully initialized
I didn't dig into older versions but the current code seems to await each IHostedService.StartAsync one-by-one so this new logic of inserting at 0 should actually work really well. The only case where it wouldn't work (that I can think of) is if user inserted their service at index 0 after we stuck our service there 😄
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.
got it. The readme can callout this as well. (that the only time we'll miss telemetry from a hosted service is if user manually inserts its ahead of ours)
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.
Note, that in .NET 8 hosted service can be configured to start concurrently with HostOptions.ServicesStartConcurrently = true
Codecov Report
@@ Coverage Diff @@
## main #4883 +/- ##
==========================================
- Coverage 83.26% 83.26% -0.01%
==========================================
Files 295 295
Lines 12294 12294
==========================================
- Hits 10237 10236 -1
- Misses 2057 2058 +1
Flags with carried forward coverage won't be shown. Click here to find out more.
|
test/OpenTelemetry.Extensions.Hosting.Tests/OpenTelemetryServicesExtensionsTests.cs
Show resolved
Hide resolved
/// cref="IServiceCollection"/>. | ||
/// cref="IServiceCollection"/>.</item> | ||
/// <item>OpenTelemetry SDK services are inserted at the beginning of the | ||
/// <see cref="IServiceCollection"/> in an attempt to provide a better |
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.
/// <see cref="IServiceCollection"/> in an attempt to provide a better | |
/// <see cref="IServiceCollection"/> in an attempt to ensure telemetry generated from other services are also collected, but this is not guranteed. |
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.
Seems hard to capture all the nuance here. Maybe we should just link to a doc?
/// <item>OpenTelemetry SDK services are inserted at the beginning of the <see cref="IServiceCollection"/> and started with the host. For details about the ordering of events and capturing telemetry in <see cref="IHostedService" />s see: <see href="https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/src/OpenTelemetry.Extensions.Hosting/README.md#hosted-service-ordering-and-telemetry-capture" />.
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.
If we are going to put something in the README then I think including a link here makes the most sense. One potential downside is that the link could break.
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.
@CodeBlanch @cijothomas can we come to an agreement on the preferred documentation style? Just don't want this to sit stale.
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.
My vote would be... do the link as I have it above and just stub out the anchor section in the README with "TBD" as the content. We can flush it out on a follow-up.
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.
LGTM. Not a blocker - but this is worth mentioning about in the readme page of ext.hosting package?
/// Note: This is safe to be called multiple times and by library authors. | ||
/// Notes: | ||
/// <list type="bullet"> | ||
/// <item>This is safe to be called multiple times and by library authors. |
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 something added in this PR, but this is not to be called by library authors....so must be a typo here? @CodeBlanch do you think this was intended?
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.
There's really 2 questions...
Q: Is it safe for library authors to call this?
A: Yes! Multiple calls will stack. Everything is building-up/modifying a single provider for any given IServiceCollection. This is what the comments are saying.
Q: Should library authors call this?
A: It depends! If you want to force OpenTelemetry to be started, perfectly fine to call this. If you just want to configure something in the event OTel happens to be used by the host, don't call this. That is what IServiceCollection.ConfigureOpenTelemetry[Tracer|Meter|Logger]Provider
extensions are for. The current comments aren't really addressing that bit, I'll follow-up on that.
Changes
Attempt to add OpenTelemetry services to the beginning of the
IServiceCollection
. This will reduce the likelihood of missing telemetry emitted in hosted services.Prior to this change the ordering of registered services impacted whether telemetry was exported. Using the following
MyHostedService
as an example that emits a span namedtest
.Prior to this change the following would not emit
test
becauseMyHostedService
was registered before the OpenTelemetry services.Note that after this change the previous example works but it is still possible to run into issues by doing something like
Merge requirement checklist
CHANGELOG.md
files updated for non-trivial changes