Skip to content

Commit

Permalink
dotnet#7479 Trace connection open and close in RelationalConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
Fitzchak Yitzchaki committed Jan 26, 2017
1 parent ef6c735 commit f6bef29
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ internal static class RelationalDiagnostics
public const string AfterExecuteCommand = NamePrefix + nameof(AfterExecuteCommand);
public const string CommandExecutionError = NamePrefix + nameof(CommandExecutionError);

public const string ConnectionOpened = NamePrefix + nameof(ConnectionOpened);
public const string ConnectionClosed = NamePrefix + nameof(ConnectionClosed);

public const string DataReaderDisposing = NamePrefix + nameof(DataReaderDisposing);

public static void WriteCommandBefore(
Expand Down Expand Up @@ -92,6 +95,22 @@ internal static class RelationalDiagnostics
}
}

public static void WriteConnectionOpened(this DiagnosticSource diagnosticSource, DbConnection connection)
{
if (diagnosticSource.IsEnabled(ConnectionOpened))
{
diagnosticSource.Write(ConnectionOpened, connection);
}
}

public static void WriteConnectionClosed(this DiagnosticSource diagnosticSource, DbConnection connection)
{
if (diagnosticSource.IsEnabled(ConnectionClosed))
{
diagnosticSource.Write(ConnectionClosed, connection);
}
}

public static void WriteDataReaderDisposing(this DiagnosticSource diagnosticSource, DbDataReader dataReader)
{
if (diagnosticSource.IsEnabled(DataReaderDisposing))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Data;
using System.Data.Common;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
Expand Down Expand Up @@ -39,20 +40,24 @@ public abstract class RelationalConnection : IRelationalConnection
private int? _commandTimeout;
private readonly ILogger _logger;

public DiagnosticSource DiagnosticSource { get; }

/// <summary>
/// Initializes a new instance of the <see cref="IRelationalConnection" /> class.
/// </summary>
/// <param name="options"> The options for the context that this connection will be used with. </param>
/// <param name="logger"> The logger to write to. </param>
protected RelationalConnection([NotNull] IDbContextOptions options, [NotNull] ILogger logger)
/// <param name="diagnosticSource"> The diagnostic source to write to. </param>
protected RelationalConnection([NotNull] IDbContextOptions options, [NotNull] ILogger logger, [NotNull] DiagnosticSource diagnosticSource)
{
Check.NotNull(options, nameof(options));
Check.NotNull(logger, nameof(logger));

_logger = logger;
DiagnosticSource = diagnosticSource;

var relationalOptions = RelationalOptionsExtension.Extract(options);

_commandTimeout = relationalOptions.CommandTimeout;

if (relationalOptions.Connection != null)
Expand Down Expand Up @@ -281,6 +286,8 @@ public virtual void Open()

_connection.Value.Open();

DiagnosticSource.WriteConnectionOpened(_connection.Value);

if (_openedCount == 0)
{
_openedInternally = true;
Expand Down Expand Up @@ -325,6 +332,8 @@ public virtual async Task OpenAsync(CancellationToken cancellationToken = defaul

await _connection.Value.OpenAsync(cancellationToken);

DiagnosticSource.WriteConnectionOpened(_connection.Value);

if (_openedCount == 0)
{
_openedInternally = true;
Expand Down Expand Up @@ -372,6 +381,8 @@ public virtual void Close()
state.Database,
state.DataSource));
_connection.Value.Close();

DiagnosticSource.WriteConnectionClosed(_connection.Value);
}
_openedInternally = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Data.Common;
using System.Data.SqlClient;
using System.Diagnostics;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.Logging;
Expand All @@ -27,14 +28,14 @@ public class SqlServerConnection : RelationalConnection, ISqlServerConnection
public SqlServerConnection(
[NotNull] IDbContextOptions options,
// ReSharper disable once SuggestBaseTypeForParameter
[NotNull] ILogger<SqlServerConnection> logger)
: base(options, logger)
[NotNull] ILogger<SqlServerConnection> logger, [NotNull] DiagnosticSource diagnosticSource)
: base(options, logger, diagnosticSource)
{
}

private SqlServerConnection(
[NotNull] IDbContextOptions options, [NotNull] ILogger logger)
: base(options, logger)
[NotNull] IDbContextOptions options, [NotNull] ILogger logger, [NotNull] DiagnosticSource diagnosticSource)
: base(options, logger, diagnosticSource)
{
}

