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

OTEL implementation, so far matching EventCounters infrastructure. #33633

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cincuranet
Copy link
Contributor

Fixes #25880

@roji
Copy link
Member

roji commented May 17, 2024

@cincuranet @SamMonoRT I know I'm behind on reviewing this, it's high on my list and moving up - early next week for sure.

@adityamandaleeka
Copy link
Member

@cincuranet I just noticed I was marked as a reviewer on this PR. Is there any specific input you'd like me to provide? I don't have a ton of context on efcore.

@SamMonoRT
Copy link
Member

@cincuranet I just noticed I was marked as a reviewer on this PR. Is there any specific input you'd like me to provide? I don't have a ton of context on efcore.

Was mostly if you saw patterns from other Aspire OTEL efforts.

Copy link
Member

@roji roji left a comment

Choose a reason for hiding this comment

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

Looks pretty good overall - see relatively minor comments below.

@@ -152,7 +153,9 @@ public bool MoveNext()
{
var sqlQuery = _queryingEnumerable.GenerateQuery();

EntityFrameworkEventSource.Log.QueryExecuting();
#pragma warning disable EF1001 // Internal EF Core API usage.
Copy link
Member

Choose a reason for hiding this comment

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

Is this currently internal because of our plans to evolve the metrics to support database dimensionality (#33634)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. And also it does not feel like a public API. It's implementation detail.

@@ -46,6 +46,7 @@ Microsoft.EntityFrameworkCore.DbSet
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="$(MicrosoftExtensionsCachingMemoryVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="$(MicrosoftExtensionsLoggingVersion)" />
<PackageReference Include="OpenTelemetry" Version="1.8.1" />
Copy link
Member

Choose a reason for hiding this comment

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

It isn't great to force this new dependency on all EF users, I'm assuming it's needed for EntityFrameworkCoreInstrumentationMeterProviderBuilderExtensions? Is this the pattern followed by other .NET metrics providers?

(assuming we keep it, extract the version to Versions.props like the others)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Usually it's part of extension package (like OpenTelemetry.Instrumentation.Http for HtpClient). There's already one OpenTelemetry.Instrumentation.EntityFrameworkCore, but it is implemented over the "old" event source (https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/main/src/OpenTelemetry.Instrumentation.EntityFrameworkCore/Implementation/EntityFrameworkDiagnosticListener.cs).

Copy link
Member

Choose a reason for hiding this comment

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

This dependency should be isolated in a separate package, especially since it's not required for core EF functionality

@@ -52,4 +52,8 @@
<ProjectReference Include="..\EFCore.Specification.Tests\EFCore.Specification.Tests.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry.Exporter.InMemory" Version="1.8.1" />
Copy link
Member

Choose a reason for hiding this comment

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

Extract version (like for the OpenTelemetry package above)

namespace Microsoft.EntityFrameworkCore.Infrastructure;

// The tests interact with global state and should never be run in parallel
[Collection(nameof(MetricsDataCollection))]
Copy link
Member

Choose a reason for hiding this comment

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

If I understood correctly, IMeterFactory is what would allow us to not restrict the parallelism here, maybe open a backlog issue for that...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Kind of. As long as we keep the EntityFrameworkEventSource and have single logic for both, the IMeterFactory would not work.

But if/when we decide to do #33634, the implementations are going to diverge and we can lift this restriction.

[InlineData(false)]
[InlineData(true)]
public async Task Counts_when_query_is_executed(bool async)
{
TotalQueries = 0;
var initial = EntityFrameworkMetricsData.GetTotalQueriesExecuted();
Copy link
Member

Choose a reason for hiding this comment

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

The tests here seem very similar to those in EntityFrameworkMetricsDataTest - do we need both? This here seems to be more like a unit test (like the previous ones but no reflection), whereas the other ones are a bit more end-to-end, go through the actual OTel machinery - am I understanding things right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct. The EntityFrameworkMetricsTest goes through OTEL, while EntityFrameworkMetricsDataTest tests only existing implementation. We can probably drop EntityFrameworkMetricsDataTest, if you want.

@@ -53,7 +47,7 @@ private EntityFrameworkEventSource()
/// See <see href="https://aka.ms/efcore-docs-event-counters">EF Core event counters</see> for more information and examples.
/// </remarks>
public void DbContextInitializing()
=> Interlocked.Increment(ref _activeDbContexts);
=> EntityFrameworkMetricsData.ReportDbContextInitializing();
Copy link
Member

Choose a reason for hiding this comment

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

DbContextInitializing() - and all the others here - don't seem to be used any more?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Backwards compatibility, somebody might be still using EntityFrameworkEventSource.

src/EFCore/Infrastructure/EntityFrameworkMetrics.cs Outdated Show resolved Hide resolved
private readonly ObservableCounter<long> _saveChangesCounter;
private readonly ObservableGauge<double> _compiledQueryCacheHitRateGauge;
private readonly ObservableCounter<long> _executionStrategyOperationFailuresCounter;
private readonly ObservableCounter<long> _optimisticConcurrencyFailuresCounter;
Copy link
Member

Choose a reason for hiding this comment

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

This one may actually make more sense as a percentage gauge, let me know what you think.

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 mean TotalOptimisticConcurrencyFailures/TotalSaveChanges?

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.

Implement the new OpenTelemetry Metrics API
5 participants