Пример #1
0
        private Guid DoSendEvent(SentryEvent @event, Scope scope)
        {
            if (_options.SampleRate is float sample)
            {
                if (Random.NextDouble() > sample)
                {
                    _options.DiagnosticLogger?.LogDebug("Event sampled.");
                    return(Guid.Empty);
                }
            }

            _options.DiagnosticLogger?.LogInfo("Capturing event. Has scope: {0}", scope != null);

            // Evaluate and copy before invoking the callback
            scope?.Evaluate();
            scope?.Apply(@event);

            foreach (var processor in _options.GetAllEventProcessors())
            {
                @event = processor.Process(@event);
                if (@event == null)
                {
                    _options.DiagnosticLogger?.LogInfo("Event dropped by processor {0}", processor.GetType().Name);
                    return(Guid.Empty);
                }
            }

            @event = BeforeSend(@event);
            if (@event == null) // Rejected event
            {
                _options.DiagnosticLogger?.LogInfo("Event dropped by BeforeSend callback.");
                return(Guid.Empty);
            }

            if (Worker.EnqueueEvent(@event))
            {
                _options.DiagnosticLogger?.LogDebug("Event queued up.");
                return(@event.EventId);
            }

            _options.DiagnosticLogger?.LogWarning("The attempt to queue the event failed. Items in queue: {0}",
                                                  Worker.QueuedItems);
            return(Guid.Empty);
        }
Пример #2
0
        /// <summary>
        /// Queues the event to be sent to Sentry
        /// </summary>
        /// <remarks>
        /// An optional scope, if provided, will be applied to the event.
        /// </remarks>
        /// <param name="event">The event to send to Sentry.</param>
        /// <param name="scope">The optional scope to augment the event with.</param>
        /// <returns></returns>
        /// <inheritdoc />
        public Guid CaptureEvent(SentryEvent @event, Scope scope = null)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(nameof(SentryClient));
            }

            if (@event == null)
            {
                return(Guid.Empty);
            }

            // Evaluate and copy before invoking the callback
            scope?.Evaluate();
            scope?.Apply(@event);

            foreach (var processor in _options.GetAllEventProcessors())
            {
                @event = processor.Process(@event);
                if (@event == null)
                {
                    // TODO: Log here which processor dropped it
                    return(Guid.Empty);
                }
            }

            @event = BeforeSend(@event);
            if (@event == null) // Rejected event
            {
                // TODO: Log BeforeSend callback dropped it
                return(Guid.Empty);
            }

            if (Worker.EnqueueEvent(@event))
            {
                return(@event.EventId);
            }

            // TODO: Notify error handler
            Debug.WriteLine("Failed to enqueue event. Current queue depth: " + Worker.QueuedItems);
            return(Guid.Empty);
        }
Пример #3
0
        /// <summary>
        /// Queues the event to be sent to Sentry
        /// </summary>
        /// <remarks>
        /// An optional scope, if provided, will be applied to the event.
        /// </remarks>
        /// <param name="event">The event to send to Sentry.</param>
        /// <param name="scope">The optional scope to augment the event with.</param>
        /// <returns></returns>
        /// <inheritdoc />
        public SentryId CaptureEvent(SentryEvent @event, Scope scope = null)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(nameof(SentryClient));
            }

            if (@event == null)
            {
                return(SentryId.Empty);
            }

            try
            {
                return(DoSendEvent(@event, scope));
            }
            catch (Exception e)
            {
                _options.DiagnosticLogger?.LogError("An error occured when capturing the event {0}.", e, @event.EventId);
                return(SentryId.Empty);
            }
        }
