Ctrl K
ComunidadGuías…

constructor/elasticache.go

Rama main · réplica de lectura del repositorio original.

Archivo original ↗Preguntar a KiBot
package constructor

import (
	"strings"

	"github.com/coroot/coroot/db"
	"github.com/coroot/coroot/model"
	"github.com/coroot/coroot/timeseries"
)

func ecKey(id string) string { return "elasticache/" + id }

func (c *Constructor) loadElasticacheMetadata(w *model.World, metrics map[string][]*model.MetricValues, pjs promJobStatuses, cloudInstancesById map[string]*model.Instance, project *db.Project) {
	for _, m := range metrics["aws_elasticache_info"] {
		ecId := m.Labels["ec_instance_id"]
		if ecId == "" {
			continue
		}
		instance := cloudInstancesById[ecKey(ecId)]
		if instance == nil {
			var appId model.ApplicationId
			instanceParts := strings.SplitN(ecId, "/", 3)
			if len(instanceParts) != 3 {
				continue
			}
			appId = c.newApplicationId(project.ClusterId(), "", model.ApplicationKindElasticacheCluster, m.Labels["cluster_id"])
			instanceName := instanceParts[1] + "-" + instanceParts[2]
			instance = w.GetOrCreateApplication(appId, false).GetOrCreateInstance(instanceName, nil)
			cloudInstancesById[ecKey(ecId)] = instance
			instance.Elasticache = &model.Elasticache{Id: ecId}
		}
		if instance.Node == nil {
			name := "elasticache:" + instance.Name
			instance.Node = model.NewNode(string(project.Id), model.NewNodeId(name, name))
			instance.Node.Name.Update(m.Values, name)
			instance.Node.Instances = append(instance.Node.Instances, instance)
			w.Nodes = append(w.Nodes, instance.Node)
		}
		instance.TcpListens[model.Listen{IP: m.Labels["ipv4"], Port: m.Labels["port"]}] = true
		if ip := m.Labels["ipv4"]; ip != "" && len(instance.Node.NetInterfaces) == 0 {
			instance.Node.NetInterfaces = append(instance.Node.NetInterfaces, &model.InterfaceStats{
				Name: "eth0", Addresses: []string{ip}, Up: m.Values.WithNewValue(1),
			})
		}
		instance.Elasticache.Engine.Update(m.Values, m.Labels["engine"])
		instance.Elasticache.EngineVersion.Update(m.Values, m.Labels["engine_version"])
		instance.Node.InstanceType.Update(m.Values, m.Labels["instance_type"])
		instance.Node.CloudProvider.Update(m.Values, model.CloudProviderAWS)
		instance.Node.Region.Update(m.Values, m.Labels["region"])
		instance.Node.AvailabilityZone.Update(m.Values, m.Labels["availability_zone"])
	}
}

func (c *Constructor) loadElasticache(w *model.World, metrics map[string][]*model.MetricValues, pjs promJobStatuses, cloudInstancesById map[string]*model.Instance) {
	for _, q := range QUERIES {
		if !strings.HasPrefix(q.Name, "aws_elasticache_") {
			continue
		}
		for _, m := range metrics[q.Name] {
			ecId := m.Labels["ec_instance_id"]
			if ecId == "" {
				continue
			}
			instance := cloudInstancesById[ecKey(ecId)]
			if instance == nil {
				continue
			}
			switch q.Name {
			case "aws_elasticache_status":
				instance.Elasticache.LifeSpan = merge(instance.Elasticache.LifeSpan, m.Values, timeseries.Any)
				instance.Elasticache.Status.Update(m.Values, m.Labels["status"])
			}
		}
	}
	if c.pricing != nil {
		for key, instance := range cloudInstancesById {
			if !strings.HasPrefix(key, "elasticache/") {
				continue
			}
			instance.Node.Price = c.pricing.GetNodePrice(nil, instance.Node)
		}
	}
}