| | | 1 | | using System.Data; |
| | | 2 | | using System.Data.Common; |
| | | 3 | | using Elsa.Diagnostics.StructuredLogs.Contracts; |
| | | 4 | | using Elsa.Diagnostics.StructuredLogs.Models; |
| | | 5 | | using Elsa.Diagnostics.StructuredLogs.Persistence.Relational.Contracts; |
| | | 6 | | using Elsa.Diagnostics.StructuredLogs.Persistence.Relational.Models; |
| | | 7 | | using Elsa.Diagnostics.StructuredLogs.Persistence.Relational.Services; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Diagnostics.StructuredLogs.Persistence.Relational.Stores; |
| | | 10 | | |
| | 1 | 11 | | public class RelationalStructuredLogStore( |
| | 1 | 12 | | IRelationalStructuredLogConnectionFactory connectionFactory, |
| | 1 | 13 | | IRelationalStructuredLogDialect dialect, |
| | 1 | 14 | | RelationalStructuredLogSqlBuilder sqlBuilder, |
| | 1 | 15 | | RelationalStructuredLogMapper mapper) : IStructuredLogStore |
| | | 16 | | { |
| | | 17 | | public async ValueTask WriteAsync(StructuredLogEvent logEvent, CancellationToken cancellationToken = default) |
| | | 18 | | { |
| | 0 | 19 | | await using var connection = await connectionFactory.OpenConnectionAsync(cancellationToken); |
| | 0 | 20 | | await InsertAsync(connection, mapper.Map(logEvent), null, cancellationToken); |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | public async ValueTask WriteManyAsync(IReadOnlyCollection<StructuredLogEvent> logEvents, CancellationToken cancellat |
| | | 24 | | { |
| | 0 | 25 | | if (logEvents.Count == 0) |
| | 0 | 26 | | return; |
| | | 27 | | |
| | 0 | 28 | | await using var connection = await connectionFactory.OpenConnectionAsync(cancellationToken); |
| | 0 | 29 | | await using var transaction = await connection.BeginTransactionAsync(cancellationToken); |
| | | 30 | | |
| | 0 | 31 | | foreach (var logEvent in logEvents) |
| | 0 | 32 | | await InsertAsync(connection, mapper.Map(logEvent), transaction, cancellationToken); |
| | | 33 | | |
| | 0 | 34 | | await transaction.CommitAsync(cancellationToken); |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | public async ValueTask<RecentStructuredLogsResult> QueryAsync(StructuredLogFilter filter, CancellationToken cancella |
| | | 38 | | { |
| | 0 | 39 | | var query = sqlBuilder.BuildQuery(filter); |
| | 0 | 40 | | await using var connection = await connectionFactory.OpenConnectionAsync(cancellationToken); |
| | 0 | 41 | | await using var command = CreateCommand(connection, query.Sql, query.Parameters); |
| | 0 | 42 | | await using var reader = await command.ExecuteReaderAsync(cancellationToken); |
| | 0 | 43 | | var items = new List<StructuredLogEvent>(); |
| | | 44 | | |
| | 0 | 45 | | while (await reader.ReadAsync(cancellationToken)) |
| | 0 | 46 | | items.Add(mapper.Map(reader)); |
| | | 47 | | |
| | 0 | 48 | | items.Reverse(); |
| | 0 | 49 | | return new(items, 0); |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | public async ValueTask<IReadOnlyCollection<StructuredLogSource>> ListSourcesAsync(CancellationToken cancellationToke |
| | | 53 | | { |
| | 0 | 54 | | await using var connection = await connectionFactory.OpenConnectionAsync(cancellationToken); |
| | 0 | 55 | | await using var command = CreateCommand(connection, sqlBuilder.BuildListSources()); |
| | 0 | 56 | | await using var reader = await command.ExecuteReaderAsync(cancellationToken); |
| | 0 | 57 | | var sources = new List<StructuredLogSource>(); |
| | | 58 | | |
| | 0 | 59 | | while (await reader.ReadAsync(cancellationToken)) |
| | | 60 | | { |
| | 0 | 61 | | var sourceId = reader.GetString(reader.GetOrdinal("SourceId")); |
| | 0 | 62 | | var lastSeen = RelationalStructuredLogMapper.ParseTimestamp(reader.GetString(reader.GetOrdinal("LastSeen"))) |
| | 0 | 63 | | sources.Add(new() |
| | 0 | 64 | | { |
| | 0 | 65 | | Id = sourceId, |
| | 0 | 66 | | DisplayName = sourceId, |
| | 0 | 67 | | MachineName = sourceId, |
| | 0 | 68 | | ProcessId = 0, |
| | 0 | 69 | | LastSeen = lastSeen, |
| | 0 | 70 | | Status = StructuredLogSourceStatus.Connected |
| | 0 | 71 | | }); |
| | | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | return sources; |
| | 0 | 75 | | } |
| | | 76 | | |
| | | 77 | | private async ValueTask InsertAsync(DbConnection connection, RelationalStructuredLogRecord record, DbTransaction? tr |
| | | 78 | | { |
| | 0 | 79 | | await using var command = CreateCommand(connection, sqlBuilder.BuildInsert(), CreateParameters(record)); |
| | 0 | 80 | | command.Transaction = transaction; |
| | 0 | 81 | | await command.ExecuteNonQueryAsync(cancellationToken); |
| | 0 | 82 | | } |
| | | 83 | | |
| | | 84 | | private DbCommand CreateCommand(DbConnection connection, string sql, IReadOnlyDictionary<string, object?>? parameter |
| | | 85 | | { |
| | 0 | 86 | | var command = connection.CreateCommand(); |
| | 0 | 87 | | command.CommandText = sql; |
| | 0 | 88 | | command.CommandType = CommandType.Text; |
| | | 89 | | |
| | 0 | 90 | | if (parameters == null) |
| | 0 | 91 | | return command; |
| | | 92 | | |
| | 0 | 93 | | foreach (var (name, value) in parameters) |
| | | 94 | | { |
| | 0 | 95 | | var parameter = command.CreateParameter(); |
| | 0 | 96 | | parameter.ParameterName = name.StartsWith(dialect.ParameterPrefix, StringComparison.Ordinal) ? name : $"{dia |
| | 0 | 97 | | parameter.Value = value ?? DBNull.Value; |
| | 0 | 98 | | command.Parameters.Add(parameter); |
| | | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | return command; |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | private static IReadOnlyDictionary<string, object?> CreateParameters(RelationalStructuredLogRecord record) |
| | | 105 | | { |
| | 0 | 106 | | return new Dictionary<string, object?> |
| | 0 | 107 | | { |
| | 0 | 108 | | ["Id"] = record.Id, |
| | 0 | 109 | | ["Sequence"] = record.Sequence, |
| | 0 | 110 | | ["Timestamp"] = record.Timestamp, |
| | 0 | 111 | | ["ReceivedAt"] = record.ReceivedAt, |
| | 0 | 112 | | ["Level"] = (int)record.Level, |
| | 0 | 113 | | ["Category"] = record.Category, |
| | 0 | 114 | | ["EventId"] = record.EventId, |
| | 0 | 115 | | ["EventName"] = record.EventName, |
| | 0 | 116 | | ["Message"] = record.Message, |
| | 0 | 117 | | ["MessageTemplate"] = record.MessageTemplate, |
| | 0 | 118 | | ["ExceptionJson"] = record.ExceptionJson, |
| | 0 | 119 | | ["ScopesJson"] = record.ScopesJson, |
| | 0 | 120 | | ["PropertiesJson"] = record.PropertiesJson, |
| | 0 | 121 | | ["TraceId"] = record.TraceId, |
| | 0 | 122 | | ["SpanId"] = record.SpanId, |
| | 0 | 123 | | ["CorrelationId"] = record.CorrelationId, |
| | 0 | 124 | | ["TenantId"] = record.TenantId, |
| | 0 | 125 | | ["WorkflowDefinitionId"] = record.WorkflowDefinitionId, |
| | 0 | 126 | | ["WorkflowInstanceId"] = record.WorkflowInstanceId, |
| | 0 | 127 | | ["SourceId"] = record.SourceId |
| | 0 | 128 | | }; |
| | | 129 | | } |
| | | 130 | | } |