Skip to content

Commit

Permalink
Grishina/ef commands metrics (#138)
Browse files Browse the repository at this point in the history
Modify AddEfCommandsMetrics to allow several metrics counters

---------

Co-authored-by: Ekaterina Grishina <[email protected]>
Co-authored-by: Vitaly Shcherbinov <[email protected]>
  • Loading branch information
3 people authored Nov 20, 2024
1 parent b34d5b9 commit a4e8b6a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
14 changes: 11 additions & 3 deletions src/EntityFramework/DbContextOptionsBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace Mindbox.DiagnosticContext.EntityFramework;

public static class EntityFrameworkDiagnosticContextExtensions
{
public static DbContextOptionsBuilder AddEfCommandsMetrics(this DbContextOptionsBuilder serviceCollection)
public static DbContextOptionsBuilder AddEfCommandsMetrics(
this DbContextOptionsBuilder serviceCollection,
IEnumerable<IEfCommandMetricsCounter>? metricsCounters = null)
{
return serviceCollection
.AddInterceptors(new EfCommandsScorerInterceptor());
var counters = metricsCounters?.ToList() ?? [];

Check failure on line 27 in src/EntityFramework/DbContextOptionsBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Invalid expression term '['

Check failure on line 27 in src/EntityFramework/DbContextOptionsBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Syntax error; value expected

Check failure on line 27 in src/EntityFramework/DbContextOptionsBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Invalid expression term '['

if (counters.All(counter => counter is not EfCommandsMetrics))
counters.Add(EfCommandsMetrics.Instance);

return serviceCollection.AddInterceptors(new EfCommandsScorerInterceptor(counters));
}
}
2 changes: 1 addition & 1 deletion src/EntityFramework/EfCommandsMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace Mindbox.DiagnosticContext.EntityFramework;

internal class EfCommandsMetrics
internal class EfCommandsMetrics : IEfCommandMetricsCounter
{
private static readonly AsyncLocal<EfCommandsMetrics> _instance = new();

Expand Down
11 changes: 8 additions & 3 deletions src/EntityFramework/EfCommandsScorerInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Collections.Generic;
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Diagnostics;

namespace Mindbox.DiagnosticContext.EntityFramework;

internal class EfCommandsScorerInterceptor : DbCommandInterceptor
internal class EfCommandsScorerInterceptor(

Check failure on line 23 in src/EntityFramework/EfCommandsScorerInterceptor.cs

View workflow job for this annotation

GitHub Actions / build

{ expected

Check failure on line 23 in src/EntityFramework/EfCommandsScorerInterceptor.cs

View workflow job for this annotation

GitHub Actions / build

} expected
IEnumerable<IEfCommandMetricsCounter> metricsCounters)

Check failure on line 24 in src/EntityFramework/EfCommandsScorerInterceptor.cs

View workflow job for this annotation

GitHub Actions / build

Tuple must contain at least two elements.
: DbCommandInterceptor

Check failure on line 25 in src/EntityFramework/EfCommandsScorerInterceptor.cs

View workflow job for this annotation

GitHub Actions / build

Type or namespace definition, or end-of-file expected

Check failure on line 25 in src/EntityFramework/EfCommandsScorerInterceptor.cs

View workflow job for this annotation

GitHub Actions / build

A namespace cannot directly contain members such as fields, methods or statements
{

Check failure on line 26 in src/EntityFramework/EfCommandsScorerInterceptor.cs

View workflow job for this annotation

GitHub Actions / build

Type or namespace definition, or end-of-file expected
public override InterceptionResult<DbDataReader> ReaderExecuting(
DbCommand command,
Expand Down Expand Up @@ -87,9 +90,11 @@ public override async ValueTask<int> NonQueryExecutedAsync(
int result,
CancellationToken cancellationToken = default) => ReportCommandFinished(result);

private static T ReportCommandStarted<T>(T result)
private T ReportCommandStarted<T>(T result)
{
EfCommandsMetrics.Instance.ReportCommandStarted();
foreach (var counter in metricsCounters)
counter.ReportCommandStarted();

return result;
}

Expand Down
20 changes: 20 additions & 0 deletions src/EntityFramework/IEfCommandMetricsCounter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2021 Mindbox Ltd
//
// 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.

namespace Mindbox.DiagnosticContext.EntityFramework;

public interface IEfCommandMetricsCounter
{
void ReportCommandStarted();
}

0 comments on commit a4e8b6a

Please sign in to comment.