Skip to content

Commit

Permalink
Cosmos: Add diagnostic events with statistics
Browse files Browse the repository at this point in the history
Fixes #17298
  • Loading branch information
AndriySvyryd committed Aug 5, 2021
1 parent 15167f6 commit c74f3c1
Show file tree
Hide file tree
Showing 12 changed files with 910 additions and 66 deletions.
93 changes: 86 additions & 7 deletions src/EFCore.Cosmos/Diagnostics/CosmosEventId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,116 @@ public static class CosmosEventId
// Try to use <Noun><Verb> naming and be consistent with existing names.
private enum Id
{
// Update events
// Database events

// Query events
// These events are actually in Event in `DbLoggerCategory.Database.Command`.
// Leaving the ID unchanged to avoid changing it after release.
// Command events
ExecutingSqlQuery = CoreEventId.ProviderBaseId + 100,
ExecutingReadItem
ExecutingReadItem,
ExecutedReadNext,
ExecutedReadItem,
ExecutedCreateItem,
ExecutedReplaceItem,
ExecutedDeleteItem
}

private static readonly string _commandPrefix = DbLoggerCategory.Database.Command.Name + ".";

/// <summary>
/// <para>
/// A SQL query was executed.
/// A SQL query is going to be executed.
/// </para>
/// <para>
/// This event is in the <see cref="DbLoggerCategory.Database.Command" /> category.
/// </para>
/// <para>
/// This event uses the <see cref="CosmosQueryEventData" /> payload when used with a <see cref="DiagnosticSource" />.
/// </para>
/// </summary>
public static readonly EventId ExecutingSqlQuery
= new((int)Id.ExecutingSqlQuery, _commandPrefix + Id.ExecutingSqlQuery);

/// <summary>
/// <para>
/// ReadItem was executed.
/// ReadItem is going to be executed.
/// </para>
/// <para>
/// This event is in the <see cref="DbLoggerCategory.Database.Command" /> category.
/// </para>
/// <para>
/// This event uses the <see cref="CosmosReadItemEventData" /> payload when used with a <see cref="DiagnosticSource" />.
/// </para>
/// </summary>
public static readonly EventId ExecutingReadItem
= new((int)Id.ExecutingReadItem, _commandPrefix + Id.ExecutingReadItem);

/// <summary>
/// <para>
/// ReadNext was executed.
/// </para>
/// <para>
/// This event is in the <see cref="DbLoggerCategory.Database.Command" /> category.
/// </para>
/// <para>
/// This event uses the <see cref="CosmosQueryExecutedEventData" /> payload when used with a <see cref="DiagnosticSource" />.
/// </para>
/// </summary>
public static readonly EventId ExecutedReadNext
= new((int)Id.ExecutedReadNext, _commandPrefix + Id.ExecutedReadNext);

/// <summary>
/// <para>
/// ReadItem was executed.
/// </para>
/// <para>
/// This event is in the <see cref="DbLoggerCategory.Database.Command" /> category.
/// </para>
/// <para>
/// This event uses the <see cref="CosmosItemCommandExecutedEventData" /> payload when used with a <see cref="DiagnosticSource" />.
/// </para>
/// </summary>
public static readonly EventId ExecutedReadItem
= new((int)Id.ExecutedReadItem, _commandPrefix + Id.ExecutedReadItem);

/// <summary>
/// <para>
/// CreateItem was executed.
/// </para>
/// <para>
/// This event is in the <see cref="DbLoggerCategory.Database.Command" /> category.
/// </para>
/// <para>
/// This event uses the <see cref="CosmosItemCommandExecutedEventData" /> payload when used with a <see cref="DiagnosticSource" />.
/// </para>
/// </summary>
public static readonly EventId ExecutedCreateItem
= new((int)Id.ExecutedCreateItem, _commandPrefix + Id.ExecutedCreateItem);

/// <summary>
/// <para>
/// ReplaceItem was executed.
/// </para>
/// <para>
/// This event is in the <see cref="DbLoggerCategory.Database.Command" /> category.
/// </para>
/// <para>
/// This event uses the <see cref="CosmosItemCommandExecutedEventData" /> payload when used with a <see cref="DiagnosticSource" />.
/// </para>
/// </summary>
public static readonly EventId ExecutedReplaceItem
= new((int)Id.ExecutedReplaceItem, _commandPrefix + Id.ExecutedReplaceItem);