Пример #4
0
        private SentryId DoSendEvent(SentryEvent @event, Scope scope)
        {
            if (_options.SampleRate is float sample)
            {
                if (Random.NextDouble() > sample)
                {
                    _options.DiagnosticLogger?.LogDebug("Event sampled.");
                    return(SentryId.Empty);
                }
            }
            scope = scope ?? new Scope(_options);

            _options.DiagnosticLogger?.LogInfo("Capturing event.");

            // Evaluate and copy before invoking the callback
            scope.Evaluate();
            scope.Apply(@event);

            if (scope.Level != null)
            {
                // Level on scope takes precedence over the one on event
                _options.DiagnosticLogger?.LogInfo("Overriding level set on event '{0}' with level set on scope '{1}'.", @event.Level, scope.Level);
                @event.Level = scope.Level;
            }

            if (@event.Exception != null)
            {
                // Depends on Options instead of the processors to allow application adding new processors
                // after the SDK is initialized. Useful for example once a DI container is up
                foreach (var processor in scope.GetAllExceptionProcessors())
                {
                    processor.Process(@event.Exception, @event);
                }
            }

            foreach (var processor in scope.GetAllEventProcessors())
            {
                @event = processor.Process(@event);
                if (@event == null)
                {
                    _options.DiagnosticLogger?.LogInfo("Event dropped by processor {0}", processor.GetType().Name);
                    return(SentryId.Empty);
                }
            }

            @event = BeforeSend(@event);
            if (@event == null) // Rejected event
            {
                _options.DiagnosticLogger?.LogInfo("Event dropped by BeforeSend callback.");
                return(SentryId.Empty);
            }

            if (Worker.EnqueueEvent(@event))
            {
                _options.DiagnosticLogger?.LogDebug("Event queued up.");
                return(@event.EventId);
            }

            _options.DiagnosticLogger?.LogWarning("The attempt to queue the event failed. Items in queue: {0}",
                                                  Worker.QueuedItems);
            return(SentryId.Empty);
        }
Пример #5
0
        // TODO: this method needs to be refactored, it's really hard to analyze nullability
        private SentryId DoSendEvent(SentryEvent @event, Scope?scope)
        {
            if (_options.SampleRate != null)
            {
                if (Random.NextDouble() > _options.SampleRate.Value)
                {
                    _options.DiagnosticLogger?.LogDebug("Event sampled.");
                    return(SentryId.Empty);
                }
            }
            if (@event.Exception != null && _options.ExceptionFilters?.Length > 0)
            {
                if (_options.ExceptionFilters.Any(f => f.Filter(@event.Exception)))
                {
                    _options.DiagnosticLogger?.LogInfo(
                        "Event with exception of type '{0}' was dropped by an exception filter.", @event.Exception.GetType());
                    return(SentryId.Empty);
                }
            }
            scope ??= new Scope(_options);

            _options.DiagnosticLogger?.LogInfo("Capturing event.");

            // Evaluate and copy before invoking the callback
            scope.Evaluate();
            scope.Apply(@event);

            if (scope.Level != null)
            {
                // Level on scope takes precedence over the one on event
                _options.DiagnosticLogger?.LogInfo("Overriding level set on event '{0}' with level set on scope '{1}'.", @event.Level, scope.Level);
                @event.Level = scope.Level;
            }

            if (@event.Exception != null)
            {
                // Depends on Options instead of the processors to allow application adding new processors
                // after the SDK is initialized. Useful for example once a DI container is up
                foreach (var processor in scope.GetAllExceptionProcessors())
                {
                    processor.Process(@event.Exception, @event);
                }
            }

            SentryEvent?processedEvent = @event;

            foreach (var processor in scope.GetAllEventProcessors())
            {
                processedEvent = processor.Process(processedEvent);
                if (processedEvent == null)
                {
                    _options.DiagnosticLogger?.LogInfo("Event dropped by processor {0}", processor.GetType().Name);
                    return(SentryId.Empty);
                }
            }

            processedEvent = BeforeSend(processedEvent);
            if (processedEvent == null) // Rejected event
            {
                _options.DiagnosticLogger?.LogInfo("Event dropped by BeforeSend callback.");
                return(SentryId.Empty);
            }

            return(CaptureEnvelope(Envelope.FromEvent(processedEvent)) ?
                   processedEvent.EventId : SentryId.Empty);
        }
 internal static bool IsErrored(this SentryEvent @event)
 => @event.Exception is not null || @event.SentryExceptions?.Any() == true;