public async Task ReportAsync(CLRStatsRequest statsRequest, CancellationToken cancellationToken = default(CancellationToken)) { if (!_connectionManager.Ready) { return; } var connection = _connectionManager.GetConnection(); try { var request = new CLRMetricCollection { Service = _instrumentConfig.ServiceName ?? _instrumentConfig.ApplicationCode, ServiceInstance = _instrumentConfig.ServiceInstanceName, }; var metric = new CLRMetric { Cpu = new CPU { UsagePercent = statsRequest.CPU.UsagePercent }, Gc = new ClrGC { Gen0CollectCount = statsRequest.GC.Gen0CollectCount, Gen1CollectCount = statsRequest.GC.Gen1CollectCount, Gen2CollectCount = statsRequest.GC.Gen2CollectCount, HeapMemory = statsRequest.GC.HeapMemory }, Thread = new ClrThread { AvailableWorkerThreads = statsRequest.Thread.MaxWorkerThreads, AvailableCompletionPortThreads = statsRequest.Thread.MaxCompletionPortThreads, MaxWorkerThreads = statsRequest.Thread.MaxWorkerThreads, MaxCompletionPortThreads = statsRequest.Thread.MaxCompletionPortThreads }, Time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }; request.Metrics.Add(metric); var client = new CLRMetricReportService.CLRMetricReportServiceClient(connection); await client.collectAsync(request, _config.GetMeta(), _config.GetTimeout(), cancellationToken); } catch (Exception e) { _logger.Warning("Report CLR Stats error. " + e); } }
private void clr(Channel channel, int serviceId, int serviceInstanceId) { var availableWorkerThreads = 0; var availableCompletionPortThreads = 0; var maxWorkerThreads = 0; var maxCompletionPortThreads = 0; var cpuStats = new CPUStatsRequest { UsagePercent = CpuHelpers.UsagePercent }; var gcStats = new GCStatsRequest { Gen0CollectCount = GCHelpers.Gen0CollectCount, Gen1CollectCount = GCHelpers.Gen1CollectCount, Gen2CollectCount = GCHelpers.Gen2CollectCount, HeapMemory = GCHelpers.TotalMemory }; ThreadPool.GetAvailableThreads(out availableWorkerThreads, out availableCompletionPortThreads); ThreadPool.GetMaxThreads(out maxWorkerThreads, out maxCompletionPortThreads); var threadStats = new ThreadStatsRequest { MaxCompletionPortThreads = maxCompletionPortThreads, MaxWorkerThreads = maxWorkerThreads, AvailableCompletionPortThreads = availableCompletionPortThreads, AvailableWorkerThreads = availableWorkerThreads }; var statsRequest = new CLRStatsRequest { CPU = cpuStats, GC = gcStats, Thread = threadStats }; try { var client = new CLRMetricReportService.CLRMetricReportServiceClient(channel); var request = new CLRMetricCollection { ServiceInstanceId = serviceInstanceId }; var metric = new CLRMetric { Cpu = new CPU { UsagePercent = statsRequest.CPU.UsagePercent }, Gc = new ClrGC { Gen0CollectCount = statsRequest.GC.Gen0CollectCount, Gen1CollectCount = statsRequest.GC.Gen1CollectCount, Gen2CollectCount = statsRequest.GC.Gen2CollectCount, HeapMemory = statsRequest.GC.HeapMemory }, Thread = new ClrThread { AvailableWorkerThreads = statsRequest.Thread.MaxWorkerThreads, AvailableCompletionPortThreads = statsRequest.Thread.MaxCompletionPortThreads, MaxWorkerThreads = statsRequest.Thread.MaxWorkerThreads, MaxCompletionPortThreads = statsRequest.Thread.MaxCompletionPortThreads }, Time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }; request.Metrics.Add(metric); var reply = Task.Run(async() => { return(await client.collectAsync(request)); }).Result; } catch (Exception ex) { throw ex; } }