Example #1
0
        private async Task ProcessMetricsAsync(Cluster cluster, Kubernetes client)
        {
            try
            {
                var metrics = await client.GetKubernetesNodesMetricsAsync();

                var metricsGathered = new List <Metric>();

                foreach (var metric in metrics.Items)
                {
                    var node          = cluster.Nodes.FirstOrDefault(n => n.Name == metric.Metadata.Name);
                    var extractItemId = string.Empty;

                    if (node != null)
                    {
                        extractItemId = node.Id;
                    }
                    else if (metric.Metadata.Name.Contains("master"))
                    {
                        extractItemId = cluster.Id;
                    }

                    if (!string.IsNullOrEmpty(extractItemId))
                    {
                        metricsGathered.Add(new Metric()
                        {
                            EntityId    = extractItemId,
                            CpuValue    = long.Parse(metric.Usage["cpu"].CanonicalizeString().Replace("n", "")),
                            MemoryValue = long.Parse(metric.Usage["memory"].CanonicalizeString().Replace("Ki", ""))
                        });
                    }
                }

                await metricRepository.InsertMetricsWithStrategyAsync(metricsGathered.ToArray());
            }
            catch (Exception e)
            {
                logger.LogError(e, "Error on processing state");
            }
        }