Exemple #1
0
        public void OnCommandError(CommandErrorEventData payload)
        {
            var operation  = payload.Command.CommandText.Split().First().ToUpper();
            var parameters = payload.Command.Parameters.OfType <DbParameter>().Select(param => new {
                value = param.Value,
                name  = param.ParameterName,
                type  = Enum.GetName(typeof(DbType), param.DbType),
            }).ToList();

            // do not report unnecessary events
            if (operation == "PRAGMA")
            {
                return;
            }

            using (var scope = GlobalTracer.Instance
                               .BuildSpan(operation)
                               .WithStartTimestamp(payload.StartTime.UtcDateTime)
                               .StartActive(finishSpanOnDispose: true)) {
                scope.Span.SetTag("event.id", Guid.NewGuid().ToString());
                scope.Span.SetTag("event.origin", "system-data");
                scope.Span.SetTag("resource.name", payload.Command.Connection.Database);
                scope.Span.SetTag("resource.type", "database");
                scope.Span.SetTag("resource.operation", operation);
                scope.Span.SetTag("sql.driver", payload.Command.Connection.GetType().FullName);
                scope.Span.SetTag("sql.statement", payload.Command.CommandText);
                scope.Span.SetTag("sql.table_name", TableNameExtractor.ExtractTableName(payload.Command.CommandText));
                scope.Span.SetTag("sql.connection_string", payload.Command.Connection.ConnectionString);
                scope.Span.SetDataIfNeeded("sql.parameters", parameters);

                scope.Span.AddException(payload.Exception);
            }
        }
        public Task CommandFailedAsync(DbCommand command, CommandErrorEventData eventData,
                                       CancellationToken cancellationToken = new CancellationToken())
        {
            TrackEvent(nameof(DataReaderDisposing), eventData.Connection, eventData.ConnectionId, eventData.Duration);

            return(Task.CompletedTask);
        }
Exemple #3
0
        private void HandleCommandError(CommandErrorEventData eventData)
        {
            if (_commands.TryRemove(eventData.CommandId, out var commandMetric))
            {
                commandMetric.Record(eventData);

                if (LogExceptions && (eventData.Exception != null))
                {
                    if (commandMetric.ShortenedQuery.Length < commandMetric.Query.Length)
                    {
                        Log.Write(_configuration.ExceptionSeverity, LogSystem, commandMetric.MessageSourceProvider, null, eventData.Exception, LogWriteMode.Queued, null, LogCategory,
                                  "Database Call failed due to " + eventData.Exception.GetType() + ": " + commandMetric.ShortenedQuery,
                                  "Exception: {2}\r\n\r\nFull Query:\r\n\r\n{0}\r\n\r\nParameters: {1}\r\n\r\nServer:\r\n    DataSource: {3}\r\n    Database: {4}\r\n",
                                  commandMetric.Query, commandMetric.Parameters ?? "(none)", eventData.Exception.Message,
                                  commandMetric.Server, commandMetric.Database);
                    }
                    else
                    {
                        Log.Write(_configuration.ExceptionSeverity, LogSystem, commandMetric.MessageSourceProvider, null, eventData.Exception, LogWriteMode.Queued, null, LogCategory,
                                  "Database Call failed due to " + eventData.Exception.GetType() + ": " + commandMetric.ShortenedQuery,
                                  "Exception: {1}\r\n\r\nParameters: {0}\r\n\r\nServer:\r\n    DataSource: {2}\r\n    Database: {3}\r\n",
                                  commandMetric.Parameters ?? "(none)", eventData.Exception.Message, commandMetric.Server, commandMetric.Database);
                    }
                }
            }
        }
 public void CommandFailed(DbCommand command, CommandErrorEventData eventData)
 {
     for (var i = 0; i < _interceptors.Length; i++)
     {
         _interceptors[i].CommandFailed(command, eventData);
     }
 }
        public override async Task CommandFailedAsync(DbCommand command, CommandErrorEventData eventData, CancellationToken cancellationToken = default)
        {
            string log = $"异常:{eventData.Exception.Message}\r\n" +
                         $"语句:{command.CommandText}";

            _logger?.LogError(log);
            await base.CommandFailedAsync(command, eventData, cancellationToken);
        }
