Skip to content

Commit

Permalink
(❗) fix: add operation name to sql dependency tracking (#460)
Browse files Browse the repository at this point in the history
* fix: add operation name to sql dependency tracking

* pr-fix: remove blank lines
  • Loading branch information
stijnmoreels committed Sep 1, 2022
1 parent 2bc33b9 commit 542301c
Show file tree
Hide file tree
Showing 5 changed files with 421 additions and 22 deletions.
8 changes: 4 additions & 4 deletions docs/preview/03-Features/writing-different-telemetry-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ durationMeasurement.Start();
// Interact with database
var products = await _repository.GetProducts();

logger.LogSqlDependency("Company SQL server", "Stock Database", "SELECT ProductName FROM Products", isSuccessful: true, startTime: startTime, duration: durationMeasurement.Elapsed);
// Output: {"DependencyType": "Sql", "DependencyName": "Stock Database", "DependencyData": "SELECT ProductName FROM Products", "TargetName": "Company SQL Server", "Duration": "00:00:01.2396312", "StartTime": "03/23/2020 09:32:02 +00:00", "IsSuccessful": true, "Context": {}}
logger.LogSqlDependency("Company SQL Server", "Stock Database", "GET ProductName FROM Products", "Get product names", isSuccessful: true, startTime: startTime, duration: durationMeasurement.Elapsed);
// Output: {"DependencyType": "Sql", "DependencyName": "Stock Database/Get product names", "DependencyData": "GET ProductName FROM Products", "TargetName": "Company SQL Server", "Duration": "00:00:01.2396312", "StartTime": "03/23/2020 09:32:02 +00:00", "IsSuccessful": true, "Context": {}}
```

Or alternatively, when one already got the SQL connection string, you can use the overload that takes this directly:
Expand Down Expand Up @@ -286,8 +286,8 @@ durationMeasurement.Start();
// Interact with database
var products = await _repository.GetProducts();

logger.LogSqlDependency(connectionString, "SELECT ProductName FROM Products", isSuccessful: true, startTime, durationMeasurement.Elapsed);
// Output: {"DependencyType": "Sql", "DependencyName": "Stock Database", "DependencyData": "SELECT ProductName FROM Proucts", "TargetName": "Company SQL Server", "Duration": "00:00:01.2396312", "StartTime": "03/23/2020 09:32:02 +00:00", "IsSuccessful": true, "Context": {}}
logger.LogSqlDependency(connectionString, "GET ProductName FROM Products", "Get product names", isSuccessful: true, measurement: measurement);
// Output: {"DependencyType": "Sql", "DependencyName": "Stock Database/Get product names", "DependencyData": "GET ProductName FROM Products", "TargetName": "Company SQL Server", "Duration": "00:00:01.2396312", "StartTime": "03/23/2020 09:32:02 +00:00", "IsSuccessful": true, "Context": {}}
```

### Measuring custom dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static partial class ILoggerExtensions
/// <exception cref="ArgumentException">
/// Thrown when the <paramref name="serverName"/>, <paramref name="databaseName"/>, <paramref name="tableName"/>, or <paramref name="operationName"/> is blank.
/// </exception>
[Obsolete("Use the " + nameof(LogSqlDependency) + " with a pseudo SQL command instead of specifying the table and operation name")]
[Obsolete("Use the " + nameof(LogSqlDependency) + " with a pseudo SQL command and operation name instead of specifying the table name")]
public static void LogSqlDependency(
this ILogger logger,
string serverName,
Expand Down Expand Up @@ -92,6 +92,7 @@ public static partial class ILoggerExtensions
/// <param name="context">The context that provides more insights on the dependency that was measured.</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="logger"/> or <paramref name="measurement"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">Thrown when the <paramref name="serverName"/> or <paramref name="databaseName"/> is blank.</exception>
[Obsolete("Use the " + nameof(LogSqlDependency) + " with a pseudo SQL command and operation name instead of specifying the table name")]
public static void LogSqlDependency(
this ILogger logger,
string serverName,
Expand Down Expand Up @@ -122,6 +123,7 @@ public static partial class ILoggerExtensions
/// <param name="context">The context that provides more insights on the dependency that was measured.</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="logger"/> or <paramref name="measurement"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">Thrown when the <paramref name="serverName"/> or <paramref name="databaseName"/> is blank.</exception>
[Obsolete("Use the " + nameof(LogSqlDependency) + " with a pseudo SQL command and operation name instead of specifying the table name")]
public static void LogSqlDependency(
this ILogger logger,
string serverName,
Expand Down Expand Up @@ -157,7 +159,7 @@ public static partial class ILoggerExtensions
/// Thrown when the <paramref name="serverName"/>, <paramref name="databaseName"/>, <paramref name="tableName"/>, or <paramref name="operationName"/> is blank.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the <paramref name="duration"/> is a negative time range.</exception>
[Obsolete("Use the " + nameof(LogSqlDependency) + " with a pseudo SQL command instead of specifying the table and operation name")]
[Obsolete("Use the " + nameof(LogSqlDependency) + " with a pseudo SQL command and operation name instead of specifying the table name")]
public static void LogSqlDependency(
this ILogger logger,
string serverName,
Expand Down Expand Up @@ -206,6 +208,7 @@ public static partial class ILoggerExtensions
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="logger"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">Thrown when the <paramref name="serverName"/> or <paramref name="databaseName"/> is blank.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the <paramref name="duration"/> is a negative time range.</exception>
[Obsolete("Use the " + nameof(LogSqlDependency) + " with a pseudo SQL command and operation name instead of specifying the table name")]
public static void LogSqlDependency(
this ILogger logger,
string serverName,
Expand Down Expand Up @@ -241,6 +244,7 @@ public static partial class ILoggerExtensions
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="logger"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">Thrown when the <paramref name="serverName"/> or <paramref name="databaseName"/> is blank.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the <paramref name="duration"/> is a negative time range.</exception>
[Obsolete("Use the " + nameof(LogSqlDependency) + " with a pseudo SQL command and operation name instead of specifying the table name")]
public static void LogSqlDependency(
this ILogger logger,
string serverName,
Expand Down Expand Up @@ -271,5 +275,88 @@ public static partial class ILoggerExtensions
isSuccessful: isSuccessful,
context: context));
}

/// <summary>
/// Logs a SQL dependency.
/// </summary>
/// <param name="logger">The logger to track the telemetry.</param>
/// <param name="serverName">The name of server hosting the database.</param>
/// <param name="databaseName">The name of database.</param>
/// <param name="sqlCommand">The pseudo SQL command information that gets executed against the SQL dependency.</param>
/// <param name="operationName">The functional description of the operation that was performed on the SQL database.</param>
/// <param name="isSuccessful">The indication whether or not the operation was successful.</param>
/// <param name="measurement">The measuring the latency to call the SQL dependency.</param>
/// <param name="dependencyId">The ID of the dependency to link as parent ID.</param>
/// <param name="context">The context that provides more insights on the dependency that was measured.</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="logger"/> or the <paramref name="measurement"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">Thrown when the <paramref name="serverName"/> or <paramref name="databaseName"/> is blank.</exception>
public static void LogSqlDependency(
this ILogger logger,
string serverName,
string databaseName,
string sqlCommand,
string operationName,
bool isSuccessful,
DurationMeasurement measurement,
string dependencyId,
Dictionary<string, object> context = null)
{
Guard.NotNull(logger, nameof(logger), "Requires a logger instance to track telemetry");
Guard.NotNullOrWhitespace(serverName, nameof(serverName), "Requires a non-blank SQL server name to track a SQL dependency");
Guard.NotNullOrWhitespace(databaseName, nameof(databaseName), "Requires a non-blank SQL database name to track a SQL dependency");
Guard.NotNull(measurement, nameof(measurement), "Requires a dependency measurement instance to measure the latency of the SQL storage when tracking an SQL dependency");

context = context ?? new Dictionary<string, object>();

LogSqlDependency(logger, serverName, databaseName, sqlCommand, operationName, isSuccessful, measurement.StartTime, measurement.Elapsed, dependencyId, context);
}

/// <summary>
/// Logs a SQL dependency.
/// </summary>
/// <param name="logger">The logger to track the telemetry.</param>
/// <param name="serverName">The name of server hosting the database.</param>
/// <param name="databaseName">The name of database.</param>
/// <param name="sqlCommand">The pseudo SQL command information that gets executed against the SQL dependency.</param>
/// <param name="operationName">The functional description of the operation that was performed on the SQL database.</param>
/// <param name="isSuccessful">The indication whether or not the operation was successful.</param>
/// <param name="startTime">The point in time when the interaction with the SQL dependency was started.</param>
/// <param name="duration">The duration of the operation.</param>
/// <param name="dependencyId">The ID of the dependency to link as parent ID.</param>
/// <param name="context">The context that provides more insights on the dependency that was measured.</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="logger"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">Thrown when the <paramref name="serverName"/> or <paramref name="databaseName"/> is blank.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the <paramref name="duration"/> is a negative time range.</exception>
public static void LogSqlDependency(
this ILogger logger,
string serverName,
string databaseName,
string sqlCommand,
string operationName,
bool isSuccessful,
DateTimeOffset startTime,
TimeSpan duration,
string dependencyId,
Dictionary<string, object> context = null)
{
Guard.NotNull(logger, nameof(logger), "Requires a logger instance to track telemetry");
Guard.NotNullOrWhitespace(serverName, nameof(serverName), "Requires a non-blank SQL server name to track a SQL dependency");
Guard.NotNullOrWhitespace(databaseName, nameof(databaseName), "Requires a non-blank SQL database name to track a SQL dependency");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the SQL dependency operation");

context = context ?? new Dictionary<string, object>();

logger.LogWarning(MessageFormats.SqlDependencyFormat, new DependencyLogEntry(
dependencyType: "Sql",
targetName: serverName,
dependencyName: databaseName + "/" + operationName,
dependencyData: sqlCommand,
duration: duration,
startTime: startTime,
dependencyId: dependencyId,
resultCode: null,
isSuccessful: isSuccessful,
context: context));
}
}
}
Loading

0 comments on commit 542301c

Please sign in to comment.