Ejemplo n.º 1
0
        public Task <ActionResult> GetDump(
            ProcessFilter?processFilter,
            [FromQuery] DumpType type         = DumpType.WithHeap,
            [FromQuery] string egressProvider = null)
        {
            return(InvokeForProcess(async processInfo =>
            {
                string dumpFileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
                                      FormattableString.Invariant($"dump_{GetFileNameTimeStampUtcNow()}.dmp") :
                                      FormattableString.Invariant($"core_{GetFileNameTimeStampUtcNow()}");

                if (string.IsNullOrEmpty(egressProvider))
                {
                    Stream dumpStream = await _diagnosticServices.GetDump(processInfo, type, HttpContext.RequestAborted);

                    //Compression is done automatically by the response
                    //Chunking is done because the result has no content-length
                    return File(dumpStream, ContentTypes.ApplicationOctectStream, dumpFileName);
                }
                else
                {
                    KeyValueLogScope scope = new KeyValueLogScope();
                    scope.AddArtifactType(ArtifactType_Dump);
                    scope.AddEndpointInfo(processInfo.EndpointInfo);

                    return new EgressStreamResult(
                        token => _diagnosticServices.GetDump(processInfo, type, token),
                        egressProvider,
                        dumpFileName,
                        processInfo.EndpointInfo,
                        ContentTypes.ApplicationOctectStream,
                        scope);
                }
            }, processFilter, ArtifactType_Dump));
        }
Ejemplo n.º 2
0
        private ActionResult Result(
            string artifactType,
            string providerName,
            Func <Stream, CancellationToken, Task> action,
            string fileName,
            string contentType,
            IEndpointInfo endpointInfo,
            bool asAttachment = true)
        {
            KeyValueLogScope scope = new KeyValueLogScope();

            scope.AddArtifactType(artifactType);
            scope.AddEndpointInfo(endpointInfo);

            if (string.IsNullOrEmpty(providerName))
            {
                return(new OutputStreamResult(
                           action,
                           contentType,
                           asAttachment ? fileName : null,
                           scope));
            }
            else
            {
                return(new EgressStreamResult(
                           action,
                           providerName,
                           fileName,
                           endpointInfo,
                           contentType,
                           scope));
            }
        }
Ejemplo n.º 3
0
        private async Task <ActionResult <T> > InvokeForProcess <T>(Func <IProcessInfo, Task <ActionResult <T> > > func, ProcessFilter?filter, string artifactType = null)
        {
            IDisposable artifactTypeRegistration = null;

            if (!string.IsNullOrEmpty(artifactType))
            {
                KeyValueLogScope artifactTypeScope = new KeyValueLogScope();
                artifactTypeScope.AddArtifactType(artifactType);
                artifactTypeRegistration = _logger.BeginScope(artifactTypeScope);
            }

            try
            {
                return(await this.InvokeService(async() =>
                {
                    IProcessInfo processInfo = await _diagnosticServices.GetProcessAsync(filter, HttpContext.RequestAborted);

                    KeyValueLogScope processInfoScope = new KeyValueLogScope();
                    processInfoScope.AddEndpointInfo(processInfo.EndpointInfo);
                    using var _ = _logger.BeginScope(processInfoScope);

                    _logger.LogDebug("Resolved target process.");

                    return await func(processInfo);
                }, _logger));
            }
            finally
            {
                artifactTypeRegistration?.Dispose();
            }
        }
            protected override async Task <CollectionRuleActionResult> ExecuteCoreAsync(
                TaskCompletionSource <object> startCompleteSource,
                CancellationToken token)
            {
                string egress = Options.Egress;

                string gcdumpFileName = GCDumpUtilities.GenerateGCDumpFileName(EndpointInfo);

                KeyValueLogScope scope = Utils.CreateArtifactScope(Utils.ArtifactType_GCDump, EndpointInfo);

                EgressOperation egressOperation = new EgressOperation(
                    (stream, token) =>
                {
                    startCompleteSource.TrySetResult(null);
                    return(GCDumpUtilities.CaptureGCDumpAsync(EndpointInfo, stream, token));
                },
                    egress,
                    gcdumpFileName,
                    EndpointInfo,
                    ContentTypes.ApplicationOctetStream,
                    scope);

                ExecutionResult <EgressResult> result = await egressOperation.ExecuteAsync(_serviceProvider, token);

                string gcdumpFilePath = result.Result.Value;

                return(new CollectionRuleActionResult()
                {
                    OutputValues = new Dictionary <string, string>(StringComparer.Ordinal)
                    {
                        { CollectionRuleActionConstants.EgressPathOutputValueName, gcdumpFilePath }
                    }
                });
            }