/// <summary>
/// <para>
/// DeleteItem was executed.
/// </para>
/// <para>
/// This event is in the <see cref="DbLoggerCategory.Database.Command" /> category.
/// </para>
/// <para>
/// This event uses the <see cref="CosmosItemCommandExecutedEventData" /> payload when used with a <see cref="DiagnosticSource" />.
/// </para>
/// </summary>
public static readonly EventId ExecutedDeleteItem
= new((int)Id.ExecutedDeleteItem, _commandPrefix + Id.ExecutedDeleteItem);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;

namespace Microsoft.EntityFrameworkCore.Diagnostics
{
/// <summary>
/// A <see cref="DiagnosticSource" /> event payload class for Cosmos item command executed events.
/// </summary>
public class CosmosItemCommandExecutedEventData : EventData
{
/// <summary>
/// Constructs the event payload.
/// </summary>
/// <param name="eventDefinition"> The event definition. </param>
/// <param name="messageGenerator"> A delegate that generates a log message for this event. </param>
/// <param name="elapsed"> The time elapsed since the command was sent to the database. </param>
/// <param name="requestCharge"> The request charge in RU. </param>
/// <param name="activityId"> The activity ID. </param>
/// <param name="resourceId"> The ID of the resource being read. </param>
/// <param name="containerId"> The ID of the Cosmos container being queried. </param>
/// <param name="partitionKey"> The key of the Cosmos partition that the query is using. </param>
/// <param name="logSensitiveData"> Indicates whether the application allows logging of sensitive data. </param>
public CosmosItemCommandExecutedEventData(
EventDefinitionBase eventDefinition,
Func<EventDefinitionBase, EventData, string> messageGenerator,
TimeSpan elapsed,
double requestCharge,
string activityId,
string containerId,
string resourceId,
string? partitionKey,
bool logSensitiveData)
: base(eventDefinition, messageGenerator)
{
Elapsed = elapsed;
RequestCharge = requestCharge;
ActivityId = activityId;
ContainerId = containerId;
ResourceId = resourceId;
PartitionKey = partitionKey;
LogSensitiveData = logSensitiveData;
}

/// <summary>
/// The time elapsed since the command was sent to the database.
/// </summary>
public TimeSpan Elapsed { get; }

/// <summary>
/// The request charge in RU.
/// </summary>
public double RequestCharge { get; }

/// <summary>
/// The activity ID.
/// </summary>
public string ActivityId { get; }

/// <summary>
/// The ID of the Cosmos container being queried.
/// </summary>
public virtual string ContainerId { get; }

/// <summary>
/// The ID of the resource being read.
/// </summary>
public virtual string ResourceId { get; }

/// <summary>
/// The key of the Cosmos partition that the query is using.
/// </summary>
public virtual string? PartitionKey { get; }

/// <summary>
/// Indicates whether the application allows logging of sensitive data.
/// </summary>
public virtual bool LogSensitiveData { get; }
}
}
6 changes: 2 additions & 4 deletions src/EFCore.Cosmos/Diagnostics/CosmosQueryEventData.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Microsoft.EntityFrameworkCore.Diagnostics
Expand All @@ -21,7 +19,7 @@ public class CosmosQueryEventData : EventData
/// <param name="partitionKey"> The key of the Cosmos partition that the query is using. </param>
/// <param name="parameters"> Name/values for each parameter in the Cosmos Query. </param>
/// <param name="querySql"> The SQL representing the query. </param>
/// <param name="logSensitiveData"> Indicates whether or not the application allows logging of sensitive data. </param>
/// <param name="logSensitiveData"> Indicates whether the application allows logging of sensitive data. </param>
public CosmosQueryEventData(
EventDefinitionBase eventDefinition,
Func<EventDefinitionBase, EventData, string> messageGenerator,
Expand Down Expand Up @@ -60,7 +58,7 @@ public class CosmosQueryEventData : EventData
public virtual string QuerySql { get; }

/// <summary>
/// Indicates whether or not the application allows logging of sensitive data.
/// Indicates whether the application allows logging of sensitive data.
/// </summary>
public virtual bool LogSensitiveData { get; }
}
Expand Down
89 changes: 89 additions & 0 deletions src/EFCore.Cosmos/Diagnostics/CosmosQueryExecutedEventData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;