Exemple #6
0
 private void LogIfError(DbCommand command, CommandErrorEventData eventData)
 {
     if (eventData.Exception != null)
     {
         _logger.LogError(eventData.Exception, "Command  failed with exception, query: {0}, error:{1}", command.CommandText,
                          eventData.Exception);
     }
 }
 public async Task CommandFailedAsync(
     DbCommand command, CommandErrorEventData eventData, CancellationToken cancellationToken = default)
 {
     for (var i = 0; i < _interceptors.Length; i++)
     {
         await _interceptors[i].CommandFailedAsync(command, eventData, cancellationToken);
     }
 }
        public void Record(CommandErrorEventData eventData)
        {
            OnStop();

            Duration = eventData.Duration;
            Result   = eventData.Exception?.GetType().Name;
            EventMetric.Write(this);
        }
        public Task CommandFailedAsync(
            DbCommand command,
            CommandErrorEventData eventData,
            CancellationToken cancellationToken = default)
        {
            Assert.True(eventData.IsAsync);

            return(Task.CompletedTask);
        }
        public void DatabaseTelemetryInterceptor_CommandFailedAsync()
        {
            CommandErrorEventData eventData = null;

            // void >
            new DatabaseTelemetryInterceptor(
                new TelemetryClient(new TelemetryConfiguration()))
            .CommandFailedAsync(null, eventData);
            Assert.IsNull(eventData);
        }
Exemple #11
0
        /// <summary>
        /// Updated a Command event from the command failed data
        /// </summary>
        /// <param name="eventData">The event data</param>
        protected virtual void UpdateFailedEvent(CommandErrorEventData eventData)
        {
            var cmdEvent = _currentScope?.GetCommandEntityFrameworkEvent();

            if (cmdEvent == null)
            {
                return;
            }
            cmdEvent.Success      = false;
            cmdEvent.ErrorMessage = eventData.Exception?.GetExceptionInfo();
        }
        protected override void OnNext(string eventName, object untypedArg)
        {
            switch (eventName)
            {
            case "Microsoft.EntityFrameworkCore.Database.Command.CommandExecuting":
            {
                CommandEventData args = (CommandEventData)untypedArg;

                if (IgnoreEvent(args))
                {
                    Logger.LogDebug("Ignoring EF command due to IgnorePatterns");
                    return;
                }

                string operationName = _options.OperationNameResolver(args);

                Tracer.BuildSpan(operationName)
                .WithTag(Tags.SpanKind, Tags.SpanKindClient)
                .WithTag(Tags.Component, _options.ComponentName)
                .WithTag(Tags.DbInstance, args.Command.Connection.Database)
                .WithTag(Tags.DbStatement, args.Command.CommandText)
                .WithTag(TagMethod, args.ExecuteMethod.ToString())
                .WithTag(TagIsAsync, args.IsAsync)
                .StartActive();
            }
            break;

            case "Microsoft.EntityFrameworkCore.Database.Command.CommandExecuted":
            {
                DisposeActiveScope(isScopeRequired: false);
            }
            break;

            case "Microsoft.EntityFrameworkCore.Database.Command.CommandError":
            {
                CommandErrorEventData args = (CommandErrorEventData)untypedArg;

                // The "CommandExecuted" event is NOT called in case of an exception,
                // so we have to dispose the scope here as well!
                DisposeActiveScope(isScopeRequired: false, exception: args.Exception);
            }
            break;

            default:
            {
                ProcessUnhandledEvent(eventName, untypedArg);
            }
            break;
            }
        }
        public void CommandError([Object] CommandErrorEventData EventData)
        {
            if (EventData == null)
            {
                return;
            }
            var Context = GetCurrentContext(EventData.Command);

            if (Context != null)
            {
                Context?.Context?.Exceptions?.Add(EventData.Exception);
                TracingContext.Release(Context);
            }
        }
Exemple #14
0
        private void LogErrorSQL(DbCommand command, CommandErrorEventData eventData)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine(command.CommandText);
            if (command.Parameters.Count > 0)
            {
                sb.Append("参数:");
                foreach (SqlParameter item in command.Parameters)
                {
                    sb.AppendFormat("{0}:{1},\t{2},\t", item.ParameterName, item.Value, item.DbType.GetType());
                }
            }
            LogHelper.Error(sb.ToString(), eventData.Exception);
        }
