Exemplo n.º 1
0
        public ITransaction StartTransaction(
            ITransactionContext context,
            IReadOnlyDictionary <string, object?> customSamplingContext)
        {
            var transaction = new Transaction(this, context);

            // Transactions are not handled by event processors, so some things need to be added manually

            // Apply scope
            ScopeManager.GetCurrent().Key.Apply(transaction);

            // SDK information
            var nameAndVersion      = MainSentryEventProcessor.NameAndVersion;
            var protocolPackageName = MainSentryEventProcessor.ProtocolPackageName;

            if (transaction.Sdk.Version == null && transaction.Sdk.Name == null)
            {
                transaction.Sdk.Name    = Constants.SdkName;
                transaction.Sdk.Version = nameAndVersion.Version;
            }

            if (nameAndVersion.Version != null)
            {
                transaction.Sdk.AddPackage(protocolPackageName, nameAndVersion.Version);
            }

            // Release information
            transaction.Release ??= _options.Release ?? ReleaseLocator.GetCurrent();

            // Environment information
            var foundEnvironment = EnvironmentLocator.Locate();

            transaction.Environment ??= (string.IsNullOrWhiteSpace(foundEnvironment)
                ? string.IsNullOrWhiteSpace(_options.Environment)
                    ? Constants.ProductionEnvironmentSetting
                    : _options.Environment
                : foundEnvironment);

            // Make a sampling decision if it hasn't been made already.
            // It could have been made by this point if the transaction was started
            // from a trace header which contains a sampling decision.
            if (transaction.IsSampled is null)
            {
                var samplingContext = new TransactionSamplingContext(
                    context,
                    customSamplingContext
                    );

                var sampleRate =
                    // Custom sampler may not exist or may return null, in which case we fallback
                    // to the static sample rate.
                    _options.TracesSampler?.Invoke(samplingContext)
                    ?? _options.TracesSampleRate;

                transaction.IsSampled = sampleRate switch
                {
Exemplo n.º 2
0
        public SentryEvent Process(SentryEvent @event)
        {
            _options.DiagnosticLogger?.LogDebug("Running main event processor on: Event {0}", @event.EventId);

            if ([email protected](Runtime.Type) && Runtime != null)
            {
                @event.Contexts[Runtime.Type] = Runtime;
            }

            if ([email protected](OperatingSystem.Type))
            {
                // RuntimeInformation.OSDescription is throwing on Mono 5.12
                if (!PlatformAbstractions.Runtime.Current.IsMono())
                {
                    @event.Contexts.OperatingSystem.RawDescription = RuntimeInformation.OSDescription;
                }
            }

            if (TimeZoneInfo.Local is { } timeZoneInfo)
            {
                @event.Contexts.Device.Timezone = timeZoneInfo;
            }

            const string currentUiCultureKey = "CurrentUICulture";

            if ([email protected](currentUiCultureKey) &&
                CultureInfoToDictionary(CultureInfo.CurrentUICulture) is { } currentUiCultureMap)
            {
                @event.Contexts[currentUiCultureKey] = currentUiCultureMap;
            }

            const string cultureInfoKey = "CurrentCulture";

            if ([email protected](cultureInfoKey) &&
                CultureInfoToDictionary(CultureInfo.CurrentCulture) is { } currentCultureMap)
            {
                @event.Contexts[cultureInfoKey] = currentCultureMap;
            }

            @event.Platform = Protocol.Constants.Platform;

            if (@event.Sdk != null)
            {
                // SDK Name/Version might have be already set by an outer package
                // e.g: ASP.NET Core can set itself as the SDK
                if (@event.Sdk.Version == null && @event.Sdk.Name == null)
                {
                    @event.Sdk.Name    = Constants.SdkName;
                    @event.Sdk.Version = NameAndVersion.Version;
                }

                if (NameAndVersion.Version != null)
                {
                    @event.Sdk.AddPackage(ProtocolPackageName, NameAndVersion.Version);
                }
            }

            // Report local user if opt-in PII, no user was already set to event and feature not opted-out:
            if (_options.SendDefaultPii && _options.IsEnvironmentUser && [email protected]())
            {
                @event.User.Username = Environment.UserName;
            }

            if (@event.ServerName == null)
            {
                // Value set on the options take precedence over device name.
                if (!string.IsNullOrEmpty(_options.ServerName))
                {
                    @event.ServerName = _options.ServerName;
                }
                else if (_options.SendDefaultPii)
                {
                    @event.ServerName = Environment.MachineName;
                }
            }

            if (@event.Level == null)
            {
                @event.Level = SentryLevel.Error;
            }

            if (@event.Release == null)
            {
                @event.Release = _options.Release ?? Release;
            }

            if (@event.Environment == null)
            {
                @event.Environment = _options.Environment ?? EnvironmentLocator.Locate();
            }

            if (@event.Exception == null)
            {
                var stackTrace = SentryStackTraceFactoryAccessor().Create(@event.Exception);
                if (stackTrace != null)
                {
                    var thread = new SentryThread
                    {
                        Crashed    = false,
                        Current    = true,
                        Name       = Thread.CurrentThread.Name,
                        Id         = Thread.CurrentThread.ManagedThreadId,
                        Stacktrace = stackTrace
                    };

                    @event.SentryThreads = @event.SentryThreads?.Any() == true
                        ? new List <SentryThread>(@event.SentryThreads)
                    {
                        thread
                    }
                        : new[] { thread }.AsEnumerable();
                }
            }

            if (_options.ReportAssemblies)
            {
                foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
                {
                    if (assembly.IsDynamic)
                    {
                        continue;
                    }

                    var asmName = assembly.GetName();
                    @event.Modules[asmName.Name] = asmName.Version.ToString();
                }
            }

            return(@event);
        }