func()

in pkg/aws/cloudmap.go [154:203]


func (c *CloudMapClient) getInstanceScrapeConfiguration(sdInstance *ServiceDiscoveryInstance, serviceTags map[string]*string) (*InstanceScrapeConfig, error) {
	labels := make(map[string]string)
	targets := make([]string, 0)

	// Port number of the resource is available, by default, as an attribute with the key 'AWS_INSTANCE_PORT'
	defaultPort, present := sdInstance.attributes[PortNumberAttribute]
	if !present {
		defaultPort = aws.String("80")
	}
	// Metrics port is expected as a resource tag with the key 'METRICS_PORT'
	metricsPort, present := serviceTags[MetricsPortTag]
	if !present {
		metricsPort = defaultPort
	}

	// IP address of the resource is available, by default, as an attribute with the key 'AWS_INSTANCE_IPV4'
	address, present := sdInstance.attributes[IpAddressAttribute]
	if !present {
		return nil, errors.New(fmt.Sprintf("Cannot find IP address for instance in service %v", sdInstance.service))
	}
	targets = append(targets, fmt.Sprintf("%s:%s", *address, *metricsPort))

	// Path for metrics endpoint is expected as a resource tag with the key '__metrics_path__'
	defaultPath := aws.String("/metrics")
	metricsPath, present := serviceTags[MetricsPathTag]
	if !present {
		metricsPath = defaultPath
	}
	labels["__metrics_path__"] = *metricsPath

	//
	// ECS Task instances registered in Cloud Map are assigned the following default attributes
	// ECS_CLUSTER_NAME, ECS_SERVICE_NAME, ECS_TASK_DEFINITION_FAMILY
	// Add these attributes as labels to be attached to the Prometheus metric
	//
	cluster, present := sdInstance.attributes[ClusterNameAttribute]
	if present {
		labels["cluster"] = *cluster
	}
	service, present := sdInstance.attributes[ServiceNameAttribute]
	if present {
		labels["service"] = *service
	}
	taskdefinition, present := sdInstance.attributes[TaskDefinitionAttribute]
	if present {
		labels["taskdefinition"] = *taskdefinition
	}

	return &InstanceScrapeConfig{Targets: targets, Labels: labels}, nil
}