Ejemplo n.º 5
0
        private Task <ActionResult> Result(
            string artifactType,
            string providerName,
            Func <Stream, CancellationToken, Task> action,
            string fileName,
            string contentType,
            IEndpointInfo endpointInfo,
            bool asAttachment = true)
        {
            KeyValueLogScope scope = Utilities.CreateArtifactScope(artifactType, endpointInfo);

            if (string.IsNullOrEmpty(providerName))
            {
                return(Task.FromResult <ActionResult>(new OutputStreamResult(
                                                          action,
                                                          contentType,
                                                          asAttachment ? fileName : null,
                                                          scope)));
            }
            else
            {
                return(SendToEgress(new EgressOperation(
                                        action,
                                        providerName,
                                        fileName,
                                        endpointInfo,
                                        contentType,
                                        scope),
                                    limitKey: artifactType));
            }
        }
Ejemplo n.º 6
0
            protected override async Task <CollectionRuleActionResult> ExecuteCoreAsync(
                TaskCompletionSource <object> startCompletionSource,
                CancellationToken token)
            {
                TimeSpan duration       = Options.Duration.GetValueOrDefault(TimeSpan.Parse(CollectTraceOptionsDefaults.Duration));
                string   egressProvider = Options.Egress;

                MonitoringSourceConfiguration configuration;

                if (Options.Profile.HasValue)
                {
                    TraceProfile profile = Options.Profile.Value;
                    float        metricsIntervalSeconds = _counterOptions.CurrentValue.GetIntervalSeconds();

                    configuration = TraceUtilities.GetTraceConfiguration(profile, metricsIntervalSeconds);
                }
                else
                {
                    EventPipeProvider[] optionsProviders = Options.Providers.ToArray();
                    bool requestRundown      = Options.RequestRundown.GetValueOrDefault(CollectTraceOptionsDefaults.RequestRundown);
                    int  bufferSizeMegabytes = Options.BufferSizeMegabytes.GetValueOrDefault(CollectTraceOptionsDefaults.BufferSizeMegabytes);

                    configuration = TraceUtilities.GetTraceConfiguration(optionsProviders, requestRundown, bufferSizeMegabytes);
                }

                string fileName = TraceUtilities.GenerateTraceFileName(EndpointInfo);

                KeyValueLogScope scope = Utils.CreateArtifactScope(Utils.ArtifactType_Trace, EndpointInfo);

                EgressOperation egressOperation = new EgressOperation(
                    async(outputStream, token) =>
                {
                    using IDisposable operationRegistration = _operationTrackerService.Register(EndpointInfo);
                    await TraceUtilities.CaptureTraceAsync(startCompletionSource, EndpointInfo, configuration, duration, outputStream, token);
                },
                    egressProvider,
                    fileName,
                    EndpointInfo,
                    ContentTypes.ApplicationOctetStream,
                    scope);

                ExecutionResult <EgressResult> result = await egressOperation.ExecuteAsync(_serviceProvider, token);

                string traceFilePath = result.Result.Value;

                return(new CollectionRuleActionResult()
                {
                    OutputValues = new Dictionary <string, string>(StringComparer.Ordinal)
                    {
                        { CollectionRuleActionConstants.EgressPathOutputValueName, traceFilePath }
                    }
                });
            }
        public void Configure(EgressOptions options)
        {
            IConfigurationSection egressSection = _configuration.GetEgressSection();

            IConfigurationSection propertiesSection = egressSection.GetSection(nameof(EgressOptions.Properties));

            propertiesSection.Bind(options.Properties);

            _logger.LogDebug("Start loading egress providers.");
            IConfigurationSection providersSection = egressSection.GetSection(nameof(EgressOptions.Providers));

            foreach (var providerSection in providersSection.GetChildren())
            {
                string providerName = providerSection.Key;

                KeyValueLogScope providerNameScope = new KeyValueLogScope();
                providerNameScope.Values.Add("EgressProviderName", providerName);
                using var providerNameRegistration = _logger.BeginScope(providerNameScope);

                CommonEgressProviderOptions commonOptions = new CommonEgressProviderOptions();
                providerSection.Bind(commonOptions);

                EgressProviderValidation validation = new EgressProviderValidation(providerName, _logger);
                if (!validation.TryValidate(commonOptions))
                {
                    _logger.LogWarning("Provider '{0}': Skipped: Invalid options.", providerName);
                }

                string           providerType      = commonOptions.Type;
                KeyValueLogScope providerTypeScope = new KeyValueLogScope();
                providerTypeScope.Values.Add("EgressProviderType", providerType);
                using var providerTypeRegistration = _logger.BeginScope(providerTypeScope);

                if (!_factories.TryGetValue(providerType, out EgressFactory factory))
                {
                    _logger.LogWarning("Provider '{0}': Skipped: Type '{1}' is not supported.", providerName, providerType);
                    continue;
                }

                if (!factory.TryCreate(providerName, providerSection, options.Properties, out ConfiguredEgressProvider provider))
                {
                    _logger.LogWarning("Provider '{0}': Skipped: Invalid options.", providerName);
                    continue;
                }

                options.Providers.Add(providerName, provider);

                _logger.LogDebug("Added egress provider '{0}'.", providerName);
            }
            _logger.LogDebug("End loading egress providers.");
        }