Exemple #15
0
        private void ProcessCommandError(CommandErrorEventData eventData)
        {
            Entity subsegment;

            try
            {
                subsegment = _recorder.GetEntity();
            }
            catch (EntityNotAvailableException e)
            {
                _recorder.TraceContext.HandleEntityMissing(_recorder, e, "Cannot get entity while processing failure of Entity Framework command.");
                return;
            }

            subsegment.AddException(eventData.Exception);

            _recorder.EndSubsegment();
        }
 public async Task CommandFailedAsync(DbCommand command, CommandErrorEventData eventData,
                                      CancellationToken cancellationToken = new CancellationToken())
 {
     throw new NotImplementedException();
 }
 public void CommandFailed(DbCommand command, CommandErrorEventData eventData)
 {
     // should not contain anything
 }
Exemple #18
0
 /// <summary>
 /// Trace after command fails.
 /// </summary>
 /// <param name="command">Instance of <see cref="DbCommand"/>.</param>
 /// <param name="eventData">Instance of <see cref="CommandErrorEventData"/>.</param>
 public override void CommandFailed(DbCommand command, CommandErrorEventData eventData)
 {
     EFUtil.ProcessCommandError(eventData.Exception);
     base.CommandFailed(command, eventData);
 }
Exemple #19
0
 public void CommandFailed(DbCommand command, CommandErrorEventData eventData)
 {
     LogIfError(command, eventData);
 }
 public void CommandFailed(DbCommand command, CommandErrorEventData eventData)
 {
     TrackEvent(nameof(DataReaderDisposing), eventData.Connection, eventData.ConnectionId, eventData.Duration);
 }
Exemple #21
0
 /// <summary>
 /// Trace after async command fails.
 /// </summary>
 /// <param name="command">Instance of <see cref="DbCommand"/>.</param>
 /// <param name="eventData">Instance of <see cref="CommandErrorEventData"/>.</param>
 /// <param name="cancellationToken">Instance of <see cref="CancellationToken"/>.</param>
 /// <returns>Task representing the async operation.</returns>
 public override Task CommandFailedAsync(DbCommand command, CommandErrorEventData eventData, CancellationToken cancellationToken = default)
 {
     EFUtil.ProcessCommandError(eventData.Exception);
     return(base.CommandFailedAsync(command, eventData, cancellationToken));
 }
Exemple #22
0
 public override void CommandFailed(DbCommand command, CommandErrorEventData eventData)
 {
     LogErrorSQL(command, eventData);
     base.CommandFailed(command, eventData);
 }
Exemple #23
0
 public override Task CommandFailedAsync(DbCommand command, CommandErrorEventData eventData, CancellationToken cancellationToken = default)
 {
     LogErrorSQL(command, eventData);
     return(base.CommandFailedAsync(command, eventData, cancellationToken));
 }
Exemple #24
0
 public override void CommandFailed(DbCommand command, CommandErrorEventData eventData)
 {
     UpdateFailedEvent(eventData);
     EndScope();
 }
Exemple #25
0
 public override async Task CommandFailedAsync(DbCommand command, CommandErrorEventData eventData, CancellationToken cancellationToken = new CancellationToken())
 {
     await base.CommandFailedAsync(command, eventData, cancellationToken);
 }
 public void CommandFailed(
     DbCommand command,
     CommandErrorEventData eventData)
 {
     Assert.False(eventData.IsAsync);
 }
 public override Task CommandFailedAsync(DbCommand command, CommandErrorEventData data, CancellationToken cancellation)
 {
     WriteLine(data);
     return(Task.CompletedTask);
 }
 public override void CommandFailed(DbCommand command, CommandErrorEventData data)
 {
     WriteLine(data);
 }
 public Task CommandFailedAsync(DbCommand command, CommandErrorEventData eventData,
                                CancellationToken cancellationToken = new CancellationToken())
 {
     return(Task.CompletedTask);
 }
Exemple #30
0
 public override async Task CommandFailedAsync(DbCommand command, CommandErrorEventData eventData, CancellationToken cancellationToken = default)
 {
     UpdateFailedEvent(eventData);
     await EndScopeAsync();
 }