namespace Microsoft.EntityFrameworkCore.Diagnostics
{
/// <summary>
/// A <see cref="DiagnosticSource" /> event payload class for Cosmos query events.
/// </summary>
public class CosmosQueryExecutedEventData : EventData
{
/// <summary>
/// Constructs the event payload.
/// </summary>
/// <param name="eventDefinition"> The event definition. </param>
/// <param name="messageGenerator"> A delegate that generates a log message for this event. </param>
/// <param name="elapsed"> The time elapsed since the command was sent to the database. </param>
/// <param name="requestCharge"> The request charge in RU. </param>
/// <param name="activityId"> The activity ID. </param>
/// <param name="containerId"> The ID of the Cosmos container being queried. </param>
/// <param name="partitionKey"> The key of the Cosmos partition that the query is using. </param>
/// <param name="parameters"> Name/values for each parameter in the Cosmos Query. </param>
/// <param name="querySql"> The SQL representing the query. </param>
/// <param name="logSensitiveData"> Indicates whether the application allows logging of sensitive data. </param>
public CosmosQueryExecutedEventData(
EventDefinitionBase eventDefinition,
Func<EventDefinitionBase, EventData, string> messageGenerator,
TimeSpan elapsed,
double requestCharge,
string activityId,
string containerId,
string? partitionKey,
IReadOnlyList<(string Name, object? Value)> parameters,
string querySql,
bool logSensitiveData)
: base(eventDefinition, messageGenerator)
{
Elapsed = elapsed;
RequestCharge = requestCharge;
ActivityId = activityId;
ContainerId = containerId;
PartitionKey = partitionKey;
Parameters = parameters;
QuerySql = querySql;
LogSensitiveData = logSensitiveData;
}

/// <summary>
/// The time elapsed since the command was sent to the database.
/// </summary>
public virtual TimeSpan Elapsed { get; }

/// <summary>
/// The request charge in RU.
/// </summary>
public virtual double RequestCharge { get; }

/// <summary>
/// The activity ID.
/// </summary>
public virtual string ActivityId { get; }

/// <summary>
/// The ID of the Cosmos container being queried.
/// </summary>
public virtual string ContainerId { get; }

/// <summary>
/// The key of the Cosmos partition that the query is using.
/// </summary>
public virtual string? PartitionKey { get; }

/// <summary>
/// Name/values for each parameter in the Cosmos Query.
/// </summary>
public virtual IReadOnlyList<(string Name, object? Value)> Parameters { get; }

/// <summary>
/// The SQL representing the query.
/// </summary>
public virtual string QuerySql { get; }

/// <summary>
/// Indicates whether the application allows logging of sensitive data.
/// </summary>
public virtual bool LogSensitiveData { get; }
}
}
5 changes: 2 additions & 3 deletions src/EFCore.Cosmos/Diagnostics/CosmosReadItemEventData.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics;

namespace Microsoft.EntityFrameworkCore.Diagnostics
Expand All @@ -19,7 +18,7 @@ public class CosmosReadItemEventData : EventData
/// <param name="resourceId"> The ID of the resource being read. </param>
/// <param name="containerId"> The ID of the Cosmos container being queried. </param>
/// <param name="partitionKey"> The key of the Cosmos partition that the query is using. </param>
/// <param name="logSensitiveData"> Indicates whether or not the application allows logging of sensitive data. </param>
/// <param name="logSensitiveData"> Indicates whether the application allows logging of sensitive data. </param>
public CosmosReadItemEventData(
EventDefinitionBase eventDefinition,
Func<EventDefinitionBase, EventData, string> messageGenerator,
Expand Down Expand Up @@ -51,7 +50,7 @@ public class CosmosReadItemEventData : EventData
public virtual string? PartitionKey { get; }

/// <summary>
/// Indicates whether or not the application allows logging of sensitive data.
/// Indicates whether the application allows logging of sensitive data.
/// </summary>
public virtual bool LogSensitiveData { get; }
}
Expand Down
Loading

0 comments on commit c74f3c1

Please sign in to comment.