Add OpenTelemetry health check publisher
What this PR does / why we need it: This PR adds a generic OpenTelemetry health check publisher that can be used for backends like for Aspire and Azure Monitor.
Which issue(s) this PR fixes:
Please reference the issue this PR will close: #2254 #2025
Special notes for your reviewer:
OpenTelemetry guidelines advices that we should create extension methods on MeterProviderBuilder, instead of calling services.AddOpenTelemetry().WithMetrics(x => ...) from a library.
While I believe this is correct 99% of the time, this would require clients of this package to first add the OpenTelemetryPublisher on the IHealthCheckBuilder and then also manually add the Meter with name "Microsoft.AspNetCore.Diagnostics.HealthChecks" in their MeterProviderBuilder. Forget one of the two and it will not publish anything.
The client has already consented to using OpenTelemetry by calling our healthCheckBuilder.AddOpenTelemetryPublisher(), so I think it is beneficial to register the Meter for them immediately.
Multiple calls to services.AddOpenTelemetry() will not result in multiple providers. Only a single MeterProvider will be created in the target, so no harm will be done.
Also a note on the unit tests:
Unit testing with the internals of OpenTelemetry is very hard and cumbersome.
I currently can not think of a way to make the functional test shorter.
I also wish I could assert that the Meter was added in OpenTelemetryBuilder.WithMetrics(x => ...), in the extension method, but I could not find any way of doing so.
I'm open for feedback and debate on any subject of thir PR. Please let me know what you think.
Does this PR introduce a user-facing change?: No
Please make sure you've completed the relevant tasks for this PR, out of the following list:
- [x] Code compiles correctly
- [x] Created/updated tests
- [x] Unit tests passing
- [x] End-to-end tests passing
- [x] Extended the documentation
- [x] Provided sample for the feature
@dotnet-policy-service agree
@lupusbytes i have been thinking about this some more & what is your thought about releasing this from the open telemetry contrib repo instead given there is no dependency on any other project from this library hence can be used with any do net health checks.
is this change going to be merged? it looks like something i'd be intersted in using