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

feat: add dependency id to sql dep tracking #376

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,66 @@ public static void LogSqlDependency(
LogSqlDependency(logger, serverName, databaseName, tableName, operationName, isSuccessful, measurement.StartTime, measurement.Elapsed, 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="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="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>
public static void LogSqlDependency(
this ILogger logger,
string serverName,
string databaseName,
string sqlCommand,
bool isSuccessful,
DurationMeasurement measurement,
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");

LogSqlDependency(logger, serverName, databaseName, sqlCommand, isSuccessful, measurement.StartTime, measurement.Elapsed, 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="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 <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,
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");

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

/// <summary>
/// Logs a SQL dependency
/// </summary>
Expand Down Expand Up @@ -129,5 +189,85 @@ public static void LogSqlDependency(
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="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="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,
bool isSuccessful,
DateTimeOffset startTime,
TimeSpan duration,
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>();

LogSqlDependency(logger, serverName, databaseName, sqlCommand, isSuccessful, startTime, duration, dependencyId: null, 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="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,
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,
dependencyData: sqlCommand,
duration: duration,
startTime: startTime,
dependencyId: dependencyId,
resultCode: null,
isSuccessful: isSuccessful,
context: context));
}
}
}
114 changes: 113 additions & 1 deletion src/Arcus.Observability.Telemetry.Sql/Extensions/ILoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,61 @@ public static void LogSqlDependency(
}

/// <summary>
/// Logs a SQL dependency
/// Logs a SQL dependency.
/// </summary>
/// <param name="logger">The logger instance to track the SQL dependency.</param>
/// <param name="connectionString">The SQL connection string.</param>
/// <param name="sqlCommand">The pseudo SQL command information that gets executed against the SQL dependency.</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="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="connectionString"/> is blank.</exception>
public static void LogSqlDependency(
this ILogger logger,
string connectionString,
string sqlCommand,
bool isSuccessful,
DurationMeasurement measurement,
Dictionary<string, object> context = null)
{
Guard.NotNull(logger, nameof(logger), "Requires a logger instance to track telemetry");
Guard.NotNullOrWhitespace(connectionString, nameof(connectionString), "Requires a SQL connection string to retrieve database information while tracking the SQL dependency");
Guard.NotNull(measurement, nameof(measurement));

LogSqlDependency(logger, connectionString, sqlCommand, isSuccessful, measurement.StartTime, measurement.Elapsed, context);
}

/// <summary>
/// Logs a SQL dependency.
/// </summary>
/// <param name="logger">The logger instance to track the SQL dependency.</param>
/// <param name="connectionString">The SQL connection string.</param>
/// <param name="sqlCommand">The pseudo SQL command information that gets executed against the SQL dependency.</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="connectionString"/> is blank.</exception>
public static void LogSqlDependency(
this ILogger logger,
string connectionString,
string sqlCommand,
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(connectionString, nameof(connectionString), "Requires a SQL connection string to retrieve database information while tracking the SQL dependency");
Guard.NotNull(measurement, nameof(measurement));

LogSqlDependency(logger, connectionString, sqlCommand, isSuccessful, measurement.StartTime, measurement.Elapsed, dependencyId, context);
}

/// <summary>
/// Logs a SQL dependency.
/// </summary>
/// <param name="logger">The logger to track the SQL dependency.</param>
/// <param name="connectionString">The SQL connection string.</param>
Expand Down Expand Up @@ -98,5 +152,63 @@ public static void LogSqlDependency(
var connection = new SqlConnectionStringBuilder(connectionString);
logger.LogSqlDependency(connection.DataSource, connection.InitialCatalog, tableName, operationName, isSuccessful, startTime, duration, context);
}

/// <summary>
/// Logs a SQL dependency.
/// </summary>
/// <param name="logger">The logger to track the SQL dependency.</param>
/// <param name="connectionString">The SQL connection string.</param>
/// <param name="sqlCommand">The pseudo SQL command information that gets executed against the SQL dependency.</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 HTTP dependency was started</param>
/// <param name="duration">The duration of the operation</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="connectionString"/> is blank.</exception>
public static void LogSqlDependency(
this ILogger logger,
string connectionString,
string sqlCommand,
bool isSuccessful,
DateTimeOffset startTime,
TimeSpan duration,
Dictionary<string, object> context = null)
{
Guard.NotNull(logger, nameof(logger), "Requires a logger instance to track telemetry");
Guard.NotNullOrWhitespace(connectionString, nameof(connectionString), "Requires a SQL connection string to retrieve database information while tracking the SQL dependency");

var connection = new SqlConnectionStringBuilder(connectionString);
logger.LogSqlDependency(connection.DataSource, connection.InitialCatalog, sqlCommand, isSuccessful, startTime, duration, context);
}

/// <summary>
/// Logs a SQL dependency.
/// </summary>
/// <param name="logger">The logger to track the SQL dependency.</param>
/// <param name="connectionString">The SQL connection string.</param>
/// <param name="sqlCommand">The pseudo SQL command information that gets executed against the SQL dependency.</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 HTTP 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="connectionString"/> is blank.</exception>
public static void LogSqlDependency(
this ILogger logger,
string connectionString,
string sqlCommand,
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(connectionString, nameof(connectionString), "Requires a SQL connection string to retrieve database information while tracking the SQL dependency");

var connection = new SqlConnectionStringBuilder(connectionString);
logger.LogSqlDependency(connection.DataSource, connection.InitialCatalog, sqlCommand, isSuccessful, startTime, duration, dependencyId, context);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<ItemGroup>
<ProjectReference Include="..\Arcus.Observability.Telemetry.AspNetCore\Arcus.Observability.Telemetry.AspNetCore.csproj" />
<ProjectReference Include="..\Arcus.Observability.Telemetry.Core\Arcus.Observability.Telemetry.Core.csproj" />
<ProjectReference Include="..\Arcus.Observability.Telemetry.Sql\Arcus.Observability.Telemetry.Sql.csproj" />
<ProjectReference Include="..\Arcus.Observability.Telemetry.Serilog.Filters\Arcus.Observability.Telemetry.Serilog.Filters.csproj" />
<ProjectReference Include="..\Arcus.Observability.Telemetry.Serilog.Sinks.ApplicationInsights\Arcus.Observability.Telemetry.Serilog.Sinks.ApplicationInsights.csproj" />
<ProjectReference Include="..\Arcus.Observability.Tests.Core\Arcus.Observability.Tests.Core.csproj" />
Expand Down
Loading