Expand All @@ -55,7 +56,7 @@ public virtual ISqlServerConnection CreateMasterConnection()
builder.Remove("AttachDBFilename");

return new SqlServerConnection(new DbContextOptionsBuilder()
.UseSqlServer(builder.ConnectionString, b => b.CommandTimeout(CommandTimeout ?? DefaultMasterConnectionCommandTimeout)).Options, Logger);
.UseSqlServer(builder.ConnectionString, b => b.CommandTimeout(CommandTimeout ?? DefaultMasterConnectionCommandTimeout)).Options, Logger, DiagnosticSource);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Data.Common;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -32,8 +33,9 @@ public class SqliteRelationalConnection : RelationalConnection
[NotNull] IRawSqlCommandBuilder rawSqlCommandBuilder,
[NotNull] IDbContextOptions options,
// ReSharper disable once SuggestBaseTypeForParameter
[NotNull] ILogger<SqliteRelationalConnection> logger)
: base(options, logger)
[NotNull] ILogger<SqliteRelationalConnection> logger,
[NotNull] DiagnosticSource diagnosticSource)
: base(options, logger, diagnosticSource)
{
Check.NotNull(rawSqlCommandBuilder, nameof(rawSqlCommandBuilder));

Expand Down Expand Up @@ -130,7 +132,8 @@ public virtual SqliteRelationalConnection CreateReadOnlyConnection()
return new SqliteRelationalConnection(
_rawSqlCommandBuilder,
options.Options,
(ILogger<SqliteRelationalConnection>)Logger);
(ILogger<SqliteRelationalConnection>)Logger,
DiagnosticSource);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.Data.Common;
using System.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.Logging;
Expand All @@ -14,7 +15,7 @@ public class FakeRelationalConnection : RelationalConnection
private readonly List<FakeDbConnection> _dbConnections = new List<FakeDbConnection>();

public FakeRelationalConnection(IDbContextOptions options)
: base(options, new Logger<FakeRelationalConnection>(new LoggerFactory()))
: base(options, new Logger<FakeRelationalConnection>(new LoggerFactory()), new DiagnosticListener("FakeDiagnosticListener"))
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Infrastructure;
Expand All @@ -18,8 +19,8 @@ public class TestSqlServerConnection : ISqlServerConnection
{
private readonly ISqlServerConnection _realConnection;

public TestSqlServerConnection(IDbContextOptions options, ILogger<SqlServerConnection> logger)
: this(new SqlServerConnection(options, logger))
public TestSqlServerConnection(IDbContextOptions options, ILogger<SqlServerConnection> logger, DiagnosticSource diagnosticSource)
: this(new SqlServerConnection(options, logger, diagnosticSource))
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Infrastructure;
Expand Down Expand Up @@ -155,8 +156,8 @@ private class FakeSqlServerConnection : SqlServerConnection
private readonly IDbContextOptions _options;
private readonly ILoggerFactory _loggerFactory;

public FakeSqlServerConnection(IDbContextOptions options, ILoggerFactory loggerFactory)
: base(options, new Logger<SqlServerConnection>(loggerFactory))
public FakeSqlServerConnection(IDbContextOptions options, ILoggerFactory loggerFactory, DiagnosticSource diagnosticSource)
: base(options, new Logger<SqlServerConnection>(loggerFactory), diagnosticSource)
{
_options = options;
_loggerFactory = loggerFactory;
Expand Down Expand Up @@ -184,7 +185,7 @@ public override Task OpenAsync(CancellationToken cancellationToken = new Cancell
return Task.FromResult(0);
}

public override ISqlServerConnection CreateMasterConnection() => new FakeSqlServerConnection(_options, _loggerFactory);
public override ISqlServerConnection CreateMasterConnection() => new FakeSqlServerConnection(_options, _loggerFactory, DiagnosticSource);
}

private class FakeRelationalCommandBuilderFactory : IRelationalCommandBuilderFactory
Expand Down

0 comments on commit f6bef29

Please sign in to comment.