Skip to content

Commit

Permalink
OTEL implementation, so far matching EventCounters infrastructure.
Browse files Browse the repository at this point in the history
  • Loading branch information
cincuranet committed Apr 29, 2024
1 parent 385b562 commit 0288f34
Show file tree
Hide file tree
Showing 21 changed files with 714 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text;
using Microsoft.EntityFrameworkCore.Cosmos.Internal;
using Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure.Internal;
using Newtonsoft.Json.Linq;

namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal;
Expand Down Expand Up @@ -150,7 +151,9 @@ public bool MoveNext()
{
var sqlQuery = _queryingEnumerable.GenerateQuery();

EntityFrameworkEventSource.Log.QueryExecuting();
#pragma warning disable EF1001 // Internal EF Core API usage.
EntityFrameworkMetricsData.ReportQueryExecuting();
#pragma warning restore EF1001 // Internal EF Core API usage.

_enumerator = _cosmosQueryContext.CosmosClient
.ExecuteSqlQuery(
Expand Down Expand Up @@ -248,7 +251,9 @@ public async ValueTask<bool> MoveNextAsync()
{
var sqlQuery = _queryingEnumerable.GenerateQuery();

EntityFrameworkEventSource.Log.QueryExecuting();
#pragma warning disable EF1001 // Internal EF Core API usage.
EntityFrameworkMetricsData.ReportQueryExecuting();
#pragma warning restore EF1001 // Internal EF Core API usage.

_enumerator = _cosmosQueryContext.CosmosClient
.ExecuteSqlQueryAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Cosmos.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore.Internal;
using Newtonsoft.Json.Linq;

Expand Down Expand Up @@ -218,7 +219,9 @@ public bool MoveNext()
throw new InvalidOperationException(CosmosStrings.PartitionKeyMissing);
}

EntityFrameworkEventSource.Log.QueryExecuting();
#pragma warning disable EF1001 // Internal EF Core API usage.
EntityFrameworkMetricsData.ReportQueryExecuting();
#pragma warning restore EF1001 // Internal EF Core API usage.

_item = _cosmosQueryContext.CosmosClient.ExecuteReadItem(
_readItemExpression.Container,
Expand Down Expand Up @@ -270,7 +273,9 @@ public async ValueTask<bool> MoveNextAsync()
throw new InvalidOperationException(CosmosStrings.PartitionKeyMissing);
}

EntityFrameworkEventSource.Log.QueryExecuting();
#pragma warning disable EF1001 // Internal EF Core API usage.
EntityFrameworkMetricsData.ReportQueryExecuting();
#pragma warning restore EF1001 // Internal EF Core API usage.

_item = await _cosmosQueryContext.CosmosClient.ExecuteReadItemAsync(
_readItemExpression.Container,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using Microsoft.EntityFrameworkCore.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore.InMemory.Internal;

namespace Microsoft.EntityFrameworkCore.InMemory.Query.Internal;
Expand Down Expand Up @@ -155,7 +156,9 @@ private bool MoveNextHelper()
{
if (_enumerator == null)
{
EntityFrameworkEventSource.Log.QueryExecuting();
#pragma warning disable EF1001 // Internal EF Core API usage.
EntityFrameworkMetricsData.ReportQueryExecuting();
#pragma warning restore EF1001 // Internal EF Core API usage.

_enumerator = _innerEnumerable.GetEnumerator();
_queryContext.InitializeStateManager(_standAloneStateManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using Microsoft.EntityFrameworkCore.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore.Internal;

namespace Microsoft.EntityFrameworkCore.Query.Internal;
Expand Down Expand Up @@ -269,7 +270,9 @@ public bool MoveNext()

private static bool InitializeReader(Enumerator enumerator)
{
EntityFrameworkEventSource.Log.QueryExecuting();
#pragma warning disable EF1001 // Internal EF Core API usage.
EntityFrameworkMetricsData.ReportQueryExecuting();
#pragma warning restore EF1001 // Internal EF Core API usage.

var relationalCommand = enumerator._relationalCommand =
enumerator._relationalCommandCache.RentAndPopulateRelationalCommand(enumerator._relationalQueryContext);
Expand Down Expand Up @@ -391,7 +394,9 @@ public async ValueTask<bool> MoveNextAsync()

private static async Task<bool> InitializeReaderAsync(AsyncEnumerator enumerator, CancellationToken cancellationToken)
{
EntityFrameworkEventSource.Log.QueryExecuting();
#pragma warning disable EF1001 // Internal EF Core API usage.
EntityFrameworkMetricsData.ReportQueryExecuting();
#pragma warning restore EF1001 // Internal EF Core API usage.

var relationalCommand = enumerator._relationalCommand =
enumerator._relationalCommandCache.RentAndPopulateRelationalCommand(enumerator._relationalQueryContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using Microsoft.EntityFrameworkCore.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore.Internal;

namespace Microsoft.EntityFrameworkCore.Query.Internal;
Expand Down Expand Up @@ -339,7 +340,9 @@ public bool MoveNext()

private static bool InitializeReader(Enumerator enumerator)
{
EntityFrameworkEventSource.Log.QueryExecuting();
#pragma warning disable EF1001 // Internal EF Core API usage.
EntityFrameworkMetricsData.ReportQueryExecuting();
#pragma warning restore EF1001 // Internal EF Core API usage.

var relationalCommand = enumerator._relationalCommand =
enumerator._relationalCommandCache.RentAndPopulateRelationalCommand(enumerator._relationalQueryContext);
Expand Down Expand Up @@ -526,7 +529,9 @@ public async ValueTask<bool> MoveNextAsync()

private static async Task<bool> InitializeReaderAsync(AsyncEnumerator enumerator, CancellationToken cancellationToken)
{
EntityFrameworkEventSource.Log.QueryExecuting();
#pragma warning disable EF1001 // Internal EF Core API usage.
EntityFrameworkMetricsData.ReportQueryExecuting();
#pragma warning restore EF1001 // Internal EF Core API usage.

var relationalCommand = enumerator._relationalCommand =
enumerator._relationalCommandCache.RentAndPopulateRelationalCommand(enumerator._relationalQueryContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using Microsoft.EntityFrameworkCore.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore.Internal;

namespace Microsoft.EntityFrameworkCore.Query.Internal;
Expand Down Expand Up @@ -339,7 +340,9 @@ public bool MoveNext()

private static bool InitializeReader(Enumerator enumerator)
{
EntityFrameworkEventSource.Log.QueryExecuting();
#pragma warning disable EF1001 // Internal EF Core API usage.
EntityFrameworkMetricsData.ReportQueryExecuting();
#pragma warning restore EF1001 // Internal EF Core API usage.

var relationalCommand = enumerator._relationalCommand =
enumerator._relationalCommandCache.RentAndPopulateRelationalCommand(enumerator._relationalQueryContext);
Expand Down Expand Up @@ -517,7 +520,9 @@ public async ValueTask<bool> MoveNextAsync()

private static async Task<bool> InitializeReaderAsync(AsyncEnumerator enumerator, CancellationToken cancellationToken)
{
EntityFrameworkEventSource.Log.QueryExecuting();
#pragma warning disable EF1001 // Internal EF Core API usage.
EntityFrameworkMetricsData.ReportQueryExecuting();
#pragma warning restore EF1001 // Internal EF Core API usage.

var relationalCommand = enumerator._relationalCommand =
enumerator._relationalCommandCache.RentAndPopulateRelationalCommand(enumerator._relationalQueryContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using Microsoft.EntityFrameworkCore.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore.Internal;

namespace Microsoft.EntityFrameworkCore.Query.Internal;
Expand Down Expand Up @@ -266,7 +267,9 @@ public bool MoveNext()

private static bool InitializeReader(Enumerator enumerator)
{
EntityFrameworkEventSource.Log.QueryExecuting();
#pragma warning disable EF1001 // Internal EF Core API usage.
EntityFrameworkMetricsData.ReportQueryExecuting();
#pragma warning restore EF1001 // Internal EF Core API usage.

var relationalCommand = enumerator._relationalCommand =
enumerator._relationalCommandCache.RentAndPopulateRelationalCommand(enumerator._relationalQueryContext);
Expand Down Expand Up @@ -427,7 +430,9 @@ public async ValueTask<bool> MoveNextAsync()

private static async Task<bool> InitializeReaderAsync(AsyncEnumerator enumerator, CancellationToken cancellationToken)
{
EntityFrameworkEventSource.Log.QueryExecuting();
#pragma warning disable EF1001 // Internal EF Core API usage.
EntityFrameworkMetricsData.ReportQueryExecuting();
#pragma warning restore EF1001 // Internal EF Core API usage.

var relationalCommand = enumerator._relationalCommand =
enumerator._relationalCommandCache.RentAndPopulateRelationalCommand(enumerator._relationalQueryContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using Microsoft.EntityFrameworkCore.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore.Internal;

namespace Microsoft.EntityFrameworkCore.Query.Internal;
Expand Down Expand Up @@ -260,7 +261,9 @@ public bool MoveNext()

private static bool InitializeReader(Enumerator enumerator)
{
EntityFrameworkEventSource.Log.QueryExecuting();
#pragma warning disable EF1001 // Internal EF Core API usage.
EntityFrameworkMetricsData.ReportQueryExecuting();
#pragma warning restore EF1001 // Internal EF Core API usage.

var relationalCommand = enumerator._relationalCommand =
enumerator._relationalCommandCache.RentAndPopulateRelationalCommand(enumerator._relationalQueryContext);
Expand Down Expand Up @@ -415,7 +418,9 @@ public async ValueTask<bool> MoveNextAsync()

private static async Task<bool> InitializeReaderAsync(AsyncEnumerator enumerator, CancellationToken cancellationToken)
{
EntityFrameworkEventSource.Log.QueryExecuting();
#pragma warning disable EF1001 // Internal EF Core API usage.
EntityFrameworkMetricsData.ReportQueryExecuting();
#pragma warning restore EF1001 // Internal EF Core API usage.

var relationalCommand = enumerator._relationalCommand =
enumerator._relationalCommandCache.RentAndPopulateRelationalCommand(enumerator._relationalQueryContext);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Query.Internal;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
Expand Down Expand Up @@ -118,7 +119,9 @@ protected virtual Expression VisitNonQuery(NonQueryExpression nonQueryExpression
(relationalQueryContext, relationalCommandCache, commandSource),
static (_, state) =>
{
EntityFrameworkEventSource.Log.QueryExecuting();
#pragma warning disable EF1001 // Internal EF Core API usage.
EntityFrameworkMetricsData.ReportQueryExecuting();
#pragma warning restore EF1001 // Internal EF Core API usage.
var relationalCommand = state.relationalCommandCache.RentAndPopulateRelationalCommand(state.relationalQueryContext);
Expand Down Expand Up @@ -196,7 +199,9 @@ protected virtual Expression VisitNonQuery(NonQueryExpression nonQueryExpression
(relationalQueryContext, relationalCommandCache, commandSource),
static (_, state, cancellationToken) =>
{
EntityFrameworkEventSource.Log.QueryExecuting();
#pragma warning disable EF1001 // Internal EF Core API usage.
EntityFrameworkMetricsData.ReportQueryExecuting();
#pragma warning restore EF1001 // Internal EF Core API usage.
var relationalCommand = state.relationalCommandCache.RentAndPopulateRelationalCommand(state.relationalQueryContext);
Expand Down
5 changes: 3 additions & 2 deletions src/EFCore/ChangeTracking/Internal/StateManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

Expand Down Expand Up @@ -1282,7 +1283,7 @@ protected virtual int SaveChanges(IList<IUpdateEntry> entriesToSave)

try
{
EntityFrameworkEventSource.Log.SavingChanges();
EntityFrameworkMetricsData.ReportSavingChanges();

return _database.SaveChanges(entriesToSave);
}
Expand All @@ -1306,7 +1307,7 @@ protected virtual int SaveChanges(IList<IUpdateEntry> entriesToSave)

try
{
EntityFrameworkEventSource.Log.SavingChanges();
EntityFrameworkMetricsData.ReportSavingChanges();

return await _database.SaveChangesAsync(entriesToSave, cancellationToken)
.ConfigureAwait(false);
Expand Down
9 changes: 5 additions & 4 deletions src/EFCore/DbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Runtime.CompilerServices;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

Expand Down Expand Up @@ -130,7 +131,7 @@ public DbContext(DbContextOptions options)
.GetRequiredService<IDbSetInitializer>()
.InitializeSets(this);

EntityFrameworkEventSource.Log.DbContextInitializing();
EntityFrameworkMetricsData.ReportDbContextInitializing();
}

/// <summary>
Expand Down Expand Up @@ -659,7 +660,7 @@ public virtual int SaveChanges(bool acceptAllChangesOnSuccess)
}
catch (DbUpdateConcurrencyException exception)
{
EntityFrameworkEventSource.Log.OptimisticConcurrencyFailure();
EntityFrameworkMetricsData.ReportOptimisticConcurrencyFailure();

SaveChangesFailed?.Invoke(this, new SaveChangesFailedEventArgs(acceptAllChangesOnSuccess, exception));

Expand Down Expand Up @@ -804,7 +805,7 @@ public virtual Task<int> SaveChangesAsync(CancellationToken cancellationToken =
}
catch (DbUpdateConcurrencyException exception)
{
EntityFrameworkEventSource.Log.OptimisticConcurrencyFailure();
EntityFrameworkMetricsData.ReportOptimisticConcurrencyFailure();

SaveChangesFailed?.Invoke(this, new SaveChangesFailedEventArgs(acceptAllChangesOnSuccess, exception));

Expand Down Expand Up @@ -1119,7 +1120,7 @@ private bool DisposeSync(bool leaseActive, bool contextShouldBeDisposed)
}
else if (!_disposed)
{
EntityFrameworkEventSource.Log.DbContextDisposing();
EntityFrameworkMetricsData.ReportDbContextDisposing();

_dbContextDependencies?.InfrastructureLogger.ContextDisposed(this);

Expand Down
1 change: 1 addition & 0 deletions src/EFCore/EFCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using OpenTelemetry.Metrics;

namespace Microsoft.EntityFrameworkCore.Infrastructure;

/// <summary>
/// Extension methods to simplify registering of Entity Framework Core instrumentation.
/// </summary>
public static class EntityFrameworkCoreInstrumentationMeterProviderBuilderExtensions
{
/// <summary>
/// Enables the automatic data collection for Entity Framework Core.
/// </summary>
/// <param name="builder"><see cref="MeterProviderBuilder"/> being configured.</param>
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
public static MeterProviderBuilder AddEntityFrameworkCoreInstrumentation(this MeterProviderBuilder builder)
=> builder
.AddMeter(EntityFrameworkMetrics.MeterName);
}

0 comments on commit 0288f34

Please sign in to comment.