-
Notifications
You must be signed in to change notification settings - Fork 855
[hosting] Support .NET 8 IMetricsBuilder API #4958
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
CodeBlanch
merged 53 commits into
open-telemetry:main
from
CodeBlanch:poc/metricsbuilder
Nov 22, 2023
Merged
Changes from all commits
Commits
Show all changes
53 commits
Select commit
Hold shift + click to select a range
9331203
Implement an IMetricsListener over OpenTelemetry SDK.
CodeBlanch b70f529
Tweak.
CodeBlanch e411e22
Tweak.
CodeBlanch dc4f60e
Merge from main.
CodeBlanch 6db3bcb
Manually fix merge conflicts and update API to released versions.
CodeBlanch 680c506
Package updates.
CodeBlanch 496002f
Ceremony.
CodeBlanch 3a03fa3
Fixed dropped metrics in IMetricsBuilder when composite reader is used.
CodeBlanch 558df37
Rename API and add todo.
CodeBlanch 32a0388
Define SDK AddOpenTelemetryLogging extension.
CodeBlanch 25e7ef9
Bug fixes.
CodeBlanch 5ea6211
Merge from main.
CodeBlanch 5d645eb
Merge from main.
CodeBlanch 82b384d
Port public api updates.
CodeBlanch f4c254a
Merge branch 'main' into poc/metricsbuilder
CodeBlanch 995d2c4
Merge branch 'main' into poc/metricsbuilder
CodeBlanch 9c87e60
Code review.
CodeBlanch 3fd9045
Warning cleanup.
CodeBlanch c507377
Test coverage.
CodeBlanch fe10eb6
Merge from main.
CodeBlanch 24322e8
Merge fixes.
CodeBlanch 3fe5702
Test coverage for the UseOpenTelemetry logging extension.
CodeBlanch bfec275
Ignore external subscriptions if AddMeter is also called.
CodeBlanch c728717
Verify hot reload of metrics via IConfiguration works and doesn't pro…
CodeBlanch 934e75b
Switch WithLogging and WithMetrics to turn on hosting features.
CodeBlanch 467008e
Make IMetricsBuilder.UseOpenTelemetry an experimental API.
CodeBlanch f22cdf8
XML comment tweaks.
CodeBlanch f5b2e6f
Test fix.
CodeBlanch ec19a81
Warning cleanup.
CodeBlanch 01114b3
Update WithMetrics XML comments to capture the IMetricsListener behav…
CodeBlanch 4012f83
Make IMetricsBuilder.UseOpenTelemetry internal.
CodeBlanch cd393f7
Remove logging changes.
CodeBlanch 1500698
Remove more logging changes.
CodeBlanch c2cb6be
Merge from main.
CodeBlanch 0b623e9
A few misc changes and fixes.
CodeBlanch 9831c9f
Merge from main.
CodeBlanch 6905583
Refactor a little bit of duplicated code.
CodeBlanch cc195ba
Code review.
CodeBlanch d993289
Code review.
CodeBlanch 0862f7f
Code review.
CodeBlanch f73d309
Code review and race condition cleanup.
CodeBlanch da5554b
Tweak.
CodeBlanch f748914
Reuse metric instances when an instrument is reactivated.
CodeBlanch 7685521
Expand TODOs.
CodeBlanch 8c4c9d2
Merge branch 'main' into poc/metricsbuilder
CodeBlanch 54da2d9
Lower reference to Microsoft.Extensions.Diagnostics.Abstractions.
CodeBlanch 97f3bf5
Warning cleanup.
CodeBlanch abbcbb1
Revert logging options ordering change.
CodeBlanch 7744d5e
Code review.
CodeBlanch 9ab7c58
CHANGELOG update.
CodeBlanch f2fce4a
Test tweak.
CodeBlanch 152220c
Merge remote-tracking branch 'upstream/main' into poc/metricsbuilder
CodeBlanch 1c264e8
Clean up nits.
CodeBlanch 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
|
|
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
120 changes: 120 additions & 0 deletions
120
src/OpenTelemetry.Extensions.Hosting/Implementation/OpenTelemetryMetricsListener.cs
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,120 @@ | ||
| // <copyright file="OpenTelemetryMetricsListener.cs" company="OpenTelemetry Authors"> | ||
| // Copyright The OpenTelemetry Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // </copyright> | ||
|
|
||
| using System.Diagnostics; | ||
| using System.Diagnostics.Metrics; | ||
| using Microsoft.Extensions.Diagnostics.Metrics; | ||
|
|
||
| namespace OpenTelemetry.Metrics; | ||
|
|
||
| internal sealed class OpenTelemetryMetricsListener : IMetricsListener, IDisposable | ||
| { | ||
| private readonly MeterProviderSdk meterProviderSdk; | ||
| private IObservableInstrumentsSource? observableInstrumentsSource; | ||
|
|
||
| public OpenTelemetryMetricsListener(MeterProvider meterProvider) | ||
| { | ||
| var meterProviderSdk = meterProvider as MeterProviderSdk; | ||
|
|
||
| Debug.Assert(meterProviderSdk != null, "meterProvider was not MeterProviderSdk"); | ||
|
|
||
| this.meterProviderSdk = meterProviderSdk!; | ||
|
|
||
| this.meterProviderSdk.OnCollectObservableInstruments += this.OnCollectObservableInstruments; | ||
| } | ||
|
|
||
| public string Name => "OpenTelemetry"; | ||
|
|
||
| public void Dispose() | ||
| { | ||
| this.meterProviderSdk.OnCollectObservableInstruments -= this.OnCollectObservableInstruments; | ||
| } | ||
|
|
||
| public MeasurementHandlers GetMeasurementHandlers() | ||
| { | ||
| return new MeasurementHandlers() | ||
| { | ||
| ByteHandler = (instrument, value, tags, state) | ||
| => this.MeasurementRecordedLong(instrument, value, tags, state), | ||
| ShortHandler = (instrument, value, tags, state) | ||
| => this.MeasurementRecordedLong(instrument, value, tags, state), | ||
| IntHandler = (instrument, value, tags, state) | ||
| => this.MeasurementRecordedLong(instrument, value, tags, state), | ||
| LongHandler = this.MeasurementRecordedLong, | ||
| FloatHandler = (instrument, value, tags, state) | ||
| => this.MeasurementRecordedDouble(instrument, value, tags, state), | ||
| DoubleHandler = this.MeasurementRecordedDouble, | ||
| }; | ||
| } | ||
|
|
||
| public bool InstrumentPublished(Instrument instrument, out object? userState) | ||
| { | ||
| userState = this.meterProviderSdk.InstrumentPublished(instrument, listeningIsManagedExternally: true); | ||
| return userState != null; | ||
| } | ||
|
|
||
| public void MeasurementsCompleted(Instrument instrument, object? userState) | ||
| { | ||
| var meterProvider = this.meterProviderSdk; | ||
|
|
||
| if (meterProvider.ViewCount > 0) | ||
| { | ||
| meterProvider.MeasurementsCompleted(instrument, userState); | ||
| } | ||
| else | ||
| { | ||
| meterProvider.MeasurementsCompletedSingleStream(instrument, userState); | ||
| } | ||
| } | ||
|
|
||
| public void Initialize(IObservableInstrumentsSource source) | ||
| { | ||
| this.observableInstrumentsSource = source; | ||
| } | ||
|
|
||
| private void OnCollectObservableInstruments() | ||
| { | ||
| this.observableInstrumentsSource?.RecordObservableInstruments(); | ||
| } | ||
|
|
||
| private void MeasurementRecordedDouble(Instrument instrument, double value, ReadOnlySpan<KeyValuePair<string, object?>> tagsRos, object? userState) | ||
| { | ||
| var meterProvider = this.meterProviderSdk; | ||
|
|
||
| if (meterProvider.ViewCount > 0) | ||
| { | ||
| meterProvider.MeasurementRecordedDouble(instrument, value, tagsRos, userState); | ||
| } | ||
| else | ||
| { | ||
| meterProvider.MeasurementRecordedDoubleSingleStream(instrument, value, tagsRos, userState); | ||
| } | ||
| } | ||
|
|
||
| private void MeasurementRecordedLong(Instrument instrument, long value, ReadOnlySpan<KeyValuePair<string, object?>> tagsRos, object? userState) | ||
| { | ||
| var meterProvider = this.meterProviderSdk; | ||
|
|
||
| if (meterProvider.ViewCount > 0) | ||
| { | ||
| meterProvider.MeasurementRecordedLong(instrument, value, tagsRos, userState); | ||
| } | ||
| else | ||
| { | ||
| meterProvider.MeasurementRecordedLongSingleStream(instrument, value, tagsRos, userState); | ||
| } | ||
| } | ||
| } |
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
81 changes: 81 additions & 0 deletions
81
src/OpenTelemetry.Extensions.Hosting/OpenTelemetryMetricsBuilderExtensions.cs
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,81 @@ | ||
| // <copyright file="OpenTelemetryMetricsBuilderExtensions.cs" company="OpenTelemetry Authors"> | ||
| // Copyright The OpenTelemetry Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // </copyright> | ||
|
|
||
| using System.Diagnostics; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.DependencyInjection.Extensions; | ||
| using OpenTelemetry.Internal; | ||
| using OpenTelemetry.Metrics; | ||
|
|
||
| namespace Microsoft.Extensions.Diagnostics.Metrics; | ||
|
|
||
| /// <summary> | ||
| /// Contains extension methods for registering OpenTelemetry metrics with an | ||
| /// <see cref="IMetricsBuilder"/> instance. | ||
| /// </summary> | ||
| internal static class OpenTelemetryMetricsBuilderExtensions | ||
| { | ||
| /// <summary> | ||
| /// Adds an OpenTelemetry <see cref="IMetricsListener"/> named 'OpenTelemetry' to the <see cref="IMetricsBuilder"/>. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Note: This is safe to be called multiple times and by library authors. | ||
utpilla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// Only a single <see cref="MeterProvider"/> will be created for a given | ||
| /// <see cref="IServiceCollection"/>. | ||
| /// </remarks> | ||
| /// <param name="metricsBuilder"><see cref="IMetricsBuilder"/>.</param> | ||
| /// <returns>The supplied <see cref="IMetricsBuilder"/> for chaining | ||
| /// calls.</returns> | ||
| public static IMetricsBuilder UseOpenTelemetry( | ||
| this IMetricsBuilder metricsBuilder) | ||
| => UseOpenTelemetry(metricsBuilder, b => { }); | ||
|
|
||
| /// <summary> | ||
| /// Adds an OpenTelemetry <see cref="IMetricsListener"/> named 'OpenTelemetry' to the <see cref="IMetricsBuilder"/>. | ||
| /// </summary> | ||
| /// <remarks><inheritdoc cref="UseOpenTelemetry(IMetricsBuilder)" path="/remarks"/></remarks> | ||
| /// <param name="metricsBuilder"><see cref="IMetricsBuilder"/>.</param> | ||
| /// <param name="configure"><see cref="MeterProviderBuilder"/> | ||
| /// configuration callback.</param> | ||
| /// <returns>The supplied <see cref="IMetricsBuilder"/> for chaining | ||
| /// calls.</returns> | ||
| public static IMetricsBuilder UseOpenTelemetry( | ||
| this IMetricsBuilder metricsBuilder, | ||
| Action<MeterProviderBuilder> configure) | ||
| { | ||
| Guard.ThrowIfNull(metricsBuilder); | ||
|
|
||
| RegisterMetricsListener(metricsBuilder.Services, configure); | ||
|
|
||
| return metricsBuilder; | ||
| } | ||
|
|
||
| internal static void RegisterMetricsListener( | ||
| IServiceCollection services, | ||
| Action<MeterProviderBuilder> configure) | ||
| { | ||
| Debug.Assert(services != null, "services was null"); | ||
|
|
||
| Guard.ThrowIfNull(configure); | ||
|
|
||
| var builder = new MeterProviderBuilderBase(services!); | ||
|
|
||
| services!.TryAddEnumerable( | ||
| ServiceDescriptor.Singleton<IMetricsListener, OpenTelemetryMetricsListener>()); | ||
|
|
||
| configure(builder); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.