Ejemplo n.º 8
0
        public void Configure(EgressOptions options)
        {
            IConfigurationSection egressSection = _configuration.GetEgressSection();

            IConfigurationSection propertiesSection = egressSection.GetSection(nameof(EgressOptions.Properties));

            propertiesSection.Bind(options.Properties);

            IConfigurationSection providersSection = egressSection.GetSection(nameof(EgressOptions.Providers));

            foreach (var providerSection in providersSection.GetChildren())
            {
                string providerName = providerSection.Key;

                KeyValueLogScope providerNameScope = new KeyValueLogScope();
                providerNameScope.Values.Add("EgressProviderName", providerName);
                using var providerNameRegistration = _logger.BeginScope(providerNameScope);

                CommonEgressProviderOptions commonOptions = new CommonEgressProviderOptions();
                providerSection.Bind(commonOptions);

                EgressProviderValidation validation = new EgressProviderValidation(providerName, _logger);
                if (!validation.TryValidate(commonOptions))
                {
                    _logger.EgressProviderInvalidOptions(providerName);
                }

                string           providerType      = commonOptions.Type;
                KeyValueLogScope providerTypeScope = new KeyValueLogScope();
                providerTypeScope.Values.Add("EgressProviderType", providerType);
                using var providerTypeRegistration = _logger.BeginScope(providerTypeScope);

                if (!_factories.TryGetValue(providerType, out EgressFactory factory))
                {
                    _logger.EgressProviderInvalidType(providerName, providerType);
                    continue;
                }

                if (!factory.TryCreate(providerName, providerSection, options.Properties, out ConfiguredEgressProvider provider))
                {
                    _logger.EgressProviderInvalidOptions(providerName);
                    continue;
                }

                options.Providers.Add(providerName, provider);

                _logger.EgressProviderAdded(providerName);
            }
        }
Ejemplo n.º 9
0
            protected override async Task <CollectionRuleActionResult> ExecuteCoreAsync(
                TaskCompletionSource <object> startCompletionSource,
                CancellationToken token)
            {
                TimeSpan duration      = Options.Duration.GetValueOrDefault(TimeSpan.Parse(CollectLogsOptionsDefaults.Duration));
                bool     useAppFilters = Options.UseAppFilters.GetValueOrDefault(CollectLogsOptionsDefaults.UseAppFilters);
                LogLevel defaultLevel  = Options.DefaultLevel.GetValueOrDefault(CollectLogsOptionsDefaults.DefaultLevel);
                Dictionary <string, LogLevel?> filterSpecs = Options.FilterSpecs;
                string    egressProvider = Options.Egress;
                LogFormat logFormat      = Options.Format.GetValueOrDefault(CollectLogsOptionsDefaults.Format);

                var settings = new EventLogsPipelineSettings()
                {
                    Duration      = duration,
                    LogLevel      = defaultLevel,
                    UseAppFilters = useAppFilters,
                    FilterSpecs   = filterSpecs
                };

                string fileName    = LogsUtilities.GenerateLogsFileName(EndpointInfo);
                string contentType = LogsUtilities.GetLogsContentType(logFormat);

                KeyValueLogScope scope = Utils.CreateArtifactScope(Utils.ArtifactType_Logs, EndpointInfo);

                EgressOperation egressOperation = new EgressOperation(
                    (outputStream, token) => LogsUtilities.CaptureLogsAsync(startCompletionSource, logFormat, EndpointInfo, settings, outputStream, token),
                    egressProvider,
                    fileName,
                    EndpointInfo,
                    contentType,
                    scope);

                ExecutionResult <EgressResult> result = await egressOperation.ExecuteAsync(_serviceProvider, token);

                string logsFilePath = result.Result.Value;

                return(new CollectionRuleActionResult()
                {
                    OutputValues = new Dictionary <string, string>(StringComparer.Ordinal)
                    {
                        { CollectionRuleActionConstants.EgressPathOutputValueName, logsFilePath }
                    }
                });
            }
Ejemplo n.º 10
0
            protected override async Task <CollectionRuleActionResult> ExecuteCoreAsync(
                TaskCompletionSource <object> startCompletionSource,
                CancellationToken token)
            {
                DumpType dumpType       = Options.Type.GetValueOrDefault(CollectDumpOptionsDefaults.Type);
                string   egressProvider = Options.Egress;

                string dumpFileName = DumpUtilities.GenerateDumpFileName();

                string dumpFilePath = string.Empty;

                KeyValueLogScope scope = Utils.CreateArtifactScope(Utils.ArtifactType_Dump, EndpointInfo);

                try
                {
                    EgressOperation egressOperation = new EgressOperation(
                        token => {
                        startCompletionSource.TrySetResult(null);
                        return(_dumpService.DumpAsync(EndpointInfo, dumpType, token));
                    },
                        egressProvider,
                        dumpFileName,
                        EndpointInfo,
                        ContentTypes.ApplicationOctetStream,
                        scope);

                    ExecutionResult <EgressResult> result = await egressOperation.ExecuteAsync(_serviceProvider, token);

                    dumpFilePath = result.Result.Value;
                }
                catch (Exception ex)
                {
                    throw new CollectionRuleActionException(ex);
                }

                return(new CollectionRuleActionResult()
                {
                    OutputValues = new Dictionary <string, string>(StringComparer.Ordinal)
                    {
                        { CollectionRuleActionConstants.EgressPathOutputValueName, dumpFilePath }
                    }
                });
            }
Ejemplo n.º 11
0
        public Task <ActionResult> CaptureDump(
            [FromQuery]
            int?pid = null,
            [FromQuery]
            Guid?uid = null,
            [FromQuery]
            string name = null,
            [FromQuery]
            Models.DumpType type = Models.DumpType.WithHeap,
            [FromQuery]
            string egressProvider = null)
        {
            ProcessKey?processKey = GetProcessKey(pid, uid, name);

            return(InvokeForProcess(async processInfo =>
            {
                string dumpFileName = DumpUtilities.GenerateDumpFileName();

                if (string.IsNullOrEmpty(egressProvider))
                {
                    Stream dumpStream = await _dumpService.DumpAsync(processInfo.EndpointInfo, type, HttpContext.RequestAborted);

                    _logger.WrittenToHttpStream();
                    //Compression is done automatically by the response
                    //Chunking is done because the result has no content-length
                    return File(dumpStream, ContentTypes.ApplicationOctetStream, dumpFileName);
                }
                else
                {
                    KeyValueLogScope scope = Utilities.CreateArtifactScope(Utilities.ArtifactType_Dump, processInfo.EndpointInfo);

                    return await SendToEgress(new EgressOperation(
                                                  token => _dumpService.DumpAsync(processInfo.EndpointInfo, type, token),
                                                  egressProvider,
                                                  dumpFileName,
                                                  processInfo.EndpointInfo,
                                                  ContentTypes.ApplicationOctetStream,
                                                  scope), limitKey: Utilities.ArtifactType_Dump);
                }
            }, processKey, Utilities.ArtifactType_Dump));
        }
Ejemplo n.º 12
0
        public ActionResult GetMetrics()
        {
            return(this.InvokeService(() =>
            {
                if (!_metricsOptions.Enabled.GetValueOrDefault(MetricsOptionsDefaults.Enabled))
                {
                    throw new InvalidOperationException("Metrics was not enabled");
                }

                KeyValueLogScope scope = new KeyValueLogScope();
                scope.AddArtifactType(ArtifactType_Metrics);

                return new OutputStreamResult(async(outputStream, token) =>
                {
                    await _metricsStore.MetricsStore.SnapshotMetrics(outputStream, token);
                },
                                              ContentTypes.TextPlain_v0_0_4,
                                              null,
                                              scope);
            }, _logger));
        }
Ejemplo n.º 13
0
        public ActionResult Metrics()
        {
            return(this.InvokeService(() =>
            {
                if (!_metricsOptions.Enabled)
                {
                    throw new InvalidOperationException("Metrics was not enabled");
                }

                KeyValueLogScope scope = new KeyValueLogScope();
                scope.AddArtifactType(ArtifactType_Metrics);

                return new OutputStreamResult(async(outputStream, token) =>
                {
                    await _metricsStore.MetricsStore.SnapshotMetrics(outputStream, token);
                },
                                              "text/plain; version=0.0.4",
                                              null,
                                              scope);
            }, _logger